Salome HOME
Copyright update: 2016
[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 Restore message info.
332
333   Restores constant text message when previous information status message is removed.
334
335   \param txt previous message (being removed)
336   \sa putInfo()
337 */
338 void CAM_Module::onInfoChanged( QString txt )
339 {
340   if ( txt.isEmpty() && isActiveModule() && !myInfo.isEmpty() && application() )
341     application()->putInfo( myInfo );
342 }
343
344 /*!
345   \brief Called when application is closed.
346
347   Nullify application pointer if the application is being closed.
348
349   \param theApp application
350 */
351 void CAM_Module::onApplicationClosed( SUIT_Application* theApp )
352 {
353   if (myApp == theApp)
354     myApp = NULL;
355 }
356
357 /*!
358   \brief Create data model.
359   \return created data model object or 0 if it could not be created
360 */
361 CAM_DataModel* CAM_Module::createDataModel()
362 {
363   return new CAM_DataModel( this );
364 }
365
366 /*!
367   \brief Set module (internal) name
368   \param name new module name
369   \sa name(), moduleName(), setModuleName()
370  */
371 void CAM_Module::setName( const QString& name )
372 {
373   setObjectName( name );
374 }
375
376 /*!
377   \brief Set module title (user name)
378   \param name new module title
379   \sa moduleName(), name(), setName()
380  */
381 void CAM_Module::setModuleName( const QString& name )
382 {
383   myName = name;
384 }
385
386 /*!
387   \brief Get menu manager.
388   \return menu manager pointer
389 */
390 QtxActionMenuMgr* CAM_Module::menuMgr() const
391 {
392   QtxActionMenuMgr* mgr = 0;
393   if ( application() && application()->desktop() )
394     mgr = application()->desktop()->menuMgr();
395   return mgr;
396 }
397
398 /*!
399   \brief Get toolbar manager.
400   \return toolbar manager pointer
401 */
402 QtxActionToolMgr* CAM_Module::toolMgr() const
403 {
404   QtxActionToolMgr* mgr = 0;
405   if ( application() && application()->desktop() )
406     mgr = application()->desktop()->toolMgr();
407   return mgr;
408 }
409
410 /*!
411   \brief Create toolbar with speicifed \a name.
412
413   If the toolbar has been already created, its ID is just returned.
414
415   \param title toolbar title
416   \param name toolbar name (identifier)
417   \return toolbar ID or -1 if toolbar could not be created
418 */
419 int CAM_Module::createTool( const QString& title, const QString& name )
420 {
421   if ( !toolMgr() )
422     return -1;
423
424   ActionMgrLocker lock( toolMgr(), !myToolShown );
425
426   return toolMgr()->createToolBar( title, name );
427 }
428
429 /*!
430   \brief Add toolbar item.
431
432   Insert action \a to the toolbar manager and register it with specified \a id.
433   Resulting action ID may differ from the requested one. This can happen if
434   requested ID is already in use.
435
436   If action has been already added previously, its ID is just returned.
437
438   If \a id < 0, the action ID is generated automatically.
439
440   If \a idx < 0, the action is added to the end of the toolbar.
441
442   \param a action
443   \param tBar toolbar ID
444   \param id requested action ID
445   \param idx action index (desired position in the toolbar)
446   \return action ID or -1 if toolbar item could not be added
447 */
448 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
449 {
450   if ( !toolMgr() )
451     return -1;
452
453   ActionMgrLocker lock( toolMgr(), !myToolShown );
454
455   int regId = registerAction( id, a );
456   int intId = toolMgr()->insert( a, tBar, idx );
457
458   if ( !myToolShown )
459     setToolShown( a, false );
460
461   return intId != -1 ? regId : -1;
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 name
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 QString& 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 with \a id identifier to the toolbar manager.
503   It is assumed that action has been already registered.
504
505   Resulting action ID may differ from the requested one. This can happen if
506   requested ID is already in use.
507
508   If action has been already added previously, its ID is just returned.
509
510   If \a idx < 0, the action is added to the end of the toolbar.
511
512   \param id action ID
513   \param tBar toolbar ID
514   \param idx action index (desired position in the toolbar)
515   \return action ID or -1 if toolbar item could not be added
516 */
517 int CAM_Module::createTool( const int id, const int tBar, const int idx )
518 {
519   if ( !toolMgr() )
520     return -1;
521
522   ActionMgrLocker lock( toolMgr(), !myToolShown );
523
524   int intId = toolMgr()->insert( action( id ), tBar, idx );
525
526   if ( !myToolShown )
527     setToolShown( action( id ), false );
528
529   return intId != -1 ? id : -1;
530 }
531
532 /*!
533   \brief Add toolbar item.
534
535   Insert action with \a id identifier to the toolbar manager.
536   It is assumed that action has been already registered.
537
538   Resulting action ID may differ from the requested one. This can happen if
539   requested ID is already in use.
540
541   If action has been already added previously, its ID is just returned.
542
543   If \a idx < 0, the action is added to the end of the toolbar.
544
545   \param id action ID
546   \param tBar toolbar name
547   \param idx action index (desired position in the toolbar)
548   \return action ID or -1 if toolbar item could not be added
549 */
550 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
551 {
552   if ( !toolMgr() )
553     return -1;
554
555   ActionMgrLocker lock( toolMgr(), !myToolShown );
556
557   int intId = toolMgr()->insert( action( id ), tBar, idx );
558
559   if ( !myToolShown )
560     setToolShown( action( id ), false );
561
562   return intId != -1 ? id : -1;
563 }
564
565 /*!
566   \brief Create menu or submenu.
567
568   Create main menu or popup submenu and register it with specified \a id.
569   Resulting action ID may differ from the requested one. This can happen if
570   requested ID is already in use.
571
572   If \a id < 0, the menu ID is generated automatically.
573   If menu has been already created previously, its ID is just returned.
574
575   The \a menu parameter represents the menu name - it could be a sequence
576   of strings, separated by '|' symbol. For example, "File|Edit" means
577   File->Edit submenu. If menu doesn't exist, it is created automatically.
578
579   Parameter \a idx defines the index of the menu item in the menu group which
580   is defined by the \a group. If \a idx < 0, the menu/submenu is added to the
581   end of the menu group.
582
583   \param subMenu subMenu name
584   \param menu parent menu ID
585   \param id requested menu ID
586   \param group menu group ID
587   \param idx menu item index (desired position in the menu group)
588   \return menu item ID or -1 if menu item could not be added
589 */
590 int CAM_Module::createMenu( const QString& subMenu, const int menu,
591                             const int id, const int group, const int idx, QMenu * menuObj )
592 {
593   if ( !menuMgr() )
594     return -1;
595   
596   return menuMgr()->insert( subMenu, menu, group, id, idx, menuObj );
597 }
598
599 /*!
600   \brief Create menu or submenu.
601
602   Create main menu or popup submenu and register it with specified \a id.
603   Resulting action ID may differ from the requested one. This can happen if
604   requested ID is already in use.
605
606   If \a id < 0, the menu ID is generated automatically.
607   If menu has been already created previously, its ID is just returned.
608
609   The \a menu parameter represents the menu name - it could be a sequence
610   of strings, separated by '|' symbol. For example, "File|Edit" means
611   File->Edit submenu. If menu doesn't exist, it is created automatically.
612
613   Parameter \a idx defines the index of the menu item in the menu group which
614   is defined by the \a group. If \a idx < 0, the menu/submenu is added to the
615   end of the menu group.
616
617   \param subMenu subMenu name
618   \param menu parent menu name(s)
619   \param id requested menu ID
620   \param group menu group ID
621   \param idx menu item index (desired position in the menu group)
622   \return menu item ID or -1 if menu item could not be added
623 */
624 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
625                             const int id, const int group, const int idx )
626 {
627   if ( !menuMgr() )
628     return -1;
629
630   return menuMgr()->insert( subMenu, menu, group, id, idx );
631 }
632
633 /*!
634   \brief Add menu item.
635
636   Insert action \a to the menu manager and register it with specified \a id.
637   Resulting action ID may differ from the requested one. This can happen if
638   requested ID is already in use.
639
640   If \a id < 0, the action ID is generated automatically.
641
642   If action has been already added previously, its ID is just returned.
643
644   Parameter \a idx defines the index of the menu item in the menu group which
645   is defined by the \a group. If \a idx < 0, the action is added to the
646   end of the menu group.
647
648   \param a action
649   \param menu menu ID
650   \param id requested action ID
651   \param group menu group ID
652   \param idx action index (desired position in the menu group)
653   \return action ID or -1 if menu item could not be added
654 */
655 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int idx )
656 {
657   if ( !a || !menuMgr() )
658     return -1;
659   
660   ActionMgrLocker lock( menuMgr(), !myMenuShown );
661
662   int regId = registerAction( id, a );
663   int intId = menuMgr()->insert( a, menu, group, idx );
664
665   if ( !myMenuShown )
666     setMenuShown( a, false );
667
668   return intId != -1 ? regId : -1;
669 }
670
671 /*!
672   \brief Add menu item.
673
674   Insert action \a to the menu manager and register it with specified \a id.
675   Resulting action ID may differ from the requested one. This can happen if
676   requested ID is already in use.
677
678   If \a id < 0, the action ID is generated automatically.
679
680   If action has been already added previously, its ID is just returned.
681
682   The \a menu parameter represents the menu name - it could be a sequence
683   of strings, separated by '|' symbol. For example, "File|Edit" means
684   File->Edit submenu. If menu doesn't exist, it is created automatically.
685
686   Parameter \a idx defines the index of the menu item in the menu group which
687   is defined by the \a group. If \a idx < 0, the action is added to the
688   end of the menu group.
689
690   \param a action
691   \param menu menu name(s)
692   \param id requested action ID
693   \param group menu group ID
694   \param idx action index (desired position in the menu group)
695   \return action ID or -1 if menu item could not be added
696 */
697 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx )
698 {
699   if ( !a || !menuMgr() )
700     return -1;
701
702   ActionMgrLocker lock( menuMgr(), !myMenuShown );
703
704   int regId = registerAction( id, a );
705   int intId = menuMgr()->insert( a, menu, group, idx );
706
707   if ( !myMenuShown )
708     setMenuShown( a, false );
709   
710   return intId != -1 ? regId : -1;
711 }
712
713 /*!
714   \brief Add menu item.
715
716   Insert action with \a id identifier to the menu manager.
717   It is assumed that action has been already registered.
718
719   Resulting action ID may differ from the requested one. This can happen if
720   requested ID is already in use.
721
722   If action has been already added previously, its ID is just returned.
723
724   Parameter \a idx defines the index of the menu item in the menu group which
725   is defined by the \a group. If \a idx < 0, the action is added to the
726   end of the menu group.
727
728   \param id action ID
729   \param menu menu ID
730   \param group menu group ID
731   \param idx action index (desired position in the menu group)
732   \return action ID or -1 if menu item could not be added
733 */
734 int CAM_Module::createMenu( const int id, const int menu, const int group, const int idx )
735 {
736   if ( !menuMgr() )
737     return -1;
738
739   ActionMgrLocker lock( menuMgr(), !myMenuShown );
740
741   int intId = menuMgr()->insert( action( id ), menu, group, idx );
742
743   if ( !myMenuShown )
744     setMenuShown( action( id ), false );
745
746   return intId != -1 ? id : -1;
747 }
748
749 /*!
750   \brief Add menu item.
751
752   Insert action with \a id identifier to the menu manager.
753   It is assumed that action has been already registered.
754
755   Resulting action ID may differ from the requested one. This can happen if
756   requested ID is already in use.
757
758   If action has been already added previously, its ID is just returned.
759
760   The \a menu parameter represents the menu name - it could be a sequence
761   of strings, separated by '|' symbol. For example, "File|Edit" means
762   File->Edit submenu. If menu doesn't exist, it is created automatically.
763
764   Parameter \a idx defines the index of the menu item in the menu group which
765   is defined by the \a group. If \a idx < 0, the action is added to the
766   end of the menu group.
767
768   \param id action ID
769   \param menu menu name(s)
770   \param group menu group ID
771   \param idx action index (desired position in the menu group)
772   \return action ID or -1 if menu item could not be added
773 */
774 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int idx )
775 {
776   if ( !menuMgr() )
777     return -1;
778
779   ActionMgrLocker lock( menuMgr(), !myMenuShown );
780
781   int intId = menuMgr()->insert( action( id ), menu, group, idx );
782
783   if ( !myMenuShown )
784     setMenuShown( action( id ), false );
785
786   return intId != -1 ? id : -1;
787 }
788
789 /*!
790   \brief Show/hide all module's menus.
791   \param on if \c true, show menus, otherwise, hide all menus
792   \sa setToolShown()
793 */
794 void CAM_Module::setMenuShown( const bool on )
795 {
796   myMenuShown = on;
797
798   QtxActionMenuMgr* mMgr = menuMgr();
799   if ( !mMgr )
800     return;
801
802   bool upd = mMgr->isUpdatesEnabled();
803   mMgr->setUpdatesEnabled( false );
804
805   QAction* sep = separator();
806   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
807   {
808     if ( it.value() != sep )
809       mMgr->setShown( mMgr->actionId( it.value() ), on );
810   }
811
812   mMgr->setUpdatesEnabled( upd );
813   if ( upd )
814     mMgr->update();
815 }
816
817 /*!
818   \brief Show/hide specified menu item.
819   \param a action
820   \param on if \c true, show menu item, otherwise, hide it
821 */
822 void CAM_Module::setMenuShown( QAction* a, const bool on )
823 {
824   if ( menuMgr() )
825     menuMgr()->setShown( menuMgr()->actionId( a ), on );
826 }
827
828 /*!
829   \brief Show/hide specified menu item.
830   \param id menu item ID
831   \param on if \c true, show menu item, otherwise, hide it
832 */
833 void CAM_Module::setMenuShown( const int id, const bool on )
834 {
835   setMenuShown( action( id ), on );
836 }
837
838 /*!
839   \brief Show/hide all module's toolbars.
840   \param on if \c true, show toolbars, otherwise, hide all toolbars
841   \sa setMenuShown()
842 */
843 void CAM_Module::setToolShown( const bool on )
844 {
845   myToolShown = on;
846
847   QtxActionToolMgr* tMgr = toolMgr();
848   if ( !tMgr )
849     return;
850
851   bool upd = tMgr->isUpdatesEnabled();
852   tMgr->setUpdatesEnabled( false );
853
854   QAction* sep = separator();
855   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
856   {
857     if ( it.value() != sep )
858       tMgr->setShown( tMgr->actionId( it.value() ), on );
859   }
860
861   tMgr->setUpdatesEnabled( upd );
862   if ( upd )
863     tMgr->update();
864 }
865
866 /*!
867   \brief Show/hide specified toolbar item.
868   \param a action
869   \param on if \c true, show toolbar item, otherwise, hide it
870 */
871 void CAM_Module::setToolShown( QAction* a, const bool on )
872 {
873   if ( toolMgr() )
874     toolMgr()->setShown( toolMgr()->actionId( a ), on );
875 }
876
877 /*!
878   \brief Show/hide specified toolbar item.
879   \param id toolbar item ID
880   \param on if \c true, show toolbar item, otherwise, hide it
881 */
882 void CAM_Module::setToolShown( const int id, const bool on )
883 {
884   setToolShown( action( id ), on );
885 }
886
887 /*!
888   \brief Get action by specified \a id.
889   \param id action ID
890   \return action or 0 if not found
891 */
892 QAction* CAM_Module::action( const int id ) const
893 {
894   QAction* a = 0;
895   if ( myActionMap.contains( id ) )
896     a = myActionMap[id];
897   else if ( menuMgr() ) {
898     QMenu* m = menuMgr()->findMenu( id );
899     if ( m ) a = m->menuAction();
900   }
901   return a;
902 }
903
904 /*!
905   \brief Get action ID.
906   \param a action
907   \return action ID or -1 if not found
908 */
909 int CAM_Module::actionId( const QAction* a ) const
910 {
911   int id = -1;
912   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
913   {
914     if ( it.value() == a )
915       id = it.key();
916   }
917   return id;
918 }
919
920 /*!
921   \brief Create new instance of QtxAction and register action with specified \a id.
922
923   Resulting action ID may differ from the requested one. This can happen if
924   requested ID is already in use.
925
926   If \a id < 0, the action ID is generated automatically.
927
928   \param id required action ID
929   \param text tooltip text
930   \param icon action icon
931   \param menu menu text
932   \param tip status bar tip
933   \param key keyboard accelerator
934   \param parent parent object
935   \param toggle if \c true, the action will be toggled
936   \param reciever action activation signal receiver object
937   \param member action activation signal receiver slot
938 */
939 QAction* CAM_Module::createAction( const int id, const QString& text, const QIcon& icon,
940                                    const QString& menu, const QString& tip, const int key,
941                                    QObject* parent, const bool toggle, QObject* reciever,
942                                    const char* member, const QString& shortcutAction )
943 {
944   QtxAction* a = new QtxAction( text, icon, menu, key, parent, toggle, shortcutAction );
945   a->setStatusTip( tip );
946
947   if ( reciever && member )
948     connect( a, SIGNAL( triggered( bool ) ), reciever, member );
949
950   registerAction( id, a );
951
952   return a;
953 }
954
955 /*!
956   \brief Create new action group.
957   \param id action group ID
958   \param exclusive \c true for exclusive action group
959   \return created action group
960 */
961 QtxActionGroup* CAM_Module::createActionGroup( const int id, const bool exclusive )
962 {
963   QtxActionGroup* a = qobject_cast<QtxActionGroup*>( action( id ) );
964   if ( !a ) {
965     a = new QtxActionGroup( this );
966     registerAction( id, a );
967   }
968   a->setExclusive( exclusive );
969   return a;
970 }
971
972 /*!
973   \brief Register action in the internal action map.
974
975   If action has been already added previously, its ID is just returned.
976   If \a id < 0, the action ID is generated automatically.
977
978   \param id action required ID
979   \param a action
980   \return action ID
981 */
982 int CAM_Module::registerAction( const int id, QAction* a )
983 {
984   int ident = -1;
985   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
986     if ( it.value() == a )
987       ident = it.key();
988
989   if ( ident != -1 )
990     return ident;
991
992   static int generatedId = -1;
993   ident = id < 0 ? --generatedId : id;
994
995   myActionMap.insert( ident, a );
996
997   if ( menuMgr() )
998     menuMgr()->registerAction( a );
999
1000   if ( toolMgr() )
1001     toolMgr()->registerAction( a );
1002
1003   if ( application() && application()->desktop() )
1004     application()->desktop()->addAction( a );
1005
1006   return ident;
1007 }
1008
1009 /*!
1010   \brief Unregister action from the internal action map.
1011
1012   \param id action ID
1013   \return \c true on success or \c false if action is in use
1014 */
1015 bool CAM_Module::unregisterAction( const int id )
1016 {
1017   return unregisterAction( action( id ) );
1018 }
1019
1020 /*!
1021   \brief Unregister action from the internal action map.
1022
1023   \param a action
1024   \return \c true on success or \c false if action is in use
1025 */
1026 bool CAM_Module::unregisterAction( QAction* a )
1027 {
1028   if ( !a )
1029     return false;
1030   if ( menuMgr() ) {
1031     int id = menuMgr()->actionId( a );
1032     if ( id != -1 && menuMgr()->containsMenu( id, -1 ) )
1033       return false;
1034   }
1035   if ( toolMgr() ) {
1036     int id = toolMgr()->actionId( a );
1037     if ( id != -1 && toolMgr()->containsAction( id ) )
1038       return false;
1039   }
1040   if ( menuMgr() )
1041     menuMgr()->unRegisterAction( menuMgr()->actionId( a ) );
1042   if ( toolMgr() )
1043     toolMgr()->unRegisterAction( toolMgr()->actionId( a ) );
1044   return true;
1045 }
1046
1047 /*!
1048   \brief Create separator action.
1049
1050   Separator action can be used in menus or toolbars.
1051
1052   \return new separator action
1053 */
1054 QAction* CAM_Module::separator()
1055 {
1056   return QtxActionMgr::separator();
1057 }
1058
1059 /*!
1060   \brief Update visibility state of the module objects.
1061 */
1062 void CAM_Module::updateModuleVisibilityState() {
1063
1064 }
1065
1066 /*!
1067   \brief Activate GUI operation of module by its ID.
1068   This method is called from CAM_Application::startOperation().
1069   \param actionId is a numerical unique operation id.
1070 */
1071 bool CAM_Module::activateOperation( int actionId )
1072 {
1073   return false;
1074 }
1075
1076 /*!
1077   \brief Activate GUI operation of module by its ID.
1078   This method is called from CAM_Application::startOperation().
1079   \param actionId is a string unique operation id.
1080 */
1081 bool CAM_Module::activateOperation( const QString& actionId )
1082 {
1083   return false;
1084 }
1085
1086 /*!
1087   \brief Activate GUI operation of module by its ID and \a pluginName.
1088   This method is called from CAM_Application::startOperation().
1089   \param actionId is a string unique operation id.
1090   \param pluginName is a name of a plugin where the operation is implemented.
1091 */
1092 bool CAM_Module::activateOperation( const QString& actionId, const QString& pluginName )
1093 {
1094   return false;
1095 }
1096
1097
1098 /*!
1099   \brief Connect data model of the module to the active study
1100   \param camStudy CAM study object
1101 */
1102 void CAM_Module::connectToStudy( CAM_Study* camStudy )
1103 {
1104   CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
1105   if( !app )
1106     return;
1107
1108   CAM_DataModel* prev = 0;
1109   CAM_Application::ModuleList mods = app->modules();
1110   for( QList<CAM_Module*>::const_iterator it = mods.begin(); it != mods.end(); ++it )
1111   {
1112     CAM_DataModel* dm = (*it)->dataModel();
1113     if( (*it) == this && !camStudy->containsDataModel( dm ) )
1114     {
1115       if ( prev )
1116         camStudy->insertDataModel( (*it)->dataModel(), prev );
1117       else
1118         camStudy->insertDataModel( (*it)->dataModel(), 0 );
1119     }
1120     prev = dm;
1121   }
1122 }
1123
1124 /*!
1125   \fn void CAM_Module::contextMenuPopup( const QString& type, QMenu* menu, QString& title );
1126   \brief Create context popup menu.
1127   \param type popup menu context
1128   \param menu popup menu
1129   \param title popup menu title, which can be set by the module if required
1130 */
1131
1132 /*!
1133   \fn void CAM_Module::updateCommandsStatus();
1134   \brief Update menu/toolbar actions.
1135 */
1136
1137
1138 bool CAM_Module::abortAllOperations()
1139 {
1140   return true;
1141 }