1 // Copyright (C) 2007-2012 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
23 #include "CAM_Module.h"
25 #include "CAM_DataModel.h"
26 #include "CAM_Application.h"
27 #include "CAM_Study.h"
29 #include <QtxAction.h>
30 #include <QtxActionMenuMgr.h>
31 #include <QtxActionToolMgr.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_ResourceMgr.h>
39 \brief Base implementation of the module in the CAM application architecture.
41 Provides support of menu/toolbars management.
47 ActionMgrLocker( QtxActionMgr* m, bool use ) : myMgr( m ), myUseLock( use )
50 myUpdEnabled = myMgr->isUpdatesEnabled();
51 myMgr->setUpdatesEnabled( false );
57 myMgr->setUpdatesEnabled( myUpdEnabled );
68 \brief Default constructor.
70 Creates unnamed module.
72 CAM_Module::CAM_Module()
82 Creates module with the specified \a name.
84 \param name module name
86 CAM_Module::CAM_Module( const QString& name )
101 CAM_Module::~CAM_Module()
108 \brief Initialize module.
110 This method is usually called when the module is created (for example,
111 on the module library loading).
112 Successor classes can use this method to create menu/toolbar actions
113 and perform other module initialization.
115 \param app parent application object
116 \sa activateModule(), deactivateModule()
118 void CAM_Module::initialize( CAM_Application* app )
123 SUIT_Session* aSession = SUIT_Session::session();
124 connect( aSession, SIGNAL( applicationClosed( SUIT_Application* ) ),
125 this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
127 connect( myApp, SIGNAL( infoChanged( QString ) ), this, SLOT( onInfoChanged( QString ) ) );
132 \brief Get module icon.
133 \return module icon pixmap
136 QPixmap CAM_Module::moduleIcon() const
138 if ( myIcon.isNull() ) {
139 QString iname = iconName();
140 if ( !iname.isEmpty() ) {
141 CAM_Module* that = (CAM_Module*)this;
142 that->myIcon = application()->resourceMgr()->loadPixmap( name(), iname, false );
149 \brief Get module icon's name.
151 This function is used to get module icon's file name.
152 Default implementation returns empty string.
154 \return module icon's name.
157 QString CAM_Module::iconName() const
159 return application()->moduleIcon( name() );
163 \brief Get module (internal) name
165 \sa setName(), moduleName(), setModuleName()
167 QString CAM_Module::name() const
173 \brief Get module title (user name)
175 \sa setModuleName(), name(), setName()
177 QString CAM_Module::moduleName() const
183 \brief Get data model.
185 Creates data model, if it is not yet created.
187 \return data model pointer
188 \sa createDataModel()
190 CAM_DataModel* CAM_Module::dataModel() const
194 CAM_Module* that = (CAM_Module*)this;
195 that->myDataModel = that->createDataModel();
196 that->myDataModel->initialize();
202 \brief Get application.
203 \return application pointer
205 CAM_Application* CAM_Module::application() const
211 \brief If return false, selection will be cleared at module activation
213 bool CAM_Module::isSelectionCompatible()
219 \brief Activate module.
221 This method is called when the user activates module.
222 Successor classes can use this method to customize module activation process,
223 for example, to show own menus, toolbars, etc.
225 Default implementation always returns \c true.
227 \return \c true if module is activated successfully.
228 \sa initialize(), deactivateModule()
230 bool CAM_Module::activateModule( SUIT_Study* /*study*/ )
236 \brief Deactivate module.
238 This method is called when the user deactivates module.
239 Successor classes can use this method to customize module deactivation process,
240 for example, to hide own menus, toolbars, etc.
242 Default implementation always returns \c true.
244 \return \c true if module is deactivated successfully.
245 \sa initialize(), activateModule()
247 bool CAM_Module::deactivateModule( SUIT_Study* )
253 \brief Called when study is closed.
255 Removes data model from the \a study.
257 \param study study being closed
259 void CAM_Module::studyClosed( SUIT_Study* study )
261 CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
265 CAM_DataModel* dm = dataModel();
266 if ( dm && camDoc->containsDataModel( dm ) ) {
268 camDoc->removeDataModel( dm );
273 \brief Called when study is changed (obsolete).
275 Default implementation does nothing.
277 \param oldStudy old study
278 \param newStudy new study
280 void CAM_Module::studyChanged( SUIT_Study* /*oldStudy*/, SUIT_Study* /*newStudy*/ )
285 \brief Check if the module is active.
286 \return \c true if module is active.
288 bool CAM_Module::isActiveModule() const
290 return application() ? application()->activeModule() == this : false;
294 \brief Put the text message into the status bar of the application main window.
296 If \a msec > 0, the message will be shown \a msec milliseconds.
297 If \a msec < 0, the message will be constantly displayed until module is active.
299 \param msg text message
300 \param msec message displaying duration in milliseconds
302 void CAM_Module::putInfo( const QString& msg, const int msec )
305 application()->putInfo( msg, msec );
312 \brief Restore message info.
314 Restores constant text message when previous information status message is removed.
316 \param txt previous message (being removed)
319 void CAM_Module::onInfoChanged( QString txt )
321 if ( txt.isEmpty() && isActiveModule() && !myInfo.isEmpty() && application() )
322 application()->putInfo( myInfo );
326 \brief Called when application is closed.
328 Nullify application pointer if the application is being closed.
330 \param theApp application
332 void CAM_Module::onApplicationClosed( SUIT_Application* theApp )
339 \brief Create data model.
340 \return created data model object or 0 if it could not be created
342 CAM_DataModel* CAM_Module::createDataModel()
344 return new CAM_DataModel( this );
348 \brief Set module (internal) name
349 \param name new module name
350 \sa name(), moduleName(), setModuleName()
352 void CAM_Module::setName( const QString& name )
354 setObjectName( name );
358 \brief Set module title (user name)
359 \param name new module title
360 \sa moduleName(), name(), setName()
362 void CAM_Module::setModuleName( const QString& name )
368 \brief Get menu manager.
369 \return menu manager pointer
371 QtxActionMenuMgr* CAM_Module::menuMgr() const
373 QtxActionMenuMgr* mgr = 0;
374 if ( application() && application()->desktop() )
375 mgr = application()->desktop()->menuMgr();
380 \brief Get toolbar manager.
381 \return toolbar manager pointer
383 QtxActionToolMgr* CAM_Module::toolMgr() const
385 QtxActionToolMgr* mgr = 0;
386 if ( application() && application()->desktop() )
387 mgr = application()->desktop()->toolMgr();
392 \brief Create toolbar with speicifed \a name.
394 If the toolbar has been already created, its ID is just returned.
396 \param name toolbar name
397 \return toolbar ID or -1 if toolbar could not be created
399 int CAM_Module::createTool( const QString& name )
404 ActionMgrLocker lock( toolMgr(), !myToolShown );
406 return toolMgr()->createToolBar( name, myToolShown );
410 \brief Add toolbar item.
412 Insert action \a to the toolbar manager and register it with specified \a id.
413 Resulting action ID may differ from the requested one. This can happen if
414 requested ID is already in use.
416 If action has been already added previously, its ID is just returned.
418 If \a id < 0, the action ID is generated automatically.
420 If \a idx < 0, the action is added to the end of the toolbar.
423 \param tBar toolbar ID
424 \param id requested action ID
425 \param idx action index (desired position in the toolbar)
426 \return action ID or -1 if toolbar item could not be added
428 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
433 ActionMgrLocker lock( toolMgr(), !myToolShown );
435 int regId = registerAction( id, a );
436 int intId = toolMgr()->insert( a, tBar, idx );
439 setToolShown( a, false );
441 return intId != -1 ? regId : -1;
445 \brief Add toolbar item.
447 Insert action \a to the toolbar manager and register it with specified \a id.
448 Resulting action ID may differ from the requested one. This can happen if
449 requested ID is already in use.
451 If action has been already added previously, its ID is just returned.
453 If \a id < 0, the action ID is generated automatically.
455 If \a idx < 0, the action is added to the end of the toolbar.
458 \param tBar toolbar name
459 \param id requested action ID
460 \param idx action index (desired position in the toolbar)
461 \return action ID or -1 if toolbar item could not be added
463 int CAM_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
468 ActionMgrLocker lock( toolMgr(), !myToolShown );
470 int regId = registerAction( id, a );
471 int intId = toolMgr()->insert( a, tBar, idx );
474 setToolShown( a, false );
476 return intId != -1 ? regId : -1;
480 \brief Add toolbar item.
482 Insert action with \a id identifier to the toolbar manager.
483 It is assumed that action has been already registered.
485 Resulting action ID may differ from the requested one. This can happen if
486 requested ID is already in use.
488 If action has been already added previously, its ID is just returned.
490 If \a idx < 0, the action is added to the end of the toolbar.
493 \param tBar toolbar ID
494 \param idx action index (desired position in the toolbar)
495 \return action ID or -1 if toolbar item could not be added
497 int CAM_Module::createTool( const int id, const int tBar, const int idx )
502 ActionMgrLocker lock( toolMgr(), !myToolShown );
504 int intId = toolMgr()->insert( action( id ), tBar, idx );
507 setToolShown( action( id ), false );
509 return intId != -1 ? id : -1;
513 \brief Add toolbar item.
515 Insert action with \a id identifier to the toolbar manager.
516 It is assumed that action has been already registered.
518 Resulting action ID may differ from the requested one. This can happen if
519 requested ID is already in use.
521 If action has been already added previously, its ID is just returned.
523 If \a idx < 0, the action is added to the end of the toolbar.
526 \param tBar toolbar name
527 \param idx action index (desired position in the toolbar)
528 \return action ID or -1 if toolbar item could not be added
530 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
535 ActionMgrLocker lock( toolMgr(), !myToolShown );
537 int intId = toolMgr()->insert( action( id ), tBar, idx );
540 setToolShown( action( id ), false );
542 return intId != -1 ? id : -1;
546 \brief Create menu or submenu.
548 Create main menu or popup submenu and register it with specified \a id.
549 Resulting action ID may differ from the requested one. This can happen if
550 requested ID is already in use.
552 If \a id < 0, the menu ID is generated automatically.
553 If menu has been already created previously, its ID is just returned.
555 The \a menu parameter represents the menu name - it could be a sequence
556 of strings, separated by '|' symbol. For example, "File|Edit" means
557 File->Edit submenu. If menu doesn't exist, it is created automatically.
559 Parameter \a idx defines the index of the menu item in the menu group which
560 is defined by the \a group. If \a idx < 0, the menu/submenu is added to the
561 end of the menu group.
563 \param subMenu subMenu name
564 \param menu parent menu ID
565 \param id requested menu ID
566 \param group menu group ID
567 \param idx menu item index (desired position in the menu group)
568 \return menu item ID or -1 if menu item could not be added
570 int CAM_Module::createMenu( const QString& subMenu, const int menu,
571 const int id, const int group, const int idx )
576 return menuMgr()->insert( subMenu, menu, group, id, idx );
580 \brief Create menu or submenu.
582 Create main menu or popup submenu and register it with specified \a id.
583 Resulting action ID may differ from the requested one. This can happen if
584 requested ID is already in use.
586 If \a id < 0, the menu ID is generated automatically.
587 If menu has been already created previously, its ID is just returned.
589 The \a menu parameter represents the menu name - it could be a sequence
590 of strings, separated by '|' symbol. For example, "File|Edit" means
591 File->Edit submenu. If menu doesn't exist, it is created automatically.
593 Parameter \a idx defines the index of the menu item in the menu group which
594 is defined by the \a group. If \a idx < 0, the menu/submenu is added to the
595 end of the menu group.
597 \param subMenu subMenu name
598 \param menu parent menu name(s)
599 \param id requested menu ID
600 \param group menu group ID
601 \param idx menu item index (desired position in the menu group)
602 \return menu item ID or -1 if menu item could not be added
604 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
605 const int id, const int group, const int idx )
610 return menuMgr()->insert( subMenu, menu, group, id, idx );
614 \brief Add menu item.
616 Insert action \a to the menu manager and register it with specified \a id.
617 Resulting action ID may differ from the requested one. This can happen if
618 requested ID is already in use.
620 If \a id < 0, the action ID is generated automatically.
622 If action has been already added previously, its ID is just returned.
624 Parameter \a idx defines the index of the menu item in the menu group which
625 is defined by the \a group. If \a idx < 0, the action is added to the
626 end of the menu group.
630 \param id requested action ID
631 \param group menu group ID
632 \param idx action index (desired position in the menu group)
633 \return action ID or -1 if menu item could not be added
635 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int idx )
637 if ( !a || !menuMgr() )
640 ActionMgrLocker lock( menuMgr(), !myMenuShown );
642 int regId = registerAction( id, a );
643 int intId = menuMgr()->insert( a, menu, group, idx );
646 setMenuShown( a, false );
648 return intId != -1 ? regId : -1;
652 \brief Add menu item.
654 Insert action \a to the menu manager and register it with specified \a id.
655 Resulting action ID may differ from the requested one. This can happen if
656 requested ID is already in use.
658 If \a id < 0, the action ID is generated automatically.
660 If action has been already added previously, its ID is just returned.
662 The \a menu parameter represents the menu name - it could be a sequence
663 of strings, separated by '|' symbol. For example, "File|Edit" means
664 File->Edit submenu. If menu doesn't exist, it is created automatically.
666 Parameter \a idx defines the index of the menu item in the menu group which
667 is defined by the \a group. If \a idx < 0, the action is added to the
668 end of the menu group.
671 \param menu menu name(s)
672 \param id requested action ID
673 \param group menu group ID
674 \param idx action index (desired position in the menu group)
675 \return action ID or -1 if menu item could not be added
677 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx )
679 if ( !a || !menuMgr() )
682 ActionMgrLocker lock( menuMgr(), !myMenuShown );
684 int regId = registerAction( id, a );
685 int intId = menuMgr()->insert( a, menu, group, idx );
688 setMenuShown( a, false );
690 return intId != -1 ? regId : -1;
694 \brief Add menu item.
696 Insert action with \a id identifier to the menu manager.
697 It is assumed that action has been already registered.
699 Resulting action ID may differ from the requested one. This can happen if
700 requested ID is already in use.
702 If action has been already added previously, its ID is just returned.
704 Parameter \a idx defines the index of the menu item in the menu group which
705 is defined by the \a group. If \a idx < 0, the action is added to the
706 end of the menu group.
710 \param group menu group ID
711 \param idx action index (desired position in the menu group)
712 \return action ID or -1 if menu item could not be added
714 int CAM_Module::createMenu( const int id, const int menu, const int group, const int idx )
719 ActionMgrLocker lock( menuMgr(), !myMenuShown );
721 int intId = menuMgr()->insert( action( id ), menu, group, idx );
724 setMenuShown( action( id ), false );
726 return intId != -1 ? id : -1;
730 \brief Add menu item.
732 Insert action with \a id identifier to the menu manager.
733 It is assumed that action has been already registered.
735 Resulting action ID may differ from the requested one. This can happen if
736 requested ID is already in use.
738 If action has been already added previously, its ID is just returned.
740 The \a menu parameter represents the menu name - it could be a sequence
741 of strings, separated by '|' symbol. For example, "File|Edit" means
742 File->Edit submenu. If menu doesn't exist, it is created automatically.
744 Parameter \a idx defines the index of the menu item in the menu group which
745 is defined by the \a group. If \a idx < 0, the action is added to the
746 end of the menu group.
749 \param menu menu name(s)
750 \param group menu group ID
751 \param idx action index (desired position in the menu group)
752 \return action ID or -1 if menu item could not be added
754 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int idx )
759 ActionMgrLocker lock( menuMgr(), !myMenuShown );
761 int intId = menuMgr()->insert( action( id ), menu, group, idx );
764 setMenuShown( action( id ), false );
766 return intId != -1 ? id : -1;
770 \brief Show/hide all module's menus.
771 \param on if \c true, show menus, otherwise, hide all menus
774 void CAM_Module::setMenuShown( const bool on )
778 QtxActionMenuMgr* mMgr = menuMgr();
782 bool upd = mMgr->isUpdatesEnabled();
783 mMgr->setUpdatesEnabled( false );
785 QAction* sep = separator();
786 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
788 if ( it.value() != sep )
789 mMgr->setShown( mMgr->actionId( it.value() ), on );
792 mMgr->setUpdatesEnabled( upd );
798 \brief Show/hide specified menu item.
800 \param on if \c true, show menu item, otherwise, hide it
802 void CAM_Module::setMenuShown( QAction* a, const bool on )
805 menuMgr()->setShown( menuMgr()->actionId( a ), on );
809 \brief Show/hide specified menu item.
810 \param id menu item ID
811 \param on if \c true, show menu item, otherwise, hide it
813 void CAM_Module::setMenuShown( const int id, const bool on )
815 setMenuShown( action( id ), on );
819 \brief Show/hide all module's toolbars.
820 \param on if \c true, show toolbars, otherwise, hide all toolbars
823 void CAM_Module::setToolShown( const bool on )
827 QtxActionToolMgr* tMgr = toolMgr();
831 bool upd = tMgr->isUpdatesEnabled();
832 tMgr->setUpdatesEnabled( false );
834 QAction* sep = separator();
835 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
837 if ( it.value() != sep )
838 tMgr->setShown( tMgr->actionId( it.value() ), on );
841 tMgr->setUpdatesEnabled( upd );
847 \brief Show/hide specified toolbar item.
849 \param on if \c true, show toolbar item, otherwise, hide it
851 void CAM_Module::setToolShown( QAction* a, const bool on )
854 toolMgr()->setShown( toolMgr()->actionId( a ), on );
858 \brief Show/hide specified toolbar item.
859 \param id toolbar item ID
860 \param on if \c true, show toolbar item, otherwise, hide it
862 void CAM_Module::setToolShown( const int id, const bool on )
864 setToolShown( action( id ), on );
868 \brief Get action by specified \a id.
870 \return action or 0 if not found
872 QAction* CAM_Module::action( const int id ) const
875 if ( myActionMap.contains( id ) )
881 \brief Get action ID.
883 \return action ID or -1 if not found
885 int CAM_Module::actionId( const QAction* a ) const
888 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
890 if ( it.value() == a )
897 \brief Create new instance of QtxAction and register action with specified \a id.
899 Resulting action ID may differ from the requested one. This can happen if
900 requested ID is already in use.
902 If \a id < 0, the action ID is generated automatically.
904 \param id required action ID
905 \param text tooltip text
906 \param icon action icon
907 \param menu menu text
908 \param tip status bar tip
909 \param key keyboard accelerator
910 \param parent parent object
911 \param toggle if \c true, the action will be toggled
912 \param reciever action activation signal receiver object
913 \param member action activation signal receiver slot
915 QAction* CAM_Module::createAction( const int id, const QString& text, const QIcon& icon,
916 const QString& menu, const QString& tip, const int key,
917 QObject* parent, const bool toggle, QObject* reciever,
918 const char* member, const QString& shortcutAction )
920 QtxAction* a = new QtxAction( text, icon, menu, key, parent, toggle, shortcutAction );
921 a->setStatusTip( tip );
923 if ( reciever && member )
924 connect( a, SIGNAL( triggered( bool ) ), reciever, member );
926 registerAction( id, a );
932 \brief Register action in the internal action map.
934 If action has been already added previously, its ID is just returned.
935 If \a id < 0, the action ID is generated automatically.
937 \param id action required ID
941 int CAM_Module::registerAction( const int id, QAction* a )
944 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
945 if ( it.value() == a )
951 static int generatedId = -1;
952 ident = id < 0 ? --generatedId : id;
954 myActionMap.insert( ident, a );
957 menuMgr()->registerAction( a );
960 toolMgr()->registerAction( a );
962 if ( application() && application()->desktop() )
963 application()->desktop()->addAction( a );
969 \brief Unregister action from the internal action map.
972 \return \c true on success or \c false if action is in use
974 bool CAM_Module::unregisterAction( const int id )
976 return unregisterAction( action( id ) );
980 \brief Unregister action from the internal action map.
983 \return \c true on success or \c false if action is in use
985 bool CAM_Module::unregisterAction( QAction* a )
990 int id = menuMgr()->actionId( a );
991 if ( id != -1 && menuMgr()->containsMenu( id, -1 ) )
995 int id = toolMgr()->actionId( a );
996 if ( id != -1 && toolMgr()->containsAction( id ) )
1000 menuMgr()->unRegisterAction( menuMgr()->actionId( a ) );
1002 toolMgr()->unRegisterAction( toolMgr()->actionId( a ) );
1007 \brief Create separator action.
1009 Separator action can be used in menus or toolbars.
1011 \return new separator action
1013 QAction* CAM_Module::separator()
1015 return QtxActionMgr::separator();
1019 \brief Update visibility state of the module objects.
1021 void CAM_Module::updateModuleVisibilityState() {
1027 \brief Connect data model of the module to the active study
1028 \param camStudy CAM study object
1030 void CAM_Module::connectToStudy( CAM_Study* camStudy )
1032 CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
1036 CAM_DataModel* prev = 0;
1037 CAM_Application::ModuleList mods = app->modules();
1038 for( QList<CAM_Module*>::const_iterator it = mods.begin(); it != mods.end(); ++it )
1040 CAM_DataModel* dm = (*it)->dataModel();
1041 if( (*it) == this && !camStudy->containsDataModel( dm ) )
1044 camStudy->insertDataModel( (*it)->dataModel(), prev );
1046 camStudy->insertDataModel( (*it)->dataModel(), 0 );
1053 \fn void CAM_Module::contextMenuPopup( const QString& type, QMenu* menu, QString& title );
1054 \brief Create context popup menu.
1055 \param type popup menu context
1056 \param menu popup menu
1057 \param title popup menu title, which can be set by the module if required
1061 \fn void CAM_Module::updateCommandsStatus();
1062 \brief Update menu/toolbar actions.