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