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 )
91 SUIT_Session* aSession = SUIT_Session::session();
92 connect(aSession, SIGNAL( applicationClosed( SUIT_Application* ) ),
93 this, SLOT( onApplicationClosed( SUIT_Application* ) ));
97 /*!\retval Module icon.*/
98 QPixmap CAM_Module::moduleIcon() const
103 /*!\retval Module icon name.*/
104 QString CAM_Module::iconName() const
109 /*!\retval Module name.*/
110 QString CAM_Module::moduleName() const
115 /*! \brief Return data model.
116 * Create data model, if it was't created before.
118 CAM_DataModel* CAM_Module::dataModel() const
122 CAM_Module* that = (CAM_Module*)this;
123 that->myDataModel = that->createDataModel();
124 that->myDataModel->initialize();
129 /*!\retval CAM_Application pointer - application.*/
130 CAM_Application* CAM_Module::application() const
138 bool CAM_Module::activateModule( SUIT_Study* study )
146 bool CAM_Module::deactivateModule( SUIT_Study* )
151 /*!Public slot, remove data model from \a study.*/
152 void CAM_Module::studyClosed( SUIT_Study* study )
154 CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
158 CAM_DataModel* dm = dataModel();
159 if ( dm && camDoc->containsDataModel( dm ) ) {
161 camDoc->removeDataModel( dm );
165 /*!Public slot, do nothing.*/
166 void CAM_Module::studyChanged( SUIT_Study* , SUIT_Study* )
170 /*!Public slot, nullify application pointer if the application was closed.*/
171 void CAM_Module::onApplicationClosed( SUIT_Application* theApp )
177 /*!Create and return new instance of CAM_DataModel.*/
178 CAM_DataModel* CAM_Module::createDataModel()
180 return new CAM_DataModel( this );
183 /*!Sets module name to \a name.
184 * \param name - new name for module.
186 void CAM_Module::setModuleName( const QString& name )
191 /*!Sets module icon to \a icon.
192 * \param icon - new icon for module.
194 void CAM_Module::setModuleIcon( const QPixmap& icon )
199 /*! Return menu manager pointer.
200 * \retval QtxActionMenuMgr pointer - menu manager.
202 QtxActionMenuMgr* CAM_Module::menuMgr() const
204 QtxActionMenuMgr* mgr = 0;
205 if ( application() && application()->desktop() )
206 mgr = application()->desktop()->menuMgr();
210 /*! Return tool manager pointer.
211 * \retval QtxActionToolMgr pointer - tool manager.
213 QtxActionToolMgr* CAM_Module::toolMgr() const
215 QtxActionToolMgr* mgr = 0;
216 if ( application() && application()->desktop() )
217 mgr = application()->desktop()->toolMgr();
221 /*! Create tool bar with name \a name, if it was't created before.
222 * \retval -1 - if tool manager was't be created.
224 int CAM_Module::createTool( const QString& name )
229 return toolMgr()->createToolBar( name );
232 /*! Create tool. Register action \a a with id \a id.
233 * Insert QAction to tool manager.
235 *\param tBar - integer
237 *\param idx - integer
238 *\retval integer id of new action in tool manager.
239 *\retval Return -1 if something wrong.
241 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
246 int regId = registerAction( id, a );
247 int intId = toolMgr()->insert( a, tBar, idx );
248 return intId != -1 ? regId : -1;
251 /*! Create tool. Register action \a a with id \a id.
252 * Insert QAction to tool manager.
254 *\param tBar - QString&
256 *\param idx - integer
257 *\retval integer id of new action in tool manager.
258 *\retval Return -1 if something wrong.
260 int CAM_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
265 int regId = registerAction( id, a );
266 int intId = toolMgr()->insert( a, tBar, idx );
267 return intId != -1 ? regId : -1;
271 * Insert QAction with id \a id from action map(myActionMap) to tool manager.
273 *\param tBar - integer
274 *\param idx - integer
275 *\retval integer id of new action in tool manager.
276 *\retval Return -1 if something wrong.
278 int CAM_Module::createTool( const int id, const int tBar, const int idx )
283 int intId = toolMgr()->insert( action( id ), tBar, idx );
284 return intId != -1 ? id : -1;
288 * Insert QAction with id \a id from action map(myActionMap) to tool manager.
290 *\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( const int id, const QString& tBar, const int idx )
300 int intId = toolMgr()->insert( action( id ), tBar, idx );
301 return intId != -1 ? id : -1;
305 * Insert submenu \a subMenu to menu manager.
306 *\param subMenu - QString&
307 *\param menu - integer
309 *\param group - integer
310 *\param index - integer
311 *\retval integer id of new menu in tool manager.
312 *\retval Return -1 if something wrong.
314 int CAM_Module::createMenu( const QString& subMenu, const int menu,
315 const int id, const int group, const int index,
316 const bool enableEmpty )
321 return menuMgr()->insert( subMenu, menu, group, id, index, enableEmpty );
325 * Insert submenu \a subMenu to menu manager.
326 *\param subMenu - QString&
327 *\param menu - QString&
329 *\param group - integer
330 *\param index - integer
331 *\retval integer id of new menu in tool manager.
332 *\retval Return -1 if something wrong.
334 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
335 const int id, const int group, const int index,
336 const bool enableEmpty )
341 return menuMgr()->insert( subMenu, menu, group, id, index, enableEmpty );
345 /*! Create menu. Register action \a a with id \a id.
346 * Insert QAction to menu manager.
348 *\param menu - integer
350 *\param group - integer
351 *\param index - integer
352 *\retval integer id of new menu in tool manager.
353 *\retval Return -1 if something wrong.
355 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
357 if ( !a || !menuMgr() )
360 int regId = registerAction( id, a );
361 int intId = menuMgr()->insert( a, menu, group, index );
362 return intId != -1 ? regId : -1;
365 /*! Create menu. Register action \a a with id \a id.
366 * Insert QAction to menu manager.
368 *\param menu - QString&
370 *\param group - integer
371 *\param index - integer
372 *\retval integer id of new menu in tool manager.
373 *\retval Return -1 if something wrong.
375 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
377 if ( !a || !menuMgr() )
380 int regId = registerAction( id, a );
381 int intId = menuMgr()->insert( a, menu, group, index );
382 return intId != -1 ? regId : -1;
386 * Insert QAction with id \a id from action map(myActionMap) to menu manager.
387 *\param menu - integer
389 *\param group - integer
390 *\param index - integer
391 *\retval integer id of new menu in tool manager.
392 *\retval Return -1 if something wrong.
394 int CAM_Module::createMenu( const int id, const int menu, const int group, const int index )
399 int intId = menuMgr()->insert( action( id ), menu, group, index );
400 return intId != -1 ? id : -1;
404 * Insert QAction with id \a id from action map(myActionMap) to menu manager.
405 *\param menu - QString&
407 *\param group - integer
408 *\param index - integer
409 *\retval integer id of new menu in tool manager.
410 *\retval Return -1 if something wrong.
412 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int index )
417 int intId = menuMgr()->insert( action( id ), menu, group, index );
418 return intId != -1 ? id : -1;
421 /*!Sets menus shown to \a on floag.
424 void CAM_Module::setMenuShown( const bool on )
426 QtxActionMenuMgr* mMgr = menuMgr();
430 bool upd = mMgr->isUpdatesEnabled();
431 mMgr->setUpdatesEnabled( false );
433 QAction* sep = separator();
434 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
436 if ( it.data() != sep )
437 mMgr->setShown( mMgr->actionId( it.data() ), on );
440 mMgr->setUpdatesEnabled( upd );
445 /*!Sets menu shown for QAction \a a to \a on flag.
449 void CAM_Module::setMenuShown( QAction* a, const bool on )
452 menuMgr()->setShown( menuMgr()->actionId( a ), on );
455 /*!Sets menu shown for action with id=\a id to \a on flag.
456 * \param id - id of action
459 void CAM_Module::setMenuShown( const int id, const bool on )
461 setMenuShown( action( id ), on );
464 /*!Set tools shown to \a on flag.
465 *\param on - boolean flag.
467 void CAM_Module::setToolShown( const bool on )
469 QtxActionToolMgr* tMgr = toolMgr();
473 bool upd = tMgr->isUpdatesEnabled();
474 tMgr->setUpdatesEnabled( false );
476 QAction* sep = separator();
477 for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
479 if ( it.data() != sep )
480 tMgr->setShown( tMgr->actionId( it.data() ), on );
483 tMgr->setUpdatesEnabled( upd );
488 /*!Set tools shown for QAction \a a to \a on flag.
490 * \param on - boolean flag
492 void CAM_Module::setToolShown( QAction* a, const bool on )
495 toolMgr()->setShown( toolMgr()->actionId( a ), on );
498 /*!Set tools shown for action with id=\a id to \a on flag.
499 * \param id - integer action id
500 * \param on - boolean flag
502 void CAM_Module::setToolShown( const int id, const bool on )
504 setToolShown( action( id ), on );
507 /*! Return action by id.
508 * \param id - id of action.
511 QAction* CAM_Module::action( const int id ) const
514 if ( myActionMap.contains( id ) )
519 /*! Return id by action.
520 * \param a - QAction.
521 * \retval id of action.
523 int CAM_Module::actionId( const QAction* a ) const
526 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
528 if ( it.data() == a )
534 /*! Create new instance of QtxAction and register action with \a id.
535 * \param id - id for new action.
536 * \param text - parameter for creation QtxAction
537 * \param icon - parameter for creation QtxAction
538 * \param menu - parameter for creation QtxAction
539 * \param tip - tip status for QtxAction action.
540 * \param key - parameter for creation QtxAction
541 * \param parent - parent for action
542 * \param toggle - parameter for creation QtxAction
546 QAction* CAM_Module::createAction( const int id, const QString& text, const QIconSet& icon,
547 const QString& menu, const QString& tip, const int key,
548 QObject* parent, const bool toggle, QObject* reciever, const char* member )
550 QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
551 a->setStatusTip( tip );
553 if ( reciever && member )
554 connect( a, SIGNAL( activated() ), reciever, member );
556 registerAction( id, a );
561 /*! Register action in action map.
562 * \param id - id for action.
564 * \retval new id for action.
566 int CAM_Module::registerAction( const int id, QAction* a )
569 for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
570 if ( it.data() == a )
576 static int generatedId = -1;
577 ident = id < 0 ? --generatedId : id;
579 myActionMap.insert( ident, a );
582 menuMgr()->registerAction( a );
585 toolMgr()->registerAction( a );
590 /*! Unregister an action.
591 * \param id - id for action.
592 * \retval true if succeded, false if action is used
594 bool CAM_Module::unregisterAction( const int id )
596 return unregisterAction( action( id ) );
599 /*! Unregister an action.
601 * \retval true if succeded, false if action is used
603 bool CAM_Module::unregisterAction( QAction* a )
608 int id = menuMgr()->actionId( a );
609 if ( id != -1 && menuMgr()->containsMenu( id, -1 ) )
613 int id = toolMgr()->actionId( a );
614 if ( id != -1 && toolMgr()->containsAction( id ) )
618 menuMgr()->unRegisterAction( menuMgr()->actionId( a ) );
620 toolMgr()->unRegisterAction( toolMgr()->actionId( a ) );
624 /*! Return qt action manager separator.*/
625 QAction* CAM_Module::separator()
627 return QtxActionMgr::separator();
630 /*! Connect data model of module with active study */
631 void CAM_Module::connectToStudy( CAM_Study* camStudy )
633 CAM_Application* app = camStudy ? dynamic_cast<CAM_Application*>( camStudy->application() ) : 0;
637 CAM_DataModel* prev = 0;
638 for( CAM_Application::ModuleListIterator it = app->modules(); it.current(); ++it )
640 CAM_DataModel* dm = it.current()->dataModel();
641 if( it.current() == this && !camStudy->containsDataModel( dm ) )
644 camStudy->insertDataModel( it.current()->dataModel(), prev );
646 camStudy->insertDataModel( it.current()->dataModel(), 0 );