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