1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File: LightApp_Module.cxx
20 // Created: 6/20/2005 16:30:56 AM
22 // Copyright (C) CEA 2005
24 #include "LightApp_Module.h"
26 #include "CAM_Application.h"
28 #include "LightApp_Application.h"
29 #include "LightApp_DataModel.h"
30 #include "LightApp_DataObject.h"
31 #include "LightApp_Study.h"
32 #include "LightApp_Preferences.h"
33 #include "LightApp_Selection.h"
34 #include "LightApp_Operation.h"
35 #include "LightApp_SwitchOp.h"
36 #include "LightApp_UpdateFlags.h"
37 #include "LightApp_ShowHideOp.h"
39 #include "SUIT_Operation.h"
40 #include <SUIT_Study.h>
41 #include <SUIT_DataObject.h>
42 #include <SUIT_ResourceMgr.h>
44 #ifndef DISABLE_VTKVIEWER
45 #ifndef DISABLE_SALOMEOBJECT
46 #include <SVTK_ViewWindow.h>
47 #include <SVTK_ViewModel.h>
49 #include <VTKViewer_ViewWindow.h>
51 #include <VTKViewer_ViewModel.h>
53 #ifndef DISABLE_OCCVIEWER
54 #include <OCCViewer_ViewWindow.h>
55 #include <OCCViewer_ViewPort3d.h>
56 #ifndef DISABLE_SALOMEOBJECT
57 #include <SOCC_ViewModel.h>
59 #include <OCCViewer_ViewModel.h>
62 #ifndef DISABLE_GLVIEWER
63 #include <GLViewer_ViewFrame.h>
64 #include <GLViewer_ViewPort.h>
66 #ifndef DISABLE_PLOT2DVIEWER
67 #include <Plot2d_ViewWindow.h>
68 #include <Plot2d_ViewFrame.h>
69 #ifndef DISABLE_SALOMEOBJECT
70 #include <SPlot2d_ViewModel.h>
72 #include <Plot2d_ViewModel.h>
76 #include <OB_Browser.h>
78 #include <QtxPopupMgr.h>
82 #include <qstringlist.h>
85 LightApp_Module::LightApp_Module( const QString& name )
97 LightApp_Module::~LightApp_Module()
103 /*!Initialize module.*/
104 void LightApp_Module::initialize( CAM_Application* app )
106 CAM_Module::initialize( app );
108 SUIT_ResourceMgr* resMgr = app ? app->resourceMgr() : 0;
110 resMgr->raiseTranslators( name() );
114 void LightApp_Module::windows( QMap<int, int>& ) const
119 void LightApp_Module::viewManagers( QStringList& ) const
123 /*!Context menu popup.*/
124 void LightApp_Module::contextMenuPopup( const QString& client, QPopupMenu* menu, QString& /*title*/ )
126 LightApp_Selection* sel = createSelection();
127 sel->init( client, getApp()->selectionMgr() );
128 popupMgr()->updatePopup( menu, sel );
132 /*!Update object browser.
133 * For updating model or whole object browser use update() method can be used.
135 void LightApp_Module::updateObjBrowser( bool theIsUpdateDataModel,
136 SUIT_DataObject* theDataObject )
138 bool upd = getApp()->objectBrowser()->isAutoUpdate();
139 getApp()->objectBrowser()->setAutoUpdate( false );
141 if( theIsUpdateDataModel ){
142 if( CAM_DataModel* aDataModel = dataModel() ){
143 if ( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) ) {
144 SUIT_DataObject* aParent = NULL;
145 if(theDataObject && theDataObject != aDataModel->root())
146 aParent = theDataObject->parent();
148 LightApp_DataObject* anObject = dynamic_cast<LightApp_DataObject*>(theDataObject);
149 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(getApp()->activeStudy());
150 aModel->update( anObject, aStudy );
154 getApp()->objectBrowser()->setAutoUpdate( upd );
155 getApp()->objectBrowser()->updateTree( 0, false );
159 void LightApp_Module::selectionChanged()
163 /*!Activate module.*/
164 bool LightApp_Module::activateModule( SUIT_Study* study )
166 bool res = CAM_Module::activateModule( study );
168 if ( res && application() && application()->resourceMgr() )
169 application()->resourceMgr()->raiseTranslators( name() );
171 connect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
172 this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
173 connect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
174 this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
176 if ( mySwitchOp == 0 )
177 mySwitchOp = new LightApp_SwitchOp( this );
182 /*!Deactivate module.*/
183 bool LightApp_Module::deactivateModule( SUIT_Study* study )
188 disconnect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
189 this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
190 disconnect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
191 this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
193 // abort all operations
194 MapOfOperation::const_iterator anIt;
195 for( anIt = myOperations.begin(); anIt != myOperations.end(); anIt++ ) {
196 anIt.data()->abort();
199 return CAM_Module::activateModule( study );
203 void LightApp_Module::MenuItem()
208 void LightApp_Module::createPreferences()
213 void LightApp_Module::preferencesChanged( const QString&, const QString& )
217 /*!Gets application.*/
218 LightApp_Application* LightApp_Module::getApp() const
220 return (LightApp_Application*)application();
224 * \brief Update something in accordance with update flags
225 * \param theFlags - update flags
227 * Update viewer or/and object browser etc. in accordance with update flags ( see
228 * LightApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
231 void LightApp_Module::update( const int theFlags )
233 if ( theFlags & UF_Model )
235 if( CAM_DataModel* aDataModel = dataModel() )
236 if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
237 aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
239 if ( theFlags & UF_ObjBrowser )
240 getApp()->objectBrowser()->updateTree( 0 );
241 if ( theFlags & UF_Controls )
243 if ( theFlags & UF_Viewer )
245 if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
246 if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
248 #ifndef DISABLE_VTKVIEWER
249 #ifndef DISABLE_SALOMEOBJECT
250 if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
251 ( (SVTK_ViewWindow*)viewWnd )->Repaint();
253 if ( viewWnd->inherits( "VTKViewer_ViewWindow" ) )
254 ( (VTKViewer_ViewWindow*)viewWnd )->Repaint();
257 #ifndef DISABLE_OCCVIEWER
258 if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
259 ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
261 #ifndef DISABLE_PLOT2DVIEWER
262 if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
263 ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
265 #ifndef DISABLE_GLVIEWER
266 if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
267 ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
273 * \brief Updates controls
275 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
276 * called from update( UF_Controls ). You may redefine it in concrete module.
278 void LightApp_Module::updateControls()
282 /*!Create new instance of data model and return it.*/
283 CAM_DataModel* LightApp_Module::createDataModel()
285 return new LightApp_DataModel(this);
288 /*!Create and return instance of LightApp_Selection.*/
289 LightApp_Selection* LightApp_Module::createSelection() const
291 return new LightApp_Selection();
295 void LightApp_Module::onModelOpened()
300 void LightApp_Module::onModelSaved()
305 void LightApp_Module::onModelClosed()
309 /*!Gets popup manager.(create if not exist)*/
310 QtxPopupMgr* LightApp_Module::popupMgr()
314 myPopupMgr = new QtxPopupMgr( 0, this );
317 SUIT_Desktop* d = application()->desktop();
320 *disp = createAction( -1, tr( "TOP_SHOW" ), p, tr( "MEN_SHOW" ), tr( "STB_SHOW" ),
321 0, d, false, this, SLOT( onShowHide() ) ),
322 *erase = createAction( -1, tr( "TOP_HIDE" ), p, tr( "MEN_HIDE" ), tr( "STB_HIDE" ),
323 0, d, false, this, SLOT( onShowHide() ) ),
324 *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
325 0, d, false, this, SLOT( onShowHide() ) ),
326 *eraseAll = createAction( -1, tr( "TOP_ERASE_ALL" ), p, tr( "MEN_ERASE_ALL" ), tr( "STB_ERASE_ALL" ),
327 0, d, false, this, SLOT( onShowHide() ) );
328 myDisplay = actionId( disp );
329 myErase = actionId( erase );
330 myDisplayOnly = actionId( dispOnly );
331 myEraseAll = actionId( eraseAll );
333 myPopupMgr->insert( disp, -1, 0 );
334 myPopupMgr->insert( erase, -1, 0 );
335 myPopupMgr->insert( dispOnly, -1, 0 );
336 myPopupMgr->insert( eraseAll, -1, 0 );
337 myPopupMgr->insert( separator(), -1, 0 );
339 QString oneAndNotActive = "( count( $component ) = 1 ) and ( not( activeModule in $component ) )";
340 QString uniform = "true in $canBeDisplayed and %1 and ( activeModule = '%2' )";
341 uniform = uniform.arg( oneAndNotActive ).arg( name() );
342 myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, true );
343 myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, true );
344 myPopupMgr->setRule( dispOnly, uniform, true );
348 #ifndef DISABLE_OCCVIEWER
349 #ifndef DISABLE_SALOMEOBJECT
350 viewers.append( SOCC_Viewer::Type() );
352 viewers.append( OCCViewer_Viewer::Type() );
355 #ifndef DISABLE_VTKVIEWER
356 #ifndef DISABLE_SALOMEOBJECT
357 viewers.append( SVTK_Viewer::Type() );
359 viewers.append( VTKViewer_Viewer::Type() );
362 #ifndef DISABLE_PLOT2DVIEWER
363 #ifndef DISABLE_SALOMEOBJECT
364 viewers.append( SPlot2d_Viewer::Type() );
366 viewers.append( Plot2d_Viewer::Type() );
370 if( !viewers.isEmpty() )
372 QString strViewers = "{ ", temp = "'%1' ";
373 QStringList::const_iterator anIt = viewers.begin(), aLast = viewers.end();
374 for( ; anIt!=aLast; anIt++ )
375 strViewers+=temp.arg( *anIt );
377 myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( strViewers ), true );
383 /*!Gets preferences.*/
384 LightApp_Preferences* LightApp_Module::preferences() const
386 LightApp_Preferences* pref = 0;
388 pref = getApp()->preferences();
392 /*!Add preference to preferences.*/
393 int LightApp_Module::addPreference( const QString& label )
395 LightApp_Preferences* pref = preferences();
399 int catId = pref->addPreference( moduleName(), -1 );
403 return pref->addPreference( label, catId );
406 /*!Add preference to preferences.*/
407 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
408 const QString& section, const QString& param )
410 LightApp_Preferences* pref = preferences();
414 return pref->addPreference( moduleName(), label, pId, type, section, param );
417 /*!Gets property of preferences.*/
418 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
421 LightApp_Preferences* pref = preferences();
423 var = pref->itemProperty( id, prop );
427 /*!Set property of preferences.*/
428 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
430 LightApp_Preferences* pref = preferences();
432 pref->setItemProperty( id, prop, var );
436 * \brief Starts operation with given identifier
437 * \param id - identifier of operation to be started
439 * Module stores operations in map. This method starts operation by id.
440 * If operation isn't in map, then it will be created by createOperation method
441 * and will be inserted to map
443 void LightApp_Module::startOperation( const int id )
445 LightApp_Operation* op = 0;
446 if( myOperations.contains( id ) )
447 op = myOperations[ id ];
450 op = createOperation( id );
453 myOperations.insert( id, op );
454 op->setModule( this );
455 connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
456 connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
462 // be sure that operation has correct study pointer
463 op->setStudy( application() ? application()->activeStudy() : 0 );
469 * \brief Creates operation with given identifier
470 * \param id - identifier of operation to be started
471 * \return Pointer on created operation or NULL if operation is not created
473 * Creates operation with given id. You should not call this method, it will be called
474 * automatically from startOperation. You may redefine this method in concrete module to
477 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
483 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
484 else if( id==myErase )
485 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
486 else if( id==myDisplayOnly )
487 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
488 else if( id==myEraseAll )
489 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
495 * \brief Virtual protected slot called when operation stopped
496 * \param theOp - stopped operation
498 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
499 * perform actions after stopping operation
501 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
506 * \brief Virtual protected slot called when operation destroyed
507 * \param theOp - destroyed operation
509 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
510 * perform actions after destroying operation. Base implementation removes pointer on
511 * destroyed operation from the map of operations
513 void LightApp_Module::onOperationDestroyed()
515 const QObject* s = sender();
516 if( s && s->inherits( "LightApp_Operation" ) )
518 const LightApp_Operation* op = ( LightApp_Operation* )s;
519 MapOfOperation::const_iterator anIt = myOperations.begin(),
520 aLast = myOperations.end();
521 for( ; anIt!=aLast; anIt++ )
522 if( anIt.data()==op )
524 myOperations.remove( anIt.key() );
531 Must be redefined in order to use standard displayer mechanism
532 \return displayer of module
534 LightApp_Displayer* LightApp_Module::displayer()
540 SLOT: called on activating of standard operations show/hide
542 void LightApp_Module::onShowHide()
544 if( !sender()->inherits( "QAction" ) || !popupMgr() )
547 QAction* act = ( QAction* )sender();
548 int id = actionId( act );
550 startOperation( id );
554 virtual SLOT: called on view manager adding
556 void LightApp_Module::onViewManagerAdded( SUIT_ViewManager* )
561 virtual SLOT: called on view manager removing
563 void LightApp_Module::onViewManagerRemoved( SUIT_ViewManager* )