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 bool upd = getApp()->objectBrowser()->isAutoUpdate();
121 getApp()->objectBrowser()->setAutoUpdate( false );
123 SUIT_DataObject* aDataObject = theDataObject;
124 if( theIsUpdateDataModel ){
125 if( CAM_DataModel* aDataModel = dataModel() ){
126 if ( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) ) {
127 SUIT_DataObject* aParent = NULL;
128 if(theDataObject && theDataObject != aDataModel->root())
129 aParent = theDataObject->parent();
131 LightApp_DataObject* anObject = dynamic_cast<LightApp_DataObject*>(theDataObject);
132 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(getApp()->activeStudy());
133 aModel->update( anObject, aStudy );
135 if(aParent && aParent->childPos(anObject) < 0)
136 aDataObject = dynamic_cast<LightApp_DataObject*>(aParent);
140 getApp()->objectBrowser()->setAutoUpdate( upd );
141 getApp()->objectBrowser()->updateTree( 0, false /*aDataObject*/ );
145 void LightApp_Module::selectionChanged()
149 /*!Activate module.*/
150 bool LightApp_Module::activateModule( SUIT_Study* study )
152 bool res = CAM_Module::activateModule( study );
154 if ( res && application() && application()->resourceMgr() )
155 application()->resourceMgr()->raiseTranslators( name() );
157 connect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
158 this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
159 connect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
160 this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
162 if ( mySwitchOp == 0 )
163 mySwitchOp = new LightApp_SwitchOp( this );
168 /*!Deactivate module.*/
169 bool LightApp_Module::deactivateModule( SUIT_Study* study )
174 disconnect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
175 this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
176 disconnect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
177 this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
179 // abort all operations
180 MapOfOperation::const_iterator anIt;
181 for( anIt = myOperations.begin(); anIt != myOperations.end(); anIt++ ) {
182 anIt.data()->abort();
185 return CAM_Module::activateModule( study );
189 void LightApp_Module::MenuItem()
194 void LightApp_Module::createPreferences()
199 void LightApp_Module::preferencesChanged( const QString&, const QString& )
203 /*!Gets application.*/
204 LightApp_Application* LightApp_Module::getApp() const
206 return (LightApp_Application*)application();
210 * \brief Update something in accordance with update flags
211 * \param theFlags - update flags
213 * Update viewer or/and object browser etc. in accordance with update flags ( see
214 * LightApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
217 void LightApp_Module::update( const int theFlags )
219 if ( theFlags & UF_Model )
221 if( CAM_DataModel* aDataModel = dataModel() )
222 if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
223 aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
225 if ( theFlags & UF_ObjBrowser )
226 getApp()->objectBrowser()->updateTree( 0 );
227 if ( theFlags & UF_Controls )
229 if ( theFlags & UF_Viewer )
231 if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
232 if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
234 #ifndef DISABLE_VTKVIEWER
235 #ifndef DISABLE_SALOMEOBJECT
236 if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
237 ( (SVTK_ViewWindow*)viewWnd )->Repaint();
239 if ( viewWnd->inherits( "VTKViewer_ViewWindow" ) )
240 ( (VTKViewer_ViewWindow*)viewWnd )->Repaint();
243 #ifndef DISABLE_OCCVIEWER
244 if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
245 ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
247 #ifndef DISABLE_PLOT2DVIEWER
248 if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
249 ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
251 #ifndef DISABLE_GLVIEWER
252 if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
253 ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
259 * \brief Updates controls
261 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
262 * called from update( UF_Controls ). You may redefine it in concrete module.
264 void LightApp_Module::updateControls()
268 /*!Create new instance of data model and return it.*/
269 CAM_DataModel* LightApp_Module::createDataModel()
271 return new LightApp_DataModel(this);
274 /*!Create and return instance of LightApp_Selection.*/
275 LightApp_Selection* LightApp_Module::createSelection() const
277 return new LightApp_Selection();
281 void LightApp_Module::onModelOpened()
286 void LightApp_Module::onModelSaved()
291 void LightApp_Module::onModelClosed()
295 /*!Gets popup manager.(create if not exist)*/
296 QtxPopupMgr* LightApp_Module::popupMgr()
300 myPopupMgr = new QtxPopupMgr( 0, this );
303 SUIT_Desktop* d = application()->desktop();
306 *disp = createAction( -1, tr( "TOP_DISPLAY" ), p, tr( "MEN_DISPLAY" ), tr( "STB_DISPLAY" ),
307 0, d, false, this, SLOT( onShowHide() ) ),
308 *erase = createAction( -1, tr( "TOP_ERASE" ), p, tr( "MEN_ERASE" ), tr( "STB_ERASE" ),
309 0, d, false, this, SLOT( onShowHide() ) ),
310 *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
311 0, d, false, this, SLOT( onShowHide() ) ),
312 *eraseAll = createAction( -1, tr( "TOP_ERASE_ALL" ), p, tr( "MEN_ERASE_ALL" ), tr( "STB_ERASE_ALL" ),
313 0, d, false, this, SLOT( onShowHide() ) );
314 myDisplay = actionId( disp );
315 myErase = actionId( erase );
316 myDisplayOnly = actionId( dispOnly );
317 myEraseAll = actionId( eraseAll );
319 myPopupMgr->insert( disp, -1, 0 );
320 myPopupMgr->insert( erase, -1, 0 );
321 myPopupMgr->insert( dispOnly, -1, 0 );
322 myPopupMgr->insert( eraseAll, -1, 0 );
323 myPopupMgr->insert( separator(), -1, 0 );
325 QString oneAndNotActive = "( count( $component ) = 1 ) and ( component != activeModule )";
326 QString uniform = "true in $canBeDisplayed and %1 and ( activeModule = '%2' )";
327 uniform = uniform.arg( oneAndNotActive ).arg( name() );
328 myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, true );
329 myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, true );
330 myPopupMgr->setRule( dispOnly, uniform, true );
334 #ifndef DISABLE_OCCVIEWER
335 #ifndef DISABLE_SALOMEOBJECT
336 viewers.append( SOCC_Viewer::Type() );
338 viewers.append( OCCViewer_Viewer::Type() );
341 #ifndef DISABLE_VTKVIEWER
342 #ifndef DISABLE_SALOMEOBJECT
343 viewers.append( SVTK_Viewer::Type() );
345 viewers.append( VTKViewer_Viewer::Type() );
348 #ifndef DISABLE_PLOT2DVIEWER
349 #ifndef DISABLE_SALOMEOBJECT
350 viewers.append( SPlot2d_Viewer::Type() );
352 viewers.append( Plot2d_Viewer::Type() );
356 if( !viewers.isEmpty() )
358 QString strViewers = "{ ", temp = "'%1' ";
359 QStringList::const_iterator anIt = viewers.begin(), aLast = viewers.end();
360 for( ; anIt!=aLast; anIt++ )
361 strViewers+=temp.arg( *anIt );
363 myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( strViewers ), true );
369 /*!Gets preferences.*/
370 LightApp_Preferences* LightApp_Module::preferences() const
372 LightApp_Preferences* pref = 0;
374 pref = getApp()->preferences();
378 /*!Add preference to preferences.*/
379 int LightApp_Module::addPreference( const QString& label )
381 LightApp_Preferences* pref = preferences();
385 int catId = pref->addPreference( moduleName(), -1 );
389 return pref->addPreference( label, catId );
392 /*!Add preference to preferences.*/
393 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
394 const QString& section, const QString& param )
396 LightApp_Preferences* pref = preferences();
400 return pref->addPreference( moduleName(), label, pId, type, section, param );
403 /*!Gets property of preferences.*/
404 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
407 LightApp_Preferences* pref = preferences();
409 var = pref->itemProperty( id, prop );
413 /*!Set property of preferences.*/
414 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
416 LightApp_Preferences* pref = preferences();
418 pref->setItemProperty( id, prop, var );
422 * \brief Starts operation with given identifier
423 * \param id - identifier of operation to be started
425 * Module stores operations in map. This method starts operation by id.
426 * If operation isn't in map, then it will be created by createOperation method
427 * and will be inserted to map
429 void LightApp_Module::startOperation( const int id )
431 LightApp_Operation* op = 0;
432 if( myOperations.contains( id ) )
433 op = myOperations[ id ];
436 op = createOperation( id );
439 myOperations.insert( id, op );
440 op->setModule( this );
441 connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
442 connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
448 // be sure that operation has correct study pointer
449 op->setStudy( application() ? application()->activeStudy() : 0 );
455 * \brief Creates operation with given identifier
456 * \param id - identifier of operation to be started
457 * \return Pointer on created operation or NULL if operation is not created
459 * Creates operation with given id. You should not call this method, it will be called
460 * automatically from startOperation. You may redefine this method in concrete module to
463 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
469 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
470 else if( id==myErase )
471 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
472 else if( id==myDisplayOnly )
473 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
474 else if( id==myEraseAll )
475 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
481 * \brief Virtual protected slot called when operation stopped
482 * \param theOp - stopped operation
484 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
485 * perform actions after stopping operation
487 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
492 * \brief Virtual protected slot called when operation destroyed
493 * \param theOp - destroyed operation
495 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
496 * perform actions after destroying operation. Base implementation removes pointer on
497 * destroyed operation from the map of operations
499 void LightApp_Module::onOperationDestroyed()
501 const QObject* s = sender();
502 if( s && s->inherits( "LightApp_Operation" ) )
504 const LightApp_Operation* op = ( LightApp_Operation* )s;
505 MapOfOperation::const_iterator anIt = myOperations.begin(),
506 aLast = myOperations.end();
507 for( ; anIt!=aLast; anIt++ )
508 if( anIt.data()==op )
510 myOperations.remove( anIt.key() );
517 Must be redefined in order to use standard displayer mechanism
518 \return displayer of module
520 LightApp_Displayer* LightApp_Module::displayer()
526 SLOT: called on activating of standard operations show/hide
528 void LightApp_Module::onShowHide()
530 if( !sender()->inherits( "QAction" ) || !popupMgr() )
533 QAction* act = ( QAction* )sender();
534 int id = actionId( act );
536 startOperation( id );
540 virtual SLOT: called on view manager adding
542 void LightApp_Module::onViewManagerAdded( SUIT_ViewManager* )
547 virtual SLOT: called on view manager removing
549 void LightApp_Module::onViewManagerRemoved( SUIT_ViewManager* )