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