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