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