Salome HOME
691d68b98bd74f83193496fc50e4413824c52a31
[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   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();
127
128         LightApp_DataObject* anObject = dynamic_cast<LightApp_DataObject*>(theDataObject);
129         LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(getApp()->activeStudy());
130         aModel->update( anObject, aStudy );
131
132         if(aParent && aParent->childPos(anObject) < 0)
133           aDataObject = dynamic_cast<LightApp_DataObject*>(aParent);
134       }
135     }
136   }
137   getApp()->objectBrowser()->updateTree( aDataObject );
138 }
139
140 /*!NOT IMPLEMENTED*/
141 void LightApp_Module::selectionChanged()
142 {
143 }
144
145 /*!Activate module.*/
146 bool LightApp_Module::activateModule( SUIT_Study* study )
147 {
148   bool res = CAM_Module::activateModule( study );
149
150   if ( res && application() && application()->resourceMgr() )
151     application()->resourceMgr()->raiseTranslators( name() );
152
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* ) ) );
157
158   if ( mySwitchOp == 0 )
159     mySwitchOp = new LightApp_SwitchOp( this );
160
161   return res;
162 }
163
164 /*!Deactivate module.*/
165 bool LightApp_Module::deactivateModule( SUIT_Study* study )
166 {
167   delete mySwitchOp;
168   mySwitchOp = 0;
169
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* ) ) );
174
175   // abort all operations
176   MapOfOperation::const_iterator anIt;
177   for( anIt = myOperations.begin(); anIt != myOperations.end(); anIt++ ) {
178     anIt.data()->abort();
179   }
180
181   return CAM_Module::activateModule( study );
182 }
183
184 /*!NOT IMPLEMENTED*/
185 void LightApp_Module::MenuItem()
186 {
187 }
188
189 /*!NOT IMPLEMENTED*/
190 void LightApp_Module::createPreferences()
191 {
192 }
193
194 /*!NOT IMPLEMENTED*/
195 void LightApp_Module::preferencesChanged( const QString&, const QString& )
196 {
197 }
198
199 /*!Gets application.*/
200 LightApp_Application* LightApp_Module::getApp() const
201 {
202   return (LightApp_Application*)application();
203 }
204
205 /*!
206  * \brief Update something in accordance with update flags
207  * \param theFlags - update flags
208 *
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
211 * own purposes
212 */
213 void LightApp_Module::update( const int theFlags )
214 {
215   if ( theFlags & UF_Model )
216   {
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() ) );
220   }
221   if ( theFlags & UF_ObjBrowser )
222     getApp()->objectBrowser()->updateTree( 0 );
223   if ( theFlags & UF_Controls )
224     updateControls();
225   if ( theFlags & UF_Viewer )
226   {
227     if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
228       if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
229       {
230 #ifndef DISABLE_VTKVIEWER
231 #ifndef DISABLE_SALOMEOBJECT
232         if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
233           ( (SVTK_ViewWindow*)viewWnd )->Repaint();
234 #else
235         if ( viewWnd->inherits( "VTKViewer_ViewWindow" ) )
236           ( (VTKViewer_ViewWindow*)viewWnd )->Repaint();
237 #endif
238 #endif
239 #ifndef DISABLE_OCCVIEWER
240         if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
241           ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
242 #endif
243 #ifndef DISABLE_PLOT2DVIEWER
244         if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
245           ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
246 #endif
247 #ifndef DISABLE_GLVIEWER
248         if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
249           ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
250 #endif
251       }
252   }
253 }
254 /*!
255  * \brief Updates controls
256 *
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.
259 */
260 void LightApp_Module::updateControls()
261 {
262 }
263
264 /*!Create new instance of data model and return it.*/
265 CAM_DataModel* LightApp_Module::createDataModel()
266 {
267   return new LightApp_DataModel(this);
268 }
269
270 /*!Create and return instance of LightApp_Selection.*/
271 LightApp_Selection* LightApp_Module::createSelection() const
272 {
273   return new LightApp_Selection();
274 }
275
276 /*!NOT IMPLEMENTED*/
277 void LightApp_Module::onModelOpened()
278 {
279 }
280
281 /*!NOT IMPLEMENTED*/
282 void LightApp_Module::onModelSaved()
283 {
284 }
285
286 /*!NOT IMPLEMENTED*/
287 void LightApp_Module::onModelClosed()
288 {
289 }
290
291 /*!Gets popup manager.(create if not exist)*/
292 QtxPopupMgr* LightApp_Module::popupMgr()
293 {
294   if ( !myPopupMgr )
295   {
296     myPopupMgr = new QtxPopupMgr( 0, this );
297
298     QPixmap p;
299     SUIT_Desktop* d = application()->desktop();
300     
301     QAction 
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 );
314
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 );
320
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 );
327
328     QStringList viewers;
329
330 #ifndef DISABLE_OCCVIEWER
331 #ifndef DISABLE_SALOMEOBJECT
332     viewers.append( SOCC_Viewer::Type() );
333 #else
334     viewers.append( OCCViewer_Viewer::Type() );
335 #endif
336 #endif
337 #ifndef DISABLE_VTKVIEWER
338 #ifndef DISABLE_SALOMEOBJECT
339     viewers.append( SVTK_Viewer::Type() );
340 #else
341     viewers.append( VTKViewer_Viewer::Type() );
342 #endif
343 #endif
344 #ifndef DISABLE_PLOT2DVIEWER
345 #ifndef DISABLE_SALOMEOBJECT
346     viewers.append( SPlot2d_Viewer::Type() );
347 #else
348     viewers.append( Plot2d_Viewer::Type() );
349 #endif
350 #endif
351
352     if( !viewers.isEmpty() )
353     {
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 );
358       strViewers+="}";
359       myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( strViewers ), true );
360     }
361   }
362   return myPopupMgr;
363 }
364
365 /*!Gets preferences.*/
366 LightApp_Preferences* LightApp_Module::preferences() const
367 {
368   LightApp_Preferences* pref = 0;
369   if ( getApp() )
370     pref = getApp()->preferences();
371   return pref;
372 }
373
374 /*!Add preference to preferences.*/
375 int LightApp_Module::addPreference( const QString& label )
376 {
377   LightApp_Preferences* pref = preferences();
378   if ( !pref )
379     return -1;
380
381   int catId = pref->addPreference( moduleName(), -1 );
382   if ( catId == -1 )
383     return -1;
384
385   return pref->addPreference( label, catId );
386 }
387
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 )
391 {
392   LightApp_Preferences* pref = preferences();
393   if ( !pref )
394     return -1;
395
396   return pref->addPreference( moduleName(), label, pId, type, section, param );
397 }
398
399 /*!Gets property of preferences.*/
400 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
401 {
402   QVariant var;
403   LightApp_Preferences* pref = preferences();
404   if ( pref )
405     var = pref->itemProperty( id, prop );
406   return var;
407 }
408
409 /*!Set property of preferences.*/
410 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
411 {
412   LightApp_Preferences* pref = preferences();
413   if ( pref )
414     pref->setItemProperty( id, prop, var );
415 }
416
417 /*!
418  * \brief Starts operation with given identifier
419   * \param id - identifier of operation to be started
420 *
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
424 */
425 void LightApp_Module::startOperation( const int id )
426 {
427   LightApp_Operation* op = 0;
428   if( myOperations.contains( id ) )
429     op = myOperations[ id ];
430   else
431   {
432     op = createOperation( id );
433     if( op )
434     {
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() ) );
439     }
440   }
441
442   if( op )
443   {
444     // be sure that operation has correct study pointer
445     op->setStudy( application() ? application()->activeStudy() : 0 );
446     op->start();
447   }
448 }
449
450 /*!
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
454 *
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
457 * create operations. 
458 */
459 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
460 {
461   if( id==-1 )
462     return 0;
463
464   if( id==myDisplay )
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 );
472   else
473     return 0;
474 }
475
476 /*!
477  * \brief Virtual protected slot called when operation stopped
478  * \param theOp - stopped operation
479 *
480 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
481 * perform actions after stopping operation
482 */
483 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
484 {
485 }
486
487 /*!
488  * \brief Virtual protected slot called when operation destroyed
489   * \param theOp - destroyed operation
490 *
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
494 */
495 void LightApp_Module::onOperationDestroyed()
496 {
497   const QObject* s = sender();
498   if( s && s->inherits( "LightApp_Operation" ) )
499   {
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 )
505       {
506         myOperations.remove( anIt.key() );
507         break;
508       }
509   }
510 }
511
512 LightApp_Displayer* LightApp_Module::displayer()
513 {
514   return 0;
515 }
516
517 void LightApp_Module::onShowHide()
518 {
519   if( !sender()->inherits( "QAction" ) || !popupMgr() )
520     return;
521
522   QAction* act = ( QAction* )sender();
523   int id = actionId( act );
524   if( id!=-1 )
525     startOperation( id );
526 }
527
528 void LightApp_Module::onViewManagerAdded( SUIT_ViewManager* )
529 {
530 }
531
532 void LightApp_Module::onViewManagerRemoved( SUIT_ViewManager* )
533 {
534 }