Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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 a - QAction
258  *\param tBar - integer
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 int 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 tool.
274  * Insert QAction with id \a id from action map(myActionMap) to tool manager.
275  *\param a - QAction
276  *\param tBar - QString&
277  *\param id   - integer
278  *\param idx  - integer
279  *\retval integer id of new action in tool manager.
280  *\retval Return -1 if something wrong.
281  */
282 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
283 {
284   if ( !toolMgr() )
285     return -1;
286
287   int intId = toolMgr()->insert( action( id ), tBar, idx );
288   return intId != -1 ? id : -1;
289 }
290
291 /*! Create menu.
292  * Insert submenu \a subMenu to menu manager.
293  *\param subMenu - QString&
294  *\param menu    - integer
295  *\param id      - integer
296  *\param group   - integer
297  *\param index   - integer
298  *\retval integer id of new menu in tool manager.
299  *\retval Return -1 if something wrong.
300  */
301 int CAM_Module::createMenu( const QString& subMenu, const int menu,
302                             const int id, const int group, const int index,
303                             const bool enableEmpty )
304 {
305   if ( !menuMgr() )
306     return -1;
307
308   return menuMgr()->insert( subMenu, menu, group, id, index, enableEmpty );
309 }
310
311 /*! Create menu.
312  * Insert submenu \a subMenu to menu manager.
313  *\param subMenu - QString&
314  *\param menu    - QString&
315  *\param id      - integer
316  *\param group   - integer
317  *\param index   - integer
318  *\retval integer id of new menu in tool manager.
319  *\retval Return -1 if something wrong.
320  */
321 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
322                             const int id, const int group, const int index,
323                             const bool enableEmpty )
324 {
325   if ( !menuMgr() )
326     return -1;
327
328   return menuMgr()->insert( subMenu, menu, group, id, index, enableEmpty );
329 }
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    - integer
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 int 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. Register action \a a with id \a id.
353  * Insert QAction to menu manager.
354  *\param a       - Qaction
355  *\param menu    - QString&
356  *\param id      - integer
357  *\param group   - integer
358  *\param index   - integer
359  *\retval integer id of new menu in tool manager.
360  *\retval Return -1 if something wrong.
361  */
362 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
363 {
364   if ( !a || !menuMgr() )
365     return -1;
366
367   int regId = registerAction( id, a );
368   int intId = menuMgr()->insert( a, menu, group, index );
369   return intId != -1 ? regId : -1;
370 }
371
372 /*! Create menu.
373  * Insert QAction with id \a id from action map(myActionMap) to menu manager.
374  *\param menu    - integer
375  *\param id      - integer
376  *\param group   - integer
377  *\param index   - integer
378  *\retval integer id of new menu in tool manager.
379  *\retval Return -1 if something wrong.
380  */
381 int CAM_Module::createMenu( const int id, const int menu, const int group, const int index )
382 {
383   if ( !menuMgr() )
384     return -1;
385
386   int intId = menuMgr()->insert( action( id ), menu, group, index );
387   return intId != -1 ? id : -1;
388 }
389
390 /*! Create menu.
391  * Insert QAction with id \a id from action map(myActionMap) to menu manager.
392  *\param menu    - QString&
393  *\param id      - integer
394  *\param group   - integer
395  *\param index   - integer
396  *\retval integer id of new menu in tool manager.
397  *\retval Return -1 if something wrong.
398  */
399 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int index )
400 {
401   if ( !menuMgr() )
402     return -1;
403
404   int intId = menuMgr()->insert( action( id ), menu, group, index );
405   return intId != -1 ? id : -1;
406 }
407
408 /*!Sets menus shown to \a on floag.
409  *\param on - flag.
410  */
411 void CAM_Module::setMenuShown( const bool on )
412 {
413   QtxActionMenuMgr* mMgr = menuMgr();
414   if ( !mMgr )
415     return;
416
417   bool upd = mMgr->isUpdatesEnabled();
418   mMgr->setUpdatesEnabled( false );
419
420   QAction* sep = separator();
421   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
422   {
423     if ( it.data() != sep )
424       mMgr->setShown( mMgr->actionId( it.data() ), on );
425   }
426
427   mMgr->setUpdatesEnabled( upd );
428   if ( upd )
429     mMgr->update();
430 }
431
432 /*!Sets menu shown for QAction \a a to \a on flag.
433  * \param a - QAction
434  * \param on - flag
435  */
436 void CAM_Module::setMenuShown( QAction* a, const bool on )
437 {
438   if ( menuMgr() )
439     menuMgr()->setShown( menuMgr()->actionId( a ), on );
440 }
441
442 /*!Sets menu shown for action with id=\a id to \a on flag.
443  * \param id - id of action
444  * \param on - flag
445  */
446 void CAM_Module::setMenuShown( const int id, const bool on )
447 {
448   setMenuShown( action( id ), on );
449 }
450
451 /*!Set tools shown to \a on flag.
452  *\param on - boolean flag.
453  */
454 void CAM_Module::setToolShown( const bool on )
455 {
456   QtxActionToolMgr* tMgr = toolMgr();
457   if ( !tMgr )
458     return;
459
460   bool upd = tMgr->isUpdatesEnabled();
461   tMgr->setUpdatesEnabled( false );
462
463   QAction* sep = separator();
464   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
465   {
466     if ( it.data() != sep )
467       tMgr->setShown( tMgr->actionId( it.data() ), on );
468   }
469
470   tMgr->setUpdatesEnabled( upd );
471   if ( upd )
472     tMgr->update();
473 }
474
475 /*!Set tools shown for QAction \a a to \a on flag.
476  * \param a - QAction
477  * \param on - boolean flag
478  */
479 void CAM_Module::setToolShown( QAction* a, const bool on )
480 {
481   if ( toolMgr() )
482     toolMgr()->setShown( toolMgr()->actionId( a ), on );
483 }
484
485 /*!Set tools shown for action with id=\a id to \a on flag.
486  * \param id - integer action id
487  * \param on - boolean flag
488  */
489 void CAM_Module::setToolShown( const int id, const bool on )
490 {
491   setToolShown( action( id ), on );
492 }
493
494 /*! Return action by id. 
495  * \param id - id of action.
496  * \retval QAction.
497  */
498 QAction* CAM_Module::action( const int id ) const
499 {
500   QAction* a = 0;
501   if ( myActionMap.contains( id ) )
502     a = myActionMap[id];
503   return a;
504 }
505
506 /*! Return id by action. 
507  * \param a - QAction.
508  * \retval id of action.
509  */
510 int CAM_Module::actionId( const QAction* a ) const
511 {
512   int id = -1;
513   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
514   {
515     if ( it.data() == a )
516       id = it.key();
517   }
518   return id;
519 }
520
521 /*! Create new instance of QtxAction and register action with \a id.
522  * \param id - id for new action.
523  * \param text - parameter for creation QtxAction
524  * \param icon - parameter for creation QtxAction
525  * \param menu - parameter for creation QtxAction
526  * \param tip  - tip status for QtxAction action.
527  * \param key  - parameter for creation QtxAction
528  * \param parent - parent for action
529  * \param toggle - parameter for creation QtxAction
530  * \param reciever - 
531  * \param member   - 
532  */
533 QAction* CAM_Module::createAction( const int id, const QString& text, const QIconSet& icon,
534                                    const QString& menu, const QString& tip, const int key,
535                                    QObject* parent, const bool toggle, QObject* reciever, const char* member )
536 {
537   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
538   a->setStatusTip( tip );
539
540   if ( reciever && member )
541     connect( a, SIGNAL( activated() ), reciever, member );
542
543   registerAction( id, a );
544
545   return a;
546 }
547
548 /*! Register action in action map.
549  * \param id - id for action.
550  * \param a  - action
551  * \retval new id for action.
552  */
553 int CAM_Module::registerAction( const int id, QAction* a )
554 {
555   int ident = -1;
556   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
557     if ( it.data() == a )
558       ident = it.key();
559
560   if ( ident != -1 )
561     return ident;
562
563   static int generatedId = -1;
564   ident = id < 0 ? --generatedId : id;
565
566   myActionMap.insert( ident, a );
567
568   if ( menuMgr() )
569     menuMgr()->registerAction( a );
570
571   if ( toolMgr() )
572     toolMgr()->registerAction( a );
573
574   return ident;
575 }
576
577 /*! Unregister an action.
578  * \param id - id for action.
579  * \retval true if succeded, false if action is used
580  */
581 bool CAM_Module::unregisterAction( const int id )
582 {
583   return unregisterAction( action( id ) );
584 }
585
586 /*! Unregister an action.
587  * \param a  - action
588  * \retval true if succeded, false if action is used
589  */
590 bool CAM_Module::unregisterAction( QAction* a )
591 {
592   if ( !a )
593     return false;
594   if ( menuMgr() ) {
595     int id = menuMgr()->actionId( a );
596     if ( id != -1 && menuMgr()->containsMenu( id, -1 ) )
597       return false;
598   }
599   if ( toolMgr() ) {
600     int id = toolMgr()->actionId( a );
601     if ( id != -1 && toolMgr()->containsAction( id ) )
602       return false;
603   }
604   if ( menuMgr() )
605     menuMgr()->unRegisterAction( menuMgr()->actionId( a ) );
606   if ( toolMgr() )
607     toolMgr()->unRegisterAction( toolMgr()->actionId( a ) );
608   return true;
609 }
610
611 /*! Return qt action manager separator.*/
612 QAction* CAM_Module::separator()
613 {
614   return QtxActionMgr::separator();
615 }
616
617 /*! Connect data model of module with active study */
618 void CAM_Module::connectToStudy( CAM_Study* camStudy )
619 {
620   CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
621   if( !app )
622     return;
623
624   CAM_DataModel* prev = 0;
625   for( CAM_Application::ModuleListIterator it = app->modules(); it.current(); ++it )
626   {
627     CAM_DataModel* dm = it.current()->dataModel();
628     if( it.current() == this && !camStudy->containsDataModel( dm ) )
629     {
630       if( prev )
631         camStudy->insertDataModel( it.current()->dataModel(), prev );
632       else
633         camStudy->insertDataModel( it.current()->dataModel(), 0 );
634     }
635     prev = dm;
636   }
637 }