Salome HOME
Merge from OCC_development_generic_2006
[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   myEntry( "" ),
45   myName( "" )
46 {
47 }
48
49 /*!Constructor. Initialize by \a parent and SObject*/
50 SalomeApp_DataObject::SalomeApp_DataObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
51 : LightApp_DataObject( parent ),
52   CAM_DataObject( parent ),
53   myName( "" )
54 {
55   myObject = sobj;
56   myEntry = myObject->GetID().c_str();
57 }
58
59 /*!Destructor. Do nothing.*/
60 SalomeApp_DataObject::~SalomeApp_DataObject()
61 {
62 }
63
64 /*!Gets object ID.
65  *\retval QString
66  */
67 QString SalomeApp_DataObject::entry() const
68 {
69  if ( myObject )
70     return myObject->GetID().c_str();
71   return QString::null;
72   //return myEntry;
73 }
74
75 /*!Gets name of object.*/
76 QString SalomeApp_DataObject::name() const
77 {
78   //if ( myName.isEmpty() )
79   {
80     QString str;
81     if ( myObject )
82       str = myObject->GetName().c_str();
83
84     if ( str.isEmpty() )
85     {
86       _PTR(SObject) refObj = referencedObject();
87       if ( refObj )
88         str = refObj->GetName().c_str();
89     }
90
91     if ( isReference() )
92       {
93         if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
94           str = QString( "* " ) + str;
95         else
96           str = QString( "<Invalid Reference>" );
97       }
98     SalomeApp_DataObject* that = (SalomeApp_DataObject*)this;
99     that->myName = str;
100   }
101   return myName;
102 }
103
104 /*!Gets icon picture of object.*/
105 QPixmap SalomeApp_DataObject::icon() const
106 {
107   _PTR(GenericAttribute) anAttr;
108   if ( myObject && myObject->FindAttribute( anAttr, "AttributePixMap" ) ){
109     _PTR(AttributePixMap) aPixAttr ( anAttr );
110     if ( aPixAttr->HasPixMap() ){
111       QString pixmapName = QObject::tr( aPixAttr->GetPixMap().c_str() );
112       LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() );
113       if ( aRoot && aRoot->study() ) {
114         SUIT_ResourceMgr* mgr = aRoot->study()->application()->resourceMgr();
115         return mgr->loadPixmap( componentDataType(), pixmapName, false ); 
116       }
117     }
118   }
119   return QPixmap();
120 }
121
122 /*!Gets text value for one of entity:
123  *\li Value           (id = SalomeApp_DataObject::CT_Value)
124  *\li Entry           (id = SalomeApp_DataObject::CT_Entry)
125  *\li IOR             (id = SalomeApp_DataObject::CT_IOR)
126  *\li Reference entry (id = SalomeApp_DataObject::CT_RefEntry)
127  */
128 QString SalomeApp_DataObject::text( const int id ) const
129 {
130   QString txt;
131   switch ( id )
132   {
133   case CT_Value:
134 #ifndef WNT
135     if ( componentObject() != this )
136 #else
137     if ( componentObject() != (SUIT_DataObject*)this )
138 #endif
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     break;
192   case HighlightedText:
193     if ( isReference() )
194       clr = QColor( 255, 255, 255 );
195     break;
196   }
197   return clr;
198 }
199
200 /*!Gets tooltip.*/
201 QString SalomeApp_DataObject::toolTip() const
202 {
203   //return object()->Name();
204   return QString( "Object \'%1\', module \'%2\', ID=%3" ).arg( name() ).arg( componentDataType() ).arg( entry() );
205 }
206
207 /*!Get component type.*/
208 QString SalomeApp_DataObject::componentDataType() const
209 {
210   //  if ( myCompDataType.isEmpty() ) {
211     const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
212     if ( compObj && compObj->object() )
213     {
214       _PTR(SComponent) aComp( compObj->object() );
215       if ( aComp ) {
216         SalomeApp_DataObject* that = (SalomeApp_DataObject*)this;
217         that->myCompDataType = aComp->ComponentDataType().c_str();
218       }
219     }
220     //  }
221   return myCompDataType;
222 }
223
224 /*!Gets object.*/
225 _PTR(SObject) SalomeApp_DataObject::object() const
226 {
227   return myObject;
228 }
229
230 /*!Checks: Is object reference.*/
231 bool SalomeApp_DataObject::isReference() const
232 {
233   bool isRef = false;
234   if ( myObject )
235   {
236     _PTR(SObject) refObj;
237     isRef = myObject->ReferencedObject( refObj );
238   }
239   return isRef;
240 }
241
242 /*!Gets reference object.*/
243 _PTR(SObject) SalomeApp_DataObject::referencedObject() const
244 {
245   _PTR(SObject) refObj;
246   _PTR(SObject) obj = myObject;
247   while ( obj && obj->ReferencedObject( refObj ) )
248     obj = refObj;
249
250   return obj;
251 }
252
253 /*!Gets IOR*/
254 QString SalomeApp_DataObject::ior( const _PTR(SObject)& obj ) const
255 {
256   QString txt;
257   if ( obj )
258   {
259     _PTR(GenericAttribute) attr;
260     if ( obj->FindAttribute( attr, "AttributeIOR" ) )
261     {
262       _PTR(AttributeIOR) iorAttr = attr;
263       if ( iorAttr )
264       {
265         std::string str = iorAttr->Value();
266         txt = QString( str.c_str() );
267       }
268     }
269   }
270   return txt;
271 }
272
273 /*!Gets Entry*/
274 QString SalomeApp_DataObject::entry( const _PTR(SObject)& obj ) const
275 {
276   QString txt;
277   if ( obj )
278   {
279     std::string str = obj->GetID();
280     txt = QString( str.c_str() );
281   }
282   return txt;
283 }
284
285 /*!Value*/
286 QString SalomeApp_DataObject::value( const _PTR(SObject)& obj ) const
287 {
288   if ( !obj )
289     return QString::null;
290
291   QString val;
292   _PTR(GenericAttribute) attr;
293
294   if ( obj->FindAttribute( attr, "AttributeInteger" ) )
295   {
296     _PTR(AttributeInteger) intAttr = attr;
297     if ( intAttr )
298       val = QString::number( intAttr->Value() );
299   }
300   else if ( obj->FindAttribute( attr, "AttributeReal" ) )
301   {
302     _PTR(AttributeReal) realAttr = attr;
303     if ( realAttr )
304       val = QString::number( realAttr->Value() );
305   }
306   else if ( obj->FindAttribute( attr, "AttributeTableOfInteger" ) )
307   {
308     _PTR(AttributeTableOfInteger) tableAttr = attr;
309     std::string title = tableAttr->GetTitle();
310     val = QString( title.c_str() );
311     if ( !val.isEmpty() )
312       val += QString( " " );
313     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
314   }
315   else if ( obj->FindAttribute( attr, "AttributeTableOfReal" ) )
316   {
317     _PTR(AttributeTableOfReal) tableAttr = attr;
318     std::string title = tableAttr->GetTitle();
319     val = QString( title.c_str() );
320     if ( !val.isEmpty() )
321       val += QString( " " );
322     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
323   }
324   else if ( obj->FindAttribute( attr, "AttributeComment") )
325   {
326     _PTR(AttributeComment) comm = attr;
327     std::string str = comm->Value();
328     val = QString( str.c_str() );
329   }
330
331   return val;
332 }
333
334 /*
335         Class: SalomeApp_ModuleObject
336         Level: Public
337 */
338
339 /*!Constructor.Initialize by \a parent.*/
340 SalomeApp_ModuleObject::SalomeApp_ModuleObject( SUIT_DataObject* parent )
341 : SalomeApp_DataObject( parent ),
342   CAM_RootObject( parent ),
343   CAM_DataObject( parent )
344 {
345 }
346
347 /*!Constructor.Initialize by \a parent and SObject.*/
348 SalomeApp_ModuleObject::SalomeApp_ModuleObject( const _PTR(SObject)& sobj, SUIT_DataObject* parent )
349 : SalomeApp_DataObject( sobj, parent ),
350   CAM_RootObject( 0, parent ),
351   CAM_DataObject( parent )
352 {
353 }
354
355 /*!Constructor.Initialize by \a parent and CAM_DataModel.*/
356 SalomeApp_ModuleObject::SalomeApp_ModuleObject( CAM_DataModel* dm, const _PTR(SObject)& sobj, SUIT_DataObject* parent )
357 : SalomeApp_DataObject( sobj, parent ),
358   CAM_RootObject( dm, parent ),
359   CAM_DataObject( parent )
360 {
361 }
362
363 /*!Destructor. Do nothing.*/
364 SalomeApp_ModuleObject::~SalomeApp_ModuleObject()
365 {
366 }
367
368 /*!Returns module name */
369 QString SalomeApp_ModuleObject::name() const
370 {
371   return SalomeApp_DataObject::name();
372 }
373