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,
303 const bool enableEmpty )
308 return menuMgr()->insert( subMenu, menu, group, id, index, enableEmpty );
312 * Insert submenu \a subMenu to menu manager.
313 *\param subMenu - QString&
314 *\param menu - QString&
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.
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 )
328 return menuMgr()->insert( subMenu, menu, group, id, index, enableEmpty );
332 /*! Create menu. Register action \a a with id \a id.
333 * Insert QAction to menu manager.
335 *\param menu - 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.
342 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
344 if ( !a || !menuMgr() )
347 int regId = registerAction( id, a );
348 int intId = menuMgr()->insert( a, menu, group, index );
349 return intId != -1 ? regId : -1;
352 /*! Create menu. Register action \a a with id \a id.
353 * Insert QAction to menu manager.
355 *\param menu - QString&
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.
362 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
364 if ( !a || !menuMgr() )
367 int regId = registerAction( id, a );
368 int intId = menuMgr()->insert( a, menu, group, index );
369 return intId != -1 ? regId : -1;
373 * Insert QAction with id \a id from action map(myActionMap) to menu manager.
374 *\param menu - 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.
381 int CAM_Module::createMenu( const int id, const int menu, const int group, const int index )
386 int intId = menuMgr()->insert( action( id ), menu, group, index );
387 return intId != -1 ? id : -1;
391 * Insert QAction with id \a id from action map(myActionMap) to menu manager.
392 *\param menu - QString&
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.
399 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int index )
404 int intId = menuMgr()->insert( action( id ), menu, group, index );
405 return intId != -1 ? id : -1;
408 /*!Sets menus shown to \a on floag.
411 void CAM_Module::setMenuShown( const bool on )
413 QtxActionMenuMgr* mMgr = menuMgr();
417 bool upd = mMgr->isUpdatesEnabled();
418 mMgr->setUpdatesEnabled( false );
420 QAction* sep = separator();
421 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
423 if ( it.data() != sep )
424 mMgr->setShown( mMgr->actionId( it.data() ), on );
427 mMgr->setUpdatesEnabled( upd );
432 /*!Sets menu shown for QAction \a a to \a on flag.
436 void CAM_Module::setMenuShown( QAction* a, const bool on )
439 menuMgr()->setShown( menuMgr()->actionId( a ), on );
442 /*!Sets menu shown for action with id=\a id to \a on flag.
443 * \param id - id of action
446 void CAM_Module::setMenuShown( const int id, const bool on )
448 setMenuShown( action( id ), on );
451 /*!Set tools shown to \a on flag.
452 *\param on - boolean flag.
454 void CAM_Module::setToolShown( const bool on )
456 QtxActionToolMgr* tMgr = toolMgr();
460 bool upd = tMgr->isUpdatesEnabled();
461 tMgr->setUpdatesEnabled( false );
463 QAction* sep = separator();
464 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
466 if ( it.data() != sep )
467 tMgr->setShown( tMgr->actionId( it.data() ), on );
470 tMgr->setUpdatesEnabled( upd );
475 /*!Set tools shown for QAction \a a to \a on flag.
477 * \param on - boolean flag
479 void CAM_Module::setToolShown( QAction* a, const bool on )
482 toolMgr()->setShown( toolMgr()->actionId( a ), on );
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
489 void CAM_Module::setToolShown( const int id, const bool on )
491 setToolShown( action( id ), on );
494 /*! Return action by id.
495 * \param id - id of action.
498 QAction* CAM_Module::action( const int id ) const
501 if ( myActionMap.contains( id ) )
506 /*! Return id by action.
507 * \param a - QAction.
508 * \retval id of action.
510 int CAM_Module::actionId( const QAction* a ) const
513 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
515 if ( it.data() == a )
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
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 )
537 QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
538 a->setStatusTip( tip );
540 if ( reciever && member )
541 connect( a, SIGNAL( activated() ), reciever, member );
543 registerAction( id, a );
548 /*! Register action in action map.
549 * \param id - id for action.
551 * \retval new id for action.
553 int CAM_Module::registerAction( const int id, QAction* a )
556 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
557 if ( it.data() == a )
563 static int generatedId = -1;
564 ident = id < 0 ? --generatedId : id;
566 myActionMap.insert( ident, a );
569 menuMgr()->registerAction( a );
572 toolMgr()->registerAction( a );
577 /*! Unregister an action.
578 * \param id - id for action.
579 * \retval true if succeded, false if action is used
581 bool CAM_Module::unregisterAction( const int id )
583 return unregisterAction( action( id ) );
586 /*! Unregister an action.
588 * \retval true if succeded, false if action is used
590 bool CAM_Module::unregisterAction( QAction* a )
595 int id = menuMgr()->actionId( a );
596 if ( id != -1 && menuMgr()->containsMenu( id, -1 ) )
600 int id = toolMgr()->actionId( a );
601 if ( id != -1 && toolMgr()->containsAction( id ) )
605 menuMgr()->unRegisterAction( menuMgr()->actionId( a ) );
607 toolMgr()->unRegisterAction( toolMgr()->actionId( a ) );
611 /*! Return qt action manager separator.*/
612 QAction* CAM_Module::separator()
614 return QtxActionMgr::separator();
617 /*! Connect data model of module with active study */
618 void CAM_Module::connectToStudy( CAM_Study* camStudy )
620 CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
624 CAM_DataModel* prev = 0;
625 for( CAM_Application::ModuleListIterator it = app->modules(); it.current(); ++it )
627 CAM_DataModel* dm = it.current()->dataModel();
628 if( it.current() == this && !camStudy->containsDataModel( dm ) )
631 camStudy->insertDataModel( it.current()->dataModel(), prev );
633 camStudy->insertDataModel( it.current()->dataModel(), 0 );