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 #ifndef DISABLE_VTKVIEWER
27 #ifndef DISABLE_SALOMEOBJECT
28 #include <SVTK_ViewWindow.h>
29 #include <SVTK_ViewModel.h>
31 #include <VTKViewer_ViewWindow.h>
33 #include <VTKViewer_ViewModel.h>
35 #ifndef DISABLE_OCCVIEWER
36 #include <OCCViewer_ViewWindow.h>
37 #include <OCCViewer_ViewPort3d.h>
38 #ifndef DISABLE_SALOMEOBJECT
39 #include <SOCC_ViewModel.h>
41 #include <OCCViewer_ViewModel.h>
44 #ifndef DISABLE_GLVIEWER
45 #include <GLViewer_ViewFrame.h>
46 #include <GLViewer_ViewPort.h>
48 #ifndef DISABLE_PLOT2DVIEWER
49 #include <Plot2d_ViewWindow.h>
50 #include <Plot2d_ViewFrame.h>
51 #ifndef DISABLE_SALOMEOBJECT
52 #include <SPlot2d_ViewModel.h>
54 #include <Plot2d_ViewModel.h>
58 #include <OB_Browser.h>
60 #include <QtxPopupMgr.h>
64 #include <qstringlist.h>
67 LightApp_Module::LightApp_Module( const QString& name )
79 LightApp_Module::~LightApp_Module()
85 /*!Initialize module.*/
86 void LightApp_Module::initialize( CAM_Application* app )
88 CAM_Module::initialize( app );
90 SUIT_ResourceMgr* resMgr = app ? app->resourceMgr() : 0;
92 resMgr->raiseTranslators( name() );
96 void LightApp_Module::windows( QMap<int, int>& ) const
101 void LightApp_Module::viewManagers( QStringList& ) const
105 /*!Context menu popup.*/
106 void LightApp_Module::contextMenuPopup( const QString& client, QPopupMenu* menu, QString& /*title*/ )
108 LightApp_Selection* sel = createSelection();
109 sel->init( client, getApp()->selectionMgr() );
110 popupMgr()->updatePopup( menu, sel );
114 /*!Update object browser.
115 * For updating model or whole object browser use update() method can be used.
117 void LightApp_Module::updateObjBrowser( bool theIsUpdateDataModel,
118 SUIT_DataObject* theDataObject )
120 SUIT_DataObject* aDataObject = theDataObject;
121 if( theIsUpdateDataModel ){
122 if( CAM_DataModel* aDataModel = dataModel() ){
123 if ( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) ) {
124 SUIT_DataObject* aParent = NULL;
125 if(theDataObject && theDataObject != aDataModel->root())
126 aParent = theDataObject->parent();
128 LightApp_DataObject* anObject = dynamic_cast<LightApp_DataObject*>(theDataObject);
129 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(getApp()->activeStudy());
130 aModel->update( anObject, aStudy );
132 if(aParent && aParent->childPos(anObject) < 0)
133 aDataObject = dynamic_cast<LightApp_DataObject*>(aParent);
137 getApp()->objectBrowser()->updateTree( aDataObject );
141 void LightApp_Module::selectionChanged()
145 /*!Activate module.*/
146 bool LightApp_Module::activateModule( SUIT_Study* study )
148 bool res = CAM_Module::activateModule( study );
150 if ( res && application() && application()->resourceMgr() )
151 application()->resourceMgr()->raiseTranslators( name() );
153 connect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
154 this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
155 connect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
156 this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
158 if ( mySwitchOp == 0 )
159 mySwitchOp = new LightApp_SwitchOp( this );
164 /*!Deactivate module.*/
165 bool LightApp_Module::deactivateModule( SUIT_Study* study )
170 disconnect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
171 this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
172 disconnect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
173 this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
175 // abort all operations
176 MapOfOperation::const_iterator anIt;
177 for( anIt = myOperations.begin(); anIt != myOperations.end(); anIt++ ) {
178 anIt.data()->abort();
181 return CAM_Module::activateModule( study );
185 void LightApp_Module::MenuItem()
190 void LightApp_Module::createPreferences()
195 void LightApp_Module::preferencesChanged( const QString&, const QString& )
199 /*!Gets application.*/
200 LightApp_Application* LightApp_Module::getApp() const
202 return (LightApp_Application*)application();
206 * \brief Update something in accordance with update flags
207 * \param theFlags - update flags
209 * Update viewer or/and object browser etc. in accordance with update flags ( see
210 * LightApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
213 void LightApp_Module::update( const int theFlags )
215 if ( theFlags & UF_Model )
217 if( CAM_DataModel* aDataModel = dataModel() )
218 if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
219 aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
221 if ( theFlags & UF_ObjBrowser )
222 getApp()->objectBrowser()->updateTree( 0 );
223 if ( theFlags & UF_Controls )
225 if ( theFlags & UF_Viewer )
227 if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
228 if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
230 #ifndef DISABLE_VTKVIEWER
231 #ifndef DISABLE_SALOMEOBJECT
232 if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
233 ( (SVTK_ViewWindow*)viewWnd )->Repaint();
235 if ( viewWnd->inherits( "VTKViewer_ViewWindow" ) )
236 ( (VTKViewer_ViewWindow*)viewWnd )->Repaint();
239 #ifndef DISABLE_OCCVIEWER
240 if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
241 ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
243 #ifndef DISABLE_PLOT2DVIEWER
244 if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
245 ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
247 #ifndef DISABLE_GLVIEWER
248 if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
249 ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
255 * \brief Updates controls
257 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
258 * called from update( UF_Controls ). You may redefine it in concrete module.
260 void LightApp_Module::updateControls()
264 /*!Create new instance of data model and return it.*/
265 CAM_DataModel* LightApp_Module::createDataModel()
267 return new LightApp_DataModel(this);
270 /*!Create and return instance of LightApp_Selection.*/
271 LightApp_Selection* LightApp_Module::createSelection() const
273 return new LightApp_Selection();
277 void LightApp_Module::onModelOpened()
282 void LightApp_Module::onModelSaved()
287 void LightApp_Module::onModelClosed()
291 /*!Gets popup manager.(create if not exist)*/
292 QtxPopupMgr* LightApp_Module::popupMgr()
296 myPopupMgr = new QtxPopupMgr( 0, this );
299 SUIT_Desktop* d = application()->desktop();
302 *disp = createAction( -1, tr( "TOP_DISPLAY" ), p, tr( "MEN_DISPLAY" ), tr( "STB_DISPLAY" ),
303 0, d, false, this, SLOT( onShowHide() ) ),
304 *erase = createAction( -1, tr( "TOP_ERASE" ), p, tr( "MEN_ERASE" ), tr( "STB_ERASE" ),
305 0, d, false, this, SLOT( onShowHide() ) ),
306 *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
307 0, d, false, this, SLOT( onShowHide() ) ),
308 *eraseAll = createAction( -1, tr( "TOP_ERASE_ALL" ), p, tr( "MEN_ERASE_ALL" ), tr( "STB_ERASE_ALL" ),
309 0, d, false, this, SLOT( onShowHide() ) );
310 myDisplay = actionId( disp );
311 myErase = actionId( erase );
312 myDisplayOnly = actionId( dispOnly );
313 myEraseAll = actionId( eraseAll );
315 myPopupMgr->insert( disp, -1, 0 );
316 myPopupMgr->insert( erase, -1, 0 );
317 myPopupMgr->insert( dispOnly, -1, 0 );
318 myPopupMgr->insert( eraseAll, -1, 0 );
319 myPopupMgr->insert( separator(), -1, 0 );
321 QString oneAndNotActive = "( count( $component ) = 1 ) and ( component != activeModule )";
322 QString uniform = "true in $canBeDisplayed and %1 and ( activeModule = '%2' )";
323 uniform = uniform.arg( oneAndNotActive ).arg( name() );
324 myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, true );
325 myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, true );
326 myPopupMgr->setRule( dispOnly, uniform, true );
330 #ifndef DISABLE_OCCVIEWER
331 #ifndef DISABLE_SALOMEOBJECT
332 viewers.append( SOCC_Viewer::Type() );
334 viewers.append( OCCViewer_Viewer::Type() );
337 #ifndef DISABLE_VTKVIEWER
338 #ifndef DISABLE_SALOMEOBJECT
339 viewers.append( SVTK_Viewer::Type() );
341 viewers.append( VTKViewer_Viewer::Type() );
344 #ifndef DISABLE_PLOT2DVIEWER
345 #ifndef DISABLE_SALOMEOBJECT
346 viewers.append( SPlot2d_Viewer::Type() );
348 viewers.append( Plot2d_Viewer::Type() );
352 if( !viewers.isEmpty() )
354 QString strViewers = "{ ", temp = "'%1' ";
355 QStringList::const_iterator anIt = viewers.begin(), aLast = viewers.end();
356 for( ; anIt!=aLast; anIt++ )
357 strViewers+=temp.arg( *anIt );
359 myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( strViewers ), true );
365 /*!Gets preferences.*/
366 LightApp_Preferences* LightApp_Module::preferences() const
368 LightApp_Preferences* pref = 0;
370 pref = getApp()->preferences();
374 /*!Add preference to preferences.*/
375 int LightApp_Module::addPreference( const QString& label )
377 LightApp_Preferences* pref = preferences();
381 int catId = pref->addPreference( moduleName(), -1 );
385 return pref->addPreference( label, catId );
388 /*!Add preference to preferences.*/
389 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
390 const QString& section, const QString& param )
392 LightApp_Preferences* pref = preferences();
396 return pref->addPreference( moduleName(), label, pId, type, section, param );
399 /*!Gets property of preferences.*/
400 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
403 LightApp_Preferences* pref = preferences();
405 var = pref->itemProperty( id, prop );
409 /*!Set property of preferences.*/
410 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
412 LightApp_Preferences* pref = preferences();
414 pref->setItemProperty( id, prop, var );
418 * \brief Starts operation with given identifier
419 * \param id - identifier of operation to be started
421 * Module stores operations in map. This method starts operation by id.
422 * If operation isn't in map, then it will be created by createOperation method
423 * and will be inserted to map
425 void LightApp_Module::startOperation( const int id )
427 LightApp_Operation* op = 0;
428 if( myOperations.contains( id ) )
429 op = myOperations[ id ];
432 op = createOperation( id );
435 myOperations.insert( id, op );
436 op->setModule( this );
437 connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
438 connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
444 // be sure that operation has correct study pointer
445 op->setStudy( application() ? application()->activeStudy() : 0 );
451 * \brief Creates operation with given identifier
452 * \param id - identifier of operation to be started
453 * \return Pointer on created operation or NULL if operation is not created
455 * Creates operation with given id. You should not call this method, it will be called
456 * automatically from startOperation. You may redefine this method in concrete module to
459 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
465 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
466 else if( id==myErase )
467 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
468 else if( id==myDisplayOnly )
469 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
470 else if( id==myEraseAll )
471 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
477 * \brief Virtual protected slot called when operation stopped
478 * \param theOp - stopped operation
480 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
481 * perform actions after stopping operation
483 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
488 * \brief Virtual protected slot called when operation destroyed
489 * \param theOp - destroyed operation
491 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
492 * perform actions after destroying operation. Base implementation removes pointer on
493 * destroyed operation from the map of operations
495 void LightApp_Module::onOperationDestroyed()
497 const QObject* s = sender();
498 if( s && s->inherits( "LightApp_Operation" ) )
500 const LightApp_Operation* op = ( LightApp_Operation* )s;
501 MapOfOperation::const_iterator anIt = myOperations.begin(),
502 aLast = myOperations.end();
503 for( ; anIt!=aLast; anIt++ )
504 if( anIt.data()==op )
506 myOperations.remove( anIt.key() );
512 LightApp_Displayer* LightApp_Module::displayer()
517 void LightApp_Module::onShowHide()
519 if( !sender()->inherits( "QAction" ) || !popupMgr() )
522 QAction* act = ( QAction* )sender();
523 int id = actionId( act );
525 startOperation( id );
528 void LightApp_Module::onViewManagerAdded( SUIT_ViewManager* )
532 void LightApp_Module::onViewManagerRemoved( SUIT_ViewManager* )