Salome HOME
Multi column support for Object browser
[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     if ( myObject )
163     {
164       _PTR(GenericAttribute) anAttr;
165       if ( myObject->FindAttribute( anAttr, "AttributeTextColor" ) )
166       {
167         _PTR(AttributeTextColor) aColAttr = anAttr;
168         clr = QColor( (int)aColAttr->TextColor().R, (int)aColAttr->TextColor().G, (int)aColAttr->TextColor().B );
169       }
170     }
171     break;
172   case Highlight:
173     if ( isReference() )
174       clr = QColor( 255, 0, 0 );
175     break;
176   case HighlightedText:
177     if ( isReference() )
178       clr = QColor( 255, 255, 255 );
179     break;
180   }
181   return clr;
182 }
183
184 QString SalomeApp_DataObject::toolTip() const
185 {
186   //return object()->Name();
187   return QString( "Object \'%1\', module \'%2\', ID=%3" ).arg( name() ).arg( componentDataType() ).arg( entry() );
188 }
189
190 SUIT_DataObject* SalomeApp_DataObject::componentObject() const
191 {
192   SUIT_DataObject* compObj = 0;  // for root object (invisible SALOME_ROOT_OBJECT) 
193
194   if ( parent() && parent() == root() ) 
195     compObj = (SUIT_DataObject*)this; // for component-level objects
196   else 
197   {
198     compObj = parent(); // for lower level objects
199     while ( compObj && compObj->parent() != root() )
200       compObj = compObj->parent();
201   }
202
203   return compObj;
204 }
205
206 QString SalomeApp_DataObject::componentDataType() const
207 {
208   const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
209   if ( compObj && compObj->object() )
210   {
211     _PTR(SComponent) aComp( compObj->object() );
212     if ( aComp )
213       return aComp->ComponentDataType().c_str();
214   }
215
216   return "";
217 }
218
219 _PTR(SObject) SalomeApp_DataObject::object() const
220 {
221   return myObject;
222 }
223
224 bool SalomeApp_DataObject::isReference() const
225 {
226   bool isRef = false;
227   if ( myObject )
228   {
229     _PTR(SObject) refObj;
230     isRef = myObject->ReferencedObject( refObj );
231   }
232   return isRef;
233 }
234
235 _PTR(SObject) SalomeApp_DataObject::referencedObject() const
236 {
237   _PTR(SObject) refObj;
238   _PTR(SObject) obj = myObject;
239   while ( obj && obj->ReferencedObject( refObj ) )
240     obj = refObj;
241
242   return obj;
243 }
244
245 QString SalomeApp_DataObject::ior( const _PTR(SObject)& obj ) const
246 {
247   QString txt;
248   if ( obj )
249   {
250     _PTR(GenericAttribute) attr;
251     if ( obj->FindAttribute( attr, "AttributeIOR" ) )
252     {
253       _PTR(AttributeIOR) iorAttr = attr;
254       if ( iorAttr )
255       {
256         std::string str = iorAttr->Value();
257         txt = QString( str.c_str() );
258       }
259     }
260   }
261   return txt;
262 }
263
264 QString SalomeApp_DataObject::entry( const _PTR(SObject)& obj ) const
265 {
266   QString txt;
267   if ( obj )
268   {
269     std::string str = obj->GetID();
270     txt = QString( str.c_str() );
271   }
272   return txt;
273 }
274
275 QString SalomeApp_DataObject::value( const _PTR(SObject)& obj ) const
276 {
277   if ( !obj )
278     return QString::null;
279
280   QString val;
281   _PTR(GenericAttribute) attr;
282
283   if ( obj->FindAttribute( attr, "AttributeInteger" ) )
284   {
285     _PTR(AttributeInteger) intAttr = attr;
286     if ( intAttr )
287       val = QString::number( intAttr->Value() );
288   }
289   else if ( obj->FindAttribute( attr, "AttributeReal" ) )
290   {
291     _PTR(AttributeReal) realAttr = attr;
292     if ( realAttr )
293       val = QString::number( realAttr->Value() );
294   }
295   else if ( obj->FindAttribute( attr, "AttributeTableOfInteger" ) )
296   {
297     _PTR(AttributeTableOfInteger) tableAttr = attr;
298     std::string title = tableAttr->GetTitle();
299     val = QString( title.c_str() );
300     if ( !val.isEmpty() )
301       val += QString( " " );
302     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
303   }
304   else if ( obj->FindAttribute( attr, "AttributeTableOfReal" ) )
305   {
306     _PTR(AttributeTableOfReal) tableAttr = attr;
307     std::string title = tableAttr->GetTitle();
308     val = QString( title.c_str() );
309     if ( !val.isEmpty() )
310       val += QString( " " );
311     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
312   }
313   else if ( obj->FindAttribute( attr, "AttributeComment") )
314   {
315     _PTR(AttributeComment) comm = attr;
316     std::string str = comm->Value();
317     val = QString( str.c_str() );
318   }
319
320   return val;
321 }
322
323 /*!
324         Class: SalomeApp_ModuleObject
325         Level: Public
326 */
327
328 SalomeApp_ModuleObject::SalomeApp_ModuleObject( SUIT_DataObject* parent )
329 : SalomeApp_DataObject( parent ), 
330   CAM_RootObject( parent ),
331   CAM_DataObject( parent )
332 {
333 }
334
335 SalomeApp_ModuleObject::SalomeApp_ModuleObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
336 : SalomeApp_DataObject( sobj, parent ), 
337   CAM_RootObject( 0, parent ),
338   CAM_DataObject( parent )
339 {
340 }
341
342 SalomeApp_ModuleObject::SalomeApp_ModuleObject( CAM_DataModel* dm, const _PTR(SObject)& sobj, SUIT_DataObject* parent )
343 : SalomeApp_DataObject( sobj, parent ), 
344   CAM_RootObject( dm, parent ),
345   CAM_DataObject( parent )  
346 {
347 }
348
349 SalomeApp_ModuleObject::~SalomeApp_ModuleObject()
350 {
351 }