]> SALOME platform Git repositories - modules/gui.git/blob - src/SalomeApp/SalomeApp_DataObject.cxx
Salome HOME
Preparation of v.3.0.1
[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     if ( componentObject() != this )
152       txt = value( referencedObject() );
153     break;
154   case CT_Entry:
155     txt = entry( referencedObject() );
156     break;
157   case CT_IOR:
158     txt = ior( referencedObject() );
159     break;
160   case CT_RefEntry:
161     if ( isReference() )
162       txt = entry( object() );
163     break;
164   }
165   return txt;
166 }
167
168 /*!Get color value for one of entity:
169  *\li Text color
170  *\li Highlight color
171  *\li Higlighted text color
172  */
173 QColor SalomeApp_DataObject::color( const ColorRole cr ) const
174 {
175   QColor clr;
176   switch ( cr )
177   {
178   case Text:
179     if ( isReference() )
180       clr = QColor( 255, 0, 0 );
181     else if ( myObject )
182     {
183       _PTR(GenericAttribute) anAttr;
184       if ( myObject->FindAttribute( anAttr, "AttributeTextColor" ) )
185       {
186         _PTR(AttributeTextColor) aColAttr = anAttr;
187         clr = QColor( (int)aColAttr->TextColor().R, (int)aColAttr->TextColor().G, (int)aColAttr->TextColor().B );
188       }
189     }
190     break;
191   case Highlight:
192     if ( isReference() )
193       clr = QColor( 255, 0, 0 );
194     break;
195   case HighlightedText:
196     if ( isReference() )
197       clr = QColor( 255, 255, 255 );
198     break;
199   }
200   return clr;
201 }
202
203 /*!Gets tooltip.*/
204 QString SalomeApp_DataObject::toolTip() const
205 {
206   //return object()->Name();
207   return QString( "Object \'%1\', module \'%2\', ID=%3" ).arg( name() ).arg( componentDataType() ).arg( entry() );
208 }
209
210 /*!Gets component object.
211  *\retval SUIT_DataObject.
212  */
213 SUIT_DataObject* SalomeApp_DataObject::componentObject() const
214 {
215   SUIT_DataObject* compObj = 0;  // for root object (invisible SALOME_ROOT_OBJECT) 
216
217   if ( parent() && parent() == root() ) 
218     compObj = (SUIT_DataObject*)this; // for component-level objects
219   else 
220   {
221     compObj = parent(); // for lower level objects
222     while ( compObj && compObj->parent() != root() )
223       compObj = compObj->parent();
224   }
225
226   return compObj;
227 }
228
229 /*!Get component type.*/
230 QString SalomeApp_DataObject::componentDataType() const
231 {
232   const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
233   if ( compObj && compObj->object() )
234   {
235     _PTR(SComponent) aComp( compObj->object() );
236     if ( aComp )
237       return aComp->ComponentDataType().c_str();
238   }
239
240   return "";
241 }
242
243 /*!Gets object.*/
244 _PTR(SObject) SalomeApp_DataObject::object() const
245 {
246   return myObject;
247 }
248
249 /*!Checks: Is object reference.*/
250 bool SalomeApp_DataObject::isReference() const
251 {
252   bool isRef = false;
253   if ( myObject )
254   {
255     _PTR(SObject) refObj;
256     isRef = myObject->ReferencedObject( refObj );
257   }
258   return isRef;
259 }
260
261 /*!Gets reference object.*/
262 _PTR(SObject) SalomeApp_DataObject::referencedObject() const
263 {
264   _PTR(SObject) refObj;
265   _PTR(SObject) obj = myObject;
266   while ( obj && obj->ReferencedObject( refObj ) )
267     obj = refObj;
268
269   return obj;
270 }
271
272 /*!Gets IOR*/
273 QString SalomeApp_DataObject::ior( const _PTR(SObject)& obj ) const
274 {
275   QString txt;
276   if ( obj )
277   {
278     _PTR(GenericAttribute) attr;
279     if ( obj->FindAttribute( attr, "AttributeIOR" ) )
280     {
281       _PTR(AttributeIOR) iorAttr = attr;
282       if ( iorAttr )
283       {
284         std::string str = iorAttr->Value();
285         txt = QString( str.c_str() );
286       }
287     }
288   }
289   return txt;
290 }
291
292 /*!Gets Entry*/
293 QString SalomeApp_DataObject::entry( const _PTR(SObject)& obj ) const
294 {
295   QString txt;
296   if ( obj )
297   {
298     std::string str = obj->GetID();
299     txt = QString( str.c_str() );
300   }
301   return txt;
302 }
303
304 /*!Value*/
305 QString SalomeApp_DataObject::value( const _PTR(SObject)& obj ) const
306 {
307   if ( !obj )
308     return QString::null;
309
310   QString val;
311   _PTR(GenericAttribute) attr;
312
313   if ( obj->FindAttribute( attr, "AttributeInteger" ) )
314   {
315     _PTR(AttributeInteger) intAttr = attr;
316     if ( intAttr )
317       val = QString::number( intAttr->Value() );
318   }
319   else if ( obj->FindAttribute( attr, "AttributeReal" ) )
320   {
321     _PTR(AttributeReal) realAttr = attr;
322     if ( realAttr )
323       val = QString::number( realAttr->Value() );
324   }
325   else if ( obj->FindAttribute( attr, "AttributeTableOfInteger" ) )
326   {
327     _PTR(AttributeTableOfInteger) tableAttr = attr;
328     std::string title = tableAttr->GetTitle();
329     val = QString( title.c_str() );
330     if ( !val.isEmpty() )
331       val += QString( " " );
332     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
333   }
334   else if ( obj->FindAttribute( attr, "AttributeTableOfReal" ) )
335   {
336     _PTR(AttributeTableOfReal) tableAttr = attr;
337     std::string title = tableAttr->GetTitle();
338     val = QString( title.c_str() );
339     if ( !val.isEmpty() )
340       val += QString( " " );
341     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
342   }
343   else if ( obj->FindAttribute( attr, "AttributeComment") )
344   {
345     _PTR(AttributeComment) comm = attr;
346     std::string str = comm->Value();
347     val = QString( str.c_str() );
348   }
349
350   return val;
351 }
352
353 /*
354         Class: SalomeApp_ModuleObject
355         Level: Public
356 */
357
358 /*!Constructor.Initialize by \a parent.*/
359 SalomeApp_ModuleObject::SalomeApp_ModuleObject( SUIT_DataObject* parent )
360 : SalomeApp_DataObject( parent ), 
361   CAM_RootObject( parent ),
362   CAM_DataObject( parent )
363 {
364 }
365
366 /*!Constructor.Initialize by \a parent and SObject.*/
367 SalomeApp_ModuleObject::SalomeApp_ModuleObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
368 : SalomeApp_DataObject( sobj, parent ), 
369   CAM_RootObject( 0, parent ),
370   CAM_DataObject( parent )
371 {
372 }
373
374 /*!Constructor.Initialize by \a parent and CAM_DataModel.*/
375 SalomeApp_ModuleObject::SalomeApp_ModuleObject( CAM_DataModel* dm, const _PTR(SObject)& sobj, SUIT_DataObject* parent )
376 : SalomeApp_DataObject( sobj, parent ), 
377   CAM_RootObject( dm, parent ),
378   CAM_DataObject( parent )  
379 {
380 }
381
382 /*!Destructor. Do nothing.*/
383 SalomeApp_ModuleObject::~SalomeApp_ModuleObject()
384 {
385 }