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