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* study )
129 // abort all operations
130 MapOfOperation::const_iterator anIt;
131 for( anIt = myOperations.begin(); anIt != myOperations.end(); anIt++ ) {
132 anIt.data()->abort();
135 return CAM_Module::activateModule( study );
139 void LightApp_Module::MenuItem()
144 void LightApp_Module::createPreferences()
149 void LightApp_Module::preferencesChanged( const QString&, const QString& )
153 /*!Gets application.*/
154 LightApp_Application* LightApp_Module::getApp() const
156 return (LightApp_Application*)application();
160 * \brief Update something in accordance with update flags
161 * \param theFlags - update flags
163 * Update viewer or/and object browser etc. in accordance with update flags ( see
164 * LightApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
167 void LightApp_Module::update( const int theFlags )
169 if ( theFlags & UF_Model )
171 if( CAM_DataModel* aDataModel = dataModel() )
172 if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
173 aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
175 if ( theFlags & UF_ObjBrowser )
176 getApp()->objectBrowser()->updateTree( 0 );
177 if ( theFlags & UF_Controls )
179 if ( theFlags & UF_Viewer )
181 if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
182 if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
184 if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
185 ( (SVTK_ViewWindow*)viewWnd )->Repaint();
186 else if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
187 ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
188 else if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
189 ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
190 else if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
191 ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
196 * \brief Updates controls
198 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
199 * called from update( UF_Controls ). You may redefine it in concrete module.
201 void LightApp_Module::updateControls()
205 /*!Create new instance of data model and return it.*/
206 CAM_DataModel* LightApp_Module::createDataModel()
208 return new LightApp_DataModel(this);
211 /*!Create and return instance of LightApp_Selection.*/
212 LightApp_Selection* LightApp_Module::createSelection() const
214 return new LightApp_Selection();
218 void LightApp_Module::onModelOpened()
223 void LightApp_Module::onModelSaved()
228 void LightApp_Module::onModelClosed()
232 /*!Gets popup manager.(create if not exist)*/
233 QtxPopupMgr* LightApp_Module::popupMgr()
237 myPopupMgr = new QtxPopupMgr( 0, this );
240 SUIT_Desktop* d = application()->desktop();
243 *disp = createAction( -1, tr( "TOP_DISPLAY" ), p, tr( "MEN_DISPLAY" ), tr( "STB_DISPLAY" ),
244 0, d, false, this, SLOT( onShowHide() ) ),
245 *erase = createAction( -1, tr( "TOP_ERASE" ), p, tr( "MEN_ERASE" ), tr( "STB_ERASE" ),
246 0, d, false, this, SLOT( onShowHide() ) ),
247 *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
248 0, d, false, this, SLOT( onShowHide() ) ),
249 *eraseAll = createAction( -1, tr( "TOP_ERASE_ALL" ), p, tr( "MEN_ERASE_ALL" ), tr( "STB_ERASE_ALL" ),
250 0, d, false, this, SLOT( onShowHide() ) );
251 myDisplay = actionId( disp );
252 myErase = actionId( erase );
253 myDisplayOnly = actionId( dispOnly );
254 myEraseAll = actionId( eraseAll );
256 myPopupMgr->insert( disp, -1, 0 );
257 myPopupMgr->insert( erase, -1, 0 );
258 myPopupMgr->insert( dispOnly, -1, 0 );
259 myPopupMgr->insert( eraseAll, -1, 0 );
260 myPopupMgr->insert( separator(), -1, 0 );
262 QString oneAndNotActive = "( count( $component ) = 1 ) and ( component != activeModule )";
263 QString uniform = "true in $canBeDisplayed and %1 and ( activeModule = '%2' )";
264 uniform = uniform.arg( oneAndNotActive ).arg( name() );
265 myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, true );
266 myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, true );
267 myPopupMgr->setRule( dispOnly, uniform, true );
268 QString viewers = "{ '%1' '%2' '%3' }";
269 viewers = viewers.arg( SOCC_Viewer::Type() ).arg( SVTK_Viewer::Type() ).arg( SPlot2d_Viewer::Type() );
270 myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( viewers ), true );
275 /*!Gets preferences.*/
276 LightApp_Preferences* LightApp_Module::preferences() const
278 LightApp_Preferences* pref = 0;
280 pref = getApp()->preferences();
284 /*!Add preference to preferences.*/
285 int LightApp_Module::addPreference( const QString& label )
287 LightApp_Preferences* pref = preferences();
291 int catId = pref->addPreference( moduleName(), -1 );
295 return pref->addPreference( label, catId );
298 /*!Add preference to preferences.*/
299 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
300 const QString& section, const QString& param )
302 LightApp_Preferences* pref = preferences();
306 return pref->addPreference( moduleName(), label, pId, type, section, param );
309 /*!Gets property of preferences.*/
310 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
313 LightApp_Preferences* pref = preferences();
315 var = pref->itemProperty( id, prop );
319 /*!Set property of preferences.*/
320 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
322 LightApp_Preferences* pref = preferences();
324 pref->setItemProperty( id, prop, var );
328 * \brief Starts operation with given identifier
329 * \param id - identifier of operation to be started
331 * Module stores operations in map. This method starts operation by id.
332 * If operation isn't in map, then it will be created by createOperation method
333 * and will be inserted to map
335 void LightApp_Module::startOperation( const int id )
337 LightApp_Operation* op = 0;
338 if( myOperations.contains( id ) )
339 op = myOperations[ id ];
342 op = createOperation( id );
345 myOperations.insert( id, op );
346 op->setModule( this );
347 connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
348 connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
354 // be sure that operation has correct study pointer
355 op->setStudy( application() ? application()->activeStudy() : 0 );
361 * \brief Creates operation with given identifier
362 * \param id - identifier of operation to be started
363 * \return Pointer on created operation or NULL if operation is not created
365 * Creates operation with given id. You should not call this method, it will be called
366 * automatically from startOperation. You may redefine this method in concrete module to
369 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
375 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
376 else if( id==myErase )
377 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
378 else if( id==myDisplayOnly )
379 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
380 else if( id==myEraseAll )
381 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
387 * \brief Virtual protected slot called when operation stopped
388 * \param theOp - stopped operation
390 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
391 * perform actions after stopping operation
393 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
398 * \brief Virtual protected slot called when operation destroyed
399 * \param theOp - destroyed operation
401 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
402 * perform actions after destroying operation. Base implementation removes pointer on
403 * destroyed operation from the map of operations
405 void LightApp_Module::onOperationDestroyed()
407 const QObject* s = sender();
408 if( s && s->inherits( "LightApp_Operation" ) )
410 const LightApp_Operation* op = ( LightApp_Operation* )s;
411 MapOfOperation::const_iterator anIt = myOperations.begin(),
412 aLast = myOperations.end();
413 for( ; anIt!=aLast; anIt++ )
414 if( anIt.data()==op )
416 myOperations.remove( anIt.key() );
422 LightApp_Displayer* LightApp_Module::displayer()
427 void LightApp_Module::onShowHide()
429 if( !sender()->inherits( "QAction" ) || !popupMgr() )
432 QAction* act = ( QAction* )sender();
433 int id = actionId( act );
435 startOperation( id );