Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/gui.git] / src / SalomeApp / SalomeApp_DataObject.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "SalomeApp_DataObject.h"
20
21 #include "SalomeApp_Study.h"
22 #include "LightApp_RootObject.h"
23
24 #include <CAM_DataObject.h>
25
26 #include <SUIT_Application.h>
27 #include <SUIT_ResourceMgr.h>
28 #include <SUIT_DataObjectKey.h>
29
30 #include <qobject.h>
31
32 #include <SALOMEDSClient_AttributeReal.hxx>
33 #include <SALOMEDSClient_AttributeInteger.hxx>
34 #include <SALOMEDSClient_AttributeComment.hxx>
35 #include <SALOMEDSClient_AttributeTableOfReal.hxx>
36 #include <SALOMEDSClient_AttributeTableOfInteger.hxx>
37
38 /*
39         Class: SalomeApp_DataObject
40         Level: Public
41 */
42 /*!Constructor. Initialize by \a parent*/
43 SalomeApp_DataObject::SalomeApp_DataObject( SUIT_DataObject* parent )
44 : LightApp_DataObject( parent ),
45   CAM_DataObject( parent ),
46   myEntry( "" ),
47   myName( "" )
48 {
49 }
50
51 /*!Constructor. Initialize by \a parent and SObject*/
52 SalomeApp_DataObject::SalomeApp_DataObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
53 : LightApp_DataObject( parent ),
54   CAM_DataObject( parent ),
55   myName( "" )
56 {
57   myObject = sobj;
58   myEntry = myObject->GetID().c_str();
59 }
60
61 /*!Destructor. Do nothing.*/
62 SalomeApp_DataObject::~SalomeApp_DataObject()
63 {
64 }
65
66 /*!Gets object ID.
67  *\retval QString
68  */
69 QString SalomeApp_DataObject::entry() const
70 {
71  if ( myObject )
72     return myObject->GetID().c_str();
73   return QString::null;
74   //return myEntry;
75 }
76
77 /*!Gets name of object.*/
78 QString SalomeApp_DataObject::name() const
79 {
80   //if ( myName.isEmpty() )
81   {
82     QString str;
83     if ( myObject )
84       str = myObject->GetName().c_str();
85
86     if ( str.isEmpty() )
87     {
88       _PTR(SObject) refObj = referencedObject();
89       if ( refObj )
90         str = refObj->GetName().c_str();
91     }
92
93     if ( isReference() )
94       {
95         if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
96           str = QString( "* " ) + str;
97         else
98           str = QString( "<Invalid Reference>" );
99       }
100     SalomeApp_DataObject* that = (SalomeApp_DataObject*)this;
101     that->myName = str;
102   }
103   return myName;
104 }
105
106 /*!Gets icon picture of object.*/
107 QPixmap SalomeApp_DataObject::icon() const
108 {
109   _PTR(GenericAttribute) anAttr;
110   if ( myObject && myObject->FindAttribute( anAttr, "AttributePixMap" ) ){
111     _PTR(AttributePixMap) aPixAttr ( anAttr );
112     if ( aPixAttr->HasPixMap() ){
113       QString pixmapName = QObject::tr( aPixAttr->GetPixMap().c_str() );
114       LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() );
115       if ( aRoot && aRoot->study() ) {
116         SUIT_ResourceMgr* mgr = aRoot->study()->application()->resourceMgr();
117         return mgr->loadPixmap( componentDataType(), pixmapName, false ); 
118       }
119     }
120   }
121   return QPixmap();
122 }
123
124 /*!Gets text value for one of entity:
125  *\li Value           (id = SalomeApp_DataObject::CT_Value)
126  *\li Entry           (id = SalomeApp_DataObject::CT_Entry)
127  *\li IOR             (id = SalomeApp_DataObject::CT_IOR)
128  *\li Reference entry (id = SalomeApp_DataObject::CT_RefEntry)
129  */
130 QString SalomeApp_DataObject::text( const int id ) const
131 {
132   QString txt;
133   switch ( id )
134   {
135   case CT_Value:
136 #ifndef WNT
137     if ( componentObject() != this )
138 #else
139     if ( componentObject() != (SUIT_DataObject*)this )
140 #endif
141       txt = value( referencedObject() );
142     break;
143   case CT_Entry:
144     txt = entry( object() );
145     break;
146   case CT_IOR:
147     txt = ior( referencedObject() );
148     break;
149   case CT_RefEntry:
150     if ( isReference() )
151       txt = entry( referencedObject() );
152     break;
153   }
154   return txt;
155 }
156
157 /*!Get color value for one of entity:
158  *\li Text color
159  *\li Highlight color
160  *\li Higlighted text color
161  */
162 QColor SalomeApp_DataObject::color( const ColorRole cr ) const
163 {
164   QColor clr;
165   switch ( cr )
166   {
167   case Text:
168     if ( isReference() )
169       {
170         if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
171           clr = QColor( 255, 0, 0 );
172         else
173           clr = QColor( 200, 200, 200 );
174       }
175     else if ( myObject )
176     {
177       _PTR(GenericAttribute) anAttr;
178       if ( myObject->FindAttribute( anAttr, "AttributeTextColor" ) )
179       {
180         _PTR(AttributeTextColor) aColAttr = anAttr;
181         clr = QColor( (int)aColAttr->TextColor().R, (int)aColAttr->TextColor().G, (int)aColAttr->TextColor().B );
182       }
183     }
184     break;
185   case Highlight:
186     if ( isReference() )
187       {
188         if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
189           clr = QColor( 255, 0, 0 );
190         else
191           clr = QColor( 200, 200, 200 );
192       }
193     break;
194   case HighlightedText:
195     if ( isReference() )
196       clr = QColor( 255, 255, 255 );
197     break;
198   }
199   return clr;
200 }
201
202 /*!Gets tooltip.*/
203 QString SalomeApp_DataObject::toolTip() const
204 {
205   //return object()->Name();
206   return QString( "Object \'%1\', module \'%2\', ID=%3" ).arg( name() ).arg( componentDataType() ).arg( entry() );
207 }
208
209 /*!Get component type.*/
210 QString SalomeApp_DataObject::componentDataType() const
211 {
212   //  if ( myCompDataType.isEmpty() ) {
213     const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
214     if ( compObj && compObj->object() )
215     {
216       _PTR(SComponent) aComp( compObj->object() );
217       if ( aComp ) {
218         SalomeApp_DataObject* that = (SalomeApp_DataObject*)this;
219         that->myCompDataType = aComp->ComponentDataType().c_str();
220       }
221     }
222     //  }
223   return myCompDataType;
224 }
225
226 /*!Gets object.*/
227 _PTR(SObject) SalomeApp_DataObject::object() const
228 {
229   return myObject;
230 }
231
232 /*!Checks: Is object reference.*/
233 bool SalomeApp_DataObject::isReference() const
234 {
235   bool isRef = false;
236   if ( myObject )
237   {
238     _PTR(SObject) refObj;
239     isRef = myObject->ReferencedObject( refObj );
240   }
241   return isRef;
242 }
243
244 /*!Gets reference object.*/
245 _PTR(SObject) SalomeApp_DataObject::referencedObject() const
246 {
247   _PTR(SObject) refObj;
248   _PTR(SObject) obj = myObject;
249   while ( obj && obj->ReferencedObject( refObj ) )
250     obj = refObj;
251
252   return obj;
253 }
254
255 /*!Gets IOR*/
256 QString SalomeApp_DataObject::ior( const _PTR(SObject)& obj ) const
257 {
258   QString txt;
259   if ( obj )
260   {
261     _PTR(GenericAttribute) attr;
262     if ( obj->FindAttribute( attr, "AttributeIOR" ) )
263     {
264       _PTR(AttributeIOR) iorAttr = attr;
265       if ( iorAttr )
266       {
267         std::string str = iorAttr->Value();
268         txt = QString( str.c_str() );
269       }
270     }
271   }
272   return txt;
273 }
274
275 /*!Gets Entry*/
276 QString SalomeApp_DataObject::entry( const _PTR(SObject)& obj ) const
277 {
278   QString txt;
279   if ( obj )
280   {
281     std::string str = obj->GetID();
282     txt = QString( str.c_str() );
283   }
284   return txt;
285 }
286
287 /*!Value*/
288 QString SalomeApp_DataObject::value( const _PTR(SObject)& obj ) const
289 {
290   if ( !obj )
291     return QString::null;
292
293   QString val;
294   _PTR(GenericAttribute) attr;
295
296   if ( obj->FindAttribute( attr, "AttributeInteger" ) )
297   {
298     _PTR(AttributeInteger) intAttr = attr;
299     if ( intAttr )
300       val = QString::number( intAttr->Value() );
301   }
302   else if ( obj->FindAttribute( attr, "AttributeReal" ) )
303   {
304     _PTR(AttributeReal) realAttr = attr;
305     if ( realAttr )
306       val = QString::number( realAttr->Value() );
307   }
308   else if ( obj->FindAttribute( attr, "AttributeTableOfInteger" ) )
309   {
310     _PTR(AttributeTableOfInteger) tableAttr = attr;
311     std::string title = tableAttr->GetTitle();
312     val = QString( title.c_str() );
313     if ( !val.isEmpty() )
314       val += QString( " " );
315     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
316   }
317   else if ( obj->FindAttribute( attr, "AttributeTableOfReal" ) )
318   {
319     _PTR(AttributeTableOfReal) tableAttr = attr;
320     std::string title = tableAttr->GetTitle();
321     val = QString( title.c_str() );
322     if ( !val.isEmpty() )
323       val += QString( " " );
324     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
325   }
326   else if ( obj->FindAttribute( attr, "AttributeComment") )
327   {
328     _PTR(AttributeComment) comm = attr;
329     std::string str = comm->Value();
330     val = QString( str.c_str() );
331   }
332
333   return val;
334 }
335
336 /*
337         Class: SalomeApp_ModuleObject
338         Level: Public
339 */
340
341 /*!Constructor.Initialize by \a parent.*/
342 SalomeApp_ModuleObject::SalomeApp_ModuleObject( SUIT_DataObject* parent )
343 : SalomeApp_DataObject( parent ),
344   CAM_RootObject( parent ),
345   CAM_DataObject( parent )
346 {
347 }
348
349 /*!Constructor.Initialize by \a parent and SObject.*/
350 SalomeApp_ModuleObject::SalomeApp_ModuleObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
351 : SalomeApp_DataObject( sobj, parent ),
352   CAM_RootObject( 0, parent ),
353   CAM_DataObject( parent )
354 {
355 }
356
357 /*!Constructor.Initialize by \a parent and CAM_DataModel.*/
358 SalomeApp_ModuleObject::SalomeApp_ModuleObject( CAM_DataModel* dm, const _PTR(SObject)& sobj, SUIT_DataObject* parent )
359 : SalomeApp_DataObject( sobj, parent ),
360   CAM_RootObject( dm, parent ),
361   CAM_DataObject( parent )
362 {
363 }
364
365 /*!Destructor. Do nothing.*/
366 SalomeApp_ModuleObject::~SalomeApp_ModuleObject()
367 {
368 }
369
370 /*!Returns module name */
371 QString SalomeApp_ModuleObject::name() const
372 {
373   return SalomeApp_DataObject::name();
374 }
375
376 /*
377         Class: SalomeApp_SavePointObject
378         Level: Public
379 */
380
381 /*!Constructor.Initialize by \a parent.*/
382 SalomeApp_SavePointObject::SalomeApp_SavePointObject( SUIT_DataObject* _parent, const int id, SalomeApp_Study* study )
383   : LightApp_DataObject( _parent ), CAM_DataObject( _parent ), // IMPORTANT TO CALL ALL VIRTUAL CONSTRUCTORS!
384     myId( id ),
385     myStudy( study )
386 {
387 }
388
389 /*!Destructor. Do nothing.*/
390 SalomeApp_SavePointObject::~SalomeApp_SavePointObject()
391 {
392 }
393
394 /*!Returns save points ID */
395 int SalomeApp_SavePointObject::getId() const
396 {
397   return myId;
398 }
399
400 /*!Returns "invalid" entry, which does not correspond to any object in data structure
401   but indicates that it is a save point object  */
402 QString SalomeApp_SavePointObject::entry() const
403 {
404   return QObject::tr( "SAVE_POINT_DEF_NAME" ) + QString::number( myId );
405 }
406
407 /*!Returns displayed name of object */
408 QString SalomeApp_SavePointObject::name() const
409 {
410   return myStudy->getNameOfSavePoint( myId );
411 }
412
413 QPixmap SalomeApp_SavePointObject::icon() const
414 {
415   return QPixmap();
416 }
417
418 QString SalomeApp_SavePointObject::toolTip() const
419 {
420   return QObject::tr( "SAVE_POINT_OBJECT_TOOLTIP" ).arg( name() );
421 }
422