Back to index

salome-gui  6.5.0
LightApp_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 
00023 // File   : LightApp_DataObject.cxx
00024 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00025 
00026 #include "LightApp_DataObject.h"
00027 #include "LightApp_Study.h"
00028 #include "LightApp_DataModel.h"
00029 #include "LightApp_Module.h"
00030 #include "LightApp_Application.h"
00031 
00032 #include <CAM_Module.h>
00033 #include <SUIT_DataObjectKey.h>
00034 
00035 #include <QVariant>
00036 
00037 #include <iostream>
00038 
00046 class LightApp_DataObject::Key : public SUIT_DataObjectKey
00047 {
00048 public:
00049   Key( const QString& );
00050   virtual ~Key();
00051 
00052   virtual bool isLess( const SUIT_DataObjectKey* ) const;
00053   virtual bool isEqual( const SUIT_DataObjectKey* ) const;
00054 
00055 private:
00056   QString myEntry;
00057 };
00058 
00064 LightApp_DataObject::Key::Key( const QString& entry )
00065 : SUIT_DataObjectKey(),
00066   myEntry( entry )
00067 {
00068 }
00069 
00074 LightApp_DataObject::Key::~Key()
00075 {
00076 }
00077 
00084 bool LightApp_DataObject::Key::isLess( const SUIT_DataObjectKey* other ) const
00085 {
00086   Key* that = (Key*)other;
00087   return myEntry < that->myEntry;
00088 }
00089 
00096 bool LightApp_DataObject::Key::isEqual( const SUIT_DataObjectKey* other ) const
00097 {
00098   Key* that = (Key*)other;
00099   return myEntry == that->myEntry;
00100 }
00101 
00111 LightApp_DataObject::LightApp_DataObject( SUIT_DataObject* parent )
00112 : CAM_DataObject( parent ),
00113   myCompObject( 0 ),
00114   myCompDataType( "" )
00115 {
00116 }
00117 
00121 LightApp_DataObject::~LightApp_DataObject()
00122 {
00123 }
00124 
00125 int LightApp_DataObject::groupId() const
00126 {
00127   LightApp_DataModel* m = dynamic_cast<LightApp_DataModel*>( dataModel() );
00128   return m ? m->groupId() : CAM_DataObject::groupId();
00129 }
00130 
00134 QVariant LightApp_DataObject::customData(Qtx::CustomDataType type) {
00135   switch(type) {
00136   case Qtx::IdType:
00137     return EntryId;
00138     break;
00139   default:
00140     return QVariant();
00141     break;
00142   }
00143 }
00144 
00149 bool LightApp_DataObject::isVisible() const
00150 {
00151   LightApp_RootObject* r = dynamic_cast<LightApp_RootObject*>( root() );
00152   return r && r->study() && componentDataType() != r->study()->getVisualComponentName();
00153 }
00154 
00162 bool LightApp_DataObject::isDraggable() const
00163 {
00164   LightApp_Module* aModule = dynamic_cast<LightApp_Module*>(module());
00165   if (aModule) {
00166     return aModule->isDraggable(this);
00167   }
00168   return false;
00169 }
00170 
00180 bool LightApp_DataObject::isDropAccepted() const
00181 {
00182   LightApp_Module* aModule = dynamic_cast<LightApp_Module*>(module());
00183   if (aModule) {
00184     return aModule->isDropAccepted(this);
00185   }
00186   return false;
00187 }
00188 
00198 bool LightApp_DataObject::renameAllowed( const int id ) const
00199 {
00200   if ( id == NameId ) {
00201     LightApp_Module* m = dynamic_cast<LightApp_Module*>( module() );
00202     LightApp_Application* app = 0;
00203     LightApp_RootObject* r = dynamic_cast<LightApp_RootObject*>( root() );
00204     if(r && r->study())
00205       app  = dynamic_cast<LightApp_Application*>(r->study()->application());
00206 
00207     return ( m && m->renameAllowed( entry() ) ) ||
00208       ( app && app->renameAllowed( entry() ) );
00209   }
00210   return CAM_DataObject::renameAllowed( id );
00211 }
00212 
00213 
00222 bool LightApp_DataObject::setName(const QString& name)
00223 {
00224     LightApp_Module* m = dynamic_cast<LightApp_Module*>( module() );
00225     LightApp_RootObject* r = dynamic_cast<LightApp_RootObject*>( root() );
00226     LightApp_Application* app =
00227       (r && r->study()) ? dynamic_cast<LightApp_Application*>(r->study()->application()) : 0;
00228 
00229     return ( m && m->renameObject( entry(), name ) ) ||
00230            ( app && app->renameObject( entry(), name ) );
00231   return CAM_DataObject::setName(name);
00232 }
00233 
00234 
00243 QString LightApp_DataObject::entry() const
00244 {
00245   return QString();
00246 }
00247 
00256 QString LightApp_DataObject::refEntry() const
00257 {
00258   return QString();
00259 }
00260 
00268 bool LightApp_DataObject::isReference() const
00269 {
00270   return !refEntry().isEmpty();
00271 }
00272 
00277 SUIT_DataObjectKey* LightApp_DataObject::key() const
00278 {
00279   QString str = entry();
00280   return new Key( str );
00281 }
00282 
00296 QString LightApp_DataObject::text( const int id ) const
00297 {
00298   QString txt;
00299   
00300   switch ( id )
00301   {
00302   case EntryId:
00303     txt = entry();
00304     break;
00305   case RefEntryId:
00306     // Issue 21379: reference support at LightApp level
00307     if ( isReference() )
00308       txt = refEntry();
00309     break;
00310   default:
00311     // Issue 21379: Note that we cannot return some specially decorated
00312     // name string (like "* ref_obj_name") when isReference() returns true, 
00313     // since there is no generic way at LightApp level
00314     // to query the object name using refEntry() up to now.
00315     // TODO: Think how to make reference name generation
00316     // more generic at move it here from SalomeApp level...
00317     txt = CAM_DataObject::text( id );
00318     break;
00319   }
00320 
00321   return txt;
00322 }
00323 
00330 QColor LightApp_DataObject::color( const ColorRole role, const int id) const
00331 {
00332   QColor c;
00333 
00334   // Issue 21379: reference support at LightApp level
00335   // Centralized way for choosing text/background color for references.
00336   // Colors for "normal" objects should be chosen by sub-classes.
00337   switch ( role )
00338   {
00339   case Text:
00340   case Foreground:
00341     // text color (not selected item)
00342     // TODO: think how to detect invalid references...
00343     if ( isReference() )
00344       c = QColor( 255, 0, 0 );      // valid reference (red)
00345     break;
00346 
00347   case Highlight:
00348     // background color for the highlighted item
00349     // TODO: think how to detect invalid references...
00350     if ( isReference() ) 
00351       c = QColor( 255, 0, 0 );      // valid reference (red)
00352     break;
00353 
00354   case HighlightedText:
00355     // text color for the highlighted item
00356     if ( isReference() )
00357       c = QColor( 255, 255, 255 );   // white
00358     break;
00359 
00360   default:
00361     break;
00362   }
00363 
00364   if ( !c.isValid() )
00365     c = CAM_DataObject::color( role, id );
00366 
00367   return c;
00368 }
00369 
00374 SUIT_DataObject* LightApp_DataObject::componentObject() const
00375 {
00376   if ( !myCompObject ) {
00377     SUIT_DataObject* compObj = (SUIT_DataObject*)this;
00378 
00379     while ( compObj && compObj->parent() && compObj->parent() != root() ) {
00380       compObj = compObj->parent();
00381     }
00382     LightApp_DataObject* that = (LightApp_DataObject*)this;
00383     that->myCompObject = compObj;
00384   }
00385   return myCompObject;
00386 }
00387 
00392 QString LightApp_DataObject::componentDataType() const
00393 {
00394   if ( myCompDataType.isEmpty() ) {
00395     SUIT_DataObject* aCompObj = componentObject();
00396     LightApp_ModuleObject* anObj = dynamic_cast<LightApp_ModuleObject*>( aCompObj );
00397     if ( anObj ) {
00398       CAM_DataModel* aModel = anObj->dataModel();
00399       if ( aModel ) {
00400         LightApp_DataObject* that = (LightApp_DataObject*)this;
00401         that->myCompDataType = aModel->module()->name();
00402       }
00403     }
00404   }
00405   return myCompDataType;
00406 }
00407 
00414 bool LightApp_DataObject::customSorting( const int id ) const
00415 {
00416   // perform custom sorting for the "Entry" column
00417   return id == EntryId ? true : CAM_DataObject::customSorting( id );
00418 }
00419 
00432 bool LightApp_DataObject::compare( const QVariant& left, const QVariant& right, const int id ) const
00433 {
00434   if ( id == EntryId )
00435   {
00436     // perform custom sorting for the "Entry" column
00437     QString leftStr  = left.toString();
00438     QString rightStr = right.toString();
00439     QStringList idsLeft  = leftStr.split( ":", QString::SkipEmptyParts );
00440     QStringList idsRight = rightStr.split( ":", QString::SkipEmptyParts );
00441     if ( idsLeft.count() > 1 || idsRight.count() > 1 ) {
00442       bool result = true;
00443       bool calculated = false;
00444       for ( int i = 0; i < idsLeft.count() || i < idsRight.count(); i++ ) {
00445         bool okLeft = true, okRight = true;
00446         int lid = 0, rid = 0;
00447         if ( i < idsLeft.count() )
00448           lid = idsLeft[i].toInt( &okLeft );
00449         if ( i < idsRight.count() )
00450           rid = idsRight[i].toInt( &okRight );
00451         if ( okLeft && okRight ) {
00452           // both seem to be correct integer ID
00453           return lid < rid;
00454         }
00455         else if ( okLeft || okRight ) {
00456           // objects with correct (int) ID have higher priority
00457           return okLeft;
00458         }
00459         else {
00460           // both not integer ID
00461           int r = QString::localeAwareCompare( idsLeft[i], idsRight[i] );
00462           if ( !calculated && r != 0 ) {
00463             result = r < 0;
00464             calculated = true;
00465           }
00466         }
00467       }
00468       // we should reach this if the entries are exactly equal
00469       return result;
00470     }
00471     return QString::localeAwareCompare( leftStr, rightStr ) < 0;
00472   }
00473   return CAM_DataObject::compare( left, right, id );
00474 }
00475 
00486 LightApp_ModuleObject::LightApp_ModuleObject( SUIT_DataObject* parent )
00487 : CAM_DataObject( parent ),
00488   LightApp_DataObject( parent ),
00489   CAM_ModuleObject( parent )
00490 {
00491 }
00492 
00498 LightApp_ModuleObject::LightApp_ModuleObject( CAM_DataModel* dm, SUIT_DataObject* parent )
00499 : CAM_DataObject( parent ),
00500   LightApp_DataObject( parent ),
00501   CAM_ModuleObject( dm, parent )
00502 {
00503 }
00504 
00505 /*
00506   \brief Destructor.
00507 */
00508 LightApp_ModuleObject::~LightApp_ModuleObject()
00509 {
00510 }
00511 
00516 QString LightApp_ModuleObject::name() const
00517 {
00518   return CAM_ModuleObject::name();
00519 }
00520 
00526 QPixmap LightApp_ModuleObject::icon( const int id ) const
00527 {
00528   return CAM_ModuleObject::icon( id );
00529 }
00530 
00536 QString LightApp_ModuleObject::toolTip( const int id ) const
00537 {
00538   return CAM_ModuleObject::toolTip( id );
00539 }
00540 
00549 void LightApp_ModuleObject::insertChild( SUIT_DataObject* obj, int pos )
00550 {
00551   LightApp_DataObject::insertChild( obj, pos );
00552 
00553   CAM_DataModel* aModel = dataModel();
00554 
00555   LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( parent() );
00556 
00557   if ( aRoot )
00558     aRoot->study()->addComponent( aModel );
00559 }
00560 
00571 /*
00572   \brief Constructor.
00573   \param study study
00574 */
00575 LightApp_RootObject::LightApp_RootObject( LightApp_Study* study )
00576 : CAM_DataObject( 0 ),
00577   LightApp_DataObject( 0 ),
00578   myStudy( study )
00579 {
00580 }
00581 
00582 /*
00583   \brief Destructor.
00584 */
00585 LightApp_RootObject::~LightApp_RootObject()
00586 {
00587 }
00588 
00589 /*
00590   \brief Set study.
00591   \param study pointer to the study
00592 */
00593 void LightApp_RootObject::setStudy( LightApp_Study* study )
00594 {
00595   myStudy = study;
00596 }
00597 
00598 /*
00599   \brief Get study
00600   \return pointer to the study
00601 */
00602 LightApp_Study* LightApp_RootObject::study() const
00603 {
00604   return myStudy;
00605 }