]> SALOME platform Git repositories - modules/gui.git/blob - src/LightApp/LightApp_Module.cxx
Salome HOME
Join modifications from branch BR_3_1_0deb
[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* study )
125 {
126   delete mySwitchOp;
127   mySwitchOp = 0;
128
129   // abort all operations
130   MapOfOperation::const_iterator anIt;
131   for( anIt = myOperations.begin(); anIt != myOperations.end(); anIt++ ) {
132     anIt.data()->abort();
133   }
134
135   return CAM_Module::activateModule( study );
136 }
137
138 /*!NOT IMPLEMENTED*/
139 void LightApp_Module::MenuItem()
140 {
141 }
142
143 /*!NOT IMPLEMENTED*/
144 void LightApp_Module::createPreferences()
145 {
146 }
147
148 /*!NOT IMPLEMENTED*/
149 void LightApp_Module::preferencesChanged( const QString&, const QString& )
150 {
151 }
152
153 /*!Gets application.*/
154 LightApp_Application* LightApp_Module::getApp() const
155 {
156   return (LightApp_Application*)application();
157 }
158
159 /*!
160  * \brief Update something in accordance with update flags
161  * \param theFlags - update flags
162 *
163 * Update viewer or/and object browser etc. in accordance with update flags ( see
164 * LightApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
165 * own purposes
166 */
167 void LightApp_Module::update( const int theFlags )
168 {
169   if ( theFlags & UF_Model )
170   {
171     if( CAM_DataModel* aDataModel = dataModel() )
172       if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
173         aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
174   }
175   if ( theFlags & UF_ObjBrowser )
176     getApp()->objectBrowser()->updateTree( 0 );
177   if ( theFlags & UF_Controls )
178     updateControls();
179   if ( theFlags & UF_Viewer )
180   {
181     if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
182       if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
183       {
184         if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
185           ( (SVTK_ViewWindow*)viewWnd )->Repaint();
186         else if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
187           ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
188         else if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
189           ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
190         else if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
191           ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
192       }
193   }
194 }
195 /*!
196  * \brief Updates controls
197 *
198 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
199 * called from update( UF_Controls ). You may redefine it in concrete module.
200 */
201 void LightApp_Module::updateControls()
202 {
203 }
204
205 /*!Create new instance of data model and return it.*/
206 CAM_DataModel* LightApp_Module::createDataModel()
207 {
208   return new LightApp_DataModel(this);
209 }
210
211 /*!Create and return instance of LightApp_Selection.*/
212 LightApp_Selection* LightApp_Module::createSelection() const
213 {
214   return new LightApp_Selection();
215 }
216
217 /*!NOT IMPLEMENTED*/
218 void LightApp_Module::onModelOpened()
219 {
220 }
221
222 /*!NOT IMPLEMENTED*/
223 void LightApp_Module::onModelSaved()
224 {
225 }
226
227 /*!NOT IMPLEMENTED*/
228 void LightApp_Module::onModelClosed()
229 {
230 }
231
232 /*!Gets popup manager.(create if not exist)*/
233 QtxPopupMgr* LightApp_Module::popupMgr()
234 {
235   if ( !myPopupMgr )
236   {
237     myPopupMgr = new QtxPopupMgr( 0, this );
238
239     QPixmap p;
240     SUIT_Desktop* d = application()->desktop();
241     
242     QAction 
243       *disp = createAction( -1, tr( "TOP_DISPLAY" ), p, tr( "MEN_DISPLAY" ), tr( "STB_DISPLAY" ),
244                             0, d, false, this, SLOT( onShowHide() ) ),
245       *erase = createAction( -1, tr( "TOP_ERASE" ), p, tr( "MEN_ERASE" ), tr( "STB_ERASE" ),
246                              0, d, false, this, SLOT( onShowHide() ) ),
247       *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
248                                 0, d, false, this, SLOT( onShowHide() ) ),
249       *eraseAll = createAction( -1, tr( "TOP_ERASE_ALL" ), p, tr( "MEN_ERASE_ALL" ), tr( "STB_ERASE_ALL" ),
250                                 0, d, false, this, SLOT( onShowHide() ) );
251     myDisplay     = actionId( disp );
252     myErase       = actionId( erase );
253     myDisplayOnly = actionId( dispOnly );
254     myEraseAll    = actionId( eraseAll );
255
256     myPopupMgr->insert( disp, -1, 0 ); 
257     myPopupMgr->insert( erase, -1, 0 );
258     myPopupMgr->insert( dispOnly, -1, 0 );
259     myPopupMgr->insert( eraseAll, -1, 0 );
260     myPopupMgr->insert( separator(), -1, 0 );
261
262     QString oneAndNotActive = "( count( $component ) = 1 ) and ( component != activeModule )";
263     QString uniform = "true in $canBeDisplayed and %1 and ( activeModule = '%2' )";
264     uniform = uniform.arg( oneAndNotActive ).arg( name() );
265     myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, true );
266     myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, true );
267     myPopupMgr->setRule( dispOnly, uniform, true );
268     QString viewers = "{ '%1' '%2' '%3' }";
269     viewers = viewers.arg( SOCC_Viewer::Type() ).arg( SVTK_Viewer::Type() ).arg( SPlot2d_Viewer::Type() );
270     myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( viewers ), true );
271   }
272   return myPopupMgr;
273 }
274
275 /*!Gets preferences.*/
276 LightApp_Preferences* LightApp_Module::preferences() const
277 {
278   LightApp_Preferences* pref = 0;
279   if ( getApp() )
280     pref = getApp()->preferences();
281   return pref;
282 }
283
284 /*!Add preference to preferences.*/
285 int LightApp_Module::addPreference( const QString& label )
286 {
287   LightApp_Preferences* pref = preferences();
288   if ( !pref )
289     return -1;
290
291   int catId = pref->addPreference( moduleName(), -1 );
292   if ( catId == -1 )
293     return -1;
294
295   return pref->addPreference( label, catId );
296 }
297
298 /*!Add preference to preferences.*/
299 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
300                                     const QString& section, const QString& param )
301 {
302   LightApp_Preferences* pref = preferences();
303   if ( !pref )
304     return -1;
305
306   return pref->addPreference( moduleName(), label, pId, type, section, param );
307 }
308
309 /*!Gets property of preferences.*/
310 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
311 {
312   QVariant var;
313   LightApp_Preferences* pref = preferences();
314   if ( pref )
315     var = pref->itemProperty( id, prop );
316   return var;
317 }
318
319 /*!Set property of preferences.*/
320 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
321 {
322   LightApp_Preferences* pref = preferences();
323   if ( pref )
324     pref->setItemProperty( id, prop, var );
325 }
326
327 /*!
328  * \brief Starts operation with given identifier
329   * \param id - identifier of operation to be started
330 *
331 * Module stores operations in map. This method starts operation by id.
332 * If operation isn't in map, then it will be created by createOperation method
333 * and will be inserted to map
334 */
335 void LightApp_Module::startOperation( const int id )
336 {
337   LightApp_Operation* op = 0;
338   if( myOperations.contains( id ) )
339     op = myOperations[ id ];
340   else
341   {
342     op = createOperation( id );
343     if( op )
344     {
345       myOperations.insert( id, op );
346       op->setModule( this );
347       connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
348       connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
349     }
350   }
351
352   if( op )
353   {
354     // be sure that operation has correct study pointer
355     op->setStudy( application() ? application()->activeStudy() : 0 );
356     op->start();
357   }
358 }
359
360 /*!
361  * \brief Creates operation with given identifier
362   * \param id - identifier of operation to be started
363   * \return Pointer on created operation or NULL if operation is not created
364 *
365 * Creates operation with given id. You should not call this method, it will be called
366 * automatically from startOperation. You may redefine this method in concrete module to
367 * create operations. 
368 */
369 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
370 {
371   if( id==-1 )
372     return 0;
373
374   if( id==myDisplay )
375     return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
376   else if( id==myErase )
377     return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
378   else if( id==myDisplayOnly )
379     return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
380   else if( id==myEraseAll )
381     return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
382   else
383     return 0;
384 }
385
386 /*!
387  * \brief Virtual protected slot called when operation stopped
388  * \param theOp - stopped operation
389 *
390 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
391 * perform actions after stopping operation
392 */
393 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
394 {
395 }
396
397 /*!
398  * \brief Virtual protected slot called when operation destroyed
399   * \param theOp - destroyed operation
400 *
401 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
402 * perform actions after destroying operation. Base implementation removes pointer on
403 * destroyed operation from the map of operations
404 */
405 void LightApp_Module::onOperationDestroyed()
406 {
407   const QObject* s = sender();
408   if( s && s->inherits( "LightApp_Operation" ) )
409   {
410     const LightApp_Operation* op = ( LightApp_Operation* )s;
411     MapOfOperation::const_iterator anIt = myOperations.begin(),
412                                    aLast = myOperations.end();
413     for( ; anIt!=aLast; anIt++ )
414       if( anIt.data()==op )
415       {
416         myOperations.remove( anIt.key() );
417         break;
418       }
419   }
420 }
421
422 LightApp_Displayer* LightApp_Module::displayer()
423 {
424   return 0;
425 }
426
427 void LightApp_Module::onShowHide()
428 {
429   if( !sender()->inherits( "QAction" ) || !popupMgr() )
430     return;
431
432   QAction* act = ( QAction* )sender();
433   int id = actionId( act );
434   if( id!=-1 )
435     startOperation( id );
436 }