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_DataObject.h"
13 #include "LightApp_Study.h"
14 #include "LightApp_Preferences.h"
15 #include "LightApp_Selection.h"
16 #include "LightApp_Operation.h"
17 #include "LightApp_SwitchOp.h"
18 #include "LightApp_UpdateFlags.h"
19 #include "LightApp_ShowHideOp.h"
21 #include "SUIT_Operation.h"
22 #include <SUIT_Study.h>
23 #include <SUIT_DataObject.h>
24 #include <SUIT_ResourceMgr.h>
26 #include <SVTK_ViewWindow.h>
27 #include <SVTK_ViewModel.h>
28 #include <OCCViewer_ViewWindow.h>
29 #include <OCCViewer_ViewPort3d.h>
30 #include <SOCC_ViewModel.h>
31 #include <GLViewer_ViewFrame.h>
32 #include <GLViewer_ViewPort.h>
33 #include <Plot2d_ViewWindow.h>
34 #include <Plot2d_ViewFrame.h>
35 #include <SPlot2d_ViewModel.h>
37 #include <OB_Browser.h>
39 #include <QtxPopupMgr.h>
43 #include <qstringlist.h>
46 LightApp_Module::LightApp_Module( const QString& name )
58 LightApp_Module::~LightApp_Module()
64 /*!Initialize module.*/
65 void LightApp_Module::initialize( CAM_Application* app )
67 CAM_Module::initialize( app );
69 SUIT_ResourceMgr* resMgr = app ? app->resourceMgr() : 0;
71 resMgr->raiseTranslators( name() );
75 void LightApp_Module::windows( QMap<int, int>& ) const
80 void LightApp_Module::viewManagers( QStringList& ) const
84 /*!Context menu popup.*/
85 void LightApp_Module::contextMenuPopup( const QString& client, QPopupMenu* menu, QString& /*title*/ )
87 LightApp_Selection* sel = createSelection();
88 sel->init( client, getApp()->selectionMgr() );
89 popupMgr()->updatePopup( menu, sel );
93 /*!Update object browser.
94 * For updating model or whole object browser use update() method can be used.
96 void LightApp_Module::updateObjBrowser( bool theIsUpdateDataModel,
97 SUIT_DataObject* theDataObject )
99 SUIT_DataObject* aDataObject = theDataObject;
100 if( theIsUpdateDataModel ){
101 if( CAM_DataModel* aDataModel = dataModel() ){
102 if ( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) ) {
103 SUIT_DataObject* aParent = NULL;
104 if(theDataObject && theDataObject != aDataModel->root())
105 aParent = theDataObject->parent();
107 LightApp_DataObject* anObject = dynamic_cast<LightApp_DataObject*>(theDataObject);
108 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(getApp()->activeStudy());
109 aModel->update( anObject, aStudy );
111 if(aParent && aParent->childPos(anObject) < 0)
112 aDataObject = dynamic_cast<LightApp_DataObject*>(aParent);
116 getApp()->objectBrowser()->updateTree( aDataObject );
120 void LightApp_Module::selectionChanged()
124 /*!Activate module.*/
125 bool LightApp_Module::activateModule( SUIT_Study* study )
127 bool res = CAM_Module::activateModule( study );
129 if ( res && application() && application()->resourceMgr() )
130 application()->resourceMgr()->raiseTranslators( name() );
132 if ( mySwitchOp == 0 )
133 mySwitchOp = new LightApp_SwitchOp( this );
138 /*!Deactivate module.*/
139 bool LightApp_Module::deactivateModule( SUIT_Study* study )
144 // abort all operations
145 MapOfOperation::const_iterator anIt;
146 for( anIt = myOperations.begin(); anIt != myOperations.end(); anIt++ ) {
147 anIt.data()->abort();
150 return CAM_Module::activateModule( study );
154 void LightApp_Module::MenuItem()
159 void LightApp_Module::createPreferences()
164 void LightApp_Module::preferencesChanged( const QString&, const QString& )
168 /*!Gets application.*/
169 LightApp_Application* LightApp_Module::getApp() const
171 return (LightApp_Application*)application();
175 * \brief Update something in accordance with update flags
176 * \param theFlags - update flags
178 * Update viewer or/and object browser etc. in accordance with update flags ( see
179 * LightApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
182 void LightApp_Module::update( const int theFlags )
184 if ( theFlags & UF_Model )
186 if( CAM_DataModel* aDataModel = dataModel() )
187 if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
188 aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
190 if ( theFlags & UF_ObjBrowser )
191 getApp()->objectBrowser()->updateTree( 0 );
192 if ( theFlags & UF_Controls )
194 if ( theFlags & UF_Viewer )
196 if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
197 if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
199 if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
200 ( (SVTK_ViewWindow*)viewWnd )->Repaint();
201 else if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
202 ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
203 else if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
204 ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
205 else if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
206 ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
211 * \brief Updates controls
213 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
214 * called from update( UF_Controls ). You may redefine it in concrete module.
216 void LightApp_Module::updateControls()
220 /*!Create new instance of data model and return it.*/
221 CAM_DataModel* LightApp_Module::createDataModel()
223 return new LightApp_DataModel(this);
226 /*!Create and return instance of LightApp_Selection.*/
227 LightApp_Selection* LightApp_Module::createSelection() const
229 return new LightApp_Selection();
233 void LightApp_Module::onModelOpened()
238 void LightApp_Module::onModelSaved()
243 void LightApp_Module::onModelClosed()
247 /*!Gets popup manager.(create if not exist)*/
248 QtxPopupMgr* LightApp_Module::popupMgr()
252 myPopupMgr = new QtxPopupMgr( 0, this );
255 SUIT_Desktop* d = application()->desktop();
258 *disp = createAction( -1, tr( "TOP_DISPLAY" ), p, tr( "MEN_DISPLAY" ), tr( "STB_DISPLAY" ),
259 0, d, false, this, SLOT( onShowHide() ) ),
260 *erase = createAction( -1, tr( "TOP_ERASE" ), p, tr( "MEN_ERASE" ), tr( "STB_ERASE" ),
261 0, d, false, this, SLOT( onShowHide() ) ),
262 *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
263 0, d, false, this, SLOT( onShowHide() ) ),
264 *eraseAll = createAction( -1, tr( "TOP_ERASE_ALL" ), p, tr( "MEN_ERASE_ALL" ), tr( "STB_ERASE_ALL" ),
265 0, d, false, this, SLOT( onShowHide() ) );
266 myDisplay = actionId( disp );
267 myErase = actionId( erase );
268 myDisplayOnly = actionId( dispOnly );
269 myEraseAll = actionId( eraseAll );
271 myPopupMgr->insert( disp, -1, 0 );
272 myPopupMgr->insert( erase, -1, 0 );
273 myPopupMgr->insert( dispOnly, -1, 0 );
274 myPopupMgr->insert( eraseAll, -1, 0 );
275 myPopupMgr->insert( separator(), -1, 0 );
277 QString oneAndNotActive = "( count( $component ) = 1 ) and ( component != activeModule )";
278 QString uniform = "true in $canBeDisplayed and %1 and ( activeModule = '%2' )";
279 uniform = uniform.arg( oneAndNotActive ).arg( name() );
280 myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, true );
281 myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, true );
282 myPopupMgr->setRule( dispOnly, uniform, true );
283 QString viewers = "{ '%1' '%2' '%3' }";
284 viewers = viewers.arg( SOCC_Viewer::Type() ).arg( SVTK_Viewer::Type() ).arg( SPlot2d_Viewer::Type() );
285 myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( viewers ), true );
290 /*!Gets preferences.*/
291 LightApp_Preferences* LightApp_Module::preferences() const
293 LightApp_Preferences* pref = 0;
295 pref = getApp()->preferences();
299 /*!Add preference to preferences.*/
300 int LightApp_Module::addPreference( const QString& label )
302 LightApp_Preferences* pref = preferences();
306 int catId = pref->addPreference( moduleName(), -1 );
310 return pref->addPreference( label, catId );
313 /*!Add preference to preferences.*/
314 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
315 const QString& section, const QString& param )
317 LightApp_Preferences* pref = preferences();
321 return pref->addPreference( moduleName(), label, pId, type, section, param );
324 /*!Gets property of preferences.*/
325 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
328 LightApp_Preferences* pref = preferences();
330 var = pref->itemProperty( id, prop );
334 /*!Set property of preferences.*/
335 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
337 LightApp_Preferences* pref = preferences();
339 pref->setItemProperty( id, prop, var );
343 * \brief Starts operation with given identifier
344 * \param id - identifier of operation to be started
346 * Module stores operations in map. This method starts operation by id.
347 * If operation isn't in map, then it will be created by createOperation method
348 * and will be inserted to map
350 void LightApp_Module::startOperation( const int id )
352 LightApp_Operation* op = 0;
353 if( myOperations.contains( id ) )
354 op = myOperations[ id ];
357 op = createOperation( id );
360 myOperations.insert( id, op );
361 op->setModule( this );
362 connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
363 connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
369 // be sure that operation has correct study pointer
370 op->setStudy( application() ? application()->activeStudy() : 0 );
376 * \brief Creates operation with given identifier
377 * \param id - identifier of operation to be started
378 * \return Pointer on created operation or NULL if operation is not created
380 * Creates operation with given id. You should not call this method, it will be called
381 * automatically from startOperation. You may redefine this method in concrete module to
384 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
390 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
391 else if( id==myErase )
392 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
393 else if( id==myDisplayOnly )
394 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
395 else if( id==myEraseAll )
396 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
402 * \brief Virtual protected slot called when operation stopped
403 * \param theOp - stopped operation
405 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
406 * perform actions after stopping operation
408 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
413 * \brief Virtual protected slot called when operation destroyed
414 * \param theOp - destroyed operation
416 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
417 * perform actions after destroying operation. Base implementation removes pointer on
418 * destroyed operation from the map of operations
420 void LightApp_Module::onOperationDestroyed()
422 const QObject* s = sender();
423 if( s && s->inherits( "LightApp_Operation" ) )
425 const LightApp_Operation* op = ( LightApp_Operation* )s;
426 MapOfOperation::const_iterator anIt = myOperations.begin(),
427 aLast = myOperations.end();
428 for( ; anIt!=aLast; anIt++ )
429 if( anIt.data()==op )
431 myOperations.remove( anIt.key() );
437 LightApp_Displayer* LightApp_Module::displayer()
442 void LightApp_Module::onShowHide()
444 if( !sender()->inherits( "QAction" ) || !popupMgr() )
447 QAction* act = ( QAction* )sender();
448 int id = actionId( act );
450 startOperation( id );