Salome HOME
Fix a bug of menu management (action is registered incorrectly if -1 (default) id...
[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 static const char* ModuleIcon[] = {
12 "20 20 2 1",
13 "       c None",
14 ".      c #000000",
15 "                    ",
16 "                    ",
17 "                    ",
18 " .................. ",
19 " .                . ",
20 " .                . ",
21 " .                . ",
22 " .                . ",
23 " .                . ",
24 " .                . ",
25 " .                . ",
26 " .                . ",
27 " .................. ",
28 "    .     .     .   ",
29 "    .     .     .   ",
30 "   ...   ...   ...  ",
31 "  .. .. .. .. .. .. ",
32 "  .   . .   . .   . ",
33 "  .. .. .. .. .. .. ",
34 "   ...   ...   ...  "};
35
36 QPixmap MYPixmap( ModuleIcon );
37
38 /*!Constructor.*/
39 CAM_Module::CAM_Module()
40 : QObject(),
41 myApp( 0 ),
42 myIcon( MYPixmap ),
43 myDataModel( 0 )
44 {
45 }
46
47 /*!Constructor. initialize \a name.*/
48 CAM_Module::CAM_Module( const QString& name )
49 : QObject(),
50 myApp( 0 ),
51 myName( name ),
52 myIcon( MYPixmap ),
53 myDataModel( 0 )
54 {
55 }
56
57 /*!Destructor. Remove data model.*/
58 CAM_Module::~CAM_Module()
59 {
60   delete myDataModel;
61   myDataModel = 0;
62 }
63
64 /*!Initialize application.*/
65 void CAM_Module::initialize( CAM_Application* app )
66 {
67   myApp = app;
68 }
69
70 /*!\retval Module icon.*/
71 QPixmap CAM_Module::moduleIcon() const
72 {
73   return myIcon;
74 }
75
76 /*!\retval Module name.*/
77 QString CAM_Module::moduleName() const
78 {
79   return myName;
80 }
81
82 /*! \brief Return data model.
83  * Create data model, if it was't created before.
84  */
85 CAM_DataModel* CAM_Module::dataModel() const
86 {
87   if ( !myDataModel )
88   {
89     CAM_Module* that = (CAM_Module*)this;
90     that->myDataModel = that->createDataModel();
91     that->myDataModel->initialize();
92   }
93   return myDataModel;
94 }
95
96 /*!\retval CAM_Application pointer - application.*/
97 CAM_Application* CAM_Module::application() const
98 {
99   return myApp;
100 }
101
102 /*!Public slot
103  * \retval true.
104  */
105 bool CAM_Module::activateModule( SUIT_Study* study )
106 {
107   return true;
108 }
109
110 /*!Public slot
111  * \retval true.
112  */
113 bool CAM_Module::deactivateModule( SUIT_Study* )
114 {
115   return true;
116 }
117
118 /*!Public slot, remove data model from \a study.*/
119 void CAM_Module::studyClosed( SUIT_Study* study )
120 {
121   CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
122   if ( !camDoc ) 
123     return;
124
125   if ( camDoc->containsDataModel( dataModel() ) )
126     camDoc->removeDataModel( dataModel() );
127 }
128
129 /*!Public slot, do nothing.*/
130 void CAM_Module::studyChanged( SUIT_Study* , SUIT_Study* )
131 {
132 }
133
134 /*!Create and return new instance of CAM_DataModel.*/
135 CAM_DataModel* CAM_Module::createDataModel()
136
137   return new CAM_DataModel( this );
138 }
139
140 /*!Sets module name to \a name.
141  * \param name - new name for module.
142  */
143 void CAM_Module::setModuleName( const QString& name )
144 {
145   myName = name;
146 }
147
148 /*!Sets module icon to \a icon.
149  * \param icon - new icon for module.
150  */
151 void CAM_Module::setModuleIcon( const QPixmap& icon )
152 {
153   myIcon = icon;
154 }
155
156 /*! Return menu manager pointer.
157  * \retval QtxActionMenuMgr pointer - menu manager.
158  */
159 QtxActionMenuMgr* CAM_Module::menuMgr() const
160 {
161   QtxActionMenuMgr* mgr = 0;
162   if ( application() && application()->desktop() )
163     mgr = application()->desktop()->menuMgr();
164   return mgr;
165 }
166
167 /*! Return tool manager pointer.
168  * \retval QtxActionToolMgr pointer - tool manager.
169  */
170 QtxActionToolMgr* CAM_Module::toolMgr() const
171 {
172   QtxActionToolMgr* mgr = 0;
173   if ( application() && application()->desktop() )
174     mgr = application()->desktop()->toolMgr();
175   return mgr;
176 }
177
178 /*! Create tool bar with name \a name, if it was't created before.
179  * \retval -1 - if tool manager was't be created.
180  */
181 int CAM_Module::createTool( const QString& name )
182 {
183   if ( !toolMgr() )
184     return -1;
185
186   return toolMgr()->createToolBar( name );
187 }
188
189
190 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
191 {
192   if ( !toolMgr() )
193     return -1;
194
195   int regId = registerAction( id, a );
196   int intId = toolMgr()->insert( a, tBar, idx );
197   return intId != -1 ? regId : -1;
198 }
199
200 int CAM_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
201 {
202   if ( !toolMgr() )
203     return -1;
204
205   int regId = registerAction( id, a );
206   int intId = toolMgr()->insert( a, tBar, idx );
207   return intId != -1 ? regId : -1;
208 }
209
210 int CAM_Module::createTool( const int id, const int tBar, const int idx )
211 {
212   if ( !toolMgr() )
213     return -1;
214
215   int intId = toolMgr()->insert( action( id ), tBar, idx );
216   return intId != -1 ? id : -1;
217 }
218
219 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
220 {
221   if ( !toolMgr() )
222     return -1;
223
224   int intId = toolMgr()->insert( action( id ), tBar, idx );
225   return intId != -1 ? id : -1;
226 }
227
228 int CAM_Module::createMenu( const QString& subMenu, const int menu,
229                             const int id, const int group, const int index )
230 {
231   if ( !menuMgr() )
232     return -1;
233
234   return menuMgr()->insert( subMenu, menu, group, index );
235 }
236
237 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
238                             const int id, const int group, const int index )
239 {
240   if ( !menuMgr() )
241     return -1;
242
243   return menuMgr()->insert( subMenu, menu, group, index );
244 }
245
246 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
247 {
248   if ( !a || !menuMgr() )
249     return -1;
250
251   int regId = registerAction( id, a );
252   int intId = menuMgr()->insert( a, menu, group, index );
253   return intId != -1 ? regId : -1;
254 }
255
256 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
257 {
258   if ( !a || !menuMgr() )
259     return -1;
260
261   int regId = registerAction( id, a );
262   int intId = menuMgr()->insert( a, menu, group, index );
263   return intId != -1 ? regId : -1;
264 }
265
266 int CAM_Module::createMenu( const int id, const int menu, const int group, const int index )
267 {
268   if ( !menuMgr() )
269     return -1;
270
271   int intId = menuMgr()->insert( action( id ), menu, group, index );
272   return intId != -1 ? id : -1;
273 }
274
275 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int index )
276 {
277   if ( !menuMgr() )
278     return -1;
279
280   int intId = menuMgr()->insert( action( id ), menu, group, index );
281   return intId != -1 ? id : -1;
282 }
283
284 /*!Sets menus shown to \a on floag.
285  *\param on - flag.
286  */
287 void CAM_Module::setMenuShown( const bool on )
288 {
289   QtxActionMenuMgr* mMgr = menuMgr();
290   if ( !mMgr )
291     return;
292
293   bool upd = mMgr->isUpdatesEnabled();
294   mMgr->setUpdatesEnabled( false );
295
296   QAction* sep = separator();
297   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
298   {
299     if ( it.data() != sep )
300       mMgr->setShown( mMgr->actionId( it.data() ), on );
301   }
302
303   mMgr->setUpdatesEnabled( upd );
304   if ( upd )
305     mMgr->update();
306 }
307
308 /*!Sets menu shown for QAction \a a to \a on flag.
309  * \param a - QAction
310  * \param on - flag
311  */
312 void CAM_Module::setMenuShown( QAction* a, const bool on )
313 {
314   if ( menuMgr() )
315     menuMgr()->setShown( menuMgr()->actionId( a ), on );
316 }
317
318 /*!Sets menu shown for action with id=\a id to \a on flag.
319  * \param id - id of action
320  * \param on - flag
321  */
322 void CAM_Module::setMenuShown( const int id, const bool on )
323 {
324   setMenuShown( action( id ), on );
325 }
326
327 /*!Set tools shown to \a on flag.
328  *\param on - boolean flag.
329  */
330 void CAM_Module::setToolShown( const bool on )
331 {
332   QtxActionToolMgr* tMgr = toolMgr();
333   if ( !tMgr )
334     return;
335
336   bool upd = tMgr->isUpdatesEnabled();
337   tMgr->setUpdatesEnabled( false );
338
339   QAction* sep = separator();
340   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
341   {
342     if ( it.data() != sep )
343       tMgr->setShown( tMgr->actionId( it.data() ), on );
344   }
345
346   tMgr->setUpdatesEnabled( upd );
347   if ( upd )
348     tMgr->update();
349 }
350
351 /*!Set tools shown for QAction \a a to \a on flag.
352  * \param a - QAction
353  * \param on - boolean flag
354  */
355 void CAM_Module::setToolShown( QAction* a, const bool on )
356 {
357   if ( toolMgr() )
358     toolMgr()->setShown( toolMgr()->actionId( a ), on );
359 }
360
361 /*!Set tools shown for action with id=\a id to \a on flag.
362  * \param id - integer action id
363  * \param on - boolean flag
364  */
365 void CAM_Module::setToolShown( const int id, const bool on )
366 {
367   setToolShown( action( id ), on );
368 }
369
370 /*! Return action by id. 
371  * \param id - id of action.
372  * \retval QAction.
373  */
374 QAction* CAM_Module::action( const int id ) const
375 {
376   QAction* a = 0;
377   if ( myActionMap.contains( id ) )
378     a = myActionMap[id];
379   return a;
380 }
381
382 /*! Return id by action. 
383  * \param a - QAction.
384  * \retval id of action.
385  */
386 int CAM_Module::actionId( const QAction* a ) const
387 {
388   int id = -1;
389   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
390   {
391     if ( it.data() == a )
392       id = it.key();
393   }
394   return id;
395 }
396
397 /*! Create new instance of QtxAction and register action with \a id.
398  * \param id - id for new action.
399  * \param text - parameter for creation QtxAction
400  * \param icon - parameter for creation QtxAction
401  * \param menu - parameter for creation QtxAction
402  * \param tip  - tip status for QtxAction action.
403  * \param key  - parameter for creation QtxAction
404  * \param parent - parent for action
405  * \param toggle - parameter for creation QtxAction
406  * \param reciever - 
407  * \param member   - 
408  */
409 QAction* CAM_Module::createAction( const int id, const QString& text, const QIconSet& icon,
410                                    const QString& menu, const QString& tip, const int key,
411                                    QObject* parent, const bool toggle, QObject* reciever, const char* member )
412 {
413   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
414   a->setStatusTip( tip );
415
416   if ( reciever && member )
417     connect( a, SIGNAL( activated() ), reciever, member );
418
419   registerAction( id, a );
420
421   return a;
422 }
423
424 /*! Register action in action map.
425  * \param id - id for action.
426  * \param a  - action
427  * \retval new id for action.
428  */
429 int CAM_Module::registerAction( const int id, QAction* a )
430 {
431   int ident = -1;
432   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
433     if ( it.data() == a )
434       ident = it.key();
435
436   if ( ident != -1 )
437     return ident;
438
439   static int generatedId = -1;
440   ident = id < 0 ? --generatedId : id;
441
442   myActionMap.insert( ident, a );
443
444   if ( menuMgr() )
445     menuMgr()->registerAction( a );
446
447   if ( toolMgr() )
448     toolMgr()->registerAction( a );
449
450   return ident;
451 }
452
453 /*! Return qt action manager separator.*/
454 QAction* CAM_Module::separator()
455 {
456   return QtxActionMgr::separator();
457 }