Salome HOME
Fix for bug 10438: Crash during Explode on Blocks operation (Global selection on...
[modules/gui.git] / src / CAM / CAM_Module.cxx
1 #include "CAM_Module.h"
2
3 #include "CAM_DataModel.h"
4 #include "CAM_Application.h"
5 #include "CAM_Study.h"
6
7 #include <QtxAction.h>
8 #include <QtxActionMenuMgr.h>
9 #include <QtxActionToolMgr.h>
10
11 /*!Icon.*/
12 static const char* ModuleIcon[] = {
13 "20 20 2 1",
14 "       c None",
15 ".      c #000000",
16 "                    ",
17 "                    ",
18 "                    ",
19 " .................. ",
20 " .                . ",
21 " .                . ",
22 " .                . ",
23 " .                . ",
24 " .                . ",
25 " .                . ",
26 " .                . ",
27 " .                . ",
28 " .................. ",
29 "    .     .     .   ",
30 "    .     .     .   ",
31 "   ...   ...   ...  ",
32 "  .. .. .. .. .. .. ",
33 "  .   . .   . .   . ",
34 "  .. .. .. .. .. .. ",
35 "   ...   ...   ...  "};
36
37 QPixmap MYPixmap( ModuleIcon );
38
39 /*!Constructor.*/
40 CAM_Module::CAM_Module()
41 : QObject(),
42 myApp( 0 ),
43 myIcon( MYPixmap ),
44 myDataModel( 0 )
45 {
46 }
47
48 /*!Constructor. initialize \a name.*/
49 CAM_Module::CAM_Module( const QString& name )
50 : QObject(),
51 myApp( 0 ),
52 myName( name ),
53 myIcon( MYPixmap ),
54 myDataModel( 0 )
55 {
56 }
57
58 /*!Destructor. Remove data model.*/
59 CAM_Module::~CAM_Module()
60 {
61   delete myDataModel;
62   myDataModel = 0;
63 }
64
65 /*!Initialize application.*/
66 void CAM_Module::initialize( CAM_Application* app )
67 {
68   myApp = app;
69 }
70
71 /*!\retval Module icon.*/
72 QPixmap CAM_Module::moduleIcon() const
73 {
74   return myIcon;
75 }
76
77 /*!\retval Module name.*/
78 QString CAM_Module::moduleName() const
79 {
80   return myName;
81 }
82
83 /*! \brief Return data model.
84  * Create data model, if it was't created before.
85  */
86 CAM_DataModel* CAM_Module::dataModel() const
87 {
88   if ( !myDataModel )
89   {
90     CAM_Module* that = (CAM_Module*)this;
91     that->myDataModel = that->createDataModel();
92     that->myDataModel->initialize();
93   }
94   return myDataModel;
95 }
96
97 /*!\retval CAM_Application pointer - application.*/
98 CAM_Application* CAM_Module::application() const
99 {
100   return myApp;
101 }
102
103 /*!Public slot
104  * \retval true.
105  */
106 bool CAM_Module::activateModule( SUIT_Study* study )
107 {
108   return true;
109 }
110
111 /*!Public slot
112  * \retval true.
113  */
114 bool CAM_Module::deactivateModule( SUIT_Study* )
115 {
116   return true;
117 }
118
119 /*!Public slot, remove data model from \a study.*/
120 void CAM_Module::studyClosed( SUIT_Study* study )
121 {
122   CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
123   if ( !camDoc ) 
124     return;
125
126   if ( camDoc->containsDataModel( dataModel() ) )
127     camDoc->removeDataModel( dataModel() );
128 }
129
130 /*!Public slot, do nothing.*/
131 void CAM_Module::studyChanged( SUIT_Study* , SUIT_Study* )
132 {
133 }
134
135 /*!Create and return new instance of CAM_DataModel.*/
136 CAM_DataModel* CAM_Module::createDataModel()
137
138   return new CAM_DataModel( this );
139 }
140
141 /*!Sets module name to \a name.
142  * \param name - new name for module.
143  */
144 void CAM_Module::setModuleName( const QString& name )
145 {
146   myName = name;
147 }
148
149 /*!Sets module icon to \a icon.
150  * \param icon - new icon for module.
151  */
152 void CAM_Module::setModuleIcon( const QPixmap& icon )
153 {
154   myIcon = icon;
155 }
156
157 /*! Return menu manager pointer.
158  * \retval QtxActionMenuMgr pointer - menu manager.
159  */
160 QtxActionMenuMgr* CAM_Module::menuMgr() const
161 {
162   QtxActionMenuMgr* mgr = 0;
163   if ( application() && application()->desktop() )
164     mgr = application()->desktop()->menuMgr();
165   return mgr;
166 }
167
168 /*! Return tool manager pointer.
169  * \retval QtxActionToolMgr pointer - tool manager.
170  */
171 QtxActionToolMgr* CAM_Module::toolMgr() const
172 {
173   QtxActionToolMgr* mgr = 0;
174   if ( application() && application()->desktop() )
175     mgr = application()->desktop()->toolMgr();
176   return mgr;
177 }
178
179 /*! Create tool bar with name \a name, if it was't created before.
180  * \retval -1 - if tool manager was't be created.
181  */
182 int CAM_Module::createTool( const QString& name )
183 {
184   if ( !toolMgr() )
185     return -1;
186
187   return toolMgr()->createToolBar( name );
188 }
189
190 /*! Create tool. Register action \a a with id \a id.
191  * Insert QAction to tool manager.
192  *\param a - QAction
193  *\param tBar - integer
194  *\param id   - integer
195  *\param idx  - integer
196  *\retval integer id of new action in tool manager.
197  *\retval Return -1 if something wrong.
198  */
199 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
200 {
201   if ( !toolMgr() )
202     return -1;
203
204   int regId = registerAction( id, a );
205   int intId = toolMgr()->insert( a, tBar, idx );
206   return intId != -1 ? regId : -1;
207 }
208
209 /*! Create tool. Register action \a a with id \a id.
210  * Insert QAction to tool manager.
211  *\param a - QAction
212  *\param tBar - QString&
213  *\param id   - integer
214  *\param idx  - integer
215  *\retval integer id of new action in tool manager.
216  *\retval Return -1 if something wrong.
217  */
218 int CAM_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
219 {
220   if ( !toolMgr() )
221     return -1;
222
223   int regId = registerAction( id, a );
224   int intId = toolMgr()->insert( a, tBar, idx );
225   return intId != -1 ? regId : -1;
226 }
227
228 /*! Create tool.
229  * Insert QAction with id \a id from action map(myActionMap) to tool manager.
230  *\param a - QAction
231  *\param tBar - integer
232  *\param id   - integer
233  *\param idx  - integer
234  *\retval integer id of new action in tool manager.
235  *\retval Return -1 if something wrong.
236  */
237 int CAM_Module::createTool( const int id, const int tBar, const int idx )
238 {
239   if ( !toolMgr() )
240     return -1;
241
242   int intId = toolMgr()->insert( action( id ), tBar, idx );
243   return intId != -1 ? id : -1;
244 }
245
246 /*! Create tool.
247  * Insert QAction with id \a id from action map(myActionMap) to tool manager.
248  *\param a - QAction
249  *\param tBar - QString&
250  *\param id   - integer
251  *\param idx  - integer
252  *\retval integer id of new action in tool manager.
253  *\retval Return -1 if something wrong.
254  */
255 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
256 {
257   if ( !toolMgr() )
258     return -1;
259
260   int intId = toolMgr()->insert( action( id ), tBar, idx );
261   return intId != -1 ? id : -1;
262 }
263
264 /*! Create menu.
265  * Insert submenu \a subMenu to menu manager.
266  *\param subMenu - QString&
267  *\param menu    - integer
268  *\param id      - integer
269  *\param group   - integer
270  *\param index   - integer
271  *\retval integer id of new menu in tool manager.
272  *\retval Return -1 if something wrong.
273  */
274 int CAM_Module::createMenu( const QString& subMenu, const int menu,
275                             const int id, const int group, const int index )
276 {
277   if ( !menuMgr() )
278     return -1;
279
280   return menuMgr()->insert( subMenu, menu, group, index );
281 }
282
283 /*! Create menu.
284  * Insert submenu \a subMenu to menu manager.
285  *\param subMenu - QString&
286  *\param menu    - QString&
287  *\param id      - integer
288  *\param group   - integer
289  *\param index   - integer
290  *\retval integer id of new menu in tool manager.
291  *\retval Return -1 if something wrong.
292  */
293 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
294                             const int id, const int group, const int index )
295 {
296   if ( !menuMgr() )
297     return -1;
298
299   return menuMgr()->insert( subMenu, menu, group, index );
300 }
301
302
303 /*! Create menu. Register action \a a with id \a id.
304  * Insert QAction to menu manager.
305  *\param a       - Qaction
306  *\param menu    - integer
307  *\param id      - integer
308  *\param group   - integer
309  *\param index   - integer
310  *\retval integer id of new menu in tool manager.
311  *\retval Return -1 if something wrong.
312  */
313 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
314 {
315   if ( !a || !menuMgr() )
316     return -1;
317
318   int regId = registerAction( id, a );
319   int intId = menuMgr()->insert( a, menu, group, index );
320   return intId != -1 ? regId : -1;
321 }
322
323 /*! Create menu. Register action \a a with id \a id.
324  * Insert QAction to menu manager.
325  *\param a       - Qaction
326  *\param menu    - QString&
327  *\param id      - integer
328  *\param group   - integer
329  *\param index   - integer
330  *\retval integer id of new menu in tool manager.
331  *\retval Return -1 if something wrong.
332  */
333 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
334 {
335   if ( !a || !menuMgr() )
336     return -1;
337
338   int regId = registerAction( id, a );
339   int intId = menuMgr()->insert( a, menu, group, index );
340   return intId != -1 ? regId : -1;
341 }
342
343 /*! Create menu.
344  * Insert QAction with id \a id from action map(myActionMap) to menu manager.
345  *\param menu    - integer
346  *\param id      - integer
347  *\param group   - integer
348  *\param index   - integer
349  *\retval integer id of new menu in tool manager.
350  *\retval Return -1 if something wrong.
351  */
352 int CAM_Module::createMenu( const int id, const int menu, const int group, const int index )
353 {
354   if ( !menuMgr() )
355     return -1;
356
357   int intId = menuMgr()->insert( action( id ), menu, group, index );
358   return intId != -1 ? id : -1;
359 }
360
361 /*! Create menu.
362  * Insert QAction with id \a id from action map(myActionMap) to menu manager.
363  *\param menu    - QString&
364  *\param id      - integer
365  *\param group   - integer
366  *\param index   - integer
367  *\retval integer id of new menu in tool manager.
368  *\retval Return -1 if something wrong.
369  */
370 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int index )
371 {
372   if ( !menuMgr() )
373     return -1;
374
375   int intId = menuMgr()->insert( action( id ), menu, group, index );
376   return intId != -1 ? id : -1;
377 }
378
379 /*!Sets menus shown to \a on floag.
380  *\param on - flag.
381  */
382 void CAM_Module::setMenuShown( const bool on )
383 {
384   QtxActionMenuMgr* mMgr = menuMgr();
385   if ( !mMgr )
386     return;
387
388   bool upd = mMgr->isUpdatesEnabled();
389   mMgr->setUpdatesEnabled( false );
390
391   QAction* sep = separator();
392   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
393   {
394     if ( it.data() != sep )
395       mMgr->setShown( mMgr->actionId( it.data() ), on );
396   }
397
398   mMgr->setUpdatesEnabled( upd );
399   if ( upd )
400     mMgr->update();
401 }
402
403 /*!Sets menu shown for QAction \a a to \a on flag.
404  * \param a - QAction
405  * \param on - flag
406  */
407 void CAM_Module::setMenuShown( QAction* a, const bool on )
408 {
409   if ( menuMgr() )
410     menuMgr()->setShown( menuMgr()->actionId( a ), on );
411 }
412
413 /*!Sets menu shown for action with id=\a id to \a on flag.
414  * \param id - id of action
415  * \param on - flag
416  */
417 void CAM_Module::setMenuShown( const int id, const bool on )
418 {
419   setMenuShown( action( id ), on );
420 }
421
422 /*!Set tools shown to \a on flag.
423  *\param on - boolean flag.
424  */
425 void CAM_Module::setToolShown( const bool on )
426 {
427   QtxActionToolMgr* tMgr = toolMgr();
428   if ( !tMgr )
429     return;
430
431   bool upd = tMgr->isUpdatesEnabled();
432   tMgr->setUpdatesEnabled( false );
433
434   QAction* sep = separator();
435   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
436   {
437     if ( it.data() != sep )
438       tMgr->setShown( tMgr->actionId( it.data() ), on );
439   }
440
441   tMgr->setUpdatesEnabled( upd );
442   if ( upd )
443     tMgr->update();
444 }
445
446 /*!Set tools shown for QAction \a a to \a on flag.
447  * \param a - QAction
448  * \param on - boolean flag
449  */
450 void CAM_Module::setToolShown( QAction* a, const bool on )
451 {
452   if ( toolMgr() )
453     toolMgr()->setShown( toolMgr()->actionId( a ), on );
454 }
455
456 /*!Set tools shown for action with id=\a id to \a on flag.
457  * \param id - integer action id
458  * \param on - boolean flag
459  */
460 void CAM_Module::setToolShown( const int id, const bool on )
461 {
462   setToolShown( action( id ), on );
463 }
464
465 /*! Return action by id. 
466  * \param id - id of action.
467  * \retval QAction.
468  */
469 QAction* CAM_Module::action( const int id ) const
470 {
471   QAction* a = 0;
472   if ( myActionMap.contains( id ) )
473     a = myActionMap[id];
474   return a;
475 }
476
477 /*! Return id by action. 
478  * \param a - QAction.
479  * \retval id of action.
480  */
481 int CAM_Module::actionId( const QAction* a ) const
482 {
483   int id = -1;
484   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
485   {
486     if ( it.data() == a )
487       id = it.key();
488   }
489   return id;
490 }
491
492 /*! Create new instance of QtxAction and register action with \a id.
493  * \param id - id for new action.
494  * \param text - parameter for creation QtxAction
495  * \param icon - parameter for creation QtxAction
496  * \param menu - parameter for creation QtxAction
497  * \param tip  - tip status for QtxAction action.
498  * \param key  - parameter for creation QtxAction
499  * \param parent - parent for action
500  * \param toggle - parameter for creation QtxAction
501  * \param reciever - 
502  * \param member   - 
503  */
504 QAction* CAM_Module::createAction( const int id, const QString& text, const QIconSet& icon,
505                                    const QString& menu, const QString& tip, const int key,
506                                    QObject* parent, const bool toggle, QObject* reciever, const char* member )
507 {
508   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
509   a->setStatusTip( tip );
510
511   if ( reciever && member )
512     connect( a, SIGNAL( activated() ), reciever, member );
513
514   registerAction( id, a );
515
516   return a;
517 }
518
519 /*! Register action in action map.
520  * \param id - id for action.
521  * \param a  - action
522  * \retval new id for action.
523  */
524 int CAM_Module::registerAction( const int id, QAction* a )
525 {
526   int ident = -1;
527   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
528     if ( it.data() == a )
529       ident = it.key();
530
531   if ( ident != -1 )
532     return ident;
533
534   static int generatedId = -1;
535   ident = id < 0 ? --generatedId : id;
536
537   myActionMap.insert( ident, a );
538
539   if ( menuMgr() )
540     menuMgr()->registerAction( a );
541
542   if ( toolMgr() )
543     toolMgr()->registerAction( a );
544
545   return ident;
546 }
547
548 /*! Return qt action manager separator.*/
549 QAction* CAM_Module::separator()
550 {
551   return QtxActionMgr::separator();
552 }
553
554 /*! Connect data model of module with active study */
555 void CAM_Module::connectToStudy( CAM_Study* camStudy )
556 {
557   CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
558   if( !app )
559     return;
560
561   CAM_DataModel* prev = 0;
562   for( CAM_Application::ModuleListIterator it = app->modules(); it.current(); ++it )
563   {
564     CAM_DataModel* dm = it.current()->dataModel();
565     if( it.current() == this && !camStudy->containsDataModel( dm ) )
566     {
567       if( prev )
568         camStudy->insertDataModel( it.current()->dataModel(), prev );
569       else
570         camStudy->insertDataModel( it.current()->dataModel(), 0 );
571     }
572     prev = dm;
573   }
574 }