1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SalomeApp_DataObject.cxx
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
25 #include "SalomeApp_DataObject.h"
26 #include "SalomeApp_Study.h"
27 #include "SalomeApp_Application.h"
29 #include <CAM_DataObject.h>
31 #include <SUIT_Session.h>
32 #include <SUIT_Application.h>
33 #include <SUIT_ResourceMgr.h>
35 #include <SALOME_LifeCycleCORBA.hxx>
36 #include <Basics_Utils.hxx>
42 \class SalomeApp_DataObject
43 \brief Implementation of the data object for use in CORBA-based
49 \param parent parent data object
51 SalomeApp_DataObject::SalomeApp_DataObject( SUIT_DataObject* parent )
52 : CAM_DataObject( parent ),
53 LightApp_DataObject( parent )
59 \param sobj SALOMEDS object
60 \param parent parent data object
62 SalomeApp_DataObject::SalomeApp_DataObject( const _PTR(SObject)& sobj,
63 SUIT_DataObject* parent )
64 : CAM_DataObject( parent ),
65 LightApp_DataObject( parent )
73 SalomeApp_DataObject::~SalomeApp_DataObject()
78 \brief Get data object name.
81 QString SalomeApp_DataObject::name() const
85 str = myObject->GetName().c_str();
87 if ( str.isEmpty() ) {
88 _PTR(SObject) refObj = referencedObject();
90 str = refObj->GetName().c_str();
93 if ( isReference() ) {
94 if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
95 str = QString( "* " ) + str;
97 str = QString( "<Invalid Reference>" );
103 \brief Get object string identifier.
106 QString SalomeApp_DataObject::entry() const
108 return entry( myObject );
112 \brief Get object text data for the specified column.
114 This method returns the data according to the specufied column \a id:
115 - NameId : object name (by calling name() method)
116 - EntryId : object entry (by calling entry() method)
117 - ValueId : object value
119 - RefEntryId : object reference entry
122 \return object text data
124 QString SalomeApp_DataObject::text( const int id ) const
128 // add "Value", "IOR", and "Reference Entry" columns
133 if ( componentObject() != this )
135 if ( componentObject() != (SUIT_DataObject*)this )
137 txt = value( object() );
139 txt = value( referencedObject() );
142 txt = ior( referencedObject() );
146 txt = entry( referencedObject() );
149 txt = LightApp_DataObject::text( id );
156 \brief Get data object icon for the specified column.
158 \return object icon for the specified column
160 QPixmap SalomeApp_DataObject::icon( const int id ) const
162 // we display icon only for the first (NameId ) column
163 if ( id == NameId ) {
164 _PTR(GenericAttribute) anAttr;
165 if ( myObject && myObject->FindAttribute( anAttr, "AttributePixMap" ) ){
166 _PTR(AttributePixMap) aPixAttr ( anAttr );
167 if ( aPixAttr->HasPixMap() ) {
168 QString componentType = componentDataType();
169 QString pixmapID = aPixAttr->GetPixMap().c_str();
170 // select a plugin within a component
171 QStringList plugin_pixmap = pixmapID.split( "::", QString::KeepEmptyParts );
172 if ( plugin_pixmap.size() == 2 ) {
173 componentType = plugin_pixmap.front();
174 pixmapID = plugin_pixmap.back();
176 QString pixmapName = QObject::tr( pixmapID.toLatin1().constData() );
177 LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() );
178 if ( aRoot && aRoot->study() ) {
179 SUIT_ResourceMgr* mgr = aRoot->study()->application()->resourceMgr();
180 return mgr->loadPixmap( componentType, pixmapName, false );
185 return LightApp_DataObject::icon( id );
189 \brief Get data object color for the specified column.
190 \param role color role
191 \param id column id (not used)
192 \return object color for the specified column
194 QColor SalomeApp_DataObject::color( const ColorRole role, const int id ) const
196 // we ignore parameter <id> in order to use the same colors for
203 // text color (not selected item)
204 if ( isReference() ) {
205 if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
206 c = QColor( 255, 0, 0 ); // valid reference (red)
208 c = QColor( 200, 200, 200 ); // invalid reference (grayed)
210 else if ( myObject ) {
211 // get color atrtribute value
212 _PTR(GenericAttribute) anAttr;
213 if ( myObject->FindAttribute( anAttr, "AttributeTextColor" ) ) {
214 _PTR(AttributeTextColor) aColAttr = anAttr;
215 c = QColor( (int)aColAttr->TextColor().R, (int)aColAttr->TextColor().G, (int)aColAttr->TextColor().B );
220 // background color for the highlighted item
221 if ( isReference() ) {
222 if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
223 c = QColor( 255, 0, 0 ); // valid reference (red)
225 c = QColor( 200, 200, 200 ); // invalid reference (grayed)
227 else if ( myObject ) {
228 // get color atrtribute value
229 _PTR(GenericAttribute) anAttr;
230 if( myObject->FindAttribute ( anAttr, "AttributeTextHighlightColor") ) {
231 _PTR(AttributeTextHighlightColor) aHighColAttr = anAttr;
232 c = QColor( (int)(aHighColAttr->TextHighlightColor().R),
233 (int)(aHighColAttr->TextHighlightColor().G),
234 (int)(aHighColAttr->TextHighlightColor().B));
238 case HighlightedText:
239 // text color for the highlighted item
241 c = QColor( 255, 255, 255 ); // white
245 c = LightApp_DataObject::color( role, id );
250 \brief Get data object tooltip for the specified column.
251 \param id column id (not used)
252 \return object tooltip for the specified column
254 QString SalomeApp_DataObject::toolTip( const int /*id*/ ) const
256 // we ignore parameter <id> in order to use the same tooltip for
259 // Get customized tooltip in case of it exists
260 const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
261 // Check if the component has been loaded.
262 // In order to avoid loading the component only for getting a custom tooltip.
263 if ( compObj && compObj != this && !ior(compObj->object()).isEmpty() ) {
264 SalomeApp_Application* app =
265 dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
267 // --- try to find (and not load) the component instance, like GEOM instance,
268 // registered in naming service under Containers/<hostname>/...
269 // with any container name, on every machine available
270 Engines::MachineParameters params;
271 app->lcc()->preSet(params); // --- any container name, anywhere
272 Engines::Component_var aComponent =
273 app->lcc()->FindComponent(params, componentDataType().toLatin1().constData() );
275 if ( !CORBA::is_nil(aComponent) && aComponent->hasObjectInfo() ) {
276 LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() );
277 if ( aRoot && aRoot->study() )
278 return QString( aComponent->getObjectInfo( aRoot->study()->id(), entry().toLatin1().constData()) );
283 return QString( "Object \'%1\', module \'%2\', ID=%3" ).arg( name() ).arg( componentDataType() ).arg( entry() );
287 \brief Get component type.
288 \return component type
290 QString SalomeApp_DataObject::componentDataType() const
292 // if ( myCompDataType.isEmpty() ) {
293 const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
294 if ( compObj && compObj->object() )
296 _PTR(SComponent) aComp( compObj->object() );
298 SalomeApp_DataObject* that = (SalomeApp_DataObject*)this;
299 that->myCompDataType = aComp->ComponentDataType().c_str();
303 return myCompDataType;
307 \brief Get SALOMEDS object.
308 \return SALOMEDS object
310 _PTR(SObject) SalomeApp_DataObject::object() const
316 \brief Check if the data object is a reference.
317 \return \c true if this data object actually refers to another one
319 bool SalomeApp_DataObject::isReference() const
324 _PTR(SObject) refObj;
325 isRef = myObject->ReferencedObject( refObj );
331 \brief Get the object referenced by this one.
332 \return referenced object
334 _PTR(SObject) SalomeApp_DataObject::referencedObject() const
336 _PTR(SObject) refObj;
337 _PTR(SObject) obj = myObject;
338 while ( obj && obj->ReferencedObject( refObj ) )
345 \brief Check if the specified column supports custom sorting.
347 \return \c true if column sorting should be customized
350 bool SalomeApp_DataObject::customSorting( const int id ) const
352 // perform custom sorting for the "Entry" and "Reference Entry" columns
353 return id == EntryId || id == RefEntryId ? true
354 : LightApp_DataObject::customSorting( id );
358 \brief Compares data from two items for sorting purposes.
360 This method is called only for those columns for which customSorting()
361 method returns \c true.
363 \param left first data to compare
364 \param right second data to compare
366 \return result of the comparison
369 bool SalomeApp_DataObject::compare( const QVariant& left, const QVariant& right, const int id ) const
371 // use the same custom sorting for the "Reference Entry" column as for the
372 // "Entry" column (call base implementation)
373 return LightApp_DataObject::compare( left, right, id == RefEntryId ? EntryId : id );
377 \brief Get data object IOR.
378 \param obj data object
379 \return data object IOR or null string if IOR is empty
381 QString SalomeApp_DataObject::ior( const _PTR(SObject)& obj ) const
386 _PTR(GenericAttribute) attr;
387 if ( obj->FindAttribute( attr, "AttributeIOR" ) )
389 _PTR(AttributeIOR) iorAttr = attr;
392 std::string str = iorAttr->Value();
393 txt = QString( str.c_str() );
401 \brief Get data object entry identifier.
402 \param obj data object
403 \return data object entry identifier or empty object does not have entry
405 QString SalomeApp_DataObject::entry( const _PTR(SObject)& obj ) const
410 std::string str = obj->GetID();
411 txt = QString( str.c_str() );
417 \brief Get data object value.
418 \param obj data object
419 \return data object value or empty string if there is no
420 value associated to the object
422 QString SalomeApp_DataObject::value( const _PTR(SObject)& obj ) const
428 _PTR(GenericAttribute) attr;
430 if ( obj->FindAttribute( attr, "AttributeString" ) )
432 _PTR(AttributeString) strAttr = attr;
433 std::string str = strAttr->Value();
434 QString aStrings = QString( str.c_str() );
436 //Special case to show NoteBook variables in the "Value" column of the OB
437 if ( LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() ) )
439 if ( SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( aRoot->study() ) )
441 _PTR(Study) studyDS( aStudy->studyDS() );
444 QStringList aSectionList = aStrings.split( "|" );
445 if ( !aSectionList.isEmpty() )
447 QString aLastSection = aSectionList.last();
448 QStringList aStringList = aLastSection.split( ":" );
449 if ( !aStringList.isEmpty() )
452 for ( int i = 0, n = aStringList.size(); i < n; i++ )
454 QString aStr = aStringList[i];
455 if ( studyDS->IsVariable( aStr.toStdString() ) )
456 val.append( aStr + ", " );
459 if ( !val.isEmpty() )
460 val.remove( val.length() - 2, 2 );
468 else if ( obj->FindAttribute( attr, "AttributeInteger" ) )
470 _PTR(AttributeInteger) intAttr = attr;
472 val = QString::number( intAttr->Value() );
474 else if ( obj->FindAttribute( attr, "AttributeReal" ) )
476 _PTR(AttributeReal) realAttr = attr;
478 val = QString::number( realAttr->Value() );
480 else if ( obj->FindAttribute( attr, "AttributeTableOfInteger" ) )
482 _PTR(AttributeTableOfInteger) tableAttr = attr;
483 std::string title = tableAttr->GetTitle();
484 val = QString( title.c_str() );
485 if ( !val.isEmpty() )
486 val += QString( " " );
487 val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
489 else if ( obj->FindAttribute( attr, "AttributeTableOfReal" ) )
491 _PTR(AttributeTableOfReal) tableAttr = attr;
492 std::string title = tableAttr->GetTitle();
493 val = QString( title.c_str() );
494 if ( !val.isEmpty() )
495 val += QString( " " );
496 val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
498 else if ( obj->FindAttribute( attr, "AttributeComment" ) )
500 _PTR(AttributeComment) comm = attr;
501 std::string str = comm->Value();
502 val = QString( str.c_str() );
509 \class SalomeApp_ModuleObject
510 \brief This class is used for optimized access to the SALOMEDS-based
511 data model from SalomeApp_DataObject class instances.
512 \sa CAM_ModuleObject class
517 \param parent parent data object
519 SalomeApp_ModuleObject::SalomeApp_ModuleObject( SUIT_DataObject* parent )
520 : CAM_DataObject( parent ),
521 LightApp_DataObject( parent ),
522 SalomeApp_DataObject( parent ),
523 CAM_ModuleObject( parent )
529 \param sobj SALOMEDS object
530 \param parent parent data object
532 SalomeApp_ModuleObject::SalomeApp_ModuleObject( const _PTR(SObject)& sobj,
533 SUIT_DataObject* parent )
534 : CAM_DataObject( parent ),
535 LightApp_DataObject( parent ),
536 SalomeApp_DataObject( sobj, parent ),
537 CAM_ModuleObject( parent )
544 \param sobj SALOMEDS object
545 \param parent parent data object
547 SalomeApp_ModuleObject::SalomeApp_ModuleObject( CAM_DataModel* dm,
548 const _PTR(SObject)& sobj,
549 SUIT_DataObject* parent )
550 : CAM_DataObject( parent ),
551 LightApp_DataObject( parent ),
552 SalomeApp_DataObject( sobj, parent ),
553 CAM_ModuleObject( dm, parent )
560 SalomeApp_ModuleObject::~SalomeApp_ModuleObject()
565 \brief Get module name.
568 QString SalomeApp_ModuleObject::name() const
570 return SalomeApp_DataObject::name();
574 \brief Get data object icon for the specified column.
576 \return object icon for the specified column
578 QPixmap SalomeApp_ModuleObject::icon( const int id ) const
580 return SalomeApp_DataObject::icon( id );
584 \brief Get data object tooltip for the specified column.
586 \return object tooltip for the specified column
588 QString SalomeApp_ModuleObject::toolTip( const int id ) const
590 return SalomeApp_DataObject::toolTip( id );
594 \class SalomeApp_RootObject
595 \brief Root data object for the CORBA-based SALOME application.
597 This class is to be instanciated by only one object - the root object
598 of the SalomeApp data object tree. This object is not shown in the object browser.
599 The goal of this class is to provide a unified access to SalomeApp_Study
600 object from SalomeApp_DataObject instances.
605 \param study pointer to the study
607 SalomeApp_RootObject::SalomeApp_RootObject( LightApp_Study* study )
608 : CAM_DataObject( 0 ),
609 LightApp_DataObject( 0 ),
610 SalomeApp_DataObject( 0 ),
611 LightApp_RootObject( study )
618 SalomeApp_RootObject::~SalomeApp_RootObject()
623 \brief Get data object name.
626 QString SalomeApp_RootObject::name() const
628 return LightApp_RootObject::name();
632 \brief Get object string identifier.
635 QString SalomeApp_RootObject::entry() const
637 return LightApp_RootObject::entry();
641 \brief Get object text data for the specified column.
643 \return object text data
645 QString SalomeApp_RootObject::text( const int id ) const
647 return LightApp_RootObject::text( id );
651 \brief Get data object icon for the specified column.
653 \return object icon for the specified column
655 QPixmap SalomeApp_RootObject::icon( const int id ) const
657 return LightApp_RootObject::icon( id );
661 \brief Get data object color for the specified column.
662 \param role color role
663 \param id column id (not used)
664 \return object color for the specified column
666 QColor SalomeApp_RootObject::color( const ColorRole role, const int id ) const
668 return LightApp_RootObject::color( role, id );
672 \brief Get data object tooltip for the specified column.
673 \param id column id (not used)
674 \return object tooltip for the specified column
676 QString SalomeApp_RootObject::toolTip( const int id ) const
678 return LightApp_RootObject::toolTip( id );
682 \class SalomeApp_SavePointObject
683 \brief Represents persistent visual_state object.
685 Save point objects are stored in the data model, but NOT in SObjects
686 structure, so they are handled separately using this special class
691 \param parent parent data object
692 \param id save point ID
695 SalomeApp_SavePointObject::SalomeApp_SavePointObject( SUIT_DataObject* parent,
697 SalomeApp_Study* study )
698 : LightApp_DataObject( parent ),
699 CAM_DataObject( parent ),
708 SalomeApp_SavePointObject::~SalomeApp_SavePointObject()
713 \brief Get save point unique identifier.
714 \return save point ID
716 int SalomeApp_SavePointObject::getId() const
722 \brief Get object string identifier.
725 QString SalomeApp_SavePointObject::entry() const
727 return QObject::tr( "SAVE_POINT_DEF_NAME" ) + QString::number( myId );
731 \brief Get data object name.
734 QString SalomeApp_SavePointObject::name() const
736 return myStudy->getNameOfSavePoint( myId );
740 \brief Get data object icon for the specified column.
742 \return object icon for the specified column
744 QPixmap SalomeApp_SavePointObject::icon( const int /*id*/ ) const
750 \brief Get data object tooltip for the specified column.
751 \param id column id (not used)
752 \return object tooltip for the specified column
754 QString SalomeApp_SavePointObject::toolTip( const int /*id*/ ) const
756 return QObject::tr( "SAVE_POINT_OBJECT_TOOLTIP" ).arg( name() );
760 \class SalomeApp_SavePointRootObject
761 \brief Represents parent object for visual_state objects.
766 \param parent parent object
768 SalomeApp_SavePointRootObject::SalomeApp_SavePointRootObject( SUIT_DataObject* parent )
769 : SUIT_DataObject( parent )
774 \brief Get data object name.
777 QString SalomeApp_SavePointRootObject::name() const
779 return QObject::tr( "SAVE_POINT_ROOT_NAME" );
783 \brief Get data object tooltip for the specified column.
784 \param id column id (not used)
785 \return object tooltip for the specified column
787 QString SalomeApp_SavePointRootObject::toolTip( const int /*id*/ ) const
789 return QObject::tr( "SAVE_POINT_ROOT_TOOLTIP" );