Salome HOME
0023097: EDF GEOM: [HYDRO 513] Fit only on selected objects
[modules/gui.git] / src / CAM / CAM_Module.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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, or (at your option) any later version.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "CAM_Module.h"
24
25 #include "CAM_DataModel.h"
26 #include "CAM_Application.h"
27 #include "CAM_Study.h"
28
29 #include <QtxAction.h>
30 #include <QtxActionGroup.h>
31 #include <QtxActionMenuMgr.h>
32 #include <QtxActionToolMgr.h>
33
34 #include <SUIT_Desktop.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_ResourceMgr.h>
37
38 #include <QMenu>
39
40 /*!
41   \class CAM_Module
42   \brief Base implementation of the module in the CAM application architecture.
43
44   Provides support of menu/toolbars management.
45 */
46
47 class ActionMgrLocker
48 {
49 public:
50   ActionMgrLocker( QtxActionMgr* m, bool use ) : myMgr( m ), myUseLock( use )
51   {
52     if ( myUseLock ) {
53       myUpdEnabled = myMgr->isUpdatesEnabled();
54       myMgr->setUpdatesEnabled( false );
55     }
56   }
57   ~ActionMgrLocker()
58   {
59     if ( myUseLock ) {
60       myMgr->setUpdatesEnabled( myUpdEnabled );
61       //myMgr->update();
62     }
63   }
64
65   QtxActionMgr* myMgr;
66   bool          myUseLock;
67   bool          myUpdEnabled;
68 };
69
70 /*!
71   \brief Default constructor.
72
73   Creates unnamed module.
74 */
75 CAM_Module::CAM_Module()
76 : QObject(),
77   myApp( 0 ),
78   myDataModel( 0 )
79 {
80 }
81
82 /*!
83   \brief Constructor.
84
85   Creates module with the specified \a name.
86
87   \param name module name
88 */
89 CAM_Module::CAM_Module( const QString& name )
90 : QObject(),
91   myApp( 0 ),
92   myName( name ),
93   myDataModel( 0 ),
94   myMenuShown( false ),
95   myToolShown( false )
96 {
97 }
98
99 /*!
100   \brief Destructor.
101
102   Destroy data model.
103 */
104 CAM_Module::~CAM_Module()
105 {
106   delete myDataModel;
107   myDataModel = 0;
108 }
109
110 /*!
111   \brief Initialize module.
112
113   This method is usually called when the module is created (for example,
114   on the module library loading).
115   Successor classes can use this method to create menu/toolbar actions
116   and perform other module initialization.
117
118   \param app parent application object
119   \sa activateModule(), deactivateModule()
120 */
121 void CAM_Module::initialize( CAM_Application* app )
122 {
123   myApp = app;
124   if ( myApp )
125   {
126     SUIT_Session* aSession = SUIT_Session::session();
127     connect( aSession, SIGNAL( applicationClosed( SUIT_Application* ) ),
128              this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
129
130     connect( myApp, SIGNAL( infoChanged( QString ) ), this, SLOT( onInfoChanged( QString ) ) );
131   }
132 }
133
134 /*!
135   \brief Get module icon.
136   \return module icon pixmap
137   \sa iconName()
138 */
139 QPixmap CAM_Module::moduleIcon() const
140 {
141   if ( myIcon.isNull() ) {
142     QString iname = iconName();
143     if ( !iname.isEmpty() ) {
144       CAM_Module* that = (CAM_Module*)this;
145       that->myIcon = application()->resourceMgr()->loadPixmap( name(), iname, false );
146     }
147   }
148   return myIcon;
149 }
150
151 /*!
152   \brief Get module icon's name.
153
154   This function is used to get module icon's file name.
155   Default implementation returns empty string.
156
157   \return module icon's name.
158   \sa moduleIcon()
159 */
160 QString CAM_Module::iconName() const
161 {
162   return application()->moduleIcon( name() );
163 }
164
165 /*!
166   \brief Get module (internal) name
167   \return module name
168   \sa setName(), moduleName(), setModuleName()
169 */
170 QString CAM_Module::name() const
171 {
172   return objectName();
173 }
174
175 /*!
176   \brief Get module title (user name)
177   \return module title
178   \sa setModuleName(), name(), setName()
179 */
180 QString CAM_Module::moduleName() const
181 {
182   return myName;
183 }
184
185 /*!
186   \brief Get data model.
187
188   Creates data model, if it is not yet created.
189
190   \return data model pointer
191   \sa createDataModel()
192 */
193 CAM_DataModel* CAM_Module::dataModel() const
194 {
195   if ( !myDataModel )
196   {
197     CAM_Module* that = (CAM_Module*)this;
198     that->myDataModel = that->createDataModel();
199     that->myDataModel->initialize();
200   }
201   return myDataModel;
202 }
203
204 /*!
205   \brief Get application.
206   \return application pointer
207 */
208 CAM_Application* CAM_Module::application() const
209 {
210   return myApp;
211 }
212
213 /*!
214   \brief If return false, selection will be cleared at module activation
215 */
216 bool CAM_Module::isSelectionCompatible()
217 {
218   return false;
219 }
220
221 /*!
222   \brief Activate module.
223
224   This method is called when the user activates module.
225   Successor classes can use this method to customize module activation process,
226   for example, to show own menus, toolbars, etc.
227
228   Default implementation always returns \c true.
229
230   \return \c true if module is activated successfully.
231   \sa initialize(), deactivateModule()
232  */
233 bool CAM_Module::activateModule( SUIT_Study* /*study*/ )
234 {
235   // IMN 05/03/2015: we copied myActionMap for reset/unset actions accelerator keys
236   // after activate/deactivate modules
237   for ( QMap<QPair<int, QAction*>, QKeySequence>::Iterator it = myActionShortcutMap.begin(); it != myActionShortcutMap.end(); ++it )
238   {
239     myActionMap.insert( it.key().first, it.key().second );
240     // Reset actions accelerator keys
241     it.key().second->setShortcut( it.value() );
242   }
243   myActionShortcutMap.clear();
244   return true;
245 }
246
247 /*!
248   \brief Deactivate module.
249
250   This method is called when the user deactivates module.
251   Successor classes can use this method to customize module deactivation process,
252   for example, to hide own menus, toolbars, etc.
253
254   Default implementation always returns \c true.
255
256   \return \c true if module is deactivated successfully.
257   \sa initialize(), activateModule()
258  */
259 bool CAM_Module::deactivateModule( SUIT_Study* )
260 {
261   // IMN 05/03/2015: we copied myActionMap for reset/unset actions accelerator keys
262   // after activate/deactivate modules
263   myActionShortcutMap.clear();
264   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
265   {
266     myActionShortcutMap.insert( qMakePair( it.key(), it.value() ), it.value()->shortcut() );
267   }
268   myActionMap.clear();
269   return true;
270 }
271
272 /*!
273   \brief Called when study is closed.
274
275   Removes data model from the \a study.
276
277   \param study study being closed
278 */
279 void CAM_Module::studyClosed( SUIT_Study* study )
280 {
281   CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
282   if ( !camDoc )
283     return;
284
285   CAM_DataModel* dm = dataModel();
286   if ( dm && camDoc->containsDataModel( dm ) ) {
287     dm->close();
288     camDoc->removeDataModel( dm );
289   }
290 }
291
292 /*!
293   \brief Called when study is changed (obsolete).
294
295   Default implementation does nothing.
296
297   \param oldStudy old study
298   \param newStudy new study
299 */
300 void CAM_Module::studyChanged( SUIT_Study* /*oldStudy*/, SUIT_Study* /*newStudy*/ )
301 {
302 }
303
304 /*!
305   \brief Check if the module is active.
306   \return \c true if module is active.
307 */
308 bool CAM_Module::isActiveModule() const
309 {
310   return application() ? application()->activeModule() == this : false;
311 }
312
313 /*!
314   \brief Put the text message into the status bar of the application main window.
315
316   If \a msec > 0, the message will be shown \a msec milliseconds.
317   If \a msec < 0, the message will be constantly displayed until module is active.
318
319   \param msg text message
320   \param msec message displaying duration in milliseconds
321 */
322 void CAM_Module::putInfo( const QString& msg, const int msec )
323 {
324   if ( application() )
325     application()->putInfo( msg, msec );
326
327   if ( msec < 0 )
328     myInfo = msg;
329 }
330
331 /*!
332   \brief Restore message info.
333
334   Restores constant text message when previous information status message is removed.
335
336   \param txt previous message (being removed)
337   \sa putInfo()
338 */
339 void CAM_Module::onInfoChanged( QString txt )
340 {
341   if ( txt.isEmpty() && isActiveModule() && !myInfo.isEmpty() && application() )
342     application()->putInfo( myInfo );
343 }
344
345 /*!
346   \brief Called when application is closed.
347
348   Nullify application pointer if the application is being closed.
349
350   \param theApp application
351 */
352 void CAM_Module::onApplicationClosed( SUIT_Application* theApp )
353 {
354   if (myApp == theApp)
355     myApp = NULL;
356 }
357
358 /*!
359   \brief Create data model.
360   \return created data model object or 0 if it could not be created
361 */
362 CAM_DataModel* CAM_Module::createDataModel()
363 {
364   return new CAM_DataModel( this );
365 }
366
367 /*!
368   \brief Set module (internal) name
369   \param name new module name
370   \sa name(), moduleName(), setModuleName()
371  */
372 void CAM_Module::setName( const QString& name )
373 {
374   setObjectName( name );
375 }
376
377 /*!
378   \brief Set module title (user name)
379   \param name new module title
380   \sa moduleName(), name(), setName()
381  */
382 void CAM_Module::setModuleName( const QString& name )
383 {
384   myName = name;
385 }
386
387 /*!
388   \brief Get menu manager.
389   \return menu manager pointer
390 */
391 QtxActionMenuMgr* CAM_Module::menuMgr() const
392 {
393   QtxActionMenuMgr* mgr = 0;
394   if ( application() && application()->desktop() )
395     mgr = application()->desktop()->menuMgr();
396   return mgr;
397 }
398
399 /*!
400   \brief Get toolbar manager.
401   \return toolbar manager pointer
402 */
403 QtxActionToolMgr* CAM_Module::toolMgr() const
404 {
405   QtxActionToolMgr* mgr = 0;
406   if ( application() && application()->desktop() )
407     mgr = application()->desktop()->toolMgr();
408   return mgr;
409 }
410
411 /*!
412   \brief Create toolbar with speicifed \a name.
413
414   If the toolbar has been already created, its ID is just returned.
415
416   \param title toolbar title
417   \param name toolbar name (identifier)
418   \return toolbar ID or -1 if toolbar could not be created
419 */
420 int CAM_Module::createTool( const QString& title, const QString& name )
421 {
422   if ( !toolMgr() )
423     return -1;
424
425   ActionMgrLocker lock( toolMgr(), !myToolShown );
426
427   return toolMgr()->createToolBar( title, name );
428 }
429
430 /*!
431   \brief Add toolbar item.
432
433   Insert action \a to the toolbar manager and register it with specified \a id.
434   Resulting action ID may differ from the requested one. This can happen if
435   requested ID is already in use.
436
437   If action has been already added previously, its ID is just returned.
438
439   If \a id < 0, the action ID is generated automatically.
440
441   If \a idx < 0, the action is added to the end of the toolbar.
442
443   \param a action
444   \param tBar toolbar ID
445   \param id requested action ID
446   \param idx action index (desired position in the toolbar)
447   \return action ID or -1 if toolbar item could not be added
448 */
449 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
450 {
451   if ( !toolMgr() )
452     return -1;
453
454   ActionMgrLocker lock( toolMgr(), !myToolShown );
455
456   int regId = registerAction( id, a );
457   int intId = toolMgr()->insert( a, tBar, idx );
458
459   if ( !myToolShown )
460     setToolShown( a, false );
461
462   return intId != -1 ? regId : -1;
463 }
464
465 /*!
466   \brief Add toolbar item.
467
468   Insert action \a to the toolbar manager and register it with specified \a id.
469   Resulting action ID may differ from the requested one. This can happen if
470   requested ID is already in use.
471
472   If action has been already added previously, its ID is just returned.
473
474   If \a id < 0, the action ID is generated automatically.
475
476   If \a idx < 0, the action is added to the end of the toolbar.
477
478   \param a action
479   \param tBar toolbar name
480   \param id requested action ID
481   \param idx action index (desired position in the toolbar)
482   \return action ID or -1 if toolbar item could not be added
483 */
484 int CAM_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
485 {
486   if ( !toolMgr() )
487     return -1;
488
489   ActionMgrLocker lock( toolMgr(), !myToolShown );
490
491   int regId = registerAction( id, a );
492   int intId = toolMgr()->insert( a, tBar, idx );
493
494   if ( !myToolShown )
495     setToolShown( a, false );
496
497   return intId != -1 ? regId : -1;
498 }
499
500 /*!
501   \brief Add toolbar item.
502
503   Insert action with \a id identifier to the toolbar manager.
504   It is assumed that action has been already registered.
505
506   Resulting action ID may differ from the requested one. This can happen if
507   requested ID is already in use.
508
509   If action has been already added previously, its ID is just returned.
510
511   If \a idx < 0, the action is added to the end of the toolbar.
512
513   \param id action ID
514   \param tBar toolbar ID
515   \param idx action index (desired position in the toolbar)
516   \return action ID or -1 if toolbar item could not be added
517 */
518 int CAM_Module::createTool( const int id, const int tBar, const int idx )
519 {
520   if ( !toolMgr() )
521     return -1;
522
523   ActionMgrLocker lock( toolMgr(), !myToolShown );
524
525   int intId = toolMgr()->insert( action( id ), tBar, idx );
526
527   if ( !myToolShown )
528     setToolShown( action( id ), false );
529
530   return intId != -1 ? id : -1;
531 }
532
533 /*!
534   \brief Add toolbar item.
535
536   Insert action with \a id identifier to the toolbar manager.
537   It is assumed that action has been already registered.
538
539   Resulting action ID may differ from the requested one. This can happen if
540   requested ID is already in use.
541
542   If action has been already added previously, its ID is just returned.
543
544   If \a idx < 0, the action is added to the end of the toolbar.
545
546   \param id action ID
547   \param tBar toolbar name
548   \param idx action index (desired position in the toolbar)
549   \return action ID or -1 if toolbar item could not be added
550 */
551 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
552 {
553   if ( !toolMgr() )
554     return -1;
555
556   ActionMgrLocker lock( toolMgr(), !myToolShown );
557
558   int intId = toolMgr()->insert( action( id ), tBar, idx );
559
560   if ( !myToolShown )
561     setToolShown( action( id ), false );
562
563   return intId != -1 ? id : -1;
564 }
565
566 /*!
567   \brief Create menu or submenu.
568
569   Create main menu or popup submenu and register it with specified \a id.
570   Resulting action ID may differ from the requested one. This can happen if
571   requested ID is already in use.
572
573   If \a id < 0, the menu ID is generated automatically.
574   If menu has been already created previously, its ID is just returned.
575
576   The \a menu parameter represents the menu name - it could be a sequence
577   of strings, separated by '|' symbol. For example, "File|Edit" means
578   File->Edit submenu. If menu doesn't exist, it is created automatically.
579
580   Parameter \a idx defines the index of the menu item in the menu group which
581   is defined by the \a group. If \a idx < 0, the menu/submenu is added to the
582   end of the menu group.
583
584   \param subMenu subMenu name
585   \param menu parent menu ID
586   \param id requested menu ID
587   \param group menu group ID
588   \param idx menu item index (desired position in the menu group)
589   \return menu item ID or -1 if menu item could not be added
590 */
591 int CAM_Module::createMenu( const QString& subMenu, const int menu,
592                             const int id, const int group, const int idx, QMenu * menuObj )
593 {
594   if ( !menuMgr() )
595     return -1;
596   
597   return menuMgr()->insert( subMenu, menu, group, id, idx, menuObj );
598 }
599
600 /*!
601   \brief Create menu or submenu.
602
603   Create main menu or popup submenu and register it with specified \a id.
604   Resulting action ID may differ from the requested one. This can happen if
605   requested ID is already in use.
606
607   If \a id < 0, the menu ID is generated automatically.
608   If menu has been already created previously, its ID is just returned.
609
610   The \a menu parameter represents the menu name - it could be a sequence
611   of strings, separated by '|' symbol. For example, "File|Edit" means
612   File->Edit submenu. If menu doesn't exist, it is created automatically.
613
614   Parameter \a idx defines the index of the menu item in the menu group which
615   is defined by the \a group. If \a idx < 0, the menu/submenu is added to the
616   end of the menu group.
617
618   \param subMenu subMenu name
619   \param menu parent menu name(s)
620   \param id requested menu ID
621   \param group menu group ID
622   \param idx menu item index (desired position in the menu group)
623   \return menu item ID or -1 if menu item could not be added
624 */
625 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
626                             const int id, const int group, const int idx )
627 {
628   if ( !menuMgr() )
629     return -1;
630
631   return menuMgr()->insert( subMenu, menu, group, id, idx );
632 }
633
634 /*!
635   \brief Add menu item.
636
637   Insert action \a to the menu manager and register it with specified \a id.
638   Resulting action ID may differ from the requested one. This can happen if
639   requested ID is already in use.
640
641   If \a id < 0, the action ID is generated automatically.
642
643   If action has been already added previously, its ID is just returned.
644
645   Parameter \a idx defines the index of the menu item in the menu group which
646   is defined by the \a group. If \a idx < 0, the action is added to the
647   end of the menu group.
648
649   \param a action
650   \param menu menu ID
651   \param id requested action ID
652   \param group menu group ID
653   \param idx action index (desired position in the menu group)
654   \return action ID or -1 if menu item could not be added
655 */
656 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int idx )
657 {
658   if ( !a || !menuMgr() )
659     return -1;
660   
661   ActionMgrLocker lock( menuMgr(), !myMenuShown );
662
663   int regId = registerAction( id, a );
664   int intId = menuMgr()->insert( a, menu, group, idx );
665
666   if ( !myMenuShown )
667     setMenuShown( a, false );
668
669   return intId != -1 ? regId : -1;
670 }
671
672 /*!
673   \brief Add menu item.
674
675   Insert action \a to the menu manager and register it with specified \a id.
676   Resulting action ID may differ from the requested one. This can happen if
677   requested ID is already in use.
678
679   If \a id < 0, the action ID is generated automatically.
680
681   If action has been already added previously, its ID is just returned.
682
683   The \a menu parameter represents the menu name - it could be a sequence
684   of strings, separated by '|' symbol. For example, "File|Edit" means
685   File->Edit submenu. If menu doesn't exist, it is created automatically.
686
687   Parameter \a idx defines the index of the menu item in the menu group which
688   is defined by the \a group. If \a idx < 0, the action is added to the
689   end of the menu group.
690
691   \param a action
692   \param menu menu name(s)
693   \param id requested action ID
694   \param group menu group ID
695   \param idx action index (desired position in the menu group)
696   \return action ID or -1 if menu item could not be added
697 */
698 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx )
699 {
700   if ( !a || !menuMgr() )
701     return -1;
702
703   ActionMgrLocker lock( menuMgr(), !myMenuShown );
704
705   int regId = registerAction( id, a );
706   int intId = menuMgr()->insert( a, menu, group, idx );
707
708   if ( !myMenuShown )
709     setMenuShown( a, false );
710   
711   return intId != -1 ? regId : -1;
712 }
713
714 /*!
715   \brief Add menu item.
716
717   Insert action with \a id identifier to the menu manager.
718   It is assumed that action has been already registered.
719
720   Resulting action ID may differ from the requested one. This can happen if
721   requested ID is already in use.
722
723   If action has been already added previously, its ID is just returned.
724
725   Parameter \a idx defines the index of the menu item in the menu group which
726   is defined by the \a group. If \a idx < 0, the action is added to the
727   end of the menu group.
728
729   \param id action ID
730   \param menu menu ID
731   \param group menu group ID
732   \param idx action index (desired position in the menu group)
733   \return action ID or -1 if menu item could not be added
734 */
735 int CAM_Module::createMenu( const int id, const int menu, const int group, const int idx )
736 {
737   if ( !menuMgr() )
738     return -1;
739
740   ActionMgrLocker lock( menuMgr(), !myMenuShown );
741
742   int intId = menuMgr()->insert( action( id ), menu, group, idx );
743
744   if ( !myMenuShown )
745     setMenuShown( action( id ), false );
746
747   return intId != -1 ? id : -1;
748 }
749
750 /*!
751   \brief Add menu item.
752
753   Insert action with \a id identifier to the menu manager.
754   It is assumed that action has been already registered.
755
756   Resulting action ID may differ from the requested one. This can happen if
757   requested ID is already in use.
758
759   If action has been already added previously, its ID is just returned.
760
761   The \a menu parameter represents the menu name - it could be a sequence
762   of strings, separated by '|' symbol. For example, "File|Edit" means
763   File->Edit submenu. If menu doesn't exist, it is created automatically.
764
765   Parameter \a idx defines the index of the menu item in the menu group which
766   is defined by the \a group. If \a idx < 0, the action is added to the
767   end of the menu group.
768
769   \param id action ID
770   \param menu menu name(s)
771   \param group menu group ID
772   \param idx action index (desired position in the menu group)
773   \return action ID or -1 if menu item could not be added
774 */
775 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int idx )
776 {
777   if ( !menuMgr() )
778     return -1;
779
780   ActionMgrLocker lock( menuMgr(), !myMenuShown );
781
782   int intId = menuMgr()->insert( action( id ), menu, group, idx );
783
784   if ( !myMenuShown )
785     setMenuShown( action( id ), false );
786
787   return intId != -1 ? id : -1;
788 }
789
790 /*!
791   \brief Show/hide all module's menus.
792   \param on if \c true, show menus, otherwise, hide all menus
793   \sa setToolShown()
794 */
795 void CAM_Module::setMenuShown( const bool on )
796 {
797   myMenuShown = on;
798
799   QtxActionMenuMgr* mMgr = menuMgr();
800   if ( !mMgr )
801     return;
802
803   bool upd = mMgr->isUpdatesEnabled();
804   mMgr->setUpdatesEnabled( false );
805
806   QAction* sep = separator();
807   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
808   {
809     if ( it.value() != sep )
810       mMgr->setShown( mMgr->actionId( it.value() ), on );
811   }
812
813   mMgr->setUpdatesEnabled( upd );
814   if ( upd )
815     mMgr->update();
816 }
817
818 /*!
819   \brief Show/hide specified menu item.
820   \param a action
821   \param on if \c true, show menu item, otherwise, hide it
822 */
823 void CAM_Module::setMenuShown( QAction* a, const bool on )
824 {
825   if ( menuMgr() )
826     menuMgr()->setShown( menuMgr()->actionId( a ), on );
827 }
828
829 /*!
830   \brief Show/hide specified menu item.
831   \param id menu item ID
832   \param on if \c true, show menu item, otherwise, hide it
833 */
834 void CAM_Module::setMenuShown( const int id, const bool on )
835 {
836   setMenuShown( action( id ), on );
837 }
838
839 /*!
840   \brief Show/hide all module's toolbars.
841   \param on if \c true, show toolbars, otherwise, hide all toolbars
842   \sa setMenuShown()
843 */
844 void CAM_Module::setToolShown( const bool on )
845 {
846   myToolShown = on;
847
848   QtxActionToolMgr* tMgr = toolMgr();
849   if ( !tMgr )
850     return;
851
852   bool upd = tMgr->isUpdatesEnabled();
853   tMgr->setUpdatesEnabled( false );
854
855   QAction* sep = separator();
856   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
857   {
858     if ( it.value() != sep )
859       tMgr->setShown( tMgr->actionId( it.value() ), on );
860   }
861
862   tMgr->setUpdatesEnabled( upd );
863   if ( upd )
864     tMgr->update();
865 }
866
867 /*!
868   \brief Show/hide specified toolbar item.
869   \param a action
870   \param on if \c true, show toolbar item, otherwise, hide it
871 */
872 void CAM_Module::setToolShown( QAction* a, const bool on )
873 {
874   if ( toolMgr() )
875     toolMgr()->setShown( toolMgr()->actionId( a ), on );
876 }
877
878 /*!
879   \brief Show/hide specified toolbar item.
880   \param id toolbar item ID
881   \param on if \c true, show toolbar item, otherwise, hide it
882 */
883 void CAM_Module::setToolShown( const int id, const bool on )
884 {
885   setToolShown( action( id ), on );
886 }
887
888 /*!
889   \brief Get action by specified \a id.
890   \param id action ID
891   \return action or 0 if not found
892 */
893 QAction* CAM_Module::action( const int id ) const
894 {
895   QAction* a = 0;
896   if ( myActionMap.contains( id ) )
897     a = myActionMap[id];
898   else if ( menuMgr() ) {
899     QMenu* m = menuMgr()->findMenu( id );
900     if ( m ) a = m->menuAction();
901   }
902   return a;
903 }
904
905 /*!
906   \brief Get action ID.
907   \param a action
908   \return action ID or -1 if not found
909 */
910 int CAM_Module::actionId( const QAction* a ) const
911 {
912   int id = -1;
913   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
914   {
915     if ( it.value() == a )
916       id = it.key();
917   }
918   return id;
919 }
920
921 /*!
922   \brief Create new instance of QtxAction and register action with specified \a id.
923
924   Resulting action ID may differ from the requested one. This can happen if
925   requested ID is already in use.
926
927   If \a id < 0, the action ID is generated automatically.
928
929   \param id required action ID
930   \param text tooltip text
931   \param icon action icon
932   \param menu menu text
933   \param tip status bar tip
934   \param key keyboard accelerator
935   \param parent parent object
936   \param toggle if \c true, the action will be toggled
937   \param reciever action activation signal receiver object
938   \param member action activation signal receiver slot
939 */
940 QAction* CAM_Module::createAction( const int id, const QString& text, const QIcon& icon,
941                                    const QString& menu, const QString& tip, const int key,
942                                    QObject* parent, const bool toggle, QObject* reciever,
943                                    const char* member, const QString& shortcutAction )
944 {
945   QtxAction* a = new QtxAction( text, icon, menu, key, parent, toggle, shortcutAction );
946   a->setStatusTip( tip );
947
948   if ( reciever && member )
949     connect( a, SIGNAL( triggered( bool ) ), reciever, member );
950
951   registerAction( id, a );
952
953   return a;
954 }
955
956 /*!
957   \brief Create new action group.
958   \param id action group ID
959   \param exclusive \c true for exclusive action group
960   \return created action group
961 */
962 QtxActionGroup* CAM_Module::createActionGroup( const int id, const bool exclusive )
963 {
964   QtxActionGroup* a = qobject_cast<QtxActionGroup*>( action( id ) );
965   if ( !a ) {
966     a = new QtxActionGroup( this );
967     registerAction( id, a );
968   }
969   a->setExclusive( exclusive );
970   return a;
971 }
972
973 /*!
974   \brief Register action in the internal action map.
975
976   If action has been already added previously, its ID is just returned.
977   If \a id < 0, the action ID is generated automatically.
978
979   \param id action required ID
980   \param a action
981   \return action ID
982 */
983 int CAM_Module::registerAction( const int id, QAction* a )
984 {
985   int ident = -1;
986   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
987     if ( it.value() == a )
988       ident = it.key();
989
990   if ( ident != -1 )
991     return ident;
992
993   static int generatedId = -1;
994   ident = id < 0 ? --generatedId : id;
995
996   myActionMap.insert( ident, a );
997
998   if ( menuMgr() )
999     menuMgr()->registerAction( a );
1000
1001   if ( toolMgr() )
1002     toolMgr()->registerAction( a );
1003
1004   if ( application() && application()->desktop() )
1005     application()->desktop()->addAction( a );
1006
1007   return ident;
1008 }
1009
1010 /*!
1011   \brief Unregister action from the internal action map.
1012
1013   \param id action ID
1014   \return \c true on success or \c false if action is in use
1015 */
1016 bool CAM_Module::unregisterAction( const int id )
1017 {
1018   return unregisterAction( action( id ) );
1019 }
1020
1021 /*!
1022   \brief Unregister action from the internal action map.
1023
1024   \param a action
1025   \return \c true on success or \c false if action is in use
1026 */
1027 bool CAM_Module::unregisterAction( QAction* a )
1028 {
1029   if ( !a )
1030     return false;
1031   if ( menuMgr() ) {
1032     int id = menuMgr()->actionId( a );
1033     if ( id != -1 && menuMgr()->containsMenu( id, -1 ) )
1034       return false;
1035   }
1036   if ( toolMgr() ) {
1037     int id = toolMgr()->actionId( a );
1038     if ( id != -1 && toolMgr()->containsAction( id ) )
1039       return false;
1040   }
1041   if ( menuMgr() )
1042     menuMgr()->unRegisterAction( menuMgr()->actionId( a ) );
1043   if ( toolMgr() )
1044     toolMgr()->unRegisterAction( toolMgr()->actionId( a ) );
1045   return true;
1046 }
1047
1048 /*!
1049   \brief Create separator action.
1050
1051   Separator action can be used in menus or toolbars.
1052
1053   \return new separator action
1054 */
1055 QAction* CAM_Module::separator()
1056 {
1057   return QtxActionMgr::separator();
1058 }
1059
1060 /*!
1061   \brief Update visibility state of the module objects.
1062 */
1063 void CAM_Module::updateModuleVisibilityState() {
1064
1065 }
1066
1067 /*!
1068   \brief Activate GUI operation of module by its ID.
1069   This method is called from CAM_Application::startOperation().
1070   \param actionId is a numerical unique operation id.
1071 */
1072 bool CAM_Module::activateOperation( int actionId )
1073 {
1074   return false;
1075 }
1076
1077 /*!
1078   \brief Activate GUI operation of module by its ID.
1079   This method is called from CAM_Application::startOperation().
1080   \param actionId is a string unique operation id.
1081 */
1082 bool CAM_Module::activateOperation( const QString& actionId )
1083 {
1084   return false;
1085 }
1086
1087 /*!
1088   \brief Activate GUI operation of module by its ID and \a pluginName.
1089   This method is called from CAM_Application::startOperation().
1090   \param actionId is a string unique operation id.
1091   \param pluginName is a name of a plugin where the operation is implemented.
1092 */
1093 bool CAM_Module::activateOperation( const QString& actionId, const QString& pluginName )
1094 {
1095   return false;
1096 }
1097
1098
1099 /*!
1100   \brief Connect data model of the module to the active study
1101   \param camStudy CAM study object
1102 */
1103 void CAM_Module::connectToStudy( CAM_Study* camStudy )
1104 {
1105   CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
1106   if( !app )
1107     return;
1108
1109   CAM_DataModel* prev = 0;
1110   CAM_Application::ModuleList mods = app->modules();
1111   for( QList<CAM_Module*>::const_iterator it = mods.begin(); it != mods.end(); ++it )
1112   {
1113     CAM_DataModel* dm = (*it)->dataModel();
1114     if( (*it) == this && !camStudy->containsDataModel( dm ) )
1115     {
1116       if ( prev )
1117         camStudy->insertDataModel( (*it)->dataModel(), prev );
1118       else
1119         camStudy->insertDataModel( (*it)->dataModel(), 0 );
1120     }
1121     prev = dm;
1122   }
1123 }
1124
1125 /*!
1126   \fn void CAM_Module::contextMenuPopup( const QString& type, QMenu* menu, QString& title );
1127   \brief Create context popup menu.
1128   \param type popup menu context
1129   \param menu popup menu
1130   \param title popup menu title, which can be set by the module if required
1131 */
1132
1133 /*!
1134   \fn void CAM_Module::updateCommandsStatus();
1135   \brief Update menu/toolbar actions.
1136 */