Salome HOME
new menu item "Erase all"
[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_Study.h"
13 #include "LightApp_Preferences.h"
14 #include "LightApp_Selection.h"
15 #include "LightApp_Operation.h"
16 #include "LightApp_SwitchOp.h"
17 #include "LightApp_UpdateFlags.h"
18 #include "LightApp_ShowHideOp.h"
19
20 #include "SUIT_Operation.h"
21 #include <SUIT_Study.h>
22 #include <SUIT_DataObject.h>
23 #include <SUIT_ResourceMgr.h>
24
25 #include <SVTK_ViewWindow.h>
26 #include <SVTK_ViewModel.h>
27 #include <OCCViewer_ViewWindow.h>
28 #include <OCCViewer_ViewPort3d.h>
29 #include <SOCC_ViewModel.h>
30 #include <GLViewer_ViewFrame.h>
31 #include <GLViewer_ViewPort.h>
32 #include <Plot2d_ViewWindow.h>
33 #include <Plot2d_ViewFrame.h>
34 #include <SPlot2d_ViewModel.h>
35
36 #include <OB_Browser.h>
37
38 #include <QtxPopupMgr.h>
39
40 #include <qvariant.h>
41 #include <qstring.h>
42 #include <qstringlist.h>
43
44 /*!Constructor.*/
45 LightApp_Module::LightApp_Module( const QString& name )
46 : CAM_Module( name ),
47   myPopupMgr( 0 ),
48   mySwitchOp( 0 ),
49   myDisplay( -1 ),
50   myErase( -1 ),
51   myDisplayOnly( -1 ),
52   myEraseAll( -1 )
53 {
54 }
55
56 /*!Destructor.*/
57 LightApp_Module::~LightApp_Module()
58 {
59   if ( mySwitchOp )
60     delete mySwitchOp;
61 }
62
63 /*!Initialize module.*/
64 void LightApp_Module::initialize( CAM_Application* app )
65 {
66   CAM_Module::initialize( app );
67
68   SUIT_ResourceMgr* resMgr = app ? app->resourceMgr() : 0;
69   if ( resMgr )
70     resMgr->raiseTranslators( name() );
71 }
72
73 /*!NOT IMPLEMENTED*/
74 void LightApp_Module::windows( QMap<int, int>& ) const
75 {
76 }
77
78 /*!NOT IMPLEMENTED*/
79 void LightApp_Module::viewManagers( QStringList& ) const
80 {
81 }
82
83 /*!Context menu popup.*/
84 void LightApp_Module::contextMenuPopup( const QString& client, QPopupMenu* menu, QString& /*title*/ )
85 {
86   LightApp_Selection* sel = createSelection();
87   sel->init( client, getApp()->selectionMgr() );
88   popupMgr()->updatePopup( menu, sel );
89   delete sel;
90 }
91
92 /*!Update object browser.
93  * For updating model or whole object browser use update() method can be used.
94 */
95 void LightApp_Module::updateObjBrowser( bool updateDataModel, SUIT_DataObject* root )
96 {
97   if( updateDataModel )
98     if( CAM_DataModel* aDataModel = dataModel() )
99       if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
100         aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
101   getApp()->objectBrowser()->updateTree( root );
102 }
103
104 /*!NOT IMPLEMENTED*/
105 void LightApp_Module::selectionChanged()
106 {
107 }
108
109 /*!Activate module.*/
110 bool LightApp_Module::activateModule( SUIT_Study* study )
111 {
112   bool res = CAM_Module::activateModule( study );
113
114   if ( res && application() && application()->resourceMgr() )
115     application()->resourceMgr()->raiseTranslators( name() );
116
117   if ( mySwitchOp == 0 )
118     mySwitchOp = new LightApp_SwitchOp( this );
119
120   return res;
121 }
122
123 /*!Deactivate module.*/
124 bool LightApp_Module::deactivateModule( SUIT_Study* )
125 {
126   delete mySwitchOp;
127   mySwitchOp = 0;
128
129   return true;
130 }
131
132 /*!NOT IMPLEMENTED*/
133 void LightApp_Module::MenuItem()
134 {
135 }
136
137 /*!NOT IMPLEMENTED*/
138 void LightApp_Module::createPreferences()
139 {
140 }
141
142 /*!NOT IMPLEMENTED*/
143 void LightApp_Module::preferencesChanged( const QString&, const QString& )
144 {
145 }
146
147 /*!Gets application.*/
148 LightApp_Application* LightApp_Module::getApp() const
149 {
150   return (LightApp_Application*)application();
151 }
152
153 /*!
154  * \brief Update something in accordance with update flags
155  * \param theFlags - update flags
156 *
157 * Update viewer or/and object browser etc. in accordance with update flags ( see
158 * LightApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
159 * own purposes
160 */
161 void LightApp_Module::update( const int theFlags )
162 {
163   if ( theFlags & UF_Model )
164   {
165     if( CAM_DataModel* aDataModel = dataModel() )
166       if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
167         aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
168   }
169   if ( theFlags & UF_ObjBrowser )
170     getApp()->objectBrowser()->updateTree( 0 );
171   if ( theFlags & UF_Controls )
172     updateControls();
173   if ( theFlags & UF_Viewer )
174   {
175     if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
176       if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
177       {
178         if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
179           ( (SVTK_ViewWindow*)viewWnd )->Repaint();
180         else if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
181           ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
182         else if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
183           ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
184         else if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
185           ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
186       }
187   }
188 }
189 /*!
190  * \brief Updates controls
191 *
192 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
193 * called from update( UF_Controls ). You may redefine it in concrete module.
194 */
195 void LightApp_Module::updateControls()
196 {
197 }
198
199 /*!Create new instance of data model and return it.*/
200 CAM_DataModel* LightApp_Module::createDataModel()
201 {
202   return new LightApp_DataModel(this);
203 }
204
205 /*!Create and return instance of LightApp_Selection.*/
206 LightApp_Selection* LightApp_Module::createSelection() const
207 {
208   return new LightApp_Selection();
209 }
210
211 /*!NOT IMPLEMENTED*/
212 void LightApp_Module::onModelOpened()
213 {
214 }
215
216 /*!NOT IMPLEMENTED*/
217 void LightApp_Module::onModelSaved()
218 {
219 }
220
221 /*!NOT IMPLEMENTED*/
222 void LightApp_Module::onModelClosed()
223 {
224 }
225
226 /*!Gets popup manager.(create if not exist)*/
227 QtxPopupMgr* LightApp_Module::popupMgr()
228 {
229   if ( !myPopupMgr )
230   {
231     myPopupMgr = new QtxPopupMgr( 0, this );
232
233     QPixmap p;
234     SUIT_Desktop* d = application()->desktop();
235     
236     QAction 
237       *disp = createAction( -1, tr( "TOP_DISPLAY" ), p, tr( "MEN_DISPLAY" ), tr( "STB_DISPLAY" ),
238                             0, d, false, this, SLOT( onShowHide() ) ),
239       *erase = createAction( -1, tr( "TOP_ERASE" ), p, tr( "MEN_ERASE" ), tr( "STB_ERASE" ),
240                              0, d, false, this, SLOT( onShowHide() ) ),
241       *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
242                                 0, d, false, this, SLOT( onShowHide() ) ),
243       *eraseAll = createAction( -1, tr( "TOP_ERASE_ALL" ), p, tr( "MEN_ERASE_ALL" ), tr( "STB_ERASE_ALL" ),
244                                 0, d, false, this, SLOT( onShowHide() ) );
245     myDisplay     = actionId( disp );
246     myErase       = actionId( erase );
247     myDisplayOnly = actionId( dispOnly );
248     myEraseAll    = actionId( eraseAll );
249
250     myPopupMgr->insert( disp, -1, 0 ); 
251     myPopupMgr->insert( erase, -1, 0 );
252     myPopupMgr->insert( dispOnly, -1, 0 );
253     myPopupMgr->insert( eraseAll, -1, 0 );
254     myPopupMgr->insert( separator(), -1, 0 );
255
256     QString uniform = "( count( $component ) = 1 ) and ( component != activeModule ) and ( activeModule = '%1' )";
257     uniform = uniform.arg( name() );
258     myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, true );
259     myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, true );
260     myPopupMgr->setRule( dispOnly, uniform, true );
261     QString viewers = "{ '%1' '%2' '%3' }";
262     viewers = viewers.arg( SOCC_Viewer::Type() ).arg( SVTK_Viewer::Type() ).arg( SPlot2d_Viewer::Type() );
263     myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( viewers ), true );
264   }
265   return myPopupMgr;
266 }
267
268 /*!Gets preferences.*/
269 LightApp_Preferences* LightApp_Module::preferences() const
270 {
271   LightApp_Preferences* pref = 0;
272   if ( getApp() )
273     pref = getApp()->preferences();
274   return pref;
275 }
276
277 /*!Add preference to preferences.*/
278 int LightApp_Module::addPreference( const QString& label )
279 {
280   LightApp_Preferences* pref = preferences();
281   if ( !pref )
282     return -1;
283
284   int catId = pref->addPreference( moduleName(), -1 );
285   if ( catId == -1 )
286     return -1;
287
288   return pref->addPreference( label, catId );
289 }
290
291 /*!Add preference to preferences.*/
292 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
293                                     const QString& section, const QString& param )
294 {
295   LightApp_Preferences* pref = preferences();
296   if ( !pref )
297     return -1;
298
299   return pref->addPreference( moduleName(), label, pId, type, section, param );
300 }
301
302 /*!Gets property of preferences.*/
303 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
304 {
305   QVariant var;
306   LightApp_Preferences* pref = preferences();
307   if ( pref )
308     var = pref->itemProperty( id, prop );
309   return var;
310 }
311
312 /*!Set property of preferences.*/
313 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
314 {
315   LightApp_Preferences* pref = preferences();
316   if ( pref )
317     pref->setItemProperty( id, prop, var );
318 }
319
320 /*!
321  * \brief Starts operation with given identifier
322   * \param id - identifier of operation to be started
323 *
324 * Module stores operations in map. This method starts operation by id.
325 * If operation isn't in map, then it will be created by createOperation method
326 * and will be inserted to map
327 */
328 void LightApp_Module::startOperation( const int id )
329 {
330   LightApp_Operation* op = 0;
331   if( myOperations.contains( id ) )
332     op = myOperations[ id ];
333   else
334   {
335     op = createOperation( id );
336     if( op )
337     {
338       myOperations.insert( id, op );
339       op->setModule( this );
340       connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
341       connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
342     }
343   }
344
345   if( op )
346     op->start();
347 }
348
349 /*!
350  * \brief Creates operation with given identifier
351   * \param id - identifier of operation to be started
352   * \return Pointer on created operation or NULL if operation is not created
353 *
354 * Creates operation with given id. You should not call this method, it will be called
355 * automatically from startOperation. You may redefine this method in concrete module to
356 * create operations. 
357 */
358 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
359 {
360   if( id==-1 )
361     return 0;
362
363   if( id==myDisplay )
364     return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
365   else if( id==myErase )
366     return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
367   else if( id==myDisplayOnly )
368     return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
369   else if( id==myEraseAll )
370     return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
371   else
372     return 0;
373 }
374
375 /*!
376  * \brief Virtual protected slot called when operation stopped
377  * \param theOp - stopped operation
378 *
379 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
380 * perform actions after stopping operation
381 */
382 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
383 {
384 }
385
386 /*!
387  * \brief Virtual protected slot called when operation destroyed
388   * \param theOp - destroyed operation
389 *
390 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
391 * perform actions after destroying operation. Base implementation removes pointer on
392 * destroyed operation from the map of operations
393 */
394 void LightApp_Module::onOperationDestroyed()
395 {
396   const QObject* s = sender();
397   if( s && s->inherits( "LightApp_Operation" ) )
398   {
399     const LightApp_Operation* op = ( LightApp_Operation* )s;
400     MapOfOperation::const_iterator anIt = myOperations.begin(),
401                                    aLast = myOperations.end();
402     for( ; anIt!=aLast; anIt++ )
403       if( anIt.data()==op )
404       {
405         myOperations.remove( anIt.key() );
406         break;
407       }
408   }
409 }
410
411 LightApp_Displayer* LightApp_Module::displayer()
412 {
413   return 0;
414 }
415
416 void LightApp_Module::onShowHide()
417 {
418   if( !sender()->inherits( "QAction" ) || !popupMgr() )
419     return;
420
421   QAction* act = ( QAction* )sender();
422   int id = actionId( act );
423   if( id!=-1 )
424     startOperation( id );
425 }