Salome HOME
Fictive 3D object for objects with 2 types of presentation (Bug #216).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DataObject.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_DataObject.h"
24
25 #include <HYDROData_DummyObject3D.h>
26 #include <HYDROData_Object.h>
27
28 #include <TDF_Tool.hxx>
29
30 #include <SUIT_DataObject.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <SUIT_Session.h>
33
34 #include <QPixmap>
35
36 HYDROGUI_DataObject::HYDROGUI_DataObject( SUIT_DataObject* theParent, 
37                                           Handle(HYDROData_Entity) theData,
38                                           const QString& theParentEntry )
39 : CAM_DataObject( theParent ),
40   LightApp_DataObject( theParent ),
41   myData( theData ),
42   myParentEntry( theParentEntry ),
43   myIsValid( true )
44 {
45 }
46
47 QString HYDROGUI_DataObject::entry() const
48 {
49   QString anEntry = HYDROGUI_DataObject::dataObjectEntry( modelObject() );
50   if( isReference() )
51     anEntry.prepend( myParentEntry + "_" );
52   return anEntry;
53 }
54
55 QString HYDROGUI_DataObject::refEntry() const
56 {
57   if( !myParentEntry.isEmpty() )
58     return HYDROGUI_DataObject::dataObjectEntry( modelObject() );
59   return QString();
60 }
61
62 QString HYDROGUI_DataObject::name() const
63 {
64   if( !myData.IsNull() )
65     return myData->GetName();
66   return QString();
67 }
68
69 QFont HYDROGUI_DataObject::font( const int theId ) const
70 {
71   QFont aFont = LightApp_DataObject::font( theId );
72   if( theId == NameId )
73   {
74     Handle(HYDROData_Entity) aDataObject = modelObject();
75     if( !aDataObject.IsNull() && aDataObject->IsMustBeUpdated() )
76     {
77       aFont.setItalic( true );
78       aFont.setBold( true );
79     }
80   }
81   return aFont;
82 }
83
84 QColor HYDROGUI_DataObject::color( const ColorRole theRole, const int theId ) const
85 {
86   QColor aColor;
87
88   if ( !isValid() ) {
89     switch ( theRole )
90     {
91       case Text:
92       case Foreground:
93       case Highlight:
94         aColor = Qt::red; // red
95       break;
96       case HighlightedText:
97         // text color for the highlighted item
98         aColor = Qt::white;   // white
99       break;
100
101       default:
102         break;
103     }
104   }
105
106   if ( !aColor.isValid() )
107   {
108     Handle(HYDROData_Entity) aDataObject = modelObject();
109     if( !aDataObject.IsNull() && aDataObject->IsMustBeUpdated() )
110     {
111       switch ( theRole )
112       {
113         case Text:
114         case Foreground:
115         case Highlight:
116           aColor = Qt::blue;    // color for objects which need updating
117         break;
118         case HighlightedText:
119           // text color for the highlighted item
120           aColor = Qt::white;   // white
121         break;
122
123         default:
124           break;
125       }
126     }
127   }
128
129   if ( !aColor.isValid() ) {
130     aColor = LightApp_DataObject::color( theRole, theId );
131   }
132
133   return aColor;
134 }
135
136 QPixmap HYDROGUI_DataObject::icon( const int theId ) const
137 {
138   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
139   if ( theId == NameId )
140   {
141     QString anIcon;
142     Handle(HYDROData_Entity) aDataObject = modelObject();
143     if( aDataObject.IsNull() )
144     {
145       anIcon = QObject::tr( "HYDRO_TYPE0_ICO" ); // KIND_UNKNOWN
146     }
147     else
148     {
149       QString aNeedUpdate( aDataObject->IsMustBeUpdated() ? "M_" : "" );
150
151       int anObjectKind = (int)aDataObject->GetKind();
152       if ( anObjectKind == KIND_DUMMY_3D )
153       {
154         Handle(HYDROData_DummyObject3D) anObject3D = 
155           Handle(HYDROData_DummyObject3D)::DownCast( aDataObject );
156         
157         Handle(HYDROData_Object) aFatherObj = anObject3D->GetObject();
158         if ( !aFatherObj.IsNull() )
159           anObjectKind = aFatherObj->GetKind();
160       }
161
162       anIcon = QObject::tr( QString("HYDRO_%1TYPE%2_ICO").arg( aNeedUpdate ).arg( anObjectKind ).toAscii() );
163     }
164
165     return aResMgr->loadPixmap( "HYDRO", anIcon );
166   }
167   return LightApp_DataObject::icon( theId );
168 }
169
170 QString HYDROGUI_DataObject::dataObjectEntry( const Handle(HYDROData_Entity)& theObject,
171                                               const bool theWithPrefix )
172 {
173   QString aEntryStr = QString::null;
174   if( !theObject.IsNull() )
175   {
176     TCollection_AsciiString aLabEntr;
177     TDF_Tool::Entry( theObject->Label(), aLabEntr );
178     aEntryStr = aLabEntr.ToCString();
179     if( theWithPrefix )
180       aEntryStr.prepend( HYDROGUI_DataObject::entryPrefix() );
181   }
182   return aEntryStr;
183 }
184
185 void HYDROGUI_DataObject::setIsValid( const bool theIsValid )
186 {
187   myIsValid = theIsValid;
188 }
189
190 bool HYDROGUI_DataObject::isValid() const
191 {
192   return myIsValid;
193 }
194
195 HYDROGUI_NamedObject::HYDROGUI_NamedObject( SUIT_DataObject* theParent,
196                                             const QString&   theName,
197                                             const QString&   theParentEntry  )
198 : CAM_DataObject( theParent ),
199   LightApp_DataObject( theParent ),
200   myName( theName ),
201   myParentEntry( theParentEntry )
202 {
203 }
204
205 QString HYDROGUI_NamedObject::entry() const
206 {
207   QString anEntry = HYDROGUI_DataObject::entryPrefix() + name();
208   if( !myParentEntry.isEmpty() )
209     anEntry.prepend( myParentEntry + "_" );
210   return anEntry;
211 }
212
213 QString HYDROGUI_NamedObject::name() const
214 {
215   return myName;
216 }
217
218 QPixmap HYDROGUI_NamedObject::icon( const int theId ) const
219 {
220   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
221   if ( theId == NameId )
222   {
223     return aResMgr->loadPixmap( "HYDRO", QObject::tr( "PARTITION_ICO" ) );
224   }
225   return LightApp_DataObject::icon( theId );
226 }
227
228 HYDROGUI_DropTargetObject::HYDROGUI_DropTargetObject( SUIT_DataObject* theParent,
229                                             const QString&   theName,
230                                             const QString&   theParentEntry  )
231 : HYDROGUI_NamedObject( theParent, theName, theParentEntry ), CAM_DataObject( theParent )
232 {
233 }