Salome HOME
ENV: Windows porting
[modules/gui.git] / src / SalomeApp / SalomeApp_DataObject.cxx
1 #include "SalomeApp_DataObject.h"
2
3 #include "SalomeApp_Study.h"
4 #include "SalomeApp_RootObject.h"
5
6 #include <SUIT_Application.h>
7 #include <SUIT_ResourceMgr.h>
8 #include <SUIT_DataObjectKey.h>
9
10 #include <qobject.h>
11
12 #include <SALOMEDSClient_AttributeReal.hxx>
13 #include <SALOMEDSClient_AttributeInteger.hxx>
14 #include <SALOMEDSClient_AttributeComment.hxx>
15 #include <SALOMEDSClient_AttributeTableOfReal.hxx>
16 #include <SALOMEDSClient_AttributeTableOfInteger.hxx>
17
18 /*!
19         Class: SalomeApp_DataObject::Key
20         Level: Internal
21 */
22 class SalomeApp_DataObject::Key : public SUIT_DataObjectKey
23 {
24 public:
25   Key( const QString& );
26   virtual ~Key();
27
28   virtual bool isLess( const SUIT_DataObjectKey* ) const;
29   virtual bool isEqual( const SUIT_DataObjectKey* ) const;
30
31 private:
32   QString myEntry;
33 };
34
35 /*!Constructor. Initialize by \a entry.*/
36 SalomeApp_DataObject::Key::Key( const QString& entry )
37 : SUIT_DataObjectKey(),
38   myEntry( entry )
39 {
40 }
41
42 /*!Destructor. Do nothing.*/
43 SalomeApp_DataObject::Key::~Key()
44 {
45 }
46
47 /*!Checks: Is current key less than \a other.*/
48 bool SalomeApp_DataObject::Key::isLess( const SUIT_DataObjectKey* other ) const
49 {
50   Key* that = (Key*)other;
51   return myEntry < that->myEntry;
52 }
53
54 /*!Checks: Is current key equal with \a other.*/
55 bool SalomeApp_DataObject::Key::isEqual( const SUIT_DataObjectKey* other ) const
56 {
57   Key* that = (Key*)other;
58   return myEntry == that->myEntry;
59 }
60
61 /*
62         Class: SalomeApp_DataObject
63         Level: Public
64 */
65 /*!Constructor. Initialize by \a parent*/
66 SalomeApp_DataObject::SalomeApp_DataObject( SUIT_DataObject* parent )
67 : CAM_DataObject( parent )
68 {
69 }
70
71 /*!Constructor. Initialize by \a parent and SObject*/
72 SalomeApp_DataObject::SalomeApp_DataObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
73 : CAM_DataObject( parent )
74 {
75   myObject = sobj;
76 }
77
78 /*!Destructor. Do nothing.*/
79 SalomeApp_DataObject::~SalomeApp_DataObject()
80 {
81 }
82
83 /*!Gets object ID.
84  *\retval QString
85  */
86 QString SalomeApp_DataObject::entry() const
87 {
88   if ( myObject )
89     return myObject->GetID().c_str();
90   return QString::null;
91 }
92
93 /*!Create and return new key object.*/
94 SUIT_DataObjectKey* SalomeApp_DataObject::key() const
95 {
96   QString str = entry();
97   return new Key( str );
98 }
99
100 /*!Gets name of object.*/
101 QString SalomeApp_DataObject::name() const
102 {
103   QString str;
104
105   if ( myObject )
106     str = myObject->GetName().c_str();
107
108   if ( str.isEmpty() )
109   {
110     _PTR(SObject) refObj = referencedObject();
111     if ( refObj )
112       str = refObj->GetName().c_str();
113   }
114
115   if ( isReference() )
116     str = QString( "* " ) + str;
117
118   return str;
119 }
120
121 /*!Gets icon picture of object.*/
122 QPixmap SalomeApp_DataObject::icon() const
123 {
124   _PTR(GenericAttribute) anAttr;
125   if ( myObject && myObject->FindAttribute( anAttr, "AttributePixMap" ) ){
126     _PTR(AttributePixMap) aPixAttr ( anAttr );
127     if ( aPixAttr->HasPixMap() ){
128       QString pixmapName = QObject::tr( aPixAttr->GetPixMap().c_str() );
129       SalomeApp_RootObject* aRoot = dynamic_cast<SalomeApp_RootObject*>( root() );
130       if ( aRoot && aRoot->study() ) {
131         QPixmap pixmap = aRoot->study()->application()->resourceMgr()->loadPixmap( componentDataType(), pixmapName, false ); 
132         return pixmap;
133       }
134     }
135   }
136   return QPixmap();
137 }
138
139 /*!Gets text value for one of entity:
140  *\li Value           (id = SalomeApp_DataObject::CT_Value)
141  *\li Entry           (id = SalomeApp_DataObject::CT_Entry)
142  *\li IOR             (id = SalomeApp_DataObject::CT_IOR)
143  *\li Reference entry (id = SalomeApp_DataObject::CT_RefEntry)
144  */
145 QString SalomeApp_DataObject::text( const int id ) const
146 {
147   QString txt;
148   switch ( id )
149   {
150   case CT_Value:
151 #ifndef WNT
152     if ( componentObject() != this )
153 #else
154     if ( componentObject() != (SUIT_DataObject*)this )
155 #endif
156       txt = value( referencedObject() );
157     break;
158   case CT_Entry:
159     txt = entry( referencedObject() );
160     break;
161   case CT_IOR:
162     txt = ior( referencedObject() );
163     break;
164   case CT_RefEntry:
165     if ( isReference() )
166       txt = entry( object() );
167     break;
168   }
169   return txt;
170 }
171
172 /*!Get color value for one of entity:
173  *\li Text color
174  *\li Highlight color
175  *\li Higlighted text color
176  */
177 QColor SalomeApp_DataObject::color( const ColorRole cr ) const
178 {
179   QColor clr;
180   switch ( cr )
181   {
182   case Text:
183     if ( isReference() )
184       clr = QColor( 255, 0, 0 );
185     else if ( myObject )
186     {
187       _PTR(GenericAttribute) anAttr;
188       if ( myObject->FindAttribute( anAttr, "AttributeTextColor" ) )
189       {
190         _PTR(AttributeTextColor) aColAttr = anAttr;
191         clr = QColor( (int)aColAttr->TextColor().R, (int)aColAttr->TextColor().G, (int)aColAttr->TextColor().B );
192       }
193     }
194     break;
195   case Highlight:
196     if ( isReference() )
197       clr = QColor( 255, 0, 0 );
198     break;
199   case HighlightedText:
200     if ( isReference() )
201       clr = QColor( 255, 255, 255 );
202     break;
203   }
204   return clr;
205 }
206
207 /*!Gets tooltip.*/
208 QString SalomeApp_DataObject::toolTip() const
209 {
210   //return object()->Name();
211   return QString( "Object \'%1\', module \'%2\', ID=%3" ).arg( name() ).arg( componentDataType() ).arg( entry() );
212 }
213
214 /*!Gets component object.
215  *\retval SUIT_DataObject.
216  */
217 SUIT_DataObject* SalomeApp_DataObject::componentObject() const
218 {
219   SUIT_DataObject* compObj = 0;  // for root object (invisible SALOME_ROOT_OBJECT) 
220
221   if ( parent() && parent() == root() ) 
222     compObj = (SUIT_DataObject*)this; // for component-level objects
223   else 
224   {
225     compObj = parent(); // for lower level objects
226     while ( compObj && compObj->parent() != root() )
227       compObj = compObj->parent();
228   }
229
230   return compObj;
231 }
232
233 /*!Get component type.*/
234 QString SalomeApp_DataObject::componentDataType() const
235 {
236   const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
237   if ( compObj && compObj->object() )
238   {
239     _PTR(SComponent) aComp( compObj->object() );
240     if ( aComp )
241       return aComp->ComponentDataType().c_str();
242   }
243
244   return "";
245 }
246
247 /*!Gets object.*/
248 _PTR(SObject) SalomeApp_DataObject::object() const
249 {
250   return myObject;
251 }
252
253 /*!Checks: Is object reference.*/
254 bool SalomeApp_DataObject::isReference() const
255 {
256   bool isRef = false;
257   if ( myObject )
258   {
259     _PTR(SObject) refObj;
260     isRef = myObject->ReferencedObject( refObj );
261   }
262   return isRef;
263 }
264
265 /*!Gets reference object.*/
266 _PTR(SObject) SalomeApp_DataObject::referencedObject() const
267 {
268   _PTR(SObject) refObj;
269   _PTR(SObject) obj = myObject;
270   while ( obj && obj->ReferencedObject( refObj ) )
271     obj = refObj;
272
273   return obj;
274 }
275
276 /*!Gets IOR*/
277 QString SalomeApp_DataObject::ior( const _PTR(SObject)& obj ) const
278 {
279   QString txt;
280   if ( obj )
281   {
282     _PTR(GenericAttribute) attr;
283     if ( obj->FindAttribute( attr, "AttributeIOR" ) )
284     {
285       _PTR(AttributeIOR) iorAttr = attr;
286       if ( iorAttr )
287       {
288         std::string str = iorAttr->Value();
289         txt = QString( str.c_str() );
290       }
291     }
292   }
293   return txt;
294 }
295
296 /*!Gets Entry*/
297 QString SalomeApp_DataObject::entry( const _PTR(SObject)& obj ) const
298 {
299   QString txt;
300   if ( obj )
301   {
302     std::string str = obj->GetID();
303     txt = QString( str.c_str() );
304   }
305   return txt;
306 }
307
308 /*!Value*/
309 QString SalomeApp_DataObject::value( const _PTR(SObject)& obj ) const
310 {
311   if ( !obj )
312     return QString::null;
313
314   QString val;
315   _PTR(GenericAttribute) attr;
316
317   if ( obj->FindAttribute( attr, "AttributeInteger" ) )
318   {
319     _PTR(AttributeInteger) intAttr = attr;
320     if ( intAttr )
321       val = QString::number( intAttr->Value() );
322   }
323   else if ( obj->FindAttribute( attr, "AttributeReal" ) )
324   {
325     _PTR(AttributeReal) realAttr = attr;
326     if ( realAttr )
327       val = QString::number( realAttr->Value() );
328   }
329   else if ( obj->FindAttribute( attr, "AttributeTableOfInteger" ) )
330   {
331     _PTR(AttributeTableOfInteger) tableAttr = attr;
332     std::string title = tableAttr->GetTitle();
333     val = QString( title.c_str() );
334     if ( !val.isEmpty() )
335       val += QString( " " );
336     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
337   }
338   else if ( obj->FindAttribute( attr, "AttributeTableOfReal" ) )
339   {
340     _PTR(AttributeTableOfReal) tableAttr = attr;
341     std::string title = tableAttr->GetTitle();
342     val = QString( title.c_str() );
343     if ( !val.isEmpty() )
344       val += QString( " " );
345     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
346   }
347   else if ( obj->FindAttribute( attr, "AttributeComment") )
348   {
349     _PTR(AttributeComment) comm = attr;
350     std::string str = comm->Value();
351     val = QString( str.c_str() );
352   }
353
354   return val;
355 }
356
357 /*
358         Class: SalomeApp_ModuleObject
359         Level: Public
360 */
361
362 /*!Constructor.Initialize by \a parent.*/
363 SalomeApp_ModuleObject::SalomeApp_ModuleObject( SUIT_DataObject* parent )
364 : SalomeApp_DataObject( parent ), 
365   CAM_RootObject( parent ),
366   CAM_DataObject( parent )
367 {
368 }
369
370 /*!Constructor.Initialize by \a parent and SObject.*/
371 SalomeApp_ModuleObject::SalomeApp_ModuleObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
372 : SalomeApp_DataObject( sobj, parent ), 
373   CAM_RootObject( 0, parent ),
374   CAM_DataObject( parent )
375 {
376 }
377
378 /*!Constructor.Initialize by \a parent and CAM_DataModel.*/
379 SalomeApp_ModuleObject::SalomeApp_ModuleObject( CAM_DataModel* dm, const _PTR(SObject)& sobj, SUIT_DataObject* parent )
380 : SalomeApp_DataObject( sobj, parent ), 
381   CAM_RootObject( dm, parent ),
382   CAM_DataObject( parent )  
383 {
384 }
385
386 /*!Destructor. Do nothing.*/
387 SalomeApp_ModuleObject::~SalomeApp_ModuleObject()
388 {
389 }