Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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/ or email : webmaster.salome@opencascade.com
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 /*!Constructor. Initialize by \a parent*/
39 SalomeApp_DataObject::SalomeApp_DataObject( SUIT_DataObject* parent )
40 : LightApp_DataObject( parent ),
41   CAM_DataObject( parent ),
42   myEntry( "" ),
43   myName( "" )
44 {
45 }
46
47 /*!Constructor. Initialize by \a parent and SObject*/
48 SalomeApp_DataObject::SalomeApp_DataObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
49 : LightApp_DataObject( parent ),
50   CAM_DataObject( parent ),
51   myName( "" )
52 {
53   myObject = sobj;
54   myEntry = myObject->GetID().c_str();
55 }
56
57 /*!Destructor. Do nothing.*/
58 SalomeApp_DataObject::~SalomeApp_DataObject()
59 {
60 }
61
62 /*!Gets object ID.
63  *\retval QString
64  */
65 QString SalomeApp_DataObject::entry() const
66 {
67  if ( myObject )
68     return myObject->GetID().c_str();
69   return QString::null;
70   //return myEntry;
71 }
72
73 /*!Gets name of object.*/
74 QString SalomeApp_DataObject::name() const
75 {
76   //if ( myName.isEmpty() )
77   {
78     QString str;
79     if ( myObject )
80       str = myObject->GetName().c_str();
81
82     if ( str.isEmpty() )
83     {
84       _PTR(SObject) refObj = referencedObject();
85       if ( refObj )
86         str = refObj->GetName().c_str();
87     }
88
89     if ( isReference() )
90       {
91         if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
92           str = QString( "* " ) + str;
93         else
94           str = QString( "<Invalid Reference>" );
95       }
96     SalomeApp_DataObject* that = (SalomeApp_DataObject*)this;
97     that->myName = str;
98   }
99   return myName;
100 }
101
102 /*!Gets icon picture of object.*/
103 QPixmap SalomeApp_DataObject::icon() const
104 {
105   _PTR(GenericAttribute) anAttr;
106   if ( myObject && myObject->FindAttribute( anAttr, "AttributePixMap" ) ){
107     _PTR(AttributePixMap) aPixAttr ( anAttr );
108     if ( aPixAttr->HasPixMap() ){
109       QString pixmapName = QObject::tr( aPixAttr->GetPixMap().c_str() );
110       LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() );
111       if ( aRoot && aRoot->study() ) {
112         SUIT_ResourceMgr* mgr = aRoot->study()->application()->resourceMgr();
113         return mgr->loadPixmap( componentDataType(), pixmapName, false ); 
114       }
115     }
116   }
117   return QPixmap();
118 }
119
120 /*!Gets text value for one of entity:
121  *\li Value           (id = SalomeApp_DataObject::CT_Value)
122  *\li Entry           (id = SalomeApp_DataObject::CT_Entry)
123  *\li IOR             (id = SalomeApp_DataObject::CT_IOR)
124  *\li Reference entry (id = SalomeApp_DataObject::CT_RefEntry)
125  */
126 QString SalomeApp_DataObject::text( const int id ) const
127 {
128   QString txt;
129   switch ( id )
130   {
131   case CT_Value:
132 #ifndef WNT
133     if ( componentObject() != this )
134 #else
135     if ( componentObject() != (SUIT_DataObject*)this )
136 #endif
137       txt = value( object() );
138       if ( txt.isEmpty() )
139         txt = value( referencedObject() );
140     break;
141   case CT_Entry:
142     txt = entry( object() );
143     break;
144   case CT_IOR:
145     txt = ior( referencedObject() );
146     break;
147   case CT_RefEntry:
148     if ( isReference() )
149       txt = entry( referencedObject() );
150     break;
151   }
152   return txt;
153 }
154
155 /*!Get color value for one of entity:
156  *\li Text color
157  *\li Highlight color
158  *\li Higlighted text color
159  */
160 QColor SalomeApp_DataObject::color( const ColorRole cr ) const
161 {
162   QColor clr;
163   switch ( cr )
164   {
165   case Text:
166     if ( isReference() )
167       {
168         if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
169           clr = QColor( 255, 0, 0 );
170         else
171           clr = QColor( 200, 200, 200 );
172       }
173     else if ( myObject )
174     {
175       _PTR(GenericAttribute) anAttr;
176       if ( myObject->FindAttribute( anAttr, "AttributeTextColor" ) )
177       {
178         _PTR(AttributeTextColor) aColAttr = anAttr;
179         clr = QColor( (int)aColAttr->TextColor().R, (int)aColAttr->TextColor().G, (int)aColAttr->TextColor().B );
180       }
181     }
182     break;
183   case Highlight:
184     if ( isReference() )
185       {
186         if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
187           clr = QColor( 255, 0, 0 );
188         else
189           clr = QColor( 200, 200, 200 );
190       }
191     else if ( myObject )
192     {
193       _PTR(GenericAttribute) anAttr;
194       if( myObject->FindAttribute ( anAttr, "AttributeTextHighlightColor") )
195       {
196         _PTR(AttributeTextHighlightColor) aHighColAttr = anAttr;
197         clr = QColor( (int)(aHighColAttr->TextHighlightColor().R), 
198                       (int)(aHighColAttr->TextHighlightColor().G), 
199                       (int)(aHighColAttr->TextHighlightColor().B));
200       }
201     }
202     break;
203   case HighlightedText:
204     if ( isReference() )
205       clr = QColor( 255, 255, 255 );
206     break;
207   }
208   return clr;
209 }
210
211 /*!Gets tooltip.*/
212 QString SalomeApp_DataObject::toolTip() const
213 {
214   //return object()->Name();
215   return QString( "Object \'%1\', module \'%2\', ID=%3" ).arg( name() ).arg( componentDataType() ).arg( entry() );
216 }
217
218 /*!Get component type.*/
219 QString SalomeApp_DataObject::componentDataType() const
220 {
221   //  if ( myCompDataType.isEmpty() ) {
222     const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
223     if ( compObj && compObj->object() )
224     {
225       _PTR(SComponent) aComp( compObj->object() );
226       if ( aComp ) {
227         SalomeApp_DataObject* that = (SalomeApp_DataObject*)this;
228         that->myCompDataType = aComp->ComponentDataType().c_str();
229       }
230     }
231     //  }
232   return myCompDataType;
233 }
234
235 /*!Gets object.*/
236 _PTR(SObject) SalomeApp_DataObject::object() const
237 {
238   return myObject;
239 }
240
241 /*!Checks: Is object reference.*/
242 bool SalomeApp_DataObject::isReference() const
243 {
244   bool isRef = false;
245   if ( myObject )
246   {
247     _PTR(SObject) refObj;
248     isRef = myObject->ReferencedObject( refObj );
249   }
250   return isRef;
251 }
252
253 /*!Gets reference object.*/
254 _PTR(SObject) SalomeApp_DataObject::referencedObject() const
255 {
256   _PTR(SObject) refObj;
257   _PTR(SObject) obj = myObject;
258   while ( obj && obj->ReferencedObject( refObj ) )
259     obj = refObj;
260
261   return obj;
262 }
263
264 /*!Gets IOR*/
265 QString SalomeApp_DataObject::ior( const _PTR(SObject)& obj ) const
266 {
267   QString txt;
268   if ( obj )
269   {
270     _PTR(GenericAttribute) attr;
271     if ( obj->FindAttribute( attr, "AttributeIOR" ) )
272     {
273       _PTR(AttributeIOR) iorAttr = attr;
274       if ( iorAttr )
275       {
276         std::string str = iorAttr->Value();
277         txt = QString( str.c_str() );
278       }
279     }
280   }
281   return txt;
282 }
283
284 /*!Gets Entry*/
285 QString SalomeApp_DataObject::entry( const _PTR(SObject)& obj ) const
286 {
287   QString txt;
288   if ( obj )
289   {
290     std::string str = obj->GetID();
291     txt = QString( str.c_str() );
292   }
293   return txt;
294 }
295
296 /*!Value*/
297 QString SalomeApp_DataObject::value( const _PTR(SObject)& obj ) const
298 {
299   if ( !obj )
300     return QString::null;
301
302   QString val;
303   _PTR(GenericAttribute) attr;
304
305   if ( obj->FindAttribute( attr, "AttributeInteger" ) )
306   {
307     _PTR(AttributeInteger) intAttr = attr;
308     if ( intAttr )
309       val = QString::number( intAttr->Value() );
310   }
311   else if ( obj->FindAttribute( attr, "AttributeReal" ) )
312   {
313     _PTR(AttributeReal) realAttr = attr;
314     if ( realAttr )
315       val = QString::number( realAttr->Value() );
316   }
317   else if ( obj->FindAttribute( attr, "AttributeTableOfInteger" ) )
318   {
319     _PTR(AttributeTableOfInteger) 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, "AttributeTableOfReal" ) )
327   {
328     _PTR(AttributeTableOfReal) tableAttr = attr;
329     std::string title = tableAttr->GetTitle();
330     val = QString( title.c_str() );
331     if ( !val.isEmpty() )
332       val += QString( " " );
333     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
334   }
335   else if ( obj->FindAttribute( attr, "AttributeComment") )
336   {
337     _PTR(AttributeComment) comm = attr;
338     std::string str = comm->Value();
339     val = QString( str.c_str() );
340   }
341
342   return val;
343 }
344
345
346
347
348
349 /*!Constructor.Initialize by \a parent.*/
350 SalomeApp_ModuleObject::SalomeApp_ModuleObject( SUIT_DataObject* parent )
351 : SalomeApp_DataObject( parent ),
352   CAM_RootObject( parent ),
353   CAM_DataObject( parent )
354 {
355 }
356
357 /*!Constructor.Initialize by \a parent and SObject.*/
358 SalomeApp_ModuleObject::SalomeApp_ModuleObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
359 : SalomeApp_DataObject( sobj, parent ),
360   CAM_RootObject( 0, parent ),
361   CAM_DataObject( parent )
362 {
363 }
364
365 /*!Constructor.Initialize by \a parent and CAM_DataModel.*/
366 SalomeApp_ModuleObject::SalomeApp_ModuleObject( CAM_DataModel* dm, const _PTR(SObject)& sobj, SUIT_DataObject* parent )
367 : SalomeApp_DataObject( sobj, parent ),
368   CAM_RootObject( dm, parent ),
369   CAM_DataObject( parent )
370 {
371 }
372
373 /*!Destructor. Do nothing.*/
374 SalomeApp_ModuleObject::~SalomeApp_ModuleObject()
375 {
376 }
377
378 /*!Returns module name */
379 QString SalomeApp_ModuleObject::name() const
380 {
381   return SalomeApp_DataObject::name();
382 }
383
384
385
386
387 /*!Constructor.Initialize by \a parent.*/
388 SalomeApp_SavePointObject::SalomeApp_SavePointObject( SUIT_DataObject* _parent, const int id, SalomeApp_Study* study )
389   : LightApp_DataObject( _parent ), CAM_DataObject( _parent ), // IMPORTANT TO CALL ALL VIRTUAL CONSTRUCTORS!
390     myId( id ),
391     myStudy( study )
392 {
393 }
394
395 /*!Destructor. Do nothing.*/
396 SalomeApp_SavePointObject::~SalomeApp_SavePointObject()
397 {
398 }
399
400 /*!Returns save points ID */
401 int SalomeApp_SavePointObject::getId() const
402 {
403   return myId;
404 }
405
406 /*!Returns "invalid" entry, which does not correspond to any object in data structure
407   but indicates that it is a save point object  */
408 QString SalomeApp_SavePointObject::entry() const
409 {
410   return QObject::tr( "SAVE_POINT_DEF_NAME" ) + QString::number( myId );
411 }
412
413 /*!Returns displayed name of object */
414 QString SalomeApp_SavePointObject::name() const
415 {
416   return myStudy->getNameOfSavePoint( myId );
417 }
418
419 /*!Gets icon picture of object.*/
420 QPixmap SalomeApp_SavePointObject::icon() const
421 {
422   return QPixmap();
423 }
424
425 /*!Gets tooltip.*/
426 QString SalomeApp_SavePointObject::toolTip() const
427 {
428   return QObject::tr( "SAVE_POINT_OBJECT_TOOLTIP" ).arg( name() );
429 }
430