Salome HOME
a5bcaf714e5925d17923871908e552d7d3f44455
[modules/gui.git] / src / CAM / CAM_Module.cxx
1 // Copyright (C) 2007-2016  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<QAction*, QKeySequence>::Iterator it = myActionShortcutMap.begin(); it != myActionShortcutMap.end(); ++it )
238   {
239     // Reset actions accelerator keys
240     it.key()->setShortcut( it.value() );
241   }
242   myActionShortcutMap.clear();
243   return true;
244 }
245
246 /*!
247   \brief Deactivate module.
248
249   This method is called when the user deactivates module.
250   Successor classes can use this method to customize module deactivation process,
251   for example, to hide own menus, toolbars, etc.
252
253   Default implementation always returns \c true.
254
255   \return \c true if module is deactivated successfully.
256   \sa initialize(), activateModule()
257  */
258 bool CAM_Module::deactivateModule( SUIT_Study* )
259 {
260   // IMN 05/03/2015: we copied myActionMap for reset/unset actions accelerator keys
261   // after activate/deactivate modules
262   myActionShortcutMap.clear();
263   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
264   {
265     myActionShortcutMap.insert( it.value(), it.value()->shortcut() );
266     it.value()->setShortcut( QKeySequence() );
267   }
268   return true;
269 }
270
271 /*!
272   \brief Called when study is closed.
273
274   Removes data model from the \a study.
275
276   \param study study being closed
277 */
278 void CAM_Module::studyClosed( SUIT_Study* study )
279 {
280   CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
281   if ( !camDoc )
282     return;
283
284   CAM_DataModel* dm = dataModel();
285   if ( dm && camDoc->containsDataModel( dm ) ) {
286     dm->close();
287     camDoc->removeDataModel( dm );
288   }
289 }
290
291 /*!
292   \brief Called when study is changed (obsolete).
293
294   Default implementation does nothing.
295
296   \param oldStudy old study
297   \param newStudy new study
298 */
299 void CAM_Module::studyChanged( SUIT_Study* /*oldStudy*/, SUIT_Study* /*newStudy*/ )
300 {
301 }
302
303 /*!
304   \brief Check if the module is active.
305   \return \c true if module is active.
306 */
307 bool CAM_Module::isActiveModule() const
308 {
309   return application() ? application()->activeModule() == this : false;
310 }
311
312 /*!
313   \brief Put the text message into the status bar of the application main window.
314
315   If \a msec > 0, the message will be shown \a msec milliseconds.
316   If \a msec < 0, the message will be constantly displayed until module is active.
317
318   \param msg text message
319   \param msec message displaying duration in milliseconds
320 */
321 void CAM_Module::putInfo( const QString& msg, const int msec )
322 {
323   if ( application() )
324     application()->putInfo( msg, msec );
325
326   if ( msec < 0 )
327     myInfo = msg;
328 }
329
330 /*!
331     \brief Shows the notifications with spectified text, title and automatic close timeout.
332     Notification will be automatically closed after specified timeout in msec. If
333     timeout is zero then automatic closing doesn't performed.
334     \param text - Notification text
335     \param title - Notification title
336     \param timeout - Notification close timeout in msec
337     \return notification identifier
338 */
339 int CAM_Module::showNotification( const QString& message, const QString& title, int timeout )
340 {
341   if ( application() )
342     application()->showNotification( message, title, timeout );
343 }
344
345 /*!
346     \brief Closes the notifications with spectified text.
347     \param text - Notification text
348 */
349 void CAM_Module::hideNotification( const QString& message )
350 {
351   if ( application() )
352     application()->hideNotification( message );
353 }
354
355 /*!
356     \brief Closes the notifications with spectified identifier.
357     \param text - Notification text
358 */
359 void CAM_Module::hideNotification( int id )
360 {
361   if ( application() )
362     application()->hideNotification( id );
363 }
364
365 /*!
366   \brief Restore message info.
367
368   Restores constant text message when previous information status message is removed.
369
370   \param txt previous message (being removed)
371   \sa putInfo()
372 */
373 void CAM_Module::onInfoChanged( QString txt )
374 {
375   if ( txt.isEmpty() && isActiveModule() && !myInfo.isEmpty() && application() )
376     application()->putInfo( myInfo );
377 }
378
379 /*!
380   \brief Called when application is closed.
381
382   Nullify application pointer if the application is being closed.
383
384   \param theApp application
385 */
386 void CAM_Module::onApplicationClosed( SUIT_Application* theApp )
387 {
388   if (myApp == theApp)
389     myApp = NULL;
390 }
391
392 /*!
393   \brief Create data model.
394   \return created data model object or 0 if it could not be created
395 */
396 CAM_DataModel* CAM_Module::createDataModel()
397 {
398   return new CAM_DataModel( this );
399 }
400
401 /*!
402   \brief Set module (internal) name
403   \param name new module name
404   \sa name(), moduleName(), setModuleName()
405  */
406 void CAM_Module::setName( const QString& name )
407 {
408   setObjectName( name );
409 }
410
411 /*!
412   \brief Set module title (user name)
413   \param name new module title
414   \sa moduleName(), name(), setName()
415  */
416 void CAM_Module::setModuleName( const QString& name )
417 {
418   myName = name;
419 }
420
421 /*!
422   \brief Get menu manager.
423   \return menu manager pointer
424 */
425 QtxActionMenuMgr* CAM_Module::menuMgr() const
426 {
427   QtxActionMenuMgr* mgr = 0;
428   if ( application() && application()->desktop() )
429     mgr = application()->desktop()->menuMgr();
430   return mgr;
431 }
432
433 /*!
434   \brief Get toolbar manager.
435   \return toolbar manager pointer
436 */
437 QtxActionToolMgr* CAM_Module::toolMgr() const
438 {
439   QtxActionToolMgr* mgr = 0;
440   if ( application() && application()->desktop() )
441     mgr = application()->desktop()->toolMgr();
442   return mgr;
443 }
444
445 /*!
446   \brief Create toolbar with speicifed \a name.
447
448   If the toolbar has been already created, its ID is just returned.
449
450   \param title toolbar title
451   \param name toolbar name (identifier)
452   \return toolbar ID or -1 if toolbar could not be created
453 */
454 int CAM_Module::createTool( const QString& title, const QString& name )
455 {
456   if ( !toolMgr() )
457     return -1;
458
459   ActionMgrLocker lock( toolMgr(), !myToolShown );
460
461   return toolMgr()->createToolBar( title, name );
462 }
463
464 /*!
465   \brief Add toolbar item.
466
467   Insert action \a to the toolbar manager and register it with specified \a id.
468   Resulting action ID may differ from the requested one. This can happen if
469   requested ID is already in use.
470
471   If action has been already added previously, its ID is just returned.
472
473   If \a id < 0, the action ID is generated automatically.
474
475   If \a idx < 0, the action is added to the end of the toolbar.
476
477   \param a action
478   \param tBar toolbar ID
479   \param id requested action ID
480   \param idx action index (desired position in the toolbar)
481   \return action ID or -1 if toolbar item could not be added
482 */
483 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
484 {
485   if ( !toolMgr() )
486     return -1;
487
488   ActionMgrLocker lock( toolMgr(), !myToolShown );
489
490   int regId = registerAction( id, a );
491   int intId = toolMgr()->insert( a, tBar, idx );
492
493   if ( !myToolShown )
494     setToolShown( a, false );
495
496   return intId != -1 ? regId : -1;
497 }
498
499 /*!
500   \brief Add toolbar item.
501
502   Insert action \a to the toolbar manager and register it with specified \a id.
503   Resulting action ID may differ from the requested one. This can happen if
504   requested ID is already in use.
505
506   If action has been already added previously, its ID is just returned.
507
508   If \a id < 0, the action ID is generated automatically.
509
510   If \a idx < 0, the action is added to the end of the toolbar.
511
512   \param a action
513   \param tBar toolbar name
514   \param id requested action 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( QAction* a, const QString& tBar, const int id, const int idx )
519 {
520   if ( !toolMgr() )
521     return -1;
522
523   ActionMgrLocker lock( toolMgr(), !myToolShown );
524
525   int regId = registerAction( id, a );
526   int intId = toolMgr()->insert( a, tBar, idx );
527
528   if ( !myToolShown )
529     setToolShown( a, false );
530
531   return intId != -1 ? regId : -1;
532 }
533
534 /*!
535   \brief Add toolbar item.
536
537   Insert action with \a id identifier to the toolbar manager.
538   It is assumed that action has been already registered.
539
540   Resulting action ID may differ from the requested one. This can happen if
541   requested ID is already in use.
542
543   If action has been already added previously, its ID is just returned.
544
545   If \a idx < 0, the action is added to the end of the toolbar.
546
547   \param id action ID
548   \param tBar toolbar ID
549   \param idx action index (desired position in the toolbar)
550   \return action ID or -1 if toolbar item could not be added
551 */
552 int CAM_Module::createTool( const int id, const int tBar, const int idx )
553 {
554   if ( !toolMgr() )
555     return -1;
556
557   ActionMgrLocker lock( toolMgr(), !myToolShown );
558
559   int intId = toolMgr()->insert( action( id ), tBar, idx );
560
561   if ( !myToolShown )
562     setToolShown( action( id ), false );
563
564   return intId != -1 ? id : -1;
565 }
566
567 /*!
568   \brief Add toolbar item.
569
570   Insert action with \a id identifier to the toolbar manager.
571   It is assumed that action has been already registered.
572
573   Resulting action ID may differ from the requested one. This can happen if
574   requested ID is already in use.
575
576   If action has been already added previously, its ID is just returned.
577
578   If \a idx < 0, the action is added to the end of the toolbar.
579
580   \param id action ID
581   \param tBar toolbar name
582   \param idx action index (desired position in the toolbar)
583   \return action ID or -1 if toolbar item could not be added
584 */
585 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
586 {
587   if ( !toolMgr() )
588     return -1;
589
590   ActionMgrLocker lock( toolMgr(), !myToolShown );
591
592   int intId = toolMgr()->insert( action( id ), tBar, idx );
593
594   if ( !myToolShown )
595     setToolShown( action( id ), false );
596
597   return intId != -1 ? id : -1;
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 ID
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 int menu,
626                             const int id, const int group, const int idx, QMenu * menuObj )
627 {
628   if ( !menuMgr() )
629     return -1;
630   
631   return menuMgr()->insert( subMenu, menu, group, id, idx, menuObj );
632 }
633
634 /*!
635   \brief Create menu or submenu.
636
637   Create main menu or popup submenu 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 menu ID is generated automatically.
642   If menu has been already created previously, its ID is just returned.
643
644   The \a menu parameter represents the menu name - it could be a sequence
645   of strings, separated by '|' symbol. For example, "File|Edit" means
646   File->Edit submenu. If menu doesn't exist, it is created automatically.
647
648   Parameter \a idx defines the index of the menu item in the menu group which
649   is defined by the \a group. If \a idx < 0, the menu/submenu is added to the
650   end of the menu group.
651
652   \param subMenu subMenu name
653   \param menu parent menu name(s)
654   \param id requested menu ID
655   \param group menu group ID
656   \param idx menu item index (desired position in the menu group)
657   \return menu item ID or -1 if menu item could not be added
658 */
659 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
660                             const int id, const int group, const int idx )
661 {
662   if ( !menuMgr() )
663     return -1;
664
665   return menuMgr()->insert( subMenu, menu, group, id, idx );
666 }
667
668 /*!
669   \brief Add menu item.
670
671   Insert action \a to the menu manager and register it with specified \a id.
672   Resulting action ID may differ from the requested one. This can happen if
673   requested ID is already in use.
674
675   If \a id < 0, the action ID is generated automatically.
676
677   If action has been already added previously, its ID is just returned.
678
679   Parameter \a idx defines the index of the menu item in the menu group which
680   is defined by the \a group. If \a idx < 0, the action is added to the
681   end of the menu group.
682
683   \param a action
684   \param menu menu ID
685   \param id requested action ID
686   \param group menu group ID
687   \param idx action index (desired position in the menu group)
688   \return action ID or -1 if menu item could not be added
689 */
690 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int idx )
691 {
692   if ( !a || !menuMgr() )
693     return -1;
694   
695   ActionMgrLocker lock( menuMgr(), !myMenuShown );
696
697   int regId = registerAction( id, a );
698   int intId = menuMgr()->insert( a, menu, group, idx );
699
700   if ( !myMenuShown )
701     setMenuShown( a, false );
702
703   return intId != -1 ? regId : -1;
704 }
705
706 /*!
707   \brief Add menu item.
708
709   Insert action \a to the menu manager and register it with specified \a id.
710   Resulting action ID may differ from the requested one. This can happen if
711   requested ID is already in use.
712
713   If \a id < 0, the action ID is generated automatically.
714
715   If action has been already added previously, its ID is just returned.
716
717   The \a menu parameter represents the menu name - it could be a sequence
718   of strings, separated by '|' symbol. For example, "File|Edit" means
719   File->Edit submenu. If menu doesn't exist, it is created automatically.
720
721   Parameter \a idx defines the index of the menu item in the menu group which
722   is defined by the \a group. If \a idx < 0, the action is added to the
723   end of the menu group.
724
725   \param a action
726   \param menu menu name(s)
727   \param id requested action ID
728   \param group menu group ID
729   \param idx action index (desired position in the menu group)
730   \return action ID or -1 if menu item could not be added
731 */
732 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx )
733 {
734   if ( !a || !menuMgr() )
735     return -1;
736
737   ActionMgrLocker lock( menuMgr(), !myMenuShown );
738
739   int regId = registerAction( id, a );
740   int intId = menuMgr()->insert( a, menu, group, idx );
741
742   if ( !myMenuShown )
743     setMenuShown( a, false );
744   
745   return intId != -1 ? regId : -1;
746 }
747
748 /*!
749   \brief Add menu item.
750
751   Insert action with \a id identifier to the menu manager.
752   It is assumed that action has been already registered.
753
754   Resulting action ID may differ from the requested one. This can happen if
755   requested ID is already in use.
756
757   If action has been already added previously, its ID is just returned.
758
759   Parameter \a idx defines the index of the menu item in the menu group which
760   is defined by the \a group. If \a idx < 0, the action is added to the
761   end of the menu group.
762
763   \param id action ID
764   \param menu menu ID
765   \param group menu group ID
766   \param idx action index (desired position in the menu group)
767   \return action ID or -1 if menu item could not be added
768 */
769 int CAM_Module::createMenu( const int id, const int menu, const int group, const int idx )
770 {
771   if ( !menuMgr() )
772     return -1;
773
774   ActionMgrLocker lock( menuMgr(), !myMenuShown );
775
776   int intId = menuMgr()->insert( action( id ), menu, group, idx );
777
778   if ( !myMenuShown )
779     setMenuShown( action( id ), false );
780
781   return intId != -1 ? id : -1;
782 }
783
784 /*!
785   \brief Add menu item.
786
787   Insert action with \a id identifier to the menu manager.
788   It is assumed that action has been already registered.
789
790   Resulting action ID may differ from the requested one. This can happen if
791   requested ID is already in use.
792
793   If action has been already added previously, its ID is just returned.
794
795   The \a menu parameter represents the menu name - it could be a sequence
796   of strings, separated by '|' symbol. For example, "File|Edit" means
797   File->Edit submenu. If menu doesn't exist, it is created automatically.
798
799   Parameter \a idx defines the index of the menu item in the menu group which
800   is defined by the \a group. If \a idx < 0, the action is added to the
801   end of the menu group.
802
803   \param id action ID
804   \param menu menu name(s)
805   \param group menu group ID
806   \param idx action index (desired position in the menu group)
807   \return action ID or -1 if menu item could not be added
808 */
809 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int idx )
810 {
811   if ( !menuMgr() )
812     return -1;
813
814   ActionMgrLocker lock( menuMgr(), !myMenuShown );
815
816   int intId = menuMgr()->insert( action( id ), menu, group, idx );
817
818   if ( !myMenuShown )
819     setMenuShown( action( id ), false );
820
821   return intId != -1 ? id : -1;
822 }
823
824 /*!
825   \brief Show/hide all module's menus.
826   \param on if \c true, show menus, otherwise, hide all menus
827   \sa setToolShown()
828 */
829 void CAM_Module::setMenuShown( const bool on )
830 {
831   myMenuShown = on;
832
833   QtxActionMenuMgr* mMgr = menuMgr();
834   if ( !mMgr )
835     return;
836
837   bool upd = mMgr->isUpdatesEnabled();
838   mMgr->setUpdatesEnabled( false );
839
840   QAction* sep = separator();
841   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
842   {
843     if ( it.value() != sep )
844       mMgr->setShown( mMgr->actionId( it.value() ), on );
845   }
846
847   mMgr->setUpdatesEnabled( upd );
848   if ( upd )
849     mMgr->update();
850 }
851
852 /*!
853   \brief Show/hide specified menu item.
854   \param a action
855   \param on if \c true, show menu item, otherwise, hide it
856 */
857 void CAM_Module::setMenuShown( QAction* a, const bool on )
858 {
859   if ( menuMgr() )
860     menuMgr()->setShown( menuMgr()->actionId( a ), on );
861 }
862
863 /*!
864   \brief Show/hide specified menu item.
865   \param id menu item ID
866   \param on if \c true, show menu item, otherwise, hide it
867 */
868 void CAM_Module::setMenuShown( const int id, const bool on )
869 {
870   setMenuShown( action( id ), on );
871 }
872
873 /*!
874   \brief Show/hide all module's toolbars.
875   \param on if \c true, show toolbars, otherwise, hide all toolbars
876   \sa setMenuShown()
877 */
878 void CAM_Module::setToolShown( const bool on )
879 {
880   myToolShown = on;
881
882   QtxActionToolMgr* tMgr = toolMgr();
883   if ( !tMgr )
884     return;
885
886   bool upd = tMgr->isUpdatesEnabled();
887   tMgr->setUpdatesEnabled( false );
888
889   QAction* sep = separator();
890   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
891   {
892     if ( it.value() != sep )
893       tMgr->setShown( tMgr->actionId( it.value() ), on );
894   }
895
896   tMgr->setUpdatesEnabled( upd );
897   if ( upd )
898     tMgr->update();
899 }
900
901 /*!
902   \brief Show/hide specified toolbar item.
903   \param a action
904   \param on if \c true, show toolbar item, otherwise, hide it
905 */
906 void CAM_Module::setToolShown( QAction* a, const bool on )
907 {
908   if ( toolMgr() )
909     toolMgr()->setShown( toolMgr()->actionId( a ), on );
910 }
911
912 /*!
913   \brief Show/hide specified toolbar item.
914   \param id toolbar item ID
915   \param on if \c true, show toolbar item, otherwise, hide it
916 */
917 void CAM_Module::setToolShown( const int id, const bool on )
918 {
919   setToolShown( action( id ), on );
920 }
921
922 /*!
923   \brief Get action by specified \a id.
924   \param id action ID
925   \return action or 0 if not found
926 */
927 QAction* CAM_Module::action( const int id ) const
928 {
929   QAction* a = 0;
930   if ( myActionMap.contains( id ) )
931     a = myActionMap[id];
932   else if ( menuMgr() ) {
933     QMenu* m = menuMgr()->findMenu( id );
934     if ( m ) a = m->menuAction();
935   }
936   return a;
937 }
938
939 /*!
940   \brief Get action ID.
941   \param a action
942   \return action ID or -1 if not found
943 */
944 int CAM_Module::actionId( const QAction* a ) const
945 {
946   int id = -1;
947   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
948   {
949     if ( it.value() == a )
950       id = it.key();
951   }
952   return id;
953 }
954
955 /*!
956   \brief Create new instance of QtxAction and register action with specified \a id.
957
958   Resulting action ID may differ from the requested one. This can happen if
959   requested ID is already in use.
960
961   If \a id < 0, the action ID is generated automatically.
962
963   \param id required action ID
964   \param text tooltip text
965   \param icon action icon
966   \param menu menu text
967   \param tip status bar tip
968   \param key keyboard accelerator
969   \param parent parent object
970   \param toggle if \c true, the action will be toggled
971   \param reciever action activation signal receiver object
972   \param member action activation signal receiver slot
973 */
974 QAction* CAM_Module::createAction( const int id, const QString& text, const QIcon& icon,
975                                    const QString& menu, const QString& tip, const int key,
976                                    QObject* parent, const bool toggle, QObject* reciever,
977                                    const char* member, const QString& shortcutAction )
978 {
979   return createAction( id, text, icon, menu, tip, QKeySequence(key), parent, toggle, reciever, member, shortcutAction );
980 }
981
982 /*!
983   \brief Create new instance of QtxAction and register action with specified \a id.
984
985   Resulting action ID may differ from the requested one. This can happen if
986   requested ID is already in use.
987
988   If \a id < 0, the action ID is generated automatically.
989
990   \param id required action ID
991   \param text tooltip text
992   \param icon action icon
993   \param menu menu text
994   \param tip status bar tip
995   \param key keyboard accelerator
996   \param parent parent object
997   \param toggle if \c true, the action will be toggled
998   \param reciever action activation signal receiver object
999   \param member action activation signal receiver slot
1000 */
1001 QAction* CAM_Module::createAction( const int id, const QString& text, const QIcon& icon,
1002                                    const QString& menu, const QString& tip, const QKeySequence& key,
1003                                    QObject* parent, const bool toggle, QObject* reciever,
1004                                    const char* member, const QString& shortcutAction )
1005 {
1006   QtxAction* a = new QtxAction( text, icon, menu, key, parent, toggle, shortcutAction );
1007   a->setStatusTip( tip );
1008
1009   if ( reciever && member )
1010     connect( a, SIGNAL( triggered( bool ) ), reciever, member );
1011
1012   registerAction( id, a );
1013
1014   return a;
1015 }
1016
1017 /*!
1018   \brief Create new action group.
1019   \param id action group ID
1020   \param exclusive \c true for exclusive action group
1021   \return created action group
1022 */
1023 QtxActionGroup* CAM_Module::createActionGroup( const int id, const bool exclusive )
1024 {
1025   QtxActionGroup* a = qobject_cast<QtxActionGroup*>( action( id ) );
1026   if ( !a ) {
1027     a = new QtxActionGroup( this );
1028     registerAction( id, a );
1029   }
1030   a->setExclusive( exclusive );
1031   return a;
1032 }
1033
1034 /*!
1035   \brief Register action in the internal action map.
1036
1037   If action has been already added previously, its ID is just returned.
1038   If \a id < 0, the action ID is generated automatically.
1039
1040   \param id action required ID
1041   \param a action
1042   \return action ID
1043 */
1044 int CAM_Module::registerAction( const int id, QAction* a )
1045 {
1046   int ident = -1;
1047   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
1048     if ( it.value() == a )
1049       ident = it.key();
1050
1051   if ( ident != -1 )
1052     return ident;
1053
1054   static int generatedId = -1;
1055   ident = id < 0 ? --generatedId : id;
1056
1057   myActionMap.insert( ident, a );
1058
1059   if ( menuMgr() )
1060     menuMgr()->registerAction( a );
1061
1062   if ( toolMgr() )
1063     toolMgr()->registerAction( a );
1064
1065   if ( application() && application()->desktop() &&
1066        a->shortcutContext() != Qt::WidgetShortcut &&
1067        a->shortcutContext() != Qt::WidgetWithChildrenShortcut )
1068     application()->desktop()->addAction( a );
1069
1070   return ident;
1071 }
1072
1073 /*!
1074   \brief Unregister action from the internal action map.
1075
1076   \param id action ID
1077   \return \c true on success or \c false if action is in use
1078 */
1079 bool CAM_Module::unregisterAction( const int id )
1080 {
1081   return unregisterAction( action( id ) );
1082 }
1083
1084 /*!
1085   \brief Unregister action from the internal action map.
1086
1087   \param a action
1088   \return \c true on success or \c false if action is in use
1089 */
1090 bool CAM_Module::unregisterAction( QAction* a )
1091 {
1092   if ( !a )
1093     return false;
1094   if ( menuMgr() ) {
1095     int id = menuMgr()->actionId( a );
1096     if ( id != -1 && menuMgr()->containsMenu( id, -1 ) )
1097       return false;
1098   }
1099   if ( toolMgr() ) {
1100     int id = toolMgr()->actionId( a );
1101     if ( id != -1 && toolMgr()->containsAction( id ) )
1102       return false;
1103   }
1104   if ( menuMgr() )
1105     menuMgr()->unRegisterAction( menuMgr()->actionId( a ) );
1106   if ( toolMgr() )
1107     toolMgr()->unRegisterAction( toolMgr()->actionId( a ) );
1108   return true;
1109 }
1110
1111 /*!
1112   \brief Create separator action.
1113
1114   Separator action can be used in menus or toolbars.
1115
1116   \return new separator action
1117 */
1118 QAction* CAM_Module::separator()
1119 {
1120   return QtxActionMgr::separator();
1121 }
1122
1123 /*!
1124   \brief Update visibility state of the module objects.
1125 */
1126 void CAM_Module::updateModuleVisibilityState() {
1127
1128 }
1129
1130 /*!
1131   \brief Activate GUI operation of module by its ID.
1132   This method is called from CAM_Application::startOperation().
1133   \param actionId is a numerical unique operation id.
1134 */
1135 bool CAM_Module::activateOperation( int actionId )
1136 {
1137   return false;
1138 }
1139
1140 /*!
1141   \brief Activate GUI operation of module by its ID.
1142   This method is called from CAM_Application::startOperation().
1143   \param actionId is a string unique operation id.
1144 */
1145 bool CAM_Module::activateOperation( const QString& actionId )
1146 {
1147   return false;
1148 }
1149
1150 /*!
1151   \brief Activate GUI operation of module by its ID and \a pluginName.
1152   This method is called from CAM_Application::startOperation().
1153   \param actionId is a string unique operation id.
1154   \param pluginName is a name of a plugin where the operation is implemented.
1155 */
1156 bool CAM_Module::activateOperation( const QString& actionId, const QString& pluginName )
1157 {
1158   return false;
1159 }
1160
1161
1162 /*!
1163   \brief Connect data model of the module to the active study
1164   \param camStudy CAM study object
1165 */
1166 void CAM_Module::connectToStudy( CAM_Study* camStudy )
1167 {
1168   CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
1169   if( !app )
1170     return;
1171
1172   CAM_DataModel* prev = 0;
1173   CAM_Application::ModuleList mods = app->modules();
1174   for( QList<CAM_Module*>::const_iterator it = mods.begin(); it != mods.end(); ++it )
1175   {
1176     CAM_DataModel* dm = (*it)->dataModel();
1177     if( (*it) == this && !camStudy->containsDataModel( dm ) )
1178     {
1179       if ( prev )
1180         camStudy->insertDataModel( (*it)->dataModel(), prev );
1181       else
1182         camStudy->insertDataModel( (*it)->dataModel(), 0 );
1183     }
1184     prev = dm;
1185   }
1186 }
1187
1188 /*!
1189   \fn void CAM_Module::contextMenuPopup( const QString& type, QMenu* menu, QString& title );
1190   \brief Create context popup menu.
1191   \param type popup menu context
1192   \param menu popup menu
1193   \param title popup menu title, which can be set by the module if required
1194 */
1195
1196 /*!
1197   \fn void CAM_Module::updateCommandsStatus();
1198   \brief Update menu/toolbar actions.
1199 */
1200
1201
1202 bool CAM_Module::abortAllOperations()
1203 {
1204   return true;
1205 }