]> SALOME platform Git repositories - modules/gui.git/blob - src/LightApp/LightApp_Module.cxx
Salome HOME
65689deed22744f8f695f36653a67cb6610337b7
[modules/gui.git] / src / LightApp / LightApp_Module.cxx
1 // File:      LightApp_Module.cxx
2 // Created:   6/20/2005 16:30:56 AM
3 // Author:    OCC team
4 // Copyright (C) CEA 2005
5
6 #include "LightApp_Module.h"
7
8 #include "CAM_Application.h"
9
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"
20
21 #include "SUIT_Operation.h"
22 #include <SUIT_Study.h>
23 #include <SUIT_DataObject.h>
24 #include <SUIT_ResourceMgr.h>
25
26 #ifndef DISABLE_VTKVIEWER
27 #ifndef DISABLE_SALOMEOBJECT
28   #include <SVTK_ViewWindow.h>
29   #include <SVTK_ViewModel.h>
30 #else
31   #include <VTKViewer_ViewWindow.h>
32 #endif
33   #include <VTKViewer_ViewModel.h>
34 #endif
35 #ifndef DISABLE_OCCVIEWER
36   #include <OCCViewer_ViewWindow.h>
37   #include <OCCViewer_ViewPort3d.h>
38 #ifndef DISABLE_SALOMEOBJECT
39   #include <SOCC_ViewModel.h>
40 #else
41   #include <OCCViewer_ViewModel.h>
42 #endif
43 #endif
44 #ifndef DISABLE_GLVIEWER
45   #include <GLViewer_ViewFrame.h>
46   #include <GLViewer_ViewPort.h>
47 #endif
48 #ifndef DISABLE_PLOT2DVIEWER
49   #include <Plot2d_ViewWindow.h>
50   #include <Plot2d_ViewFrame.h>
51 #ifndef DISABLE_SALOMEOBJECT
52   #include <SPlot2d_ViewModel.h>
53 #else
54   #include <Plot2d_ViewModel.h>
55 #endif
56 #endif
57
58 #include <OB_Browser.h>
59
60 #include <QtxPopupMgr.h>
61
62 #include <qvariant.h>
63 #include <qstring.h>
64 #include <qstringlist.h>
65
66 /*!Constructor.*/
67 LightApp_Module::LightApp_Module( const QString& name )
68 : CAM_Module( name ),
69   myPopupMgr( 0 ),
70   mySwitchOp( 0 ),
71   myDisplay( -1 ),
72   myErase( -1 ),
73   myDisplayOnly( -1 ),
74   myEraseAll( -1 )
75 {
76 }
77
78 /*!Destructor.*/
79 LightApp_Module::~LightApp_Module()
80 {
81   if ( mySwitchOp )
82     delete mySwitchOp;
83 }
84
85 /*!Initialize module.*/
86 void LightApp_Module::initialize( CAM_Application* app )
87 {
88   CAM_Module::initialize( app );
89
90   SUIT_ResourceMgr* resMgr = app ? app->resourceMgr() : 0;
91   if ( resMgr )
92     resMgr->raiseTranslators( name() );
93 }
94
95 /*!NOT IMPLEMENTED*/
96 void LightApp_Module::windows( QMap<int, int>& ) const
97 {
98 }
99
100 /*!NOT IMPLEMENTED*/
101 void LightApp_Module::viewManagers( QStringList& ) const
102 {
103 }
104
105 /*!Context menu popup.*/
106 void LightApp_Module::contextMenuPopup( const QString& client, QPopupMenu* menu, QString& /*title*/ )
107 {
108   LightApp_Selection* sel = createSelection();
109   sel->init( client, getApp()->selectionMgr() );
110   popupMgr()->updatePopup( menu, sel );
111   delete sel;
112 }
113
114 /*!Update object browser.
115  * For updating model or whole object browser use update() method can be used.
116 */
117 void LightApp_Module::updateObjBrowser( bool theIsUpdateDataModel, 
118                                         SUIT_DataObject* theDataObject )
119 {
120   bool upd = getApp()->objectBrowser()->isAutoUpdate();
121   getApp()->objectBrowser()->setAutoUpdate( false );
122
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();
130
131         LightApp_DataObject* anObject = dynamic_cast<LightApp_DataObject*>(theDataObject);
132         LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(getApp()->activeStudy());
133         aModel->update( anObject, aStudy );
134
135         if(aParent && aParent->childPos(anObject) < 0)
136           aDataObject = dynamic_cast<LightApp_DataObject*>(aParent);
137       }
138     }
139   }
140   getApp()->objectBrowser()->setAutoUpdate( upd );
141   getApp()->objectBrowser()->updateTree( 0, false /*aDataObject*/ );
142 }
143
144 /*!NOT IMPLEMENTED*/
145 void LightApp_Module::selectionChanged()
146 {
147 }
148
149 /*!Activate module.*/
150 bool LightApp_Module::activateModule( SUIT_Study* study )
151 {
152   bool res = CAM_Module::activateModule( study );
153
154   if ( res && application() && application()->resourceMgr() )
155     application()->resourceMgr()->raiseTranslators( name() );
156
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* ) ) );
161
162   if ( mySwitchOp == 0 )
163     mySwitchOp = new LightApp_SwitchOp( this );
164
165   return res;
166 }
167
168 /*!Deactivate module.*/
169 bool LightApp_Module::deactivateModule( SUIT_Study* study )
170 {
171   delete mySwitchOp;
172   mySwitchOp = 0;
173
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* ) ) );
178
179   // abort all operations
180   MapOfOperation::const_iterator anIt;
181   for( anIt = myOperations.begin(); anIt != myOperations.end(); anIt++ ) {
182     anIt.data()->abort();
183   }
184
185   return CAM_Module::activateModule( study );
186 }
187
188 /*!NOT IMPLEMENTED*/
189 void LightApp_Module::MenuItem()
190 {
191 }
192
193 /*!NOT IMPLEMENTED*/
194 void LightApp_Module::createPreferences()
195 {
196 }
197
198 /*!NOT IMPLEMENTED*/
199 void LightApp_Module::preferencesChanged( const QString&, const QString& )
200 {
201 }
202
203 /*!Gets application.*/
204 LightApp_Application* LightApp_Module::getApp() const
205 {
206   return (LightApp_Application*)application();
207 }
208
209 /*!
210  * \brief Update something in accordance with update flags
211  * \param theFlags - update flags
212 *
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
215 * own purposes
216 */
217 void LightApp_Module::update( const int theFlags )
218 {
219   if ( theFlags & UF_Model )
220   {
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() ) );
224   }
225   if ( theFlags & UF_ObjBrowser )
226     getApp()->objectBrowser()->updateTree( 0 );
227   if ( theFlags & UF_Controls )
228     updateControls();
229   if ( theFlags & UF_Viewer )
230   {
231     if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
232       if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
233       {
234 #ifndef DISABLE_VTKVIEWER
235 #ifndef DISABLE_SALOMEOBJECT
236         if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
237           ( (SVTK_ViewWindow*)viewWnd )->Repaint();
238 #else
239         if ( viewWnd->inherits( "VTKViewer_ViewWindow" ) )
240           ( (VTKViewer_ViewWindow*)viewWnd )->Repaint();
241 #endif
242 #endif
243 #ifndef DISABLE_OCCVIEWER
244         if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
245           ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
246 #endif
247 #ifndef DISABLE_PLOT2DVIEWER
248         if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
249           ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
250 #endif
251 #ifndef DISABLE_GLVIEWER
252         if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
253           ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
254 #endif
255       }
256   }
257 }
258 /*!
259  * \brief Updates controls
260 *
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.
263 */
264 void LightApp_Module::updateControls()
265 {
266 }
267
268 /*!Create new instance of data model and return it.*/
269 CAM_DataModel* LightApp_Module::createDataModel()
270 {
271   return new LightApp_DataModel(this);
272 }
273
274 /*!Create and return instance of LightApp_Selection.*/
275 LightApp_Selection* LightApp_Module::createSelection() const
276 {
277   return new LightApp_Selection();
278 }
279
280 /*!NOT IMPLEMENTED*/
281 void LightApp_Module::onModelOpened()
282 {
283 }
284
285 /*!NOT IMPLEMENTED*/
286 void LightApp_Module::onModelSaved()
287 {
288 }
289
290 /*!NOT IMPLEMENTED*/
291 void LightApp_Module::onModelClosed()
292 {
293 }
294
295 /*!Gets popup manager.(create if not exist)*/
296 QtxPopupMgr* LightApp_Module::popupMgr()
297 {
298   if ( !myPopupMgr )
299   {
300     myPopupMgr = new QtxPopupMgr( 0, this );
301
302     QPixmap p;
303     SUIT_Desktop* d = application()->desktop();
304     
305     QAction 
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 );
318
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 );
324
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 );
331
332     QStringList viewers;
333
334 #ifndef DISABLE_OCCVIEWER
335 #ifndef DISABLE_SALOMEOBJECT
336     viewers.append( SOCC_Viewer::Type() );
337 #else
338     viewers.append( OCCViewer_Viewer::Type() );
339 #endif
340 #endif
341 #ifndef DISABLE_VTKVIEWER
342 #ifndef DISABLE_SALOMEOBJECT
343     viewers.append( SVTK_Viewer::Type() );
344 #else
345     viewers.append( VTKViewer_Viewer::Type() );
346 #endif
347 #endif
348 #ifndef DISABLE_PLOT2DVIEWER
349 #ifndef DISABLE_SALOMEOBJECT
350     viewers.append( SPlot2d_Viewer::Type() );
351 #else
352     viewers.append( Plot2d_Viewer::Type() );
353 #endif
354 #endif
355
356     if( !viewers.isEmpty() )
357     {
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 );
362       strViewers+="}";
363       myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( strViewers ), true );
364     }
365   }
366   return myPopupMgr;
367 }
368
369 /*!Gets preferences.*/
370 LightApp_Preferences* LightApp_Module::preferences() const
371 {
372   LightApp_Preferences* pref = 0;
373   if ( getApp() )
374     pref = getApp()->preferences();
375   return pref;
376 }
377
378 /*!Add preference to preferences.*/
379 int LightApp_Module::addPreference( const QString& label )
380 {
381   LightApp_Preferences* pref = preferences();
382   if ( !pref )
383     return -1;
384
385   int catId = pref->addPreference( moduleName(), -1 );
386   if ( catId == -1 )
387     return -1;
388
389   return pref->addPreference( label, catId );
390 }
391
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 )
395 {
396   LightApp_Preferences* pref = preferences();
397   if ( !pref )
398     return -1;
399
400   return pref->addPreference( moduleName(), label, pId, type, section, param );
401 }
402
403 /*!Gets property of preferences.*/
404 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
405 {
406   QVariant var;
407   LightApp_Preferences* pref = preferences();
408   if ( pref )
409     var = pref->itemProperty( id, prop );
410   return var;
411 }
412
413 /*!Set property of preferences.*/
414 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
415 {
416   LightApp_Preferences* pref = preferences();
417   if ( pref )
418     pref->setItemProperty( id, prop, var );
419 }
420
421 /*!
422  * \brief Starts operation with given identifier
423   * \param id - identifier of operation to be started
424 *
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
428 */
429 void LightApp_Module::startOperation( const int id )
430 {
431   LightApp_Operation* op = 0;
432   if( myOperations.contains( id ) )
433     op = myOperations[ id ];
434   else
435   {
436     op = createOperation( id );
437     if( op )
438     {
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() ) );
443     }
444   }
445
446   if( op )
447   {
448     // be sure that operation has correct study pointer
449     op->setStudy( application() ? application()->activeStudy() : 0 );
450     op->start();
451   }
452 }
453
454 /*!
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
458 *
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
461 * create operations. 
462 */
463 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
464 {
465   if( id==-1 )
466     return 0;
467
468   if( id==myDisplay )
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 );
476   else
477     return 0;
478 }
479
480 /*!
481  * \brief Virtual protected slot called when operation stopped
482  * \param theOp - stopped operation
483 *
484 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
485 * perform actions after stopping operation
486 */
487 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
488 {
489 }
490
491 /*!
492  * \brief Virtual protected slot called when operation destroyed
493   * \param theOp - destroyed operation
494 *
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
498 */
499 void LightApp_Module::onOperationDestroyed()
500 {
501   const QObject* s = sender();
502   if( s && s->inherits( "LightApp_Operation" ) )
503   {
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 )
509       {
510         myOperations.remove( anIt.key() );
511         break;
512       }
513   }
514 }
515
516 /*!
517   Must be redefined in order to use standard displayer mechanism
518   \return displayer of module
519 */
520 LightApp_Displayer* LightApp_Module::displayer()
521 {
522   return 0;
523 }
524
525 /*!
526   SLOT: called on activating of standard operations show/hide
527 */
528 void LightApp_Module::onShowHide()
529 {
530   if( !sender()->inherits( "QAction" ) || !popupMgr() )
531     return;
532
533   QAction* act = ( QAction* )sender();
534   int id = actionId( act );
535   if( id!=-1 )
536     startOperation( id );
537 }
538
539 /*!
540   virtual SLOT: called on view manager adding
541 */
542 void LightApp_Module::onViewManagerAdded( SUIT_ViewManager* )
543 {
544 }
545
546 /*!
547   virtual SLOT: called on view manager removing
548 */
549 void LightApp_Module::onViewManagerRemoved( SUIT_ViewManager* )
550 {
551 }