Salome HOME
46d3f82afbba36a87b47afa7317d9a1e08445b21
[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 oneAndNotActive = "( count( $component ) = 1 ) and ( component != activeModule )";
257     QString uniform = "true in $canBeDisplayed and %1 and ( activeModule = '%2' )";
258     uniform = uniform.arg( oneAndNotActive ).arg( name() );
259     myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, true );
260     myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, true );
261     myPopupMgr->setRule( dispOnly, uniform, true );
262     QString viewers = "{ '%1' '%2' '%3' }";
263     viewers = viewers.arg( SOCC_Viewer::Type() ).arg( SVTK_Viewer::Type() ).arg( SPlot2d_Viewer::Type() );
264     myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( viewers ), true );
265   }
266   return myPopupMgr;
267 }
268
269 /*!Gets preferences.*/
270 LightApp_Preferences* LightApp_Module::preferences() const
271 {
272   LightApp_Preferences* pref = 0;
273   if ( getApp() )
274     pref = getApp()->preferences();
275   return pref;
276 }
277
278 /*!Add preference to preferences.*/
279 int LightApp_Module::addPreference( const QString& label )
280 {
281   LightApp_Preferences* pref = preferences();
282   if ( !pref )
283     return -1;
284
285   int catId = pref->addPreference( moduleName(), -1 );
286   if ( catId == -1 )
287     return -1;
288
289   return pref->addPreference( label, catId );
290 }
291
292 /*!Add preference to preferences.*/
293 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
294                                     const QString& section, const QString& param )
295 {
296   LightApp_Preferences* pref = preferences();
297   if ( !pref )
298     return -1;
299
300   return pref->addPreference( moduleName(), label, pId, type, section, param );
301 }
302
303 /*!Gets property of preferences.*/
304 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
305 {
306   QVariant var;
307   LightApp_Preferences* pref = preferences();
308   if ( pref )
309     var = pref->itemProperty( id, prop );
310   return var;
311 }
312
313 /*!Set property of preferences.*/
314 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
315 {
316   LightApp_Preferences* pref = preferences();
317   if ( pref )
318     pref->setItemProperty( id, prop, var );
319 }
320
321 /*!
322  * \brief Starts operation with given identifier
323   * \param id - identifier of operation to be started
324 *
325 * Module stores operations in map. This method starts operation by id.
326 * If operation isn't in map, then it will be created by createOperation method
327 * and will be inserted to map
328 */
329 void LightApp_Module::startOperation( const int id )
330 {
331   LightApp_Operation* op = 0;
332   if( myOperations.contains( id ) )
333     op = myOperations[ id ];
334   else
335   {
336     op = createOperation( id );
337     if( op )
338     {
339       myOperations.insert( id, op );
340       op->setModule( this );
341       connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
342       connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
343     }
344   }
345
346   if( op )
347   {
348     // be sure that operation has correct study pointer
349     op->setStudy( application() ? application()->activeStudy() : 0 );
350     op->start();
351   }
352 }
353
354 /*!
355  * \brief Creates operation with given identifier
356   * \param id - identifier of operation to be started
357   * \return Pointer on created operation or NULL if operation is not created
358 *
359 * Creates operation with given id. You should not call this method, it will be called
360 * automatically from startOperation. You may redefine this method in concrete module to
361 * create operations. 
362 */
363 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
364 {
365   if( id==-1 )
366     return 0;
367
368   if( id==myDisplay )
369     return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
370   else if( id==myErase )
371     return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
372   else if( id==myDisplayOnly )
373     return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
374   else if( id==myEraseAll )
375     return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
376   else
377     return 0;
378 }
379
380 /*!
381  * \brief Virtual protected slot called when operation stopped
382  * \param theOp - stopped operation
383 *
384 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
385 * perform actions after stopping operation
386 */
387 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
388 {
389 }
390
391 /*!
392  * \brief Virtual protected slot called when operation destroyed
393   * \param theOp - destroyed operation
394 *
395 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
396 * perform actions after destroying operation. Base implementation removes pointer on
397 * destroyed operation from the map of operations
398 */
399 void LightApp_Module::onOperationDestroyed()
400 {
401   const QObject* s = sender();
402   if( s && s->inherits( "LightApp_Operation" ) )
403   {
404     const LightApp_Operation* op = ( LightApp_Operation* )s;
405     MapOfOperation::const_iterator anIt = myOperations.begin(),
406                                    aLast = myOperations.end();
407     for( ; anIt!=aLast; anIt++ )
408       if( anIt.data()==op )
409       {
410         myOperations.remove( anIt.key() );
411         break;
412       }
413   }
414 }
415
416 LightApp_Displayer* LightApp_Module::displayer()
417 {
418   return 0;
419 }
420
421 void LightApp_Module::onShowHide()
422 {
423   if( !sender()->inherits( "QAction" ) || !popupMgr() )
424     return;
425
426   QAction* act = ( QAction* )sender();
427   int id = actionId( act );
428   if( id!=-1 )
429     startOperation( id );
430 }