1 // File: LightApp_Module.cxx
2 // Created: 6/20/2005 16:30:56 AM
4 // Copyright (C) CEA 2005
6 #include "LightApp_Module.h"
8 #include "CAM_Application.h"
10 #include "LightApp_Application.h"
11 #include "LightApp_DataModel.h"
12 #include "LightApp_Study.h"
13 #include "LightApp_Preferences.h"
14 #include "LightApp_Selection.h"
15 #include "LightApp_Operation.h"
16 #include "LightApp_SwitchOp.h"
17 #include "LightApp_UpdateFlags.h"
18 #include "LightApp_ShowHideOp.h"
20 #include "SUIT_Operation.h"
21 #include <SUIT_Study.h>
22 #include <SUIT_DataObject.h>
23 #include <SUIT_ResourceMgr.h>
25 #include <SVTK_ViewWindow.h>
26 #include <SVTK_ViewModel.h>
27 #include <OCCViewer_ViewWindow.h>
28 #include <OCCViewer_ViewPort3d.h>
29 #include <SOCC_ViewModel.h>
30 #include <GLViewer_ViewFrame.h>
31 #include <GLViewer_ViewPort.h>
32 #include <Plot2d_ViewWindow.h>
33 #include <Plot2d_ViewFrame.h>
34 #include <SPlot2d_ViewModel.h>
36 #include <OB_Browser.h>
38 #include <QtxPopupMgr.h>
42 #include <qstringlist.h>
45 LightApp_Module::LightApp_Module( const QString& name )
57 LightApp_Module::~LightApp_Module()
63 /*!Initialize module.*/
64 void LightApp_Module::initialize( CAM_Application* app )
66 CAM_Module::initialize( app );
68 SUIT_ResourceMgr* resMgr = app ? app->resourceMgr() : 0;
70 resMgr->raiseTranslators( name() );
74 void LightApp_Module::windows( QMap<int, int>& ) const
79 void LightApp_Module::viewManagers( QStringList& ) const
83 /*!Context menu popup.*/
84 void LightApp_Module::contextMenuPopup( const QString& client, QPopupMenu* menu, QString& /*title*/ )
86 LightApp_Selection* sel = createSelection();
87 sel->init( client, getApp()->selectionMgr() );
88 popupMgr()->updatePopup( menu, sel );
92 /*!Update object browser.
93 * For updating model or whole object browser use update() method can be used.
95 void LightApp_Module::updateObjBrowser( bool updateDataModel, SUIT_DataObject* root )
98 if( CAM_DataModel* aDataModel = dataModel() )
99 if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
100 aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
101 getApp()->objectBrowser()->updateTree( root );
105 void LightApp_Module::selectionChanged()
109 /*!Activate module.*/
110 bool LightApp_Module::activateModule( SUIT_Study* study )
112 bool res = CAM_Module::activateModule( study );
114 if ( res && application() && application()->resourceMgr() )
115 application()->resourceMgr()->raiseTranslators( name() );
117 if ( mySwitchOp == 0 )
118 mySwitchOp = new LightApp_SwitchOp( this );
123 /*!Deactivate module.*/
124 bool LightApp_Module::deactivateModule( SUIT_Study* )
133 void LightApp_Module::MenuItem()
138 void LightApp_Module::createPreferences()
143 void LightApp_Module::preferencesChanged( const QString&, const QString& )
147 /*!Gets application.*/
148 LightApp_Application* LightApp_Module::getApp() const
150 return (LightApp_Application*)application();
154 * \brief Update something in accordance with update flags
155 * \param theFlags - update flags
157 * Update viewer or/and object browser etc. in accordance with update flags ( see
158 * LightApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
161 void LightApp_Module::update( const int theFlags )
163 if ( theFlags & UF_Model )
165 if( CAM_DataModel* aDataModel = dataModel() )
166 if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
167 aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
169 if ( theFlags & UF_ObjBrowser )
170 getApp()->objectBrowser()->updateTree( 0 );
171 if ( theFlags & UF_Controls )
173 if ( theFlags & UF_Viewer )
175 if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
176 if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
178 if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
179 ( (SVTK_ViewWindow*)viewWnd )->Repaint();
180 else if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
181 ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
182 else if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
183 ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
184 else if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
185 ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
190 * \brief Updates controls
192 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
193 * called from update( UF_Controls ). You may redefine it in concrete module.
195 void LightApp_Module::updateControls()
199 /*!Create new instance of data model and return it.*/
200 CAM_DataModel* LightApp_Module::createDataModel()
202 return new LightApp_DataModel(this);
205 /*!Create and return instance of LightApp_Selection.*/
206 LightApp_Selection* LightApp_Module::createSelection() const
208 return new LightApp_Selection();
212 void LightApp_Module::onModelOpened()
217 void LightApp_Module::onModelSaved()
222 void LightApp_Module::onModelClosed()
226 /*!Gets popup manager.(create if not exist)*/
227 QtxPopupMgr* LightApp_Module::popupMgr()
231 myPopupMgr = new QtxPopupMgr( 0, this );
234 SUIT_Desktop* d = application()->desktop();
237 *disp = createAction( -1, tr( "TOP_DISPLAY" ), p, tr( "MEN_DISPLAY" ), tr( "STB_DISPLAY" ),
238 0, d, false, this, SLOT( onShowHide() ) ),
239 *erase = createAction( -1, tr( "TOP_ERASE" ), p, tr( "MEN_ERASE" ), tr( "STB_ERASE" ),
240 0, d, false, this, SLOT( onShowHide() ) ),
241 *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
242 0, d, false, this, SLOT( onShowHide() ) ),
243 *eraseAll = createAction( -1, tr( "TOP_ERASE_ALL" ), p, tr( "MEN_ERASE_ALL" ), tr( "STB_ERASE_ALL" ),
244 0, d, false, this, SLOT( onShowHide() ) );
245 myDisplay = actionId( disp );
246 myErase = actionId( erase );
247 myDisplayOnly = actionId( dispOnly );
248 myEraseAll = actionId( eraseAll );
250 myPopupMgr->insert( disp, -1, 0 );
251 myPopupMgr->insert( erase, -1, 0 );
252 myPopupMgr->insert( dispOnly, -1, 0 );
253 myPopupMgr->insert( eraseAll, -1, 0 );
254 myPopupMgr->insert( separator(), -1, 0 );
256 QString uniform = "( count( $component ) = 1 ) and ( component != activeModule ) and ( activeModule = '%1' )";
257 uniform = uniform.arg( name() );
258 myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, true );
259 myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, true );
260 myPopupMgr->setRule( dispOnly, uniform, true );
261 QString viewers = "{ '%1' '%2' '%3' }";
262 viewers = viewers.arg( SOCC_Viewer::Type() ).arg( SVTK_Viewer::Type() ).arg( SPlot2d_Viewer::Type() );
263 myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( viewers ), true );
268 /*!Gets preferences.*/
269 LightApp_Preferences* LightApp_Module::preferences() const
271 LightApp_Preferences* pref = 0;
273 pref = getApp()->preferences();
277 /*!Add preference to preferences.*/
278 int LightApp_Module::addPreference( const QString& label )
280 LightApp_Preferences* pref = preferences();
284 int catId = pref->addPreference( moduleName(), -1 );
288 return pref->addPreference( label, catId );
291 /*!Add preference to preferences.*/
292 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
293 const QString& section, const QString& param )
295 LightApp_Preferences* pref = preferences();
299 return pref->addPreference( moduleName(), label, pId, type, section, param );
302 /*!Gets property of preferences.*/
303 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
306 LightApp_Preferences* pref = preferences();
308 var = pref->itemProperty( id, prop );
312 /*!Set property of preferences.*/
313 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
315 LightApp_Preferences* pref = preferences();
317 pref->setItemProperty( id, prop, var );
321 * \brief Starts operation with given identifier
322 * \param id - identifier of operation to be started
324 * Module stores operations in map. This method starts operation by id.
325 * If operation isn't in map, then it will be created by createOperation method
326 * and will be inserted to map
328 void LightApp_Module::startOperation( const int id )
330 LightApp_Operation* op = 0;
331 if( myOperations.contains( id ) )
332 op = myOperations[ id ];
335 op = createOperation( id );
338 myOperations.insert( id, op );
339 op->setModule( this );
340 connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
341 connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
347 // be sure that operation has correct study pointer
348 op->setStudy( application() ? application()->activeStudy() : 0 );
354 * \brief Creates operation with given identifier
355 * \param id - identifier of operation to be started
356 * \return Pointer on created operation or NULL if operation is not created
358 * Creates operation with given id. You should not call this method, it will be called
359 * automatically from startOperation. You may redefine this method in concrete module to
362 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
368 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
369 else if( id==myErase )
370 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
371 else if( id==myDisplayOnly )
372 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
373 else if( id==myEraseAll )
374 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
380 * \brief Virtual protected slot called when operation stopped
381 * \param theOp - stopped operation
383 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
384 * perform actions after stopping operation
386 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
391 * \brief Virtual protected slot called when operation destroyed
392 * \param theOp - destroyed operation
394 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
395 * perform actions after destroying operation. Base implementation removes pointer on
396 * destroyed operation from the map of operations
398 void LightApp_Module::onOperationDestroyed()
400 const QObject* s = sender();
401 if( s && s->inherits( "LightApp_Operation" ) )
403 const LightApp_Operation* op = ( LightApp_Operation* )s;
404 MapOfOperation::const_iterator anIt = myOperations.begin(),
405 aLast = myOperations.end();
406 for( ; anIt!=aLast; anIt++ )
407 if( anIt.data()==op )
409 myOperations.remove( anIt.key() );
415 LightApp_Displayer* LightApp_Module::displayer()
420 void LightApp_Module::onShowHide()
422 if( !sender()->inherits( "QAction" ) || !popupMgr() )
425 QAction* act = ( QAction* )sender();
426 int id = actionId( act );
428 startOperation( id );