1 // Copyright (C) 2007-2008 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 #include "CAM_Module.h"
24 #include "CAM_DataModel.h"
25 #include "CAM_Application.h"
26 #include "CAM_Study.h"
28 #include <QtxAction.h>
29 #include <QtxActionMenuMgr.h>
30 #include <QtxActionToolMgr.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
38 \brief Base implementation of the module in the CAM application architecture.
40 Provides support of menu/toolbars management.
44 \brief Default constructor.
46 Creates unnamed module.
48 CAM_Module::CAM_Module()
58 Creates module with the specified \a name.
60 \param name module name
62 CAM_Module::CAM_Module( const QString& name )
75 CAM_Module::~CAM_Module()
82 \brief Initialize module.
84 This method is usually called when the module is created (for example,
85 on the module library loading).
86 Successor classes can use this method to create menu/toolbar actions
87 and perform other module initialization.
89 \param app parent application object
90 \sa activateModule(), deactivateModule()
92 void CAM_Module::initialize( CAM_Application* app )
97 SUIT_Session* aSession = SUIT_Session::session();
98 connect( aSession, SIGNAL( applicationClosed( SUIT_Application* ) ),
99 this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
101 connect( myApp, SIGNAL( infoChanged( QString ) ), this, SLOT( onInfoChanged( QString ) ) );
106 \brief Get module icon.
107 \return module icon pixmap
110 QPixmap CAM_Module::moduleIcon() const
112 if ( myIcon.isNull() ) {
113 QString iname = iconName();
114 if ( !iname.isEmpty() ) {
115 CAM_Module* that = (CAM_Module*)this;
116 that->myIcon = application()->resourceMgr()->loadPixmap( name(), iname, false );
123 \brief Get module icon's name.
125 This function is used to get module icon's file name.
126 Default implementation returns empty string.
128 \return module icon's name.
131 QString CAM_Module::iconName() const
133 return application()->moduleIcon( name() );
137 \brief Get module (internal) name
139 \sa setName(), moduleName(), setModuleName()
141 QString CAM_Module::name() const
147 \brief Get module title (user name)
149 \sa setModuleName(), name(), setName()
151 QString CAM_Module::moduleName() const
157 \brief Get data model.
159 Creates data model, if it is not yet created.
161 \return data model pointer
162 \sa createDataModel()
164 CAM_DataModel* CAM_Module::dataModel() const
168 CAM_Module* that = (CAM_Module*)this;
169 that->myDataModel = that->createDataModel();
170 that->myDataModel->initialize();
176 \brief Get application.
177 \return application pointer
179 CAM_Application* CAM_Module::application() const
185 \brief If return false, selection will be cleared at module activation
187 bool CAM_Module::isSelectionCompatible()
193 \brief Activate module.
195 This method is called when the user activates module.
196 Successor classes can use this method to customize module activation process,
197 for example, to show own menus, toolbars, etc.
199 Default implementation always returns \c true.
201 \return \c true if module is activated successfully.
202 \sa initialize(), deactivateModule()
204 bool CAM_Module::activateModule( SUIT_Study* /*study*/ )
210 \brief Deactivate module.
212 This method is called when the user deactivates module.
213 Successor classes can use this method to customize module deactivation process,
214 for example, to hide own menus, toolbars, etc.
216 Default implementation always returns \c true.
218 \return \c true if module is deactivated successfully.
219 \sa initialize(), activateModule()
221 bool CAM_Module::deactivateModule( SUIT_Study* )
227 \brief Called when study is closed.
229 Removes data model from the \a study.
231 \param study study being closed
233 void CAM_Module::studyClosed( SUIT_Study* study )
235 CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
239 CAM_DataModel* dm = dataModel();
240 if ( dm && camDoc->containsDataModel( dm ) ) {
242 camDoc->removeDataModel( dm );
247 \brief Called when study is changed (obsolete).
249 Default implementation does nothing.
251 \param oldStudy old study
252 \param newStudy new study
254 void CAM_Module::studyChanged( SUIT_Study* /*oldStudy*/, SUIT_Study* /*newStudy*/ )
259 \brief Check if the module is active.
260 \return \c true if module is active.
262 bool CAM_Module::isActiveModule() const
264 return application() ? application()->activeModule() == this : false;
268 \brief Put the text message into the status bar of the application main window.
270 If \a msec > 0, the message will be shown \a msec milliseconds.
271 If \a msec < 0, the message will be constantly displayed until module is active.
273 \param msg text message
274 \param msec message displaying duration in milliseconds
276 void CAM_Module::putInfo( const QString& msg, const int msec )
279 application()->putInfo( msg, msec );
286 \brief Restore message info.
288 Restores constant text message when previous information status message is removed.
290 \param txt previous message (being removed)
293 void CAM_Module::onInfoChanged( QString txt )
295 if ( txt.isEmpty() && isActiveModule() && !myInfo.isEmpty() && application() )
296 application()->putInfo( myInfo );
300 \brief Called when application is closed.
302 Nullify application pointer if the application is being closed.
304 \param theApp application
306 void CAM_Module::onApplicationClosed( SUIT_Application* theApp )
313 \brief Create data model.
314 \return created data model object or 0 if it could not be created
316 CAM_DataModel* CAM_Module::createDataModel()
318 return new CAM_DataModel( this );
322 \brief Set module (internal) name
323 \param name new module name
324 \sa name(), moduleName(), setModuleName()
326 void CAM_Module::setName( const QString& name )
328 setObjectName( name );
332 \brief Set module title (user name)
333 \param name new module title
334 \sa moduleName(), name(), setName()
336 void CAM_Module::setModuleName( const QString& name )
342 \brief Get menu manager.
343 \return menu manager pointer
345 QtxActionMenuMgr* CAM_Module::menuMgr() const
347 QtxActionMenuMgr* mgr = 0;
348 if ( application() && application()->desktop() )
349 mgr = application()->desktop()->menuMgr();
354 \brief Get toolbar manager.
355 \return toolbar manager pointer
357 QtxActionToolMgr* CAM_Module::toolMgr() const
359 QtxActionToolMgr* mgr = 0;
360 if ( application() && application()->desktop() )
361 mgr = application()->desktop()->toolMgr();
366 \brief Create toolbar with speicifed \a name.
368 If the toolbar has been already created, its ID is just returned.
370 \param name toolbar name
371 \return toolbar ID or -1 if toolbar could not be created
373 int CAM_Module::createTool( const QString& name )
378 return toolMgr()->createToolBar( name );
382 \brief Add toolbar item.
384 Insert action \a to the toolbar manager and register it with specified \a id.
385 Resulting action ID may differ from the requested one. This can happen if
386 requested ID is already in use.
388 If action has been already added previously, its ID is just returned.
390 If \a id < 0, the action ID is generated automatically.
392 If \a idx < 0, the action is added to the end of the toolbar.
395 \param tBar toolbar ID
396 \param id requested action ID
397 \param idx action index (desired position in the toolbar)
398 \return action ID or -1 if toolbar item could not be added
400 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
405 int regId = registerAction( id, a );
406 int intId = toolMgr()->insert( a, tBar, idx );
407 return intId != -1 ? regId : -1;
411 \brief Add toolbar item.
413 Insert action \a to the toolbar manager and register it with specified \a id.
414 Resulting action ID may differ from the requested one. This can happen if
415 requested ID is already in use.
417 If action has been already added previously, its ID is just returned.
419 If \a id < 0, the action ID is generated automatically.
421 If \a idx < 0, the action is added to the end of the toolbar.
424 \param tBar toolbar name
425 \param id requested action ID
426 \param idx action index (desired position in the toolbar)
427 \return action ID or -1 if toolbar item could not be added
429 int CAM_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
434 int regId = registerAction( id, a );
435 int intId = toolMgr()->insert( a, tBar, idx );
436 return intId != -1 ? regId : -1;
440 \brief Add toolbar item.
442 Insert action with \a id identifier to the toolbar manager.
443 It is assumed that action has been already registered.
445 Resulting action ID may differ from the requested one. This can happen if
446 requested ID is already in use.
448 If action has been already added previously, its ID is just returned.
450 If \a idx < 0, the action is added to the end of the toolbar.
453 \param tBar toolbar ID
454 \param idx action index (desired position in the toolbar)
455 \return action ID or -1 if toolbar item could not be added
457 int CAM_Module::createTool( const int id, const int tBar, const int idx )
462 int intId = toolMgr()->insert( action( id ), tBar, idx );
463 return intId != -1 ? id : -1;
467 \brief Add toolbar item.
469 Insert action with \a id identifier to the toolbar manager.
470 It is assumed that action has been already registered.
472 Resulting action ID may differ from the requested one. This can happen if
473 requested ID is already in use.
475 If action has been already added previously, its ID is just returned.
477 If \a idx < 0, the action is added to the end of the toolbar.
480 \param tBar toolbar name
481 \param idx action index (desired position in the toolbar)
482 \return action ID or -1 if toolbar item could not be added
484 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
489 int intId = toolMgr()->insert( action( id ), tBar, idx );
490 return intId != -1 ? id : -1;
494 \brief Create menu or submenu.
496 Create main menu or popup submenu and register it with specified \a id.
497 Resulting action ID may differ from the requested one. This can happen if
498 requested ID is already in use.
500 If \a id < 0, the menu ID is generated automatically.
501 If menu has been already created previously, its ID is just returned.
503 The \a menu parameter represents the menu name - it could be a sequence
504 of strings, separated by '|' symbol. For example, "File|Edit" means
505 File->Edit submenu. If menu doesn't exist, it is created automatically.
507 Parameter \a idx defines the index of the menu item in the menu group which
508 is defined by the \a group. If \a idx < 0, the menu/submenu is added to the
509 end of the menu group.
511 \param subMenu subMenu name
512 \param menu parent menu ID
513 \param id requested menu ID
514 \param group menu group ID
515 \param idx menu item index (desired position in the menu group)
516 \return menu item ID or -1 if menu item could not be added
518 int CAM_Module::createMenu( const QString& subMenu, const int menu,
519 const int id, const int group, const int idx )
524 return menuMgr()->insert( subMenu, menu, group, id, idx );
528 \brief Create menu or submenu.
530 Create main menu or popup submenu and register it with specified \a id.
531 Resulting action ID may differ from the requested one. This can happen if
532 requested ID is already in use.
534 If \a id < 0, the menu ID is generated automatically.
535 If menu has been already created previously, its ID is just returned.
537 The \a menu parameter represents the menu name - it could be a sequence
538 of strings, separated by '|' symbol. For example, "File|Edit" means
539 File->Edit submenu. If menu doesn't exist, it is created automatically.
541 Parameter \a idx defines the index of the menu item in the menu group which
542 is defined by the \a group. If \a idx < 0, the menu/submenu is added to the
543 end of the menu group.
545 \param subMenu subMenu name
546 \param menu parent menu name(s)
547 \param id requested menu ID
548 \param group menu group ID
549 \param idx menu item index (desired position in the menu group)
550 \return menu item ID or -1 if menu item could not be added
552 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
553 const int id, const int group, const int idx )
558 return menuMgr()->insert( subMenu, menu, group, id, idx );
562 \brief Add menu item.
564 Insert action \a to the menu manager and register it with specified \a id.
565 Resulting action ID may differ from the requested one. This can happen if
566 requested ID is already in use.
568 If \a id < 0, the action ID is generated automatically.
570 If action has been already added previously, its ID is just returned.
572 Parameter \a idx defines the index of the menu item in the menu group which
573 is defined by the \a group. If \a idx < 0, the action is added to the
574 end of the menu group.
578 \param id requested action ID
579 \param group menu group ID
580 \param idx action index (desired position in the menu group)
581 \return action ID or -1 if menu item could not be added
583 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int idx )
585 if ( !a || !menuMgr() )
588 int regId = registerAction( id, a );
589 int intId = menuMgr()->insert( a, menu, group, idx );
590 return intId != -1 ? regId : -1;
594 \brief Add menu item.
596 Insert action \a to the menu manager and register it with specified \a id.
597 Resulting action ID may differ from the requested one. This can happen if
598 requested ID is already in use.
600 If \a id < 0, the action ID is generated automatically.
602 If action has been already added previously, its ID is just returned.
604 The \a menu parameter represents the menu name - it could be a sequence
605 of strings, separated by '|' symbol. For example, "File|Edit" means
606 File->Edit submenu. If menu doesn't exist, it is created automatically.
608 Parameter \a idx defines the index of the menu item in the menu group which
609 is defined by the \a group. If \a idx < 0, the action is added to the
610 end of the menu group.
613 \param menu menu name(s)
614 \param id requested action ID
615 \param group menu group ID
616 \param idx action index (desired position in the menu group)
617 \return action ID or -1 if menu item could not be added
619 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx )
621 if ( !a || !menuMgr() )
624 int regId = registerAction( id, a );
625 int intId = menuMgr()->insert( a, menu, group, idx );
626 return intId != -1 ? regId : -1;
630 \brief Add menu item.
632 Insert action with \a id identifier to the menu manager.
633 It is assumed that action has been already registered.
635 Resulting action ID may differ from the requested one. This can happen if
636 requested ID is already in use.
638 If action has been already added previously, its ID is just returned.
640 Parameter \a idx defines the index of the menu item in the menu group which
641 is defined by the \a group. If \a idx < 0, the action is added to the
642 end of the menu group.
646 \param group menu group ID
647 \param idx action index (desired position in the menu group)
648 \return action ID or -1 if menu item could not be added
650 int CAM_Module::createMenu( const int id, const int menu, const int group, const int idx )
655 int intId = menuMgr()->insert( action( id ), menu, group, idx );
656 return intId != -1 ? id : -1;
660 \brief Add menu item.
662 Insert action with \a id identifier to the menu manager.
663 It is assumed that action has been already registered.
665 Resulting action ID may differ from the requested one. This can happen if
666 requested ID is already in use.
668 If action has been already added previously, its ID is just returned.
670 The \a menu parameter represents the menu name - it could be a sequence
671 of strings, separated by '|' symbol. For example, "File|Edit" means
672 File->Edit submenu. If menu doesn't exist, it is created automatically.
674 Parameter \a idx defines the index of the menu item in the menu group which
675 is defined by the \a group. If \a idx < 0, the action is added to the
676 end of the menu group.
679 \param menu menu name(s)
680 \param group menu group ID
681 \param idx action index (desired position in the menu group)
682 \return action ID or -1 if menu item could not be added
684 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int idx )
689 int intId = menuMgr()->insert( action( id ), menu, group, idx );
690 return intId != -1 ? id : -1;
694 \brief Show/hide all module's menus.
695 \param on if \c true, show menus, otherwise, hide all menus
698 void CAM_Module::setMenuShown( const bool on )
700 QtxActionMenuMgr* mMgr = menuMgr();
704 bool upd = mMgr->isUpdatesEnabled();
705 mMgr->setUpdatesEnabled( false );
707 QAction* sep = separator();
708 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
710 if ( it.value() != sep )
711 mMgr->setShown( mMgr->actionId( it.value() ), on );
714 mMgr->setUpdatesEnabled( upd );
720 \brief Show/hide specified menu item.
722 \param on if \c true, show menu item, otherwise, hide it
724 void CAM_Module::setMenuShown( QAction* a, const bool on )
727 menuMgr()->setShown( menuMgr()->actionId( a ), on );
731 \brief Show/hide specified menu item.
732 \param id menu item ID
733 \param on if \c true, show menu item, otherwise, hide it
735 void CAM_Module::setMenuShown( const int id, const bool on )
737 setMenuShown( action( id ), on );
741 \brief Show/hide all module's toolbars.
742 \param on if \c true, show toolbars, otherwise, hide all toolbars
745 void CAM_Module::setToolShown( const bool on )
747 QtxActionToolMgr* tMgr = toolMgr();
751 bool upd = tMgr->isUpdatesEnabled();
752 tMgr->setUpdatesEnabled( false );
754 QAction* sep = separator();
755 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
757 if ( it.value() != sep )
758 tMgr->setShown( tMgr->actionId( it.value() ), on );
761 tMgr->setUpdatesEnabled( upd );
767 \brief Show/hide specified toolbar item.
769 \param on if \c true, show toolbar item, otherwise, hide it
771 void CAM_Module::setToolShown( QAction* a, const bool on )
774 toolMgr()->setShown( toolMgr()->actionId( a ), on );
778 \brief Show/hide specified toolbar item.
779 \param id toolbar item ID
780 \param on if \c true, show toolbar item, otherwise, hide it
782 void CAM_Module::setToolShown( const int id, const bool on )
784 setToolShown( action( id ), on );
788 \brief Get action by specified \a id.
790 \return action or 0 if not found
792 QAction* CAM_Module::action( const int id ) const
795 if ( myActionMap.contains( id ) )
801 \brief Get action ID.
803 \return action ID or -1 if not found
805 int CAM_Module::actionId( const QAction* a ) const
808 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
810 if ( it.value() == a )
817 \brief Create new instance of QtxAction and register action with specified \a id.
819 Resulting action ID may differ from the requested one. This can happen if
820 requested ID is already in use.
822 If \a id < 0, the action ID is generated automatically.
824 \param id required action ID
825 \param text tooltip text
826 \param icon action icon
827 \param menu menu text
828 \param tip status bar tip
829 \param key keyboard accelerator
830 \param parent parent object
831 \param toggle if \c true, the action will be toggled
832 \param reciever action activation signal receiver object
833 \param member action activation signal receiver slot
835 QAction* CAM_Module::createAction( const int id, const QString& text, const QIcon& icon,
836 const QString& menu, const QString& tip, const int key,
837 QObject* parent, const bool toggle, QObject* reciever, const char* member )
839 QtxAction* a = new QtxAction( text, icon, menu, key, parent, toggle );
840 a->setStatusTip( tip );
842 if ( reciever && member )
843 connect( a, SIGNAL( triggered( bool ) ), reciever, member );
845 registerAction( id, a );
851 \brief Register action in the internal action map.
853 If action has been already added previously, its ID is just returned.
854 If \a id < 0, the action ID is generated automatically.
856 \param id action required ID
860 int CAM_Module::registerAction( const int id, QAction* a )
863 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
864 if ( it.value() == a )
870 static int generatedId = -1;
871 ident = id < 0 ? --generatedId : id;
873 myActionMap.insert( ident, a );
876 menuMgr()->registerAction( a );
879 toolMgr()->registerAction( a );
881 if ( application() && application()->desktop() )
882 application()->desktop()->addAction( a );
888 \brief Unregister action from the internal action map.
891 \return \c true on success or \c false if action is in use
893 bool CAM_Module::unregisterAction( const int id )
895 return unregisterAction( action( id ) );
899 \brief Unregister action from the internal action map.
902 \return \c true on success or \c false if action is in use
904 bool CAM_Module::unregisterAction( QAction* a )
909 int id = menuMgr()->actionId( a );
910 if ( id != -1 && menuMgr()->containsMenu( id, -1 ) )
914 int id = toolMgr()->actionId( a );
915 if ( id != -1 && toolMgr()->containsAction( id ) )
919 menuMgr()->unRegisterAction( menuMgr()->actionId( a ) );
921 toolMgr()->unRegisterAction( toolMgr()->actionId( a ) );
926 \brief Create separator action.
928 Separator action can be used in menus or toolbars.
930 \return new separator action
932 QAction* CAM_Module::separator()
934 return QtxActionMgr::separator();
938 \brief Connect data model of the module to the active study
939 \param camStudy CAM study object
941 void CAM_Module::connectToStudy( CAM_Study* camStudy )
943 CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
947 CAM_DataModel* prev = 0;
948 CAM_Application::ModuleList mods = app->modules();
949 for( QList<CAM_Module*>::const_iterator it = mods.begin(); it != mods.end(); ++it )
951 CAM_DataModel* dm = (*it)->dataModel();
952 if( (*it) == this && !camStudy->containsDataModel( dm ) )
955 camStudy->insertDataModel( (*it)->dataModel(), prev );
957 camStudy->insertDataModel( (*it)->dataModel(), 0 );
964 \fn void CAM_Module::contextMenuPopup( const QString& type, QMenu* menu, QString& title );
965 \brief Create context popup menu.
966 \param type popup menu context
967 \param menu popup menu
968 \param title popup menu title, which can be set by the module if required
972 \fn void CAM_Module::updateCommandsStatus();
973 \brief Update menu/toolbar actions.