Salome HOME
cb1c9cfe58919fd74dd8b6bb1097b8db9b4dbb3f
[modules/gui.git] / src / SalomeApp / SalomeApp_DataObject.cxx
1 #include "SalomeApp_DataObject.h"
2
3 #include "SalomeApp_Study.h"
4 #include "LightApp_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
20         Level: Public
21 */
22 /*!Constructor. Initialize by \a parent*/
23 SalomeApp_DataObject::SalomeApp_DataObject( SUIT_DataObject* parent )
24 : LightApp_DataObject( parent ),
25   CAM_DataObject( parent )
26 {
27 }
28
29 /*!Constructor. Initialize by \a parent and SObject*/
30 SalomeApp_DataObject::SalomeApp_DataObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
31 : LightApp_DataObject( parent ),
32   CAM_DataObject( parent )
33 {
34   myObject = sobj;
35 }
36
37 /*!Destructor. Do nothing.*/
38 SalomeApp_DataObject::~SalomeApp_DataObject()
39 {
40 }
41
42 /*!Gets object ID.
43  *\retval QString
44  */
45 QString SalomeApp_DataObject::entry() const
46 {
47   if ( myObject )
48     return myObject->GetID().c_str();
49   return QString::null;
50 }
51
52 /*!Gets name of object.*/
53 QString SalomeApp_DataObject::name() const
54 {
55   QString str;
56
57   if ( myObject )
58     str = myObject->GetName().c_str();
59
60   if ( str.isEmpty() )
61   {
62     _PTR(SObject) refObj = referencedObject();
63     if ( refObj )
64       str = refObj->GetName().c_str();
65   }
66
67   if ( isReference() )
68     {
69       if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
70         str = QString( "* " ) + str;
71       else
72         str = QString( "<Invalid Reference>" );
73     }
74
75   return str;
76 }
77
78 /*!Gets icon picture of object.*/
79 QPixmap SalomeApp_DataObject::icon() const
80 {
81   _PTR(GenericAttribute) anAttr;
82   if ( myObject && myObject->FindAttribute( anAttr, "AttributePixMap" ) ){
83     _PTR(AttributePixMap) aPixAttr ( anAttr );
84     if ( aPixAttr->HasPixMap() ){
85       QString pixmapName = QObject::tr( aPixAttr->GetPixMap().c_str() );
86       LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() );
87       if ( aRoot && aRoot->study() ) {
88         QPixmap pixmap = aRoot->study()->application()->resourceMgr()->loadPixmap( componentDataType(), pixmapName, false ); 
89         return pixmap;
90       }
91     }
92   }
93   return QPixmap();
94 }
95
96 /*!Gets text value for one of entity:
97  *\li Value           (id = SalomeApp_DataObject::CT_Value)
98  *\li Entry           (id = SalomeApp_DataObject::CT_Entry)
99  *\li IOR             (id = SalomeApp_DataObject::CT_IOR)
100  *\li Reference entry (id = SalomeApp_DataObject::CT_RefEntry)
101  */
102 QString SalomeApp_DataObject::text( const int id ) const
103 {
104   QString txt;
105   switch ( id )
106   {
107   case CT_Value:
108 #ifndef WNT
109     if ( componentObject() != this )
110 #else
111     if ( componentObject() != (SUIT_DataObject*)this )
112 #endif
113       txt = value( referencedObject() );
114     break;
115   case CT_Entry:
116     txt = entry( object() );
117     break;
118   case CT_IOR:
119     txt = ior( referencedObject() );
120     break;
121   case CT_RefEntry:
122     if ( isReference() )
123       txt = entry( referencedObject() );
124     break;
125   }
126   return txt;
127 }
128
129 /*!Get color value for one of entity:
130  *\li Text color
131  *\li Highlight color
132  *\li Higlighted text color
133  */
134 QColor SalomeApp_DataObject::color( const ColorRole cr ) const
135 {
136   QColor clr;
137   switch ( cr )
138   {
139   case Text:
140     if ( isReference() )
141       {
142         if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
143           clr = QColor( 255, 0, 0 );
144         else
145           clr = QColor( 200, 200, 200 );
146       }
147     else if ( myObject )
148     {
149       _PTR(GenericAttribute) anAttr;
150       if ( myObject->FindAttribute( anAttr, "AttributeTextColor" ) )
151       {
152         _PTR(AttributeTextColor) aColAttr = anAttr;
153         clr = QColor( (int)aColAttr->TextColor().R, (int)aColAttr->TextColor().G, (int)aColAttr->TextColor().B );
154       }
155     }
156     break;
157   case Highlight:
158     if ( isReference() )
159       {
160         if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
161           clr = QColor( 255, 0, 0 );
162         else
163           clr = QColor( 200, 200, 200 );
164       }
165     break;
166   case HighlightedText:
167     if ( isReference() )
168       clr = QColor( 255, 255, 255 );
169     break;
170   }
171   return clr;
172 }
173
174 /*!Gets tooltip.*/
175 QString SalomeApp_DataObject::toolTip() const
176 {
177   //return object()->Name();
178   return QString( "Object \'%1\', module \'%2\', ID=%3" ).arg( name() ).arg( componentDataType() ).arg( entry() );
179 }
180
181 /*!Get component type.*/
182 QString SalomeApp_DataObject::componentDataType() const
183 {
184   const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
185   if ( compObj && compObj->object() )
186   {
187     _PTR(SComponent) aComp( compObj->object() );
188     if ( aComp )
189       return aComp->ComponentDataType().c_str();
190   }
191
192   return "";
193 }
194
195 /*!Gets object.*/
196 _PTR(SObject) SalomeApp_DataObject::object() const
197 {
198   return myObject;
199 }
200
201 /*!Checks: Is object reference.*/
202 bool SalomeApp_DataObject::isReference() const
203 {
204   bool isRef = false;
205   if ( myObject )
206   {
207     _PTR(SObject) refObj;
208     isRef = myObject->ReferencedObject( refObj );
209   }
210   return isRef;
211 }
212
213 /*!Gets reference object.*/
214 _PTR(SObject) SalomeApp_DataObject::referencedObject() const
215 {
216   _PTR(SObject) refObj;
217   _PTR(SObject) obj = myObject;
218   while ( obj && obj->ReferencedObject( refObj ) )
219     obj = refObj;
220
221   return obj;
222 }
223
224 /*!Gets IOR*/
225 QString SalomeApp_DataObject::ior( const _PTR(SObject)& obj ) const
226 {
227   QString txt;
228   if ( obj )
229   {
230     _PTR(GenericAttribute) attr;
231     if ( obj->FindAttribute( attr, "AttributeIOR" ) )
232     {
233       _PTR(AttributeIOR) iorAttr = attr;
234       if ( iorAttr )
235       {
236         std::string str = iorAttr->Value();
237         txt = QString( str.c_str() );
238       }
239     }
240   }
241   return txt;
242 }
243
244 /*!Gets Entry*/
245 QString SalomeApp_DataObject::entry( const _PTR(SObject)& obj ) const
246 {
247   QString txt;
248   if ( obj )
249   {
250     std::string str = obj->GetID();
251     txt = QString( str.c_str() );
252   }
253   return txt;
254 }
255
256 /*!Value*/
257 QString SalomeApp_DataObject::value( const _PTR(SObject)& obj ) const
258 {
259   if ( !obj )
260     return QString::null;
261
262   QString val;
263   _PTR(GenericAttribute) attr;
264
265   if ( obj->FindAttribute( attr, "AttributeInteger" ) )
266   {
267     _PTR(AttributeInteger) intAttr = attr;
268     if ( intAttr )
269       val = QString::number( intAttr->Value() );
270   }
271   else if ( obj->FindAttribute( attr, "AttributeReal" ) )
272   {
273     _PTR(AttributeReal) realAttr = attr;
274     if ( realAttr )
275       val = QString::number( realAttr->Value() );
276   }
277   else if ( obj->FindAttribute( attr, "AttributeTableOfInteger" ) )
278   {
279     _PTR(AttributeTableOfInteger) tableAttr = attr;
280     std::string title = tableAttr->GetTitle();
281     val = QString( title.c_str() );
282     if ( !val.isEmpty() )
283       val += QString( " " );
284     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
285   }
286   else if ( obj->FindAttribute( attr, "AttributeTableOfReal" ) )
287   {
288     _PTR(AttributeTableOfReal) tableAttr = attr;
289     std::string title = tableAttr->GetTitle();
290     val = QString( title.c_str() );
291     if ( !val.isEmpty() )
292       val += QString( " " );
293     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
294   }
295   else if ( obj->FindAttribute( attr, "AttributeComment") )
296   {
297     _PTR(AttributeComment) comm = attr;
298     std::string str = comm->Value();
299     val = QString( str.c_str() );
300   }
301
302   return val;
303 }
304
305 /*
306         Class: SalomeApp_ModuleObject
307         Level: Public
308 */
309
310 /*!Constructor.Initialize by \a parent.*/
311 SalomeApp_ModuleObject::SalomeApp_ModuleObject( SUIT_DataObject* parent )
312 : SalomeApp_DataObject( parent ),
313   CAM_RootObject( parent ),
314   CAM_DataObject( parent )
315 {
316 }
317
318 /*!Constructor.Initialize by \a parent and SObject.*/
319 SalomeApp_ModuleObject::SalomeApp_ModuleObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
320 : SalomeApp_DataObject( sobj, parent ),
321   CAM_RootObject( 0, parent ),
322   CAM_DataObject( parent )
323 {
324 }
325
326 /*!Constructor.Initialize by \a parent and CAM_DataModel.*/
327 SalomeApp_ModuleObject::SalomeApp_ModuleObject( CAM_DataModel* dm, const _PTR(SObject)& sobj, SUIT_DataObject* parent )
328 : SalomeApp_DataObject( sobj, parent ),
329   CAM_RootObject( dm, parent ),
330   CAM_DataObject( parent )
331 {
332 }
333
334 /*!Destructor. Do nothing.*/
335 SalomeApp_ModuleObject::~SalomeApp_ModuleObject()
336 {
337 }
338
339 /*!Returns module name */
340 QString SalomeApp_ModuleObject::name() const
341 {
342   return SalomeApp_DataObject::name();
343 }
344