]> SALOME platform Git repositories - modules/gui.git/blob - src/SalomeApp/SalomeApp_DataObject.cxx
Salome HOME
04d9aaf076aed1ba8bd08378d3b4900e0e43d7c8
[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 /*!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( referencedObject() );
138     break;
139   case CT_Entry:
140     txt = entry( object() );
141     break;
142   case CT_IOR:
143     txt = ior( referencedObject() );
144     break;
145   case CT_RefEntry:
146     if ( isReference() )
147       txt = entry( referencedObject() );
148     break;
149   }
150   return txt;
151 }
152
153 /*!Get color value for one of entity:
154  *\li Text color
155  *\li Highlight color
156  *\li Higlighted text color
157  */
158 QColor SalomeApp_DataObject::color( const ColorRole cr ) const
159 {
160   QColor clr;
161   switch ( cr )
162   {
163   case Text:
164     if ( isReference() )
165       {
166         if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
167           clr = QColor( 255, 0, 0 );
168         else
169           clr = QColor( 200, 200, 200 );
170       }
171     else if ( myObject )
172     {
173       _PTR(GenericAttribute) anAttr;
174       if ( myObject->FindAttribute( anAttr, "AttributeTextColor" ) )
175       {
176         _PTR(AttributeTextColor) aColAttr = anAttr;
177         clr = QColor( (int)aColAttr->TextColor().R, (int)aColAttr->TextColor().G, (int)aColAttr->TextColor().B );
178       }
179     }
180     break;
181   case Highlight:
182     if ( isReference() )
183       {
184         if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
185           clr = QColor( 255, 0, 0 );
186         else
187           clr = QColor( 200, 200, 200 );
188       }
189     break;
190   case HighlightedText:
191     if ( isReference() )
192       clr = QColor( 255, 255, 255 );
193     break;
194   }
195   return clr;
196 }
197
198 /*!Gets tooltip.*/
199 QString SalomeApp_DataObject::toolTip() const
200 {
201   //return object()->Name();
202   return QString( "Object \'%1\', module \'%2\', ID=%3" ).arg( name() ).arg( componentDataType() ).arg( entry() );
203 }
204
205 /*!Get component type.*/
206 QString SalomeApp_DataObject::componentDataType() const
207 {
208   //  if ( myCompDataType.isEmpty() ) {
209     const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
210     if ( compObj && compObj->object() )
211     {
212       _PTR(SComponent) aComp( compObj->object() );
213       if ( aComp ) {
214         SalomeApp_DataObject* that = (SalomeApp_DataObject*)this;
215         that->myCompDataType = aComp->ComponentDataType().c_str();
216       }
217     }
218     //  }
219   return myCompDataType;
220 }
221
222 /*!Gets object.*/
223 _PTR(SObject) SalomeApp_DataObject::object() const
224 {
225   return myObject;
226 }
227
228 /*!Checks: Is object reference.*/
229 bool SalomeApp_DataObject::isReference() const
230 {
231   bool isRef = false;
232   if ( myObject )
233   {
234     _PTR(SObject) refObj;
235     isRef = myObject->ReferencedObject( refObj );
236   }
237   return isRef;
238 }
239
240 /*!Gets reference object.*/
241 _PTR(SObject) SalomeApp_DataObject::referencedObject() const
242 {
243   _PTR(SObject) refObj;
244   _PTR(SObject) obj = myObject;
245   while ( obj && obj->ReferencedObject( refObj ) )
246     obj = refObj;
247
248   return obj;
249 }
250
251 /*!Gets IOR*/
252 QString SalomeApp_DataObject::ior( const _PTR(SObject)& obj ) const
253 {
254   QString txt;
255   if ( obj )
256   {
257     _PTR(GenericAttribute) attr;
258     if ( obj->FindAttribute( attr, "AttributeIOR" ) )
259     {
260       _PTR(AttributeIOR) iorAttr = attr;
261       if ( iorAttr )
262       {
263         std::string str = iorAttr->Value();
264         txt = QString( str.c_str() );
265       }
266     }
267   }
268   return txt;
269 }
270
271 /*!Gets Entry*/
272 QString SalomeApp_DataObject::entry( const _PTR(SObject)& obj ) const
273 {
274   QString txt;
275   if ( obj )
276   {
277     std::string str = obj->GetID();
278     txt = QString( str.c_str() );
279   }
280   return txt;
281 }
282
283 /*!Value*/
284 QString SalomeApp_DataObject::value( const _PTR(SObject)& obj ) const
285 {
286   if ( !obj )
287     return QString::null;
288
289   QString val;
290   _PTR(GenericAttribute) attr;
291
292   if ( obj->FindAttribute( attr, "AttributeInteger" ) )
293   {
294     _PTR(AttributeInteger) intAttr = attr;
295     if ( intAttr )
296       val = QString::number( intAttr->Value() );
297   }
298   else if ( obj->FindAttribute( attr, "AttributeReal" ) )
299   {
300     _PTR(AttributeReal) realAttr = attr;
301     if ( realAttr )
302       val = QString::number( realAttr->Value() );
303   }
304   else if ( obj->FindAttribute( attr, "AttributeTableOfInteger" ) )
305   {
306     _PTR(AttributeTableOfInteger) 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, "AttributeTableOfReal" ) )
314   {
315     _PTR(AttributeTableOfReal) tableAttr = attr;
316     std::string title = tableAttr->GetTitle();
317     val = QString( title.c_str() );
318     if ( !val.isEmpty() )
319       val += QString( " " );
320     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
321   }
322   else if ( obj->FindAttribute( attr, "AttributeComment") )
323   {
324     _PTR(AttributeComment) comm = attr;
325     std::string str = comm->Value();
326     val = QString( str.c_str() );
327   }
328
329   return val;
330 }
331
332
333
334
335
336 /*!Constructor.Initialize by \a parent.*/
337 SalomeApp_ModuleObject::SalomeApp_ModuleObject( SUIT_DataObject* parent )
338 : SalomeApp_DataObject( parent ),
339   CAM_RootObject( parent ),
340   CAM_DataObject( parent )
341 {
342 }
343
344 /*!Constructor.Initialize by \a parent and SObject.*/
345 SalomeApp_ModuleObject::SalomeApp_ModuleObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
346 : SalomeApp_DataObject( sobj, parent ),
347   CAM_RootObject( 0, parent ),
348   CAM_DataObject( parent )
349 {
350 }
351
352 /*!Constructor.Initialize by \a parent and CAM_DataModel.*/
353 SalomeApp_ModuleObject::SalomeApp_ModuleObject( CAM_DataModel* dm, const _PTR(SObject)& sobj, SUIT_DataObject* parent )
354 : SalomeApp_DataObject( sobj, parent ),
355   CAM_RootObject( dm, parent ),
356   CAM_DataObject( parent )
357 {
358 }
359
360 /*!Destructor. Do nothing.*/
361 SalomeApp_ModuleObject::~SalomeApp_ModuleObject()
362 {
363 }
364
365 /*!Returns module name */
366 QString SalomeApp_ModuleObject::name() const
367 {
368   return SalomeApp_DataObject::name();
369 }
370
371
372
373
374 /*!Constructor.Initialize by \a parent.*/
375 SalomeApp_SavePointObject::SalomeApp_SavePointObject( SUIT_DataObject* _parent, const int id, SalomeApp_Study* study )
376   : LightApp_DataObject( _parent ), CAM_DataObject( _parent ), // IMPORTANT TO CALL ALL VIRTUAL CONSTRUCTORS!
377     myId( id ),
378     myStudy( study )
379 {
380 }
381
382 /*!Destructor. Do nothing.*/
383 SalomeApp_SavePointObject::~SalomeApp_SavePointObject()
384 {
385 }
386
387 /*!Returns save points ID */
388 int SalomeApp_SavePointObject::getId() const
389 {
390   return myId;
391 }
392
393 /*!Returns "invalid" entry, which does not correspond to any object in data structure
394   but indicates that it is a save point object  */
395 QString SalomeApp_SavePointObject::entry() const
396 {
397   return QObject::tr( "SAVE_POINT_DEF_NAME" ) + QString::number( myId );
398 }
399
400 /*!Returns displayed name of object */
401 QString SalomeApp_SavePointObject::name() const
402 {
403   return myStudy->getNameOfSavePoint( myId );
404 }
405
406 /*!Gets icon picture of object.*/
407 QPixmap SalomeApp_SavePointObject::icon() const
408 {
409   return QPixmap();
410 }
411
412 /*!Gets tooltip.*/
413 QString SalomeApp_SavePointObject::toolTip() const
414 {
415   return QObject::tr( "SAVE_POINT_OBJECT_TOOLTIP" ).arg( name() );
416 }
417