1 // Copyright (C) 2007-2013 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 // Text for "Value" and "IOR" columns
133 if ( componentObject() != this )
135 if ( componentObject() != (SUIT_DataObject*)this )
137 txt = value( object() );
139 txt = value( referencedObject() );
142 txt = ior( referencedObject() );
145 // Issue 21379: LightApp_DataObject::text() treats "Entry"
146 // and "Reference Entry" columns
147 txt = LightApp_DataObject::text( id );
154 \brief Get data object icon for the specified column.
156 \return object icon for the specified column
158 QPixmap SalomeApp_DataObject::icon( const int id ) const
160 // we display icon only for the first (NameId ) column
161 if ( id == NameId ) {
162 _PTR(GenericAttribute) anAttr;
163 if ( myObject && myObject->FindAttribute( anAttr, "AttributePixMap" ) ){
164 _PTR(AttributePixMap) aPixAttr ( anAttr );
165 if ( aPixAttr->HasPixMap() ) {
166 QString componentType = componentDataType();
167 QString pixmapID = aPixAttr->GetPixMap().c_str();
168 // select a plugin within a component
169 QStringList plugin_pixmap = pixmapID.split( "::", QString::KeepEmptyParts );
170 if ( plugin_pixmap.size() == 2 ) {
171 componentType = plugin_pixmap.front();
172 pixmapID = plugin_pixmap.back();
174 QString pixmapName = QObject::tr( pixmapID.toLatin1().constData() );
175 LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() );
176 if ( aRoot && aRoot->study() ) {
177 SUIT_ResourceMgr* mgr = aRoot->study()->application()->resourceMgr();
178 return mgr->loadPixmap( componentType, pixmapName, false );
183 return LightApp_DataObject::icon( id );
187 \brief Get data object color for the specified column.
188 \param role color role
189 \param id column id (not used)
190 \return object color for the specified column
192 QColor SalomeApp_DataObject::color( const ColorRole role, const int id ) const
194 // we ignore parameter <id> in order to use the same colors for
201 // text color (not selected item)
202 if ( isReference() ) {
203 if ( QString(referencedObject()->GetName().c_str()).isEmpty() )
204 c = QColor( 200, 200, 200 ); // invalid reference (grayed)
206 else if ( myObject ) {
207 // get color atrtribute value
208 _PTR(GenericAttribute) anAttr;
209 if ( myObject->FindAttribute( anAttr, "AttributeTextColor" ) ) {
210 _PTR(AttributeTextColor) aColAttr = anAttr;
211 c = QColor( (int)aColAttr->TextColor().R, (int)aColAttr->TextColor().G, (int)aColAttr->TextColor().B );
217 // background color for the highlighted item
218 if ( isReference() ) {
219 if ( QString(referencedObject()->GetName().c_str()).isEmpty() )
220 c = QColor( 200, 200, 200 ); // invalid reference (grayed)
222 else if ( myObject ) {
223 // get color atrtribute value
224 _PTR(GenericAttribute) anAttr;
225 if( myObject->FindAttribute ( anAttr, "AttributeTextHighlightColor") ) {
226 _PTR(AttributeTextHighlightColor) aHighColAttr = anAttr;
227 c = QColor( (int)(aHighColAttr->TextHighlightColor().R),
228 (int)(aHighColAttr->TextHighlightColor().G),
229 (int)(aHighColAttr->TextHighlightColor().B));
237 // Issue 21379: LightApp_DataObject::color() defines colors for valid references
239 c = LightApp_DataObject::color( role, id );
245 \brief Get data object tooltip for the specified column.
246 \param id column id (not used)
247 \return object tooltip for the specified column
249 QString SalomeApp_DataObject::toolTip( const int /*id*/ ) const
251 // we ignore parameter <id> in order to use the same tooltip for
254 // Get customized tooltip in case of it exists
255 const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
256 // Check if the component has been loaded.
257 // In order to avoid loading the component only for getting a custom tooltip.
258 if ( compObj && compObj != this && !ior(compObj->object()).isEmpty() ) {
259 SalomeApp_Application* app =
260 dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
262 // --- try to find (and not load) the component instance, like GEOM instance,
263 // registered in naming service under Containers/<hostname>/...
264 // with any container name, on every machine available
265 Engines::ContainerParameters params;
266 app->lcc()->preSet(params); // --- any container name, anywhere
267 Engines::EngineComponent_var aComponent =
268 app->lcc()->FindComponent(params, componentDataType().toLatin1().constData() );
270 if ( !CORBA::is_nil(aComponent) && aComponent->hasObjectInfo() ) {
271 LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() );
272 if ( aRoot && aRoot->study() )
273 return QString( (CORBA::String_var)aComponent->getObjectInfo( aRoot->study()->id(), entry().toLatin1().constData()) );
278 return QString( "Object \'%1\', module \'%2\', ID=%3" ).arg( name() ).arg( componentDataType() ).arg( entry() );
282 \brief Get font to be used for data object rendering in the item views
284 \return font to be used for the item rendering
286 QFont SalomeApp_DataObject::font( const int id ) const
288 QFont f = LightApp_DataObject::font( id );
289 if ( id == NameId ) {
290 if ( !expandable() && hasChildren() ) {
291 // set bold font to highlight the item which is non-expandable but has children
299 \brief Get component type.
300 \return component type
302 QString SalomeApp_DataObject::componentDataType() const
304 // if ( myCompDataType.isEmpty() ) {
305 const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
306 if ( compObj && compObj->object() )
308 _PTR(SComponent) aComp( compObj->object() );
310 SalomeApp_DataObject* that = (SalomeApp_DataObject*)this;
311 that->myCompDataType = aComp->ComponentDataType().c_str();
315 return myCompDataType;
319 \brief Get SALOMEDS object.
320 \return SALOMEDS object
322 _PTR(SObject) SalomeApp_DataObject::object() const
328 \brief Returns the string identifier of the data objects referenced by this one.
330 Re-implemented from LightApp_DataObject using SALOMEDS API.
332 \return ID string of the referenced SObject
334 QString SalomeApp_DataObject::refEntry() const
336 return entry( referencedObject() );
340 \brief Check if the data object is a reference.
342 Re-implemented from LightApp_DataObject using SALOMEDS API.
344 \return \c true if this data object actually refers to another one
346 bool SalomeApp_DataObject::isReference() const
351 _PTR(SObject) refObj;
352 isRef = myObject->ReferencedObject( refObj );
358 \brief Get the object referenced by this one.
359 \return referenced object
361 _PTR(SObject) SalomeApp_DataObject::referencedObject() const
363 _PTR(SObject) refObj;
364 _PTR(SObject) obj = myObject;
365 while ( obj && obj->ReferencedObject( refObj ) )
372 \brief Check if object has children
373 \return \c true if object has at least one child sub-object and \c false otherwise
375 bool SalomeApp_DataObject::hasChildren() const
380 _PTR(UseCaseBuilder) aUseCaseBuilder = myObject->GetStudy()->GetUseCaseBuilder();
381 if (aUseCaseBuilder->IsUseCaseNode(myObject)) {
382 ok = aUseCaseBuilder->HasChildren(myObject);
383 // TODO: check name as below?
386 _PTR(ChildIterator) it ( myObject->GetStudy()->NewChildIterator( myObject ) );
387 for ( ; it->More() && !ok; it->Next() ) {
388 _PTR(SObject) obj = it->Value();
390 _PTR(SObject) refObj;
391 //if ( obj->ReferencedObject( refObj ) ) continue; // omit references
392 if ( obj->GetName() != "" ) ok = true;
400 \brief Check if the object is expandable (e.g. in the data tree view)
401 \return \c true if object is expandable and \c false otherwise
403 bool SalomeApp_DataObject::expandable() const
406 _PTR(GenericAttribute) anAttr;
407 if ( myObject && myObject->FindAttribute( anAttr, "AttributeExpandable" ) ) {
408 _PTR(AttributeExpandable) aAttrExp = anAttr;
409 exp = aAttrExp->IsExpandable();
415 \brief Check if the object is visible.
416 \return \c true if this object is displayable or \c false otherwise
418 bool SalomeApp_DataObject::isVisible() const
421 _PTR(GenericAttribute) anAttr;
422 if ( myObject && myObject->FindAttribute(anAttr, "AttributeDrawable") )
424 _PTR(AttributeDrawable) aAttrDraw = anAttr;
425 isDraw = aAttrDraw->IsDrawable();
427 return isDraw && LightApp_DataObject::isVisible() && ( !name().isEmpty() || isReference() );
431 \brief Check if the specified column supports custom sorting.
433 \return \c true if column sorting should be customized
436 bool SalomeApp_DataObject::customSorting( const int id ) const
438 // perform custom sorting for the "Entry" and "Reference Entry" columns
439 return id == EntryId || id == RefEntryId ? true
440 : LightApp_DataObject::customSorting( id );
444 \brief Compares data from two items for sorting purposes.
446 This method is called only for those columns for which customSorting()
447 method returns \c true.
449 \param left first data to compare
450 \param right second data to compare
452 \return result of the comparison
455 bool SalomeApp_DataObject::compare( const QVariant& left, const QVariant& right, const int id ) const
457 // use the same custom sorting for the "Reference Entry" column as for the
458 // "Entry" column (call base implementation)
459 return LightApp_DataObject::compare( left, right, id == RefEntryId ? EntryId : id );
463 \brief Get data object IOR.
464 \param obj data object
465 \return data object IOR or null string if IOR is empty
467 QString SalomeApp_DataObject::ior( const _PTR(SObject)& obj ) const
472 _PTR(GenericAttribute) attr;
473 if ( obj->FindAttribute( attr, "AttributeIOR" ) )
475 _PTR(AttributeIOR) iorAttr = attr;
478 std::string str = iorAttr->Value();
479 txt = QString( str.c_str() );
487 \brief Get data object entry identifier.
488 \param obj data object
489 \return data object entry identifier or empty object does not have entry
491 QString SalomeApp_DataObject::entry( const _PTR(SObject)& obj ) const
496 std::string str = obj->GetID();
497 txt = QString( str.c_str() );
503 \brief Get data object value.
504 \param obj data object
505 \return data object value or empty string if there is no
506 value associated to the object
508 QString SalomeApp_DataObject::value( const _PTR(SObject)& obj ) const
514 _PTR(GenericAttribute) attr;
516 if ( obj->FindAttribute( attr, "AttributeString" ) )
518 _PTR(AttributeString) strAttr = attr;
519 std::string str = strAttr->Value();
520 QString aStrings = QString( str.c_str() );
522 //Special case to show NoteBook variables in the "Value" column of the OB
523 if ( LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() ) )
525 if ( SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( aRoot->study() ) )
527 _PTR(Study) studyDS( aStudy->studyDS() );
530 QStringList aSectionList = aStrings.split( "|" );
531 if ( !aSectionList.isEmpty() )
533 QString aLastSection = aSectionList.last();
534 QStringList aStringList = aLastSection.split( ":" );
535 if ( !aStringList.isEmpty() )
538 for ( int i = 0, n = aStringList.size(); i < n; i++ )
540 QString aStr = aStringList[i];
541 if ( studyDS->IsVariable( aStr.toStdString() ) )
542 val.append( aStr + ", " );
545 if ( !val.isEmpty() )
546 val.remove( val.length() - 2, 2 );
554 else if ( obj->FindAttribute( attr, "AttributeInteger" ) )
556 _PTR(AttributeInteger) intAttr = attr;
558 val = QString::number( intAttr->Value() );
560 else if ( obj->FindAttribute( attr, "AttributeReal" ) )
562 _PTR(AttributeReal) realAttr = attr;
564 val = QString::number( realAttr->Value() );
566 else if ( obj->FindAttribute( attr, "AttributeTableOfInteger" ) )
568 _PTR(AttributeTableOfInteger) tableAttr = attr;
569 std::string title = tableAttr->GetTitle();
570 val = QString( title.c_str() );
571 if ( !val.isEmpty() )
572 val += QString( " " );
573 val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
575 else if ( obj->FindAttribute( attr, "AttributeTableOfReal" ) )
577 _PTR(AttributeTableOfReal) tableAttr = attr;
578 std::string title = tableAttr->GetTitle();
579 val = QString( title.c_str() );
580 if ( !val.isEmpty() )
581 val += QString( " " );
582 val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
584 else if ( obj->FindAttribute( attr, "AttributeComment" ) )
586 _PTR(AttributeComment) comm = attr;
587 std::string str = comm->Value();
588 val = QString( str.c_str() );
594 void SalomeApp_DataObject::insertChildAtTag( SalomeApp_DataObject* obj, int tag )
597 int npos = qMin( tag-1,childCount() );
598 for ( int i = npos; i > 0; i-- )
600 if ( (dynamic_cast<SalomeApp_DataObject*>( childObject( i-1 ) ) )->object()->Tag() < tag )
606 insertChildAtPos( obj, pos );
609 void SalomeApp_DataObject::updateItem()
611 if ( modified() ) return;
616 \class SalomeApp_ModuleObject
617 \brief This class is used for optimized access to the SALOMEDS-based
618 data model from SalomeApp_DataObject class instances.
619 \sa CAM_ModuleObject class
624 \param parent parent data object
626 SalomeApp_ModuleObject::SalomeApp_ModuleObject( SUIT_DataObject* parent )
627 : CAM_DataObject( parent ),
628 LightApp_DataObject( parent ),
629 SalomeApp_DataObject( parent ),
630 CAM_ModuleObject( parent )
636 \param sobj SALOMEDS object
637 \param parent parent data object
639 SalomeApp_ModuleObject::SalomeApp_ModuleObject( const _PTR(SObject)& sobj,
640 SUIT_DataObject* parent )
641 : CAM_DataObject( parent ),
642 LightApp_DataObject( parent ),
643 SalomeApp_DataObject( sobj, parent ),
644 CAM_ModuleObject( parent )
651 \param sobj SALOMEDS object
652 \param parent parent data object
654 SalomeApp_ModuleObject::SalomeApp_ModuleObject( CAM_DataModel* dm,
655 const _PTR(SObject)& sobj,
656 SUIT_DataObject* parent )
657 : CAM_DataObject( parent ),
658 LightApp_DataObject( parent ),
659 SalomeApp_DataObject( sobj, parent ),
660 CAM_ModuleObject( dm, parent )
667 SalomeApp_ModuleObject::~SalomeApp_ModuleObject()
672 \brief Get module name.
675 QString SalomeApp_ModuleObject::name() const
677 return SalomeApp_DataObject::name();
681 \brief Get data object icon for the specified column.
683 \return object icon for the specified column
684 \sa CAM_ModuleObject class
686 QPixmap SalomeApp_ModuleObject::icon( const int id ) const
688 QPixmap p = SalomeApp_DataObject::icon( id );
689 // The module might not provide a separate small icon
690 // for Obj. Browser representation -> always try to scale it
691 // See CAM_ModuleObject::icon()
693 p = Qtx::scaleIcon( p, 16 );
698 \brief Get data object tooltip for the specified column.
700 \return object tooltip for the specified column
702 QString SalomeApp_ModuleObject::toolTip( const int id ) const
704 return SalomeApp_DataObject::toolTip( id );
708 \class SalomeApp_RootObject
709 \brief Root data object for the CORBA-based SALOME application.
711 This class is to be instanciated by only one object - the root object
712 of the SalomeApp data object tree. This object is not shown in the object browser.
713 The goal of this class is to provide a unified access to SalomeApp_Study
714 object from SalomeApp_DataObject instances.
719 \param study pointer to the study
721 SalomeApp_RootObject::SalomeApp_RootObject( LightApp_Study* study )
722 : CAM_DataObject( 0 ),
723 LightApp_DataObject( 0 ),
724 SalomeApp_DataObject( 0 ),
725 LightApp_RootObject( study ),
733 SalomeApp_RootObject::~SalomeApp_RootObject()
738 \brief Get data object name.
741 QString SalomeApp_RootObject::name() const
743 return LightApp_RootObject::name();
747 \brief Get object string identifier.
750 QString SalomeApp_RootObject::entry() const
752 return LightApp_RootObject::entry();
756 \brief Get object text data for the specified column.
758 \return object text data
760 QString SalomeApp_RootObject::text( const int id ) const
762 return LightApp_RootObject::text( id );
766 \brief Get data object icon for the specified column.
768 \return object icon for the specified column
770 QPixmap SalomeApp_RootObject::icon( const int id ) const
772 return LightApp_RootObject::icon( id );
776 \brief Get data object color for the specified column.
777 \param role color role
778 \param id column id (not used)
779 \return object color for the specified column
781 QColor SalomeApp_RootObject::color( const ColorRole role, const int id ) const
783 return LightApp_RootObject::color( role, id );
787 \brief Get data object tooltip for the specified column.
788 \param id column id (not used)
789 \return object tooltip for the specified column
791 QString SalomeApp_RootObject::toolTip( const int id ) const
793 return LightApp_RootObject::toolTip( id );
797 \class SalomeApp_SavePointObject
798 \brief Represents persistent visual_state object.
800 Save point objects are stored in the data model, but NOT in SObjects
801 structure, so they are handled separately using this special class
806 \param parent parent data object
807 \param id save point ID
810 SalomeApp_SavePointObject::SalomeApp_SavePointObject( SUIT_DataObject* parent,
812 SalomeApp_Study* study )
813 : LightApp_DataObject( parent ),
814 CAM_DataObject( parent ),
823 SalomeApp_SavePointObject::~SalomeApp_SavePointObject()
828 \brief Get save point unique identifier.
829 \return save point ID
831 int SalomeApp_SavePointObject::getId() const
837 \brief Get object string identifier.
840 QString SalomeApp_SavePointObject::entry() const
842 return QObject::tr( "SAVE_POINT_DEF_NAME" ) + QString::number( myId );
846 \brief Get data object name.
849 QString SalomeApp_SavePointObject::name() const
851 return myStudy->getNameOfSavePoint( myId );
855 \brief Get data object icon for the specified column.
857 \return object icon for the specified column
859 QPixmap SalomeApp_SavePointObject::icon( const int /*id*/ ) const
865 \brief Get data object tooltip for the specified column.
866 \param id column id (not used)
867 \return object tooltip for the specified column
869 QString SalomeApp_SavePointObject::toolTip( const int /*id*/ ) const
871 return QObject::tr( "SAVE_POINT_OBJECT_TOOLTIP" ).arg( name() );
875 \class SalomeApp_SavePointRootObject
876 \brief Represents parent object for visual_state objects.
881 \param parent parent object
883 SalomeApp_SavePointRootObject::SalomeApp_SavePointRootObject( SUIT_DataObject* parent )
884 : SUIT_DataObject( parent )
889 \brief Get data object name.
892 QString SalomeApp_SavePointRootObject::name() const
894 return QObject::tr( "SAVE_POINT_ROOT_NAME" );
898 \brief Get data object tooltip for the specified column.
899 \param id column id (not used)
900 \return object tooltip for the specified column
902 QString SalomeApp_SavePointRootObject::toolTip( const int /*id*/ ) const
904 return QObject::tr( "SAVE_POINT_ROOT_TOOLTIP" );