1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/
19 #include "CAM_Module.h"
21 #include "CAM_DataModel.h"
22 #include "CAM_Application.h"
23 #include "CAM_Study.h"
25 #include <QtxAction.h>
26 #include <QtxActionMenuMgr.h>
27 #include <QtxActionToolMgr.h>
30 static const char* ModuleIcon[] = {
37 " .................. ",
46 " .................. ",
50 " .. .. .. .. .. .. ",
52 " .. .. .. .. .. .. ",
55 QPixmap MYPixmap( ModuleIcon );
58 CAM_Module::CAM_Module()
66 /*!Constructor. initialize \a name.*/
67 CAM_Module::CAM_Module( const QString& name )
76 /*!Destructor. Remove data model.*/
77 CAM_Module::~CAM_Module()
83 /*!Initialize application.*/
84 void CAM_Module::initialize( CAM_Application* app )
89 /*!\retval Module icon.*/
90 QPixmap CAM_Module::moduleIcon() const
95 /*!\retval Module icon name.*/
96 QString CAM_Module::iconName() const
101 /*!\retval Module name.*/
102 QString CAM_Module::moduleName() const
107 /*! \brief Return data model.
108 * Create data model, if it was't created before.
110 CAM_DataModel* CAM_Module::dataModel() const
114 CAM_Module* that = (CAM_Module*)this;
115 that->myDataModel = that->createDataModel();
116 that->myDataModel->initialize();
121 /*!\retval CAM_Application pointer - application.*/
122 CAM_Application* CAM_Module::application() const
130 bool CAM_Module::activateModule( SUIT_Study* study )
138 bool CAM_Module::deactivateModule( SUIT_Study* )
143 /*!Public slot, remove data model from \a study.*/
144 void CAM_Module::studyClosed( SUIT_Study* study )
146 CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
150 CAM_DataModel* dm = dataModel();
151 if ( dm && camDoc->containsDataModel( dm ) ) {
153 camDoc->removeDataModel( dm );
157 /*!Public slot, do nothing.*/
158 void CAM_Module::studyChanged( SUIT_Study* , SUIT_Study* )
162 /*!Create and return new instance of CAM_DataModel.*/
163 CAM_DataModel* CAM_Module::createDataModel()
165 return new CAM_DataModel( this );
168 /*!Sets module name to \a name.
169 * \param name - new name for module.
171 void CAM_Module::setModuleName( const QString& name )
176 /*!Sets module icon to \a icon.
177 * \param icon - new icon for module.
179 void CAM_Module::setModuleIcon( const QPixmap& icon )
184 /*! Return menu manager pointer.
185 * \retval QtxActionMenuMgr pointer - menu manager.
187 QtxActionMenuMgr* CAM_Module::menuMgr() const
189 QtxActionMenuMgr* mgr = 0;
190 if ( application() && application()->desktop() )
191 mgr = application()->desktop()->menuMgr();
195 /*! Return tool manager pointer.
196 * \retval QtxActionToolMgr pointer - tool manager.
198 QtxActionToolMgr* CAM_Module::toolMgr() const
200 QtxActionToolMgr* mgr = 0;
201 if ( application() && application()->desktop() )
202 mgr = application()->desktop()->toolMgr();
206 /*! Create tool bar with name \a name, if it was't created before.
207 * \retval -1 - if tool manager was't be created.
209 int CAM_Module::createTool( const QString& name )
214 return toolMgr()->createToolBar( name );
217 /*! Create tool. Register action \a a with id \a id.
218 * Insert QAction to tool manager.
220 *\param tBar - integer
222 *\param idx - integer
223 *\retval integer id of new action in tool manager.
224 *\retval Return -1 if something wrong.
226 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
231 int regId = registerAction( id, a );
232 int intId = toolMgr()->insert( a, tBar, idx );
233 return intId != -1 ? regId : -1;
236 /*! Create tool. Register action \a a with id \a id.
237 * Insert QAction to tool manager.
239 *\param tBar - QString&
241 *\param idx - integer
242 *\retval integer id of new action in tool manager.
243 *\retval Return -1 if something wrong.
245 int CAM_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
250 int regId = registerAction( id, a );
251 int intId = toolMgr()->insert( a, tBar, idx );
252 return intId != -1 ? regId : -1;
256 * Insert QAction with id \a id from action map(myActionMap) to tool manager.
258 *\param tBar - integer
260 *\param idx - integer
261 *\retval integer id of new action in tool manager.
262 *\retval Return -1 if something wrong.
264 int CAM_Module::createTool( const int id, const int tBar, const int idx )
269 int intId = toolMgr()->insert( action( id ), tBar, idx );
270 return intId != -1 ? id : -1;
274 * Insert QAction with id \a id from action map(myActionMap) to tool manager.
276 *\param tBar - QString&
278 *\param idx - integer
279 *\retval integer id of new action in tool manager.
280 *\retval Return -1 if something wrong.
282 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
287 int intId = toolMgr()->insert( action( id ), tBar, idx );
288 return intId != -1 ? id : -1;
292 * Insert submenu \a subMenu to menu manager.
293 *\param subMenu - QString&
294 *\param menu - 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.
301 int CAM_Module::createMenu( const QString& subMenu, const int menu,
302 const int id, const int group, const int index )
307 return menuMgr()->insert( subMenu, menu, group, index );
311 * Insert submenu \a subMenu to menu manager.
312 *\param subMenu - QString&
313 *\param menu - QString&
315 *\param group - integer
316 *\param index - integer
317 *\retval integer id of new menu in tool manager.
318 *\retval Return -1 if something wrong.
320 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
321 const int id, const int group, const int index )
326 return menuMgr()->insert( subMenu, menu, group, index );
330 /*! Create menu. Register action \a a with id \a id.
331 * Insert QAction to menu manager.
333 *\param menu - 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.
340 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
342 if ( !a || !menuMgr() )
345 int regId = registerAction( id, a );
346 int intId = menuMgr()->insert( a, menu, group, index );
347 return intId != -1 ? regId : -1;
350 /*! Create menu. Register action \a a with id \a id.
351 * Insert QAction to menu manager.
353 *\param menu - QString&
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.
360 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
362 if ( !a || !menuMgr() )
365 int regId = registerAction( id, a );
366 int intId = menuMgr()->insert( a, menu, group, index );
367 return intId != -1 ? regId : -1;
371 * Insert QAction with id \a id from action map(myActionMap) to menu manager.
372 *\param menu - 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.
379 int CAM_Module::createMenu( const int id, const int menu, const int group, const int index )
384 int intId = menuMgr()->insert( action( id ), menu, group, index );
385 return intId != -1 ? id : -1;
389 * Insert QAction with id \a id from action map(myActionMap) to menu manager.
390 *\param menu - QString&
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.
397 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int index )
402 int intId = menuMgr()->insert( action( id ), menu, group, index );
403 return intId != -1 ? id : -1;
406 /*!Sets menus shown to \a on floag.
409 void CAM_Module::setMenuShown( const bool on )
411 QtxActionMenuMgr* mMgr = menuMgr();
415 bool upd = mMgr->isUpdatesEnabled();
416 mMgr->setUpdatesEnabled( false );
418 QAction* sep = separator();
419 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
421 if ( it.data() != sep )
422 mMgr->setShown( mMgr->actionId( it.data() ), on );
425 mMgr->setUpdatesEnabled( upd );
430 /*!Sets menu shown for QAction \a a to \a on flag.
434 void CAM_Module::setMenuShown( QAction* a, const bool on )
437 menuMgr()->setShown( menuMgr()->actionId( a ), on );
440 /*!Sets menu shown for action with id=\a id to \a on flag.
441 * \param id - id of action
444 void CAM_Module::setMenuShown( const int id, const bool on )
446 setMenuShown( action( id ), on );
449 /*!Set tools shown to \a on flag.
450 *\param on - boolean flag.
452 void CAM_Module::setToolShown( const bool on )
454 QtxActionToolMgr* tMgr = toolMgr();
458 bool upd = tMgr->isUpdatesEnabled();
459 tMgr->setUpdatesEnabled( false );
461 QAction* sep = separator();
462 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
464 if ( it.data() != sep )
465 tMgr->setShown( tMgr->actionId( it.data() ), on );
468 tMgr->setUpdatesEnabled( upd );
473 /*!Set tools shown for QAction \a a to \a on flag.
475 * \param on - boolean flag
477 void CAM_Module::setToolShown( QAction* a, const bool on )
480 toolMgr()->setShown( toolMgr()->actionId( a ), on );
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
487 void CAM_Module::setToolShown( const int id, const bool on )
489 setToolShown( action( id ), on );
492 /*! Return action by id.
493 * \param id - id of action.
496 QAction* CAM_Module::action( const int id ) const
499 if ( myActionMap.contains( id ) )
504 /*! Return id by action.
505 * \param a - QAction.
506 * \retval id of action.
508 int CAM_Module::actionId( const QAction* a ) const
511 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
513 if ( it.data() == a )
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
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 )
535 QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
536 a->setStatusTip( tip );
538 if ( reciever && member )
539 connect( a, SIGNAL( activated() ), reciever, member );
541 registerAction( id, a );
546 /*! Register action in action map.
547 * \param id - id for action.
549 * \retval new id for action.
551 int CAM_Module::registerAction( const int id, QAction* a )
554 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
555 if ( it.data() == a )
561 static int generatedId = -1;
562 ident = id < 0 ? --generatedId : id;
564 myActionMap.insert( ident, a );
567 menuMgr()->registerAction( a );
570 toolMgr()->registerAction( a );
575 /*! Return qt action manager separator.*/
576 QAction* CAM_Module::separator()
578 return QtxActionMgr::separator();
581 /*! Connect data model of module with active study */
582 void CAM_Module::connectToStudy( CAM_Study* camStudy )
584 CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
588 CAM_DataModel* prev = 0;
589 for( CAM_Application::ModuleListIterator it = app->modules(); it.current(); ++it )
591 CAM_DataModel* dm = it.current()->dataModel();
592 if( it.current() == this && !camStudy->containsDataModel( dm ) )
595 camStudy->insertDataModel( it.current()->dataModel(), prev );
597 camStudy->insertDataModel( it.current()->dataModel(), 0 );