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