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