Back to index

salome-gui  6.5.0
CAM_Module.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 #include "CAM_Module.h"
00024 
00025 #include "CAM_DataModel.h"
00026 #include "CAM_Application.h"
00027 #include "CAM_Study.h"
00028 
00029 #include <QtxAction.h>
00030 #include <QtxActionMenuMgr.h>
00031 #include <QtxActionToolMgr.h>
00032 
00033 #include <SUIT_Desktop.h>
00034 #include <SUIT_Session.h>
00035 #include <SUIT_ResourceMgr.h>
00036 
00044 class ActionMgrLocker
00045 {
00046 public:
00047   ActionMgrLocker( QtxActionMgr* m, bool use ) : myMgr( m ), myUseLock( use )
00048   {
00049     if ( myUseLock ) {
00050       myUpdEnabled = myMgr->isUpdatesEnabled();
00051       myMgr->setUpdatesEnabled( false );
00052     }
00053   }
00054   ~ActionMgrLocker()
00055   {
00056     if ( myUseLock ) {
00057       myMgr->setUpdatesEnabled( myUpdEnabled );
00058       //myMgr->update();
00059     }
00060   }
00061 
00062   QtxActionMgr* myMgr;
00063   bool          myUseLock;
00064   bool          myUpdEnabled;
00065 };
00066 
00072 CAM_Module::CAM_Module()
00073 : QObject(),
00074   myApp( 0 ),
00075   myDataModel( 0 )
00076 {
00077 }
00078 
00086 CAM_Module::CAM_Module( const QString& name )
00087 : QObject(),
00088   myApp( 0 ),
00089   myName( name ),
00090   myDataModel( 0 ),
00091   myMenuShown( false ),
00092   myToolShown( false )
00093 {
00094 }
00095 
00101 CAM_Module::~CAM_Module()
00102 {
00103   delete myDataModel;
00104   myDataModel = 0;
00105 }
00106 
00118 void CAM_Module::initialize( CAM_Application* app )
00119 {
00120   myApp = app;
00121   if ( myApp )
00122   {
00123     SUIT_Session* aSession = SUIT_Session::session();
00124     connect( aSession, SIGNAL( applicationClosed( SUIT_Application* ) ),
00125              this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
00126 
00127     connect( myApp, SIGNAL( infoChanged( QString ) ), this, SLOT( onInfoChanged( QString ) ) );
00128   }
00129 }
00130 
00136 QPixmap CAM_Module::moduleIcon() const
00137 {
00138   if ( myIcon.isNull() ) {
00139     QString iname = iconName();
00140     if ( !iname.isEmpty() ) {
00141       CAM_Module* that = (CAM_Module*)this;
00142       that->myIcon = application()->resourceMgr()->loadPixmap( name(), iname, false );
00143     }
00144   }
00145   return myIcon;
00146 }
00147 
00157 QString CAM_Module::iconName() const
00158 {
00159   return application()->moduleIcon( name() );
00160 }
00161 
00167 QString CAM_Module::name() const
00168 {
00169   return objectName();
00170 }
00171 
00177 QString CAM_Module::moduleName() const
00178 {
00179   return myName;
00180 }
00181 
00190 CAM_DataModel* CAM_Module::dataModel() const
00191 {
00192   if ( !myDataModel )
00193   {
00194     CAM_Module* that = (CAM_Module*)this;
00195     that->myDataModel = that->createDataModel();
00196     that->myDataModel->initialize();
00197   }
00198   return myDataModel;
00199 }
00200 
00205 CAM_Application* CAM_Module::application() const
00206 {
00207   return myApp;
00208 }
00209 
00213 bool CAM_Module::isSelectionCompatible()
00214 {
00215   return false;
00216 }
00217 
00230 bool CAM_Module::activateModule( SUIT_Study* /*study*/ )
00231 {
00232   return true;
00233 }
00234 
00247 bool CAM_Module::deactivateModule( SUIT_Study* )
00248 {
00249   return true;
00250 }
00251 
00259 void CAM_Module::studyClosed( SUIT_Study* study )
00260 {
00261   CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
00262   if ( !camDoc )
00263     return;
00264 
00265   CAM_DataModel* dm = dataModel();
00266   if ( dm && camDoc->containsDataModel( dm ) ) {
00267     dm->close();
00268     camDoc->removeDataModel( dm );
00269   }
00270 }
00271 
00280 void CAM_Module::studyChanged( SUIT_Study* /*oldStudy*/, SUIT_Study* /*newStudy*/ )
00281 {
00282 }
00283 
00288 bool CAM_Module::isActiveModule() const
00289 {
00290   return application() ? application()->activeModule() == this : false;
00291 }
00292 
00302 void CAM_Module::putInfo( const QString& msg, const int msec )
00303 {
00304   if ( application() )
00305     application()->putInfo( msg, msec );
00306 
00307   if ( msec < 0 )
00308     myInfo = msg;
00309 }
00310 
00319 void CAM_Module::onInfoChanged( QString txt )
00320 {
00321   if ( txt.isEmpty() && isActiveModule() && !myInfo.isEmpty() && application() )
00322     application()->putInfo( myInfo );
00323 }
00324 
00332 void CAM_Module::onApplicationClosed( SUIT_Application* theApp )
00333 {
00334   if (myApp == theApp)
00335     myApp = NULL;
00336 }
00337 
00342 CAM_DataModel* CAM_Module::createDataModel()
00343 {
00344   return new CAM_DataModel( this );
00345 }
00346 
00352 void CAM_Module::setName( const QString& name )
00353 {
00354   setObjectName( name );
00355 }
00356 
00362 void CAM_Module::setModuleName( const QString& name )
00363 {
00364   myName = name;
00365 }
00366 
00371 QtxActionMenuMgr* CAM_Module::menuMgr() const
00372 {
00373   QtxActionMenuMgr* mgr = 0;
00374   if ( application() && application()->desktop() )
00375     mgr = application()->desktop()->menuMgr();
00376   return mgr;
00377 }
00378 
00383 QtxActionToolMgr* CAM_Module::toolMgr() const
00384 {
00385   QtxActionToolMgr* mgr = 0;
00386   if ( application() && application()->desktop() )
00387     mgr = application()->desktop()->toolMgr();
00388   return mgr;
00389 }
00390 
00399 int CAM_Module::createTool( const QString& name )
00400 {
00401   if ( !toolMgr() )
00402     return -1;
00403 
00404   ActionMgrLocker lock( toolMgr(), !myToolShown );
00405 
00406   return toolMgr()->createToolBar( name, myToolShown );
00407 }
00408 
00428 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
00429 {
00430   if ( !toolMgr() )
00431     return -1;
00432 
00433   ActionMgrLocker lock( toolMgr(), !myToolShown );
00434 
00435   int regId = registerAction( id, a );
00436   int intId = toolMgr()->insert( a, tBar, idx );
00437 
00438   if ( !myToolShown )
00439     setToolShown( a, false );
00440 
00441   return intId != -1 ? regId : -1;
00442 }
00443 
00463 int CAM_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
00464 {
00465   if ( !toolMgr() )
00466     return -1;
00467 
00468   ActionMgrLocker lock( toolMgr(), !myToolShown );
00469 
00470   int regId = registerAction( id, a );
00471   int intId = toolMgr()->insert( a, tBar, idx );
00472 
00473   if ( !myToolShown )
00474     setToolShown( a, false );
00475 
00476   return intId != -1 ? regId : -1;
00477 }
00478 
00497 int CAM_Module::createTool( const int id, const int tBar, const int idx )
00498 {
00499   if ( !toolMgr() )
00500     return -1;
00501 
00502   ActionMgrLocker lock( toolMgr(), !myToolShown );
00503 
00504   int intId = toolMgr()->insert( action( id ), tBar, idx );
00505 
00506   if ( !myToolShown )
00507     setToolShown( action( id ), false );
00508 
00509   return intId != -1 ? id : -1;
00510 }
00511 
00530 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
00531 {
00532   if ( !toolMgr() )
00533     return -1;
00534 
00535   ActionMgrLocker lock( toolMgr(), !myToolShown );
00536 
00537   int intId = toolMgr()->insert( action( id ), tBar, idx );
00538 
00539   if ( !myToolShown )
00540     setToolShown( action( id ), false );
00541 
00542   return intId != -1 ? id : -1;
00543 }
00544 
00570 int CAM_Module::createMenu( const QString& subMenu, const int menu,
00571                             const int id, const int group, const int idx )
00572 {
00573   if ( !menuMgr() )
00574     return -1;
00575   
00576   return menuMgr()->insert( subMenu, menu, group, id, idx );
00577 }
00578 
00604 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
00605                             const int id, const int group, const int idx )
00606 {
00607   if ( !menuMgr() )
00608     return -1;
00609 
00610   return menuMgr()->insert( subMenu, menu, group, id, idx );
00611 }
00612 
00635 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int idx )
00636 {
00637   if ( !a || !menuMgr() )
00638     return -1;
00639   
00640   ActionMgrLocker lock( menuMgr(), !myMenuShown );
00641 
00642   int regId = registerAction( id, a );
00643   int intId = menuMgr()->insert( a, menu, group, idx );
00644 
00645   if ( !myMenuShown )
00646     setMenuShown( a, false );
00647 
00648   return intId != -1 ? regId : -1;
00649 }
00650 
00677 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx )
00678 {
00679   if ( !a || !menuMgr() )
00680     return -1;
00681 
00682   ActionMgrLocker lock( menuMgr(), !myMenuShown );
00683 
00684   int regId = registerAction( id, a );
00685   int intId = menuMgr()->insert( a, menu, group, idx );
00686 
00687   if ( !myMenuShown )
00688     setMenuShown( a, false );
00689   
00690   return intId != -1 ? regId : -1;
00691 }
00692 
00714 int CAM_Module::createMenu( const int id, const int menu, const int group, const int idx )
00715 {
00716   if ( !menuMgr() )
00717     return -1;
00718 
00719   ActionMgrLocker lock( menuMgr(), !myMenuShown );
00720 
00721   int intId = menuMgr()->insert( action( id ), menu, group, idx );
00722 
00723   if ( !myMenuShown )
00724     setMenuShown( action( id ), false );
00725 
00726   return intId != -1 ? id : -1;
00727 }
00728 
00754 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int idx )
00755 {
00756   if ( !menuMgr() )
00757     return -1;
00758 
00759   ActionMgrLocker lock( menuMgr(), !myMenuShown );
00760 
00761   int intId = menuMgr()->insert( action( id ), menu, group, idx );
00762 
00763   if ( !myMenuShown )
00764     setMenuShown( action( id ), false );
00765 
00766   return intId != -1 ? id : -1;
00767 }
00768 
00774 void CAM_Module::setMenuShown( const bool on )
00775 {
00776   myMenuShown = on;
00777 
00778   QtxActionMenuMgr* mMgr = menuMgr();
00779   if ( !mMgr )
00780     return;
00781 
00782   bool upd = mMgr->isUpdatesEnabled();
00783   mMgr->setUpdatesEnabled( false );
00784 
00785   QAction* sep = separator();
00786   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
00787   {
00788     if ( it.value() != sep )
00789       mMgr->setShown( mMgr->actionId( it.value() ), on );
00790   }
00791 
00792   mMgr->setUpdatesEnabled( upd );
00793   if ( upd )
00794     mMgr->update();
00795 }
00796 
00802 void CAM_Module::setMenuShown( QAction* a, const bool on )
00803 {
00804   if ( menuMgr() )
00805     menuMgr()->setShown( menuMgr()->actionId( a ), on );
00806 }
00807 
00813 void CAM_Module::setMenuShown( const int id, const bool on )
00814 {
00815   setMenuShown( action( id ), on );
00816 }
00817 
00823 void CAM_Module::setToolShown( const bool on )
00824 {
00825   myToolShown = on;
00826 
00827   QtxActionToolMgr* tMgr = toolMgr();
00828   if ( !tMgr )
00829     return;
00830 
00831   bool upd = tMgr->isUpdatesEnabled();
00832   tMgr->setUpdatesEnabled( false );
00833 
00834   QAction* sep = separator();
00835   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
00836   {
00837     if ( it.value() != sep )
00838       tMgr->setShown( tMgr->actionId( it.value() ), on );
00839   }
00840 
00841   tMgr->setUpdatesEnabled( upd );
00842   if ( upd )
00843     tMgr->update();
00844 }
00845 
00851 void CAM_Module::setToolShown( QAction* a, const bool on )
00852 {
00853   if ( toolMgr() )
00854     toolMgr()->setShown( toolMgr()->actionId( a ), on );
00855 }
00856 
00862 void CAM_Module::setToolShown( const int id, const bool on )
00863 {
00864   setToolShown( action( id ), on );
00865 }
00866 
00872 QAction* CAM_Module::action( const int id ) const
00873 {
00874   QAction* a = 0;
00875   if ( myActionMap.contains( id ) )
00876     a = myActionMap[id];
00877   return a;
00878 }
00879 
00885 int CAM_Module::actionId( const QAction* a ) const
00886 {
00887   int id = -1;
00888   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
00889   {
00890     if ( it.value() == a )
00891       id = it.key();
00892   }
00893   return id;
00894 }
00895 
00915 QAction* CAM_Module::createAction( const int id, const QString& text, const QIcon& icon,
00916                                    const QString& menu, const QString& tip, const int key,
00917                                    QObject* parent, const bool toggle, QObject* reciever,
00918                                const char* member, const QString& shortcutAction )
00919 {
00920   QtxAction* a = new QtxAction( text, icon, menu, key, parent, toggle, shortcutAction );
00921   a->setStatusTip( tip );
00922 
00923   if ( reciever && member )
00924     connect( a, SIGNAL( triggered( bool ) ), reciever, member );
00925 
00926   registerAction( id, a );
00927 
00928   return a;
00929 }
00930 
00941 int CAM_Module::registerAction( const int id, QAction* a )
00942 {
00943   int ident = -1;
00944   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
00945     if ( it.value() == a )
00946       ident = it.key();
00947 
00948   if ( ident != -1 )
00949     return ident;
00950 
00951   static int generatedId = -1;
00952   ident = id < 0 ? --generatedId : id;
00953 
00954   myActionMap.insert( ident, a );
00955 
00956   if ( menuMgr() )
00957     menuMgr()->registerAction( a );
00958 
00959   if ( toolMgr() )
00960     toolMgr()->registerAction( a );
00961 
00962   if ( application() && application()->desktop() )
00963     application()->desktop()->addAction( a );
00964 
00965   return ident;
00966 }
00967 
00974 bool CAM_Module::unregisterAction( const int id )
00975 {
00976   return unregisterAction( action( id ) );
00977 }
00978 
00985 bool CAM_Module::unregisterAction( QAction* a )
00986 {
00987   if ( !a )
00988     return false;
00989   if ( menuMgr() ) {
00990     int id = menuMgr()->actionId( a );
00991     if ( id != -1 && menuMgr()->containsMenu( id, -1 ) )
00992       return false;
00993   }
00994   if ( toolMgr() ) {
00995     int id = toolMgr()->actionId( a );
00996     if ( id != -1 && toolMgr()->containsAction( id ) )
00997       return false;
00998   }
00999   if ( menuMgr() )
01000     menuMgr()->unRegisterAction( menuMgr()->actionId( a ) );
01001   if ( toolMgr() )
01002     toolMgr()->unRegisterAction( toolMgr()->actionId( a ) );
01003   return true;
01004 }
01005 
01013 QAction* CAM_Module::separator()
01014 {
01015   return QtxActionMgr::separator();
01016 }
01017 
01021 void CAM_Module::updateModuleVisibilityState() {
01022 
01023 }
01024 
01025 
01030 void CAM_Module::connectToStudy( CAM_Study* camStudy )
01031 {
01032   CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
01033   if( !app )
01034     return;
01035 
01036   CAM_DataModel* prev = 0;
01037   CAM_Application::ModuleList mods = app->modules();
01038   for( QList<CAM_Module*>::const_iterator it = mods.begin(); it != mods.end(); ++it )
01039   {
01040     CAM_DataModel* dm = (*it)->dataModel();
01041     if( (*it) == this && !camStudy->containsDataModel( dm ) )
01042     {
01043       if ( prev )
01044         camStudy->insertDataModel( (*it)->dataModel(), prev );
01045       else
01046         camStudy->insertDataModel( (*it)->dataModel(), 0 );
01047     }
01048     prev = dm;
01049   }
01050 }
01051