1 // File: SalomeApp_Module.cxx
2 // Created: 10/25/2004 11:39:56 AM
3 // Author: Sergey LITONIN
4 // Copyright (C) CEA 2004
6 #include "SalomeApp_Module.h"
8 #include "SalomeApp_Study.h"
9 #include "SalomeApp_DataModel.h"
10 #include "SalomeApp_Application.h"
11 #include "SalomeApp_Preferences.h"
12 #include "SalomeApp_UpdateFlags.h"
13 #include "SalomeApp_Operation.h"
14 #include "SalomeApp_SwitchOp.h"
15 #include "SalomeApp_ShowHideOp.h"
17 #include <OB_Browser.h>
19 #include <CAM_Study.h>
21 #include <SUIT_MessageBox.h>
22 #include <SUIT_ResourceMgr.h>
24 #include <QtxPopupMgr.h>
26 #include <SVTK_ViewWindow.h>
27 #include <OCCViewer_ViewWindow.h>
28 #include <OCCViewer_ViewPort3d.h>
29 #include <GLViewer_ViewFrame.h>
30 #include <GLViewer_ViewPort.h>
31 #include <Plot2d_ViewWindow.h>
34 SalomeApp_Module::SalomeApp_Module( const QString& name )
45 SalomeApp_Module::~SalomeApp_Module()
51 /*!Initialize module.*/
52 void SalomeApp_Module::initialize( CAM_Application* app )
54 CAM_Module::initialize( app );
56 SUIT_ResourceMgr* resMgr = app ? app->resourceMgr() : 0;
58 resMgr->raiseTranslators( name() );
62 bool SalomeApp_Module::activateModule( SUIT_Study* study )
64 bool res = CAM_Module::activateModule( study );
66 if ( res && application() && application()->resourceMgr() )
67 application()->resourceMgr()->raiseTranslators( name() );
69 if ( mySwitchOp == 0 )
70 mySwitchOp = new SalomeApp_SwitchOp( this );
75 /*!Deactivate module.*/
76 bool SalomeApp_Module::deactivateModule( SUIT_Study* )
85 void SalomeApp_Module::selectionChanged()
90 void SalomeApp_Module::MenuItem()
95 void SalomeApp_Module::windows( QMap<int, int>& ) const
100 void SalomeApp_Module::viewManagers( QStringList& ) const
105 void SalomeApp_Module::createPreferences()
110 void SalomeApp_Module::preferencesChanged( const QString&, const QString& )
114 /*!Gets application.*/
115 SalomeApp_Application* SalomeApp_Module::getApp() const
117 return (SalomeApp_Application*)application();
121 void SalomeApp_Module::onModelOpened()
126 void SalomeApp_Module::onModelSaved()
131 void SalomeApp_Module::onModelClosed()
135 /*!Gets popup manager.(create if not exist)*/
136 QtxPopupMgr* SalomeApp_Module::popupMgr()
140 myPopupMgr = new QtxPopupMgr( 0, this );
143 SUIT_Desktop* d = application()->desktop();
146 *disp = createAction( -1, tr( "TOP_DISPLAY" ), p, tr( "MEN_DISPLAY" ), tr( "STB_DISPLAY" ),
147 0, d, false, this, SLOT( onShowHide() ) ),
148 *erase = createAction( -1, tr( "TOP_ERASE" ), p, tr( "MEN_ERASE" ), tr( "STB_ERASE" ),
149 0, d, false, this, SLOT( onShowHide() ) ),
150 *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
151 0, d, false, this, SLOT( onShowHide() ) );
152 myDisplay = actionId( disp );
153 myErase = actionId( erase );
154 myDisplayOnly = actionId( dispOnly );
156 myPopupMgr->insert( disp, -1, 0 );
157 myPopupMgr->insert( erase, -1, 0 );
158 myPopupMgr->insert( dispOnly, -1, 0 );
159 myPopupMgr->insert( separator(), -1, 0 );
161 QString uniform = "( count( $component ) = 1 ) and ( component != activeModule )";
162 myPopupMgr->setRule( disp, QString( "( not isVisible ) and " ) + uniform, true );
163 myPopupMgr->setRule( erase, QString( "( isVisible ) and " ) + uniform, true );
164 myPopupMgr->setRule( dispOnly, uniform, true );
169 /*!Gets preferences.*/
170 SalomeApp_Preferences* SalomeApp_Module::preferences() const
172 SalomeApp_Preferences* pref = 0;
174 pref = getApp()->preferences();
178 /*!Create new instance of data model and return it.*/
179 CAM_DataModel* SalomeApp_Module::createDataModel()
181 return new SalomeApp_DataModel(this);
184 /*!Update object browser.*/
185 void SalomeApp_Module::updateObjBrowser( bool updateDataModel, SUIT_DataObject* root )
187 if( updateDataModel )
188 if( CAM_DataModel* aDataModel = dataModel() )
189 if( SalomeApp_DataModel* aModel = dynamic_cast<SalomeApp_DataModel*>( aDataModel ) )
190 aModel->update( 0, dynamic_cast<SalomeApp_Study*>( getApp()->activeStudy() ) );
191 getApp()->objectBrowser()->updateTree( root );
194 /*!Context menu popup.*/
195 void SalomeApp_Module::contextMenuPopup( const QString& client, QPopupMenu* menu, QString& /*title*/ )
197 SalomeApp_Selection* sel = createSelection();
198 sel->init( client, getApp()->selectionMgr() );
199 popupMgr()->updatePopup( menu, sel );
203 /*!Create and return instance of SalomeApp_Selection.*/
204 SalomeApp_Selection* SalomeApp_Module::createSelection() const
206 return new SalomeApp_Selection();
209 /*!Add preference to preferences.*/
210 int SalomeApp_Module::addPreference( const QString& label )
212 SalomeApp_Preferences* pref = preferences();
216 int catId = pref->addPreference( moduleName(), -1 );
220 return pref->addPreference( label, catId );
223 /*!Add preference to preferences.*/
224 int SalomeApp_Module::addPreference( const QString& label, const int pId, const int type,
225 const QString& section, const QString& param )
227 SalomeApp_Preferences* pref = preferences();
231 return pref->addPreference( moduleName(), label, pId, type, section, param );
234 /*!Gets property of preferences.*/
235 QVariant SalomeApp_Module::preferenceProperty( const int id, const QString& prop ) const
238 SalomeApp_Preferences* pref = preferences();
240 var = pref->itemProperty( id, prop );
245 /*!Set property of preferences.*/
246 void SalomeApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
248 SalomeApp_Preferences* pref = preferences();
250 pref->setItemProperty( id, prop, var );
254 * \brief Update something in accordance with update flags
255 * \param theFlags - update flags
257 * Update viewer or/and object browser etc. in accordance with update flags ( see
258 * SalomeApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
261 void SalomeApp_Module::update( const int theFlags )
263 if ( theFlags & UF_Model )
265 if( CAM_DataModel* aDataModel = dataModel() )
266 if( SalomeApp_DataModel* aModel = dynamic_cast<SalomeApp_DataModel*>( aDataModel ) )
267 aModel->update( 0, dynamic_cast<SalomeApp_Study*>( getApp()->activeStudy() ) );
269 if ( theFlags & UF_ObjBrowser )
270 getApp()->objectBrowser()->updateTree( 0 );
271 if ( theFlags & UF_Controls )
273 if ( theFlags & UF_Viewer )
275 if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
276 if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
278 if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
279 ( (SVTK_ViewWindow*)viewWnd )->Repaint();
280 else if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
281 ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
282 else if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
283 ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
284 else if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
285 ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
291 * \brief Updates controls
293 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
294 * called from update( UF_Controls ). You may redefine it in concrete module.
296 void SalomeApp_Module::updateControls()
301 * \brief Starts operation with given identifier
302 * \param id - identifier of operation to be started
304 * Module stores operations in map. This method starts operation by id.
305 * If operation isn't in map, then it will be created by createOperation method
306 * and will be inserted to map
308 void SalomeApp_Module::startOperation( const int id )
310 SalomeApp_Operation* op = 0;
311 if( myOperations.contains( id ) )
312 op = myOperations[ id ];
315 op = createOperation( id );
318 myOperations.insert( id, op );
319 op->setModule( this );
320 connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
321 connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
330 * \brief Creates operation with given identifier
331 * \param id - identifier of operation to be started
332 * \return Pointer on created operation or NULL if operation is not created
334 * Creates operation with given id. You should not call this method, it will be called
335 * automatically from startOperation. You may redefine this method in concrete module to
338 SalomeApp_Operation* SalomeApp_Module::createOperation( const int id ) const
344 return new SalomeApp_ShowHideOp( SalomeApp_ShowHideOp::DISPLAY );
345 else if( id==myErase )
346 return new SalomeApp_ShowHideOp( SalomeApp_ShowHideOp::ERASE );
347 else if( id==myDisplayOnly )
348 return new SalomeApp_ShowHideOp( SalomeApp_ShowHideOp::DISPLAY_ONLY );
354 * \brief Virtual protected slot called when operation stopped
355 * \param theOp - stopped operation
357 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
358 * perform actions after stopping operation
360 void SalomeApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
365 * \brief Virtual protected slot called when operation destroyed
366 * \param theOp - destroyed operation
368 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
369 * perform actions after destroying operation. Base implementation removes pointer on
370 * destroyed operation from the map of operations
372 void SalomeApp_Module::onOperationDestroyed()
374 const QObject* s = sender();
375 if( s && s->inherits( "SalomeApp_Operation" ) )
377 const SalomeApp_Operation* op = ( SalomeApp_Operation* )s;
378 MapOfOperation::const_iterator anIt = myOperations.begin(),
379 aLast = myOperations.end();
380 for( ; anIt!=aLast; anIt++ )
381 if( anIt.data()==op )
383 myOperations.remove( anIt.key() );
389 SalomeApp_Displayer* SalomeApp_Module::displayer()
394 void SalomeApp_Module::onShowHide()
396 if( !sender()->inherits( "QAction" ) || !popupMgr() )
399 QAction* act = ( QAction* )sender();
400 int id = actionId( act );
402 startOperation( id );