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/ or email : webmaster.salome@opencascade.com
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>
29 #include <SUIT_Session.h>
30 #include <SUIT_Application.h>
33 static const char* ModuleIcon[] = {
40 " .................. ",
49 " .................. ",
53 " .. .. .. .. .. .. ",
55 " .. .. .. .. .. .. ",
58 QPixmap MYPixmap( ModuleIcon );
61 CAM_Module::CAM_Module()
69 /*!Constructor. initialize \a name.*/
70 CAM_Module::CAM_Module( const QString& name )
79 /*!Destructor. Remove data model.*/
80 CAM_Module::~CAM_Module()
86 /*!Initialize application.*/
87 void CAM_Module::initialize( CAM_Application* app )
92 SUIT_Session* aSession = SUIT_Session::session();
93 connect( aSession, SIGNAL( applicationClosed( SUIT_Application* ) ),
94 this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
96 connect( myApp, SIGNAL( infoChanged( QString ) ), this, SLOT( onInfoChanged( QString ) ) );
100 /*!\retval Module icon.*/
101 QPixmap CAM_Module::moduleIcon() const
106 /*!\retval Module icon name.*/
107 QString CAM_Module::iconName() const
112 /*!\retval Module name.*/
113 QString CAM_Module::moduleName() const
118 /*! \brief Return data model.
119 * Create data model, if it was't created before.
121 CAM_DataModel* CAM_Module::dataModel() const
125 CAM_Module* that = (CAM_Module*)this;
126 that->myDataModel = that->createDataModel();
127 that->myDataModel->initialize();
132 /*!\retval CAM_Application pointer - application.*/
133 CAM_Application* CAM_Module::application() const
141 bool CAM_Module::activateModule( SUIT_Study* study )
149 bool CAM_Module::deactivateModule( SUIT_Study* )
154 /*!Public slot, remove data model from \a study.*/
155 void CAM_Module::studyClosed( SUIT_Study* study )
157 CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
161 CAM_DataModel* dm = dataModel();
162 if ( dm && camDoc->containsDataModel( dm ) ) {
164 camDoc->removeDataModel( dm );
168 /*!Public slot, do nothing.*/
169 void CAM_Module::studyChanged( SUIT_Study* , SUIT_Study* )
173 /*!Return true if module is active.*/
174 bool CAM_Module::isActiveModule() const
176 return application() ? application()->activeModule() == this : false;
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.
184 void CAM_Module::putInfo( const QString& msg, const int msec )
187 application()->putInfo( msg, msec );
194 Restore persistently displayed info string when previos information status string erasing
197 void CAM_Module::onInfoChanged( QString txt )
199 if ( txt.isEmpty() && isActiveModule() && !myInfo.isEmpty() && application() )
200 application()->putInfo( myInfo );
205 /*!Public slot, nullify application pointer if the application was closed.*/
206 void CAM_Module::onApplicationClosed( SUIT_Application* theApp )
212 /*!Create and return new instance of CAM_DataModel.*/
213 CAM_DataModel* CAM_Module::createDataModel()
215 return new CAM_DataModel( this );
218 /*!Sets module name to \a name.
219 * \param name - new name for module.
221 void CAM_Module::setModuleName( const QString& name )
226 /*!Sets module icon to \a icon.
227 * \param icon - new icon for module.
229 void CAM_Module::setModuleIcon( const QPixmap& icon )
234 /*! Return menu manager pointer.
235 * \retval QtxActionMenuMgr pointer - menu manager.
237 QtxActionMenuMgr* CAM_Module::menuMgr() const
239 QtxActionMenuMgr* mgr = 0;
240 if ( application() && application()->desktop() )
241 mgr = application()->desktop()->menuMgr();
245 /*! Return tool manager pointer.
246 * \retval QtxActionToolMgr pointer - tool manager.
248 QtxActionToolMgr* CAM_Module::toolMgr() const
250 QtxActionToolMgr* mgr = 0;
251 if ( application() && application()->desktop() )
252 mgr = application()->desktop()->toolMgr();
256 /*! Create tool bar with name \a name, if it was't created before.
257 * \retval -1 - if tool manager was't be created.
259 int CAM_Module::createTool( const QString& name )
264 return toolMgr()->createToolBar( name );
267 /*! Create tool. Register action \a a with id \a id.
268 * Insert QAction to tool manager.
270 *\param tBar - integer
272 *\param idx - integer
273 *\retval integer id of new action in tool manager.
274 *\retval Return -1 if something wrong.
276 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
281 int regId = registerAction( id, a );
282 int intId = toolMgr()->insert( a, tBar, idx );
283 return intId != -1 ? regId : -1;
286 /*! Create tool. Register action \a a with id \a id.
287 * Insert QAction to tool manager.
289 *\param tBar - QString&
291 *\param idx - integer
292 *\retval integer id of new action in tool manager.
293 *\retval Return -1 if something wrong.
295 int CAM_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
300 int regId = registerAction( id, a );
301 int intId = toolMgr()->insert( a, tBar, idx );
302 return intId != -1 ? regId : -1;
306 * Insert QAction with id \a id from action map(myActionMap) to tool manager.
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.
313 int CAM_Module::createTool( const int id, const int tBar, const int idx )
318 int intId = toolMgr()->insert( action( id ), tBar, idx );
319 return intId != -1 ? id : -1;
323 * Insert QAction with id \a id from action map(myActionMap) to tool manager.
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.
330 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
335 int intId = toolMgr()->insert( action( id ), tBar, idx );
336 return intId != -1 ? id : -1;
340 * Insert submenu \a subMenu to menu manager.
341 *\param subMenu - QString&
342 *\param menu - 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.
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 )
356 return menuMgr()->insert( subMenu, menu, group, id, index, enableEmpty );
360 * Insert submenu \a subMenu to menu manager.
361 *\param subMenu - QString&
362 *\param menu - QString&
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.
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 )
376 return menuMgr()->insert( subMenu, menu, group, id, index, enableEmpty );
380 /*! Create menu. Register action \a a with id \a id.
381 * Insert QAction to menu manager.
383 *\param menu - 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.
390 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
392 if ( !a || !menuMgr() )
395 int regId = registerAction( id, a );
396 int intId = menuMgr()->insert( a, menu, group, index );
397 return intId != -1 ? regId : -1;
400 /*! Create menu. Register action \a a with id \a id.
401 * Insert QAction to menu manager.
403 *\param menu - QString&
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.
410 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
412 if ( !a || !menuMgr() )
415 int regId = registerAction( id, a );
416 int intId = menuMgr()->insert( a, menu, group, index );
417 return intId != -1 ? regId : -1;
421 * Insert QAction with id \a id from action map(myActionMap) to menu manager.
422 *\param menu - 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.
429 int CAM_Module::createMenu( const int id, const int menu, const int group, const int index )
434 int intId = menuMgr()->insert( action( id ), menu, group, index );
435 return intId != -1 ? id : -1;
439 * Insert QAction with id \a id from action map(myActionMap) to menu manager.
440 *\param menu - QString&
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.
447 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int index )
452 int intId = menuMgr()->insert( action( id ), menu, group, index );
453 return intId != -1 ? id : -1;
456 /*!Sets menus shown to \a on floag.
459 void CAM_Module::setMenuShown( const bool on )
461 QtxActionMenuMgr* mMgr = menuMgr();
465 bool upd = mMgr->isUpdatesEnabled();
466 mMgr->setUpdatesEnabled( false );
468 QAction* sep = separator();
469 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
471 if ( it.data() != sep )
472 mMgr->setShown( mMgr->actionId( it.data() ), on );
475 mMgr->setUpdatesEnabled( upd );
480 /*!Sets menu shown for QAction \a a to \a on flag.
484 void CAM_Module::setMenuShown( QAction* a, const bool on )
487 menuMgr()->setShown( menuMgr()->actionId( a ), on );
490 /*!Sets menu shown for action with id=\a id to \a on flag.
491 * \param id - id of action
494 void CAM_Module::setMenuShown( const int id, const bool on )
496 setMenuShown( action( id ), on );
499 /*!Set tools shown to \a on flag.
500 *\param on - boolean flag.
502 void CAM_Module::setToolShown( const bool on )
504 QtxActionToolMgr* tMgr = toolMgr();
508 bool upd = tMgr->isUpdatesEnabled();
509 tMgr->setUpdatesEnabled( false );
511 QAction* sep = separator();
512 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
514 if ( it.data() != sep )
515 tMgr->setShown( tMgr->actionId( it.data() ), on );
518 tMgr->setUpdatesEnabled( upd );
523 /*!Set tools shown for QAction \a a to \a on flag.
525 * \param on - boolean flag
527 void CAM_Module::setToolShown( QAction* a, const bool on )
530 toolMgr()->setShown( toolMgr()->actionId( a ), on );
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
537 void CAM_Module::setToolShown( const int id, const bool on )
539 setToolShown( action( id ), on );
542 /*! Return action by id.
543 * \param id - id of action.
546 QAction* CAM_Module::action( const int id ) const
549 if ( myActionMap.contains( id ) )
554 /*! Return id by action.
555 * \param a - QAction.
556 * \retval id of action.
558 int CAM_Module::actionId( const QAction* a ) const
561 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
563 if ( it.data() == a )
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
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 )
585 QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
586 a->setStatusTip( tip );
588 if ( reciever && member )
589 connect( a, SIGNAL( activated() ), reciever, member );
591 registerAction( id, a );
596 /*! Register action in action map.
597 * \param id - id for action.
599 * \retval new id for action.
601 int CAM_Module::registerAction( const int id, QAction* a )
604 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
605 if ( it.data() == a )
611 static int generatedId = -1;
612 ident = id < 0 ? --generatedId : id;
614 myActionMap.insert( ident, a );
617 menuMgr()->registerAction( a );
620 toolMgr()->registerAction( a );
625 /*! Unregister an action.
626 * \param id - id for action.
627 * \retval true if succeded, false if action is used
629 bool CAM_Module::unregisterAction( const int id )
631 return unregisterAction( action( id ) );
634 /*! Unregister an action.
636 * \retval true if succeded, false if action is used
638 bool CAM_Module::unregisterAction( QAction* a )
643 int id = menuMgr()->actionId( a );
644 if ( id != -1 && menuMgr()->containsMenu( id, -1 ) )
648 int id = toolMgr()->actionId( a );
649 if ( id != -1 && toolMgr()->containsAction( id ) )
653 menuMgr()->unRegisterAction( menuMgr()->actionId( a ) );
655 toolMgr()->unRegisterAction( toolMgr()->actionId( a ) );
659 /*! Return qt action manager separator.*/
660 QAction* CAM_Module::separator()
662 return QtxActionMgr::separator();
665 /*! Connect data model of module with active study */
666 void CAM_Module::connectToStudy( CAM_Study* camStudy )
668 CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
672 CAM_DataModel* prev = 0;
673 for( CAM_Application::ModuleListIterator it = app->modules(); it.current(); ++it )
675 CAM_DataModel* dm = it.current()->dataModel();
676 if( it.current() == this && !camStudy->containsDataModel( dm ) )
679 camStudy->insertDataModel( it.current()->dataModel(), prev );
681 camStudy->insertDataModel( it.current()->dataModel(), 0 );