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