Back to index

salome-gui  6.5.0
SalomeApp_DataObject.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 // File   : SalomeApp_DataObject.cxx
00023 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00024 
00025 #include "SalomeApp_DataObject.h"
00026 #include "SalomeApp_Study.h"
00027 #include "SalomeApp_Application.h"
00028 
00029 #include <CAM_DataObject.h>
00030 
00031 #include <SUIT_Session.h>
00032 #include <SUIT_Application.h>
00033 #include <SUIT_ResourceMgr.h>
00034 
00035 #include <SALOME_LifeCycleCORBA.hxx>
00036 #include <Basics_Utils.hxx>
00037 
00038 #include <QObject>
00039 #include <QVariant>
00040 
00051 SalomeApp_DataObject::SalomeApp_DataObject( SUIT_DataObject* parent )
00052 : CAM_DataObject( parent ),
00053   LightApp_DataObject( parent )
00054 {
00055 }
00056 
00062 SalomeApp_DataObject::SalomeApp_DataObject( const _PTR(SObject)& sobj, 
00063                                             SUIT_DataObject* parent )
00064 : CAM_DataObject( parent ),
00065   LightApp_DataObject( parent )
00066 {
00067   myObject = sobj;
00068 }
00069 
00073 SalomeApp_DataObject::~SalomeApp_DataObject()
00074 {
00075 }
00076 
00081 QString SalomeApp_DataObject::name() const
00082 {
00083   QString str;
00084   if ( myObject )
00085     str = myObject->GetName().c_str();
00086   
00087   if ( str.isEmpty() ) {
00088     _PTR(SObject) refObj = referencedObject();
00089     if ( refObj )
00090       str = refObj->GetName().c_str();
00091   }
00092   
00093   if ( isReference() ) {
00094     if ( !(QString(referencedObject()->GetName().c_str()).isEmpty()) )
00095       str = QString( "* " ) + str;
00096     else
00097       str = QString( "<Invalid Reference>" );
00098   }
00099   return str;
00100 }
00101 
00106 QString SalomeApp_DataObject::entry() const
00107 {
00108   return entry( myObject );
00109 }
00110 
00124 QString SalomeApp_DataObject::text( const int id ) const
00125 {
00126   QString txt;
00127 
00128   // Text for "Value" and "IOR" columns
00129   switch ( id )
00130   {
00131   case ValueId:
00132 #ifndef WIN32
00133     if ( componentObject() != this )
00134 #else
00135     if ( componentObject() != (SUIT_DataObject*)this )
00136 #endif
00137       txt = value( object() );
00138       if ( txt.isEmpty() )
00139         txt = value( referencedObject() );
00140     break;
00141   case IORId:
00142     txt = ior( referencedObject() );
00143     break;
00144   default:
00145     // Issue 21379: LightApp_DataObject::text() treats "Entry"
00146     // and "Reference Entry" columns
00147     txt = LightApp_DataObject::text( id );
00148     break;
00149   }
00150   return txt;
00151 }
00152 
00158 QPixmap SalomeApp_DataObject::icon( const int id ) const
00159 {
00160   // we display icon only for the first (NameId ) column
00161   if ( id == NameId ) {
00162     _PTR(GenericAttribute) anAttr;
00163     if ( myObject && myObject->FindAttribute( anAttr, "AttributePixMap" ) ){
00164       _PTR(AttributePixMap) aPixAttr ( anAttr );
00165       if ( aPixAttr->HasPixMap() ) {
00166         QString componentType = componentDataType();
00167         QString pixmapID      = aPixAttr->GetPixMap().c_str();
00168         // select a plugin within a component
00169         QStringList plugin_pixmap = pixmapID.split( "::", QString::KeepEmptyParts );
00170         if ( plugin_pixmap.size() == 2 ) {
00171           componentType = plugin_pixmap.front();
00172           pixmapID      = plugin_pixmap.back();
00173         }
00174         QString pixmapName = QObject::tr( pixmapID.toLatin1().constData() );
00175         LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() );
00176         if ( aRoot && aRoot->study() ) {
00177           SUIT_ResourceMgr* mgr = aRoot->study()->application()->resourceMgr();
00178           return mgr->loadPixmap( componentType, pixmapName, false ); 
00179         }
00180       }
00181     }
00182   }
00183   return LightApp_DataObject::icon( id );
00184 }
00185 
00192 QColor SalomeApp_DataObject::color( const ColorRole role, const int id ) const
00193 {
00194   // we ignore parameter <id> in order to use the same colors for 
00195   // all columns
00196   QColor c;
00197   switch ( role )
00198   {
00199   case Text:
00200   case Foreground:
00201     // text color (not selected item)
00202     if ( isReference() ) {
00203       if ( QString(referencedObject()->GetName().c_str()).isEmpty() )
00204         c = QColor( 200, 200, 200 );  // invalid reference (grayed)
00205     }
00206     else if ( myObject ) {
00207       // get color atrtribute value
00208       _PTR(GenericAttribute) anAttr;
00209       if ( myObject->FindAttribute( anAttr, "AttributeTextColor" ) ) {
00210         _PTR(AttributeTextColor) aColAttr = anAttr;
00211         c = QColor( (int)aColAttr->TextColor().R, (int)aColAttr->TextColor().G, (int)aColAttr->TextColor().B );
00212       }
00213     }
00214     break;
00215 
00216   case Highlight:
00217     // background color for the highlighted item
00218     if ( isReference() ) {
00219       if ( QString(referencedObject()->GetName().c_str()).isEmpty() )
00220         c = QColor( 200, 200, 200 );  // invalid reference (grayed)
00221     }
00222     else if ( myObject ) {
00223       // get color atrtribute value
00224       _PTR(GenericAttribute) anAttr;
00225       if( myObject->FindAttribute ( anAttr, "AttributeTextHighlightColor") ) {
00226         _PTR(AttributeTextHighlightColor) aHighColAttr = anAttr;
00227         c = QColor( (int)(aHighColAttr->TextHighlightColor().R), 
00228                     (int)(aHighColAttr->TextHighlightColor().G), 
00229                     (int)(aHighColAttr->TextHighlightColor().B));
00230       }
00231     }
00232     break;
00233   default:
00234     break;
00235   }
00236 
00237   // Issue 21379: LightApp_DataObject::color() defines colors for valid references
00238   if ( !c.isValid() )
00239     c = LightApp_DataObject::color( role, id );
00240 
00241   return c;
00242 }
00243 
00249 QString SalomeApp_DataObject::toolTip( const int /*id*/ ) const
00250 {
00251   // we ignore parameter <id> in order to use the same tooltip for 
00252   // all columns
00253   
00254   // Get customized tooltip in case of it exists
00255   const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
00256   // Check if the component has been loaded.
00257   // In order to avoid loading the component only for getting a custom tooltip.
00258   if ( compObj && compObj != this && !ior(compObj->object()).isEmpty() ) {
00259     SalomeApp_Application* app = 
00260       dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
00261     if ( app ) {
00262       // --- try to find (and not load) the component instance, like GEOM instance,
00263       //     registered in naming service under Containers/<hostname>/...
00264       //     with any container name, on every machine available
00265       Engines::MachineParameters params;
00266       app->lcc()->preSet(params); // --- any container name, anywhere
00267       Engines::EngineComponent_var aComponent =
00268         app->lcc()->FindComponent(params, componentDataType().toLatin1().constData() );
00269       
00270       if ( !CORBA::is_nil(aComponent) && aComponent->hasObjectInfo() ) {
00271         LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() );
00272         if ( aRoot && aRoot->study() )
00273           return QString( (CORBA::String_var)aComponent->getObjectInfo( aRoot->study()->id(), entry().toLatin1().constData()) );
00274       }
00275     }
00276   }
00277   
00278   return QString( "Object \'%1\', module \'%2\', ID=%3" ).arg( name() ).arg( componentDataType() ).arg( entry() );
00279 }
00280 
00286 QFont SalomeApp_DataObject::font( const int id ) const
00287 {
00288   QFont f = LightApp_DataObject::font( id );
00289   if ( id == NameId ) {
00290     if ( !expandable() && hasChildren() ) {
00291       // set bold font to highlight the item which is non-expandable but has children
00292       f.setBold( true );
00293     }
00294   }
00295   return f;
00296 }
00297 
00302 QString SalomeApp_DataObject::componentDataType() const
00303 {
00304   //  if ( myCompDataType.isEmpty() ) {
00305     const SalomeApp_DataObject* compObj = dynamic_cast<SalomeApp_DataObject*>( componentObject() );
00306     if ( compObj && compObj->object() )
00307     {
00308       _PTR(SComponent) aComp( compObj->object() );
00309       if ( aComp ) {
00310         SalomeApp_DataObject* that = (SalomeApp_DataObject*)this;
00311         that->myCompDataType = aComp->ComponentDataType().c_str();
00312       }
00313     }
00314     //  }
00315   return myCompDataType;
00316 }
00317 
00322 _PTR(SObject) SalomeApp_DataObject::object() const
00323 {
00324   return myObject;
00325 }
00326 
00334 QString SalomeApp_DataObject::refEntry() const
00335 {
00336   return entry( referencedObject() );
00337 }
00338 
00346 bool SalomeApp_DataObject::isReference() const
00347 {
00348   bool isRef = false;
00349   if ( myObject )
00350   {
00351     _PTR(SObject) refObj;
00352     isRef = myObject->ReferencedObject( refObj );
00353   }
00354   return isRef;
00355 }
00356 
00361 _PTR(SObject) SalomeApp_DataObject::referencedObject() const
00362 {
00363   _PTR(SObject) refObj;
00364   _PTR(SObject) obj = myObject;
00365   while ( obj && obj->ReferencedObject( refObj ) )
00366     obj = refObj;
00367 
00368   return obj;
00369 }
00370 
00375 bool SalomeApp_DataObject::hasChildren() const
00376 {
00377   bool ok = false;
00378 
00379   // tmp??
00380   _PTR(UseCaseBuilder) aUseCaseBuilder = myObject->GetStudy()->GetUseCaseBuilder();
00381   if (aUseCaseBuilder->IsUseCaseNode(myObject)) {
00382     ok = aUseCaseBuilder->HasChildren(myObject);
00383     // TODO: check name as below?
00384   }
00385   else {
00386     _PTR(ChildIterator) it ( myObject->GetStudy()->NewChildIterator( myObject ) );
00387     for ( ; it->More() && !ok; it->Next() ) {
00388       _PTR(SObject) obj = it->Value();
00389       if ( obj ) {
00390         _PTR(SObject) refObj;
00391         //if ( obj->ReferencedObject( refObj ) ) continue; // omit references
00392         if ( obj->GetName() != "" ) ok = true;
00393       }
00394     }
00395   }
00396   return ok;
00397 }
00398 
00403 bool SalomeApp_DataObject::expandable() const
00404 {
00405   bool exp = true;
00406   _PTR(GenericAttribute) anAttr;
00407   if ( myObject && myObject->FindAttribute( anAttr, "AttributeExpandable" ) ) {
00408     _PTR(AttributeExpandable) aAttrExp = anAttr;
00409     exp = aAttrExp->IsExpandable();
00410   }
00411   return exp;
00412 }
00413 
00418 bool SalomeApp_DataObject::isVisible() const
00419 {
00420   bool isDraw = true;
00421   _PTR(GenericAttribute) anAttr;
00422   if ( myObject && myObject->FindAttribute(anAttr, "AttributeDrawable") ) 
00423   {
00424     _PTR(AttributeDrawable) aAttrDraw = anAttr;
00425     isDraw = aAttrDraw->IsDrawable(); 
00426   }
00427   return isDraw && LightApp_DataObject::isVisible() && ( !name().isEmpty() || isReference() );
00428 }
00429 
00436 bool SalomeApp_DataObject::customSorting( const int id ) const
00437 {
00438   // perform custom sorting for the "Entry" and "Reference Entry" columns
00439   return id == EntryId  || id == RefEntryId  ? true 
00440     : LightApp_DataObject::customSorting( id );
00441 }
00442 
00455 bool SalomeApp_DataObject::compare( const QVariant& left, const QVariant& right, const int id ) const
00456 {
00457   // use the same custom sorting for the "Reference Entry" column as for the
00458   // "Entry" column (call base implementation)
00459   return LightApp_DataObject::compare( left, right, id == RefEntryId ? EntryId : id );
00460 }
00461 
00467 QString SalomeApp_DataObject::ior( const _PTR(SObject)& obj ) const
00468 {
00469   QString txt;
00470   if ( obj )
00471   {
00472     _PTR(GenericAttribute) attr;
00473     if ( obj->FindAttribute( attr, "AttributeIOR" ) )
00474     {
00475       _PTR(AttributeIOR) iorAttr = attr;
00476       if ( iorAttr )
00477       {
00478         std::string str = iorAttr->Value();
00479         txt = QString( str.c_str() );
00480       }
00481     }
00482   }
00483   return txt;
00484 }
00485 
00491 QString SalomeApp_DataObject::entry( const _PTR(SObject)& obj ) const
00492 {
00493   QString txt;
00494   if ( obj )
00495   {
00496     std::string str = obj->GetID();
00497     txt = QString( str.c_str() );
00498   }
00499   return txt;
00500 }
00501 
00508 QString SalomeApp_DataObject::value( const _PTR(SObject)& obj ) const
00509 {
00510   if ( !obj )
00511     return QString();
00512 
00513   QString val;
00514   _PTR(GenericAttribute) attr;
00515 
00516   if ( obj->FindAttribute( attr, "AttributeString" ) )
00517   {
00518     _PTR(AttributeString) strAttr = attr;
00519     std::string str = strAttr->Value();
00520     QString aStrings = QString( str.c_str() );
00521     
00522     //Special case to show NoteBook variables in the "Value" column of the OB 
00523     if ( LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root() ) )
00524     {
00525       if ( SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( aRoot->study() ) )
00526       {
00527         _PTR(Study) studyDS( aStudy->studyDS() );
00528 
00529         bool ok = false;
00530         QStringList aSectionList = aStrings.split( "|" );
00531         if ( !aSectionList.isEmpty() )
00532         {
00533           QString aLastSection = aSectionList.last();
00534           QStringList aStringList = aLastSection.split( ":" );
00535           if ( !aStringList.isEmpty() )
00536           {
00537             ok = true;
00538             for ( int i = 0, n = aStringList.size(); i < n; i++ )
00539             {
00540               QString aStr = aStringList[i];
00541               if ( studyDS->IsVariable( aStr.toStdString() ) )
00542                 val.append( aStr + ", " );
00543             }
00544 
00545             if ( !val.isEmpty() )
00546               val.remove( val.length() - 2, 2 );
00547           }
00548         }
00549         if( !ok )
00550           val = aStrings;
00551       }
00552     }
00553   }
00554   else if ( obj->FindAttribute( attr, "AttributeInteger" ) )
00555   {
00556     _PTR(AttributeInteger) intAttr = attr;
00557     if ( intAttr )
00558       val = QString::number( intAttr->Value() );
00559   }
00560   else if ( obj->FindAttribute( attr, "AttributeReal" ) )
00561   {
00562     _PTR(AttributeReal) realAttr = attr;
00563     if ( realAttr )
00564       val = QString::number( realAttr->Value() );
00565   }
00566   else if ( obj->FindAttribute( attr, "AttributeTableOfInteger" ) )
00567   {
00568     _PTR(AttributeTableOfInteger) tableAttr = attr;
00569     std::string title = tableAttr->GetTitle();
00570     val = QString( title.c_str() );
00571     if ( !val.isEmpty() )
00572       val += QString( " " );
00573     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
00574   }
00575   else if ( obj->FindAttribute( attr, "AttributeTableOfReal" ) )
00576   {
00577     _PTR(AttributeTableOfReal) tableAttr = attr;
00578     std::string title = tableAttr->GetTitle();
00579     val = QString( title.c_str() );
00580     if ( !val.isEmpty() )
00581       val += QString( " " );
00582     val += QString( "[%1,%2]" ).arg( tableAttr->GetNbRows() ).arg( tableAttr->GetNbColumns() );
00583   }
00584   else if ( obj->FindAttribute( attr, "AttributeComment" ) )
00585   {
00586     _PTR(AttributeComment) comm = attr;
00587     std::string str = comm->Value();
00588     val = QString( str.c_str() );
00589   }
00590 
00591   return val;
00592 }
00593 
00594 void SalomeApp_DataObject::insertChildAtTag( SalomeApp_DataObject* obj, int tag )
00595 {
00596   int pos = 0;
00597   int npos = qMin( tag-1,childCount() );
00598   for ( int i = npos; i > 0; i-- )
00599   {
00600     if ( (dynamic_cast<SalomeApp_DataObject*>( childObject( i-1 ) ) )->object()->Tag() < tag )
00601     {
00602       pos = i;
00603       break;
00604     }
00605   }
00606   insertChildAtPos( obj, pos );
00607 }
00608 
00609 void SalomeApp_DataObject::updateItem()
00610 {
00611   if ( modified() ) return;
00612   setModified( true );
00613 }
00614 
00626 SalomeApp_ModuleObject::SalomeApp_ModuleObject( SUIT_DataObject* parent )
00627 : CAM_DataObject( parent ),
00628   LightApp_DataObject( parent ),
00629   SalomeApp_DataObject( parent ),
00630   CAM_ModuleObject( parent )
00631 {
00632 }
00633 
00639 SalomeApp_ModuleObject::SalomeApp_ModuleObject( const _PTR(SObject)& sobj, 
00640                                                 SUIT_DataObject* parent )
00641 : CAM_DataObject( parent ),
00642   LightApp_DataObject( parent ),
00643   SalomeApp_DataObject( sobj, parent ),
00644   CAM_ModuleObject( parent )
00645 {
00646 }
00647 
00654 SalomeApp_ModuleObject::SalomeApp_ModuleObject( CAM_DataModel* dm, 
00655                                                 const _PTR(SObject)& sobj, 
00656                                                 SUIT_DataObject* parent )
00657 : CAM_DataObject( parent ),
00658   LightApp_DataObject( parent ),
00659   SalomeApp_DataObject( sobj, parent ),
00660   CAM_ModuleObject( dm, parent )
00661 {
00662 }
00663 
00667 SalomeApp_ModuleObject::~SalomeApp_ModuleObject()
00668 {
00669 }
00670 
00675 QString SalomeApp_ModuleObject::name() const
00676 {
00677   return SalomeApp_DataObject::name();
00678 }
00679 
00686 QPixmap SalomeApp_ModuleObject::icon( const int id ) const
00687 {
00688   QPixmap p = SalomeApp_DataObject::icon( id );
00689   // The module might not provide a separate small icon
00690   // for Obj. Browser representation -> always try to scale it
00691   // See CAM_ModuleObject::icon()
00692   if ( !p.isNull() )
00693     p = Qtx::scaleIcon( p, 16 );
00694   return p;
00695 }
00696 
00702 QString SalomeApp_ModuleObject::toolTip( const int id ) const
00703 {
00704   return SalomeApp_DataObject::toolTip( id );
00705 }
00706 
00721 SalomeApp_RootObject::SalomeApp_RootObject( LightApp_Study* study )
00722 : CAM_DataObject( 0 ),
00723   LightApp_DataObject( 0 ),
00724   SalomeApp_DataObject( 0 ),
00725   LightApp_RootObject( study ),
00726   _toSynchronize(true)
00727 {
00728 }
00729 
00733 SalomeApp_RootObject::~SalomeApp_RootObject()
00734 {
00735 }
00736 
00741 QString SalomeApp_RootObject::name() const
00742 {
00743   return LightApp_RootObject::name();
00744 }
00745  
00750 QString SalomeApp_RootObject::entry() const
00751 {
00752   return LightApp_RootObject::entry();
00753 }
00754 
00760 QString SalomeApp_RootObject::text( const int id ) const
00761 {
00762   return LightApp_RootObject::text( id );
00763 }
00764 
00770 QPixmap SalomeApp_RootObject::icon( const int id ) const
00771 {
00772   return LightApp_RootObject::icon( id );
00773 }
00774 
00781 QColor SalomeApp_RootObject::color( const ColorRole role, const int id ) const
00782 {
00783   return LightApp_RootObject::color( role, id );
00784 }
00785 
00791 QString SalomeApp_RootObject::toolTip( const int id ) const
00792 {
00793   return LightApp_RootObject::toolTip( id );
00794 }
00795 
00810 SalomeApp_SavePointObject::SalomeApp_SavePointObject( SUIT_DataObject* parent, 
00811                                                       const int id, 
00812                                                       SalomeApp_Study* study )
00813 : LightApp_DataObject( parent ), 
00814   CAM_DataObject( parent ),
00815   myId( id ),
00816   myStudy( study )
00817 {
00818 }
00819 
00823 SalomeApp_SavePointObject::~SalomeApp_SavePointObject()
00824 {
00825 }
00826 
00831 int SalomeApp_SavePointObject::getId() const
00832 {
00833   return myId;
00834 }
00835 
00840 QString SalomeApp_SavePointObject::entry() const
00841 {
00842   return QObject::tr( "SAVE_POINT_DEF_NAME" ) + QString::number( myId );
00843 }
00844 
00849 QString SalomeApp_SavePointObject::name() const
00850 {
00851   return myStudy->getNameOfSavePoint( myId );
00852 }
00853 
00859 QPixmap SalomeApp_SavePointObject::icon( const int /*id*/ ) const
00860 {
00861   return QPixmap();
00862 }
00863 
00869 QString SalomeApp_SavePointObject::toolTip( const int /*id*/ ) const
00870 {
00871   return QObject::tr( "SAVE_POINT_OBJECT_TOOLTIP" ).arg( name() );
00872 }
00873 
00883 SalomeApp_SavePointRootObject::SalomeApp_SavePointRootObject( SUIT_DataObject* parent )
00884 : SUIT_DataObject( parent )
00885 {
00886 }
00887 
00892 QString SalomeApp_SavePointRootObject::name() const
00893 {
00894   return QObject::tr( "SAVE_POINT_ROOT_NAME" ); 
00895 }
00896 
00902 QString SalomeApp_SavePointRootObject::toolTip( const int /*id*/ ) const
00903 {
00904   return QObject::tr( "SAVE_POINT_ROOT_TOOLTIP" ); 
00905 }