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