Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / LightApp / LightApp_Module.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // File:      LightApp_Module.cxx
20 // Created:   6/20/2005 16:30:56 AM
21 // Author:    OCC team
22 // Copyright (C) CEA 2005
23
24 #include "LightApp_Module.h"
25
26 #include "CAM_Application.h"
27
28 #include "LightApp_Application.h"
29 #include "LightApp_DataModel.h"
30 #include "LightApp_DataObject.h"
31 #include "LightApp_Study.h"
32 #include "LightApp_Preferences.h"
33 #include "LightApp_Selection.h"
34 #include "LightApp_Operation.h"
35 #include "LightApp_SwitchOp.h"
36 #include "LightApp_UpdateFlags.h"
37 #include "LightApp_ShowHideOp.h"
38
39 #include "SUIT_Operation.h"
40 #include <SUIT_Study.h>
41 #include <SUIT_DataObject.h>
42 #include <SUIT_ResourceMgr.h>
43
44 #ifndef DISABLE_VTKVIEWER
45 #ifndef DISABLE_SALOMEOBJECT
46   #include <SVTK_ViewWindow.h>
47   #include <SVTK_ViewModel.h>
48 #else
49   #include <VTKViewer_ViewWindow.h>
50 #endif
51   #include <VTKViewer_ViewModel.h>
52 #endif
53 #ifndef DISABLE_OCCVIEWER
54   #include <OCCViewer_ViewWindow.h>
55   #include <OCCViewer_ViewPort3d.h>
56 #ifndef DISABLE_SALOMEOBJECT
57   #include <SOCC_ViewModel.h>
58 #else
59   #include <OCCViewer_ViewModel.h>
60 #endif
61 #endif
62 #ifndef DISABLE_GLVIEWER
63   #include <GLViewer_ViewFrame.h>
64   #include <GLViewer_ViewPort.h>
65 #endif
66 #ifndef DISABLE_PLOT2DVIEWER
67   #include <Plot2d_ViewWindow.h>
68   #include <Plot2d_ViewFrame.h>
69 #ifndef DISABLE_SALOMEOBJECT
70   #include <SPlot2d_ViewModel.h>
71 #else
72   #include <Plot2d_ViewModel.h>
73 #endif
74 #endif
75
76 #include <OB_Browser.h>
77
78 #include <QtxPopupMgr.h>
79
80 #include <qvariant.h>
81 #include <qstring.h>
82 #include <qstringlist.h>
83
84 /*!Constructor.*/
85 LightApp_Module::LightApp_Module( const QString& name )
86 : CAM_Module( name ),
87   myPopupMgr( 0 ),
88   mySwitchOp( 0 ),
89   myDisplay( -1 ),
90   myErase( -1 ),
91   myDisplayOnly( -1 ),
92   myEraseAll( -1 )
93 {
94 }
95
96 /*!Destructor.*/
97 LightApp_Module::~LightApp_Module()
98 {
99   if ( mySwitchOp )
100     delete mySwitchOp;
101 }
102
103 /*!Initialize module.*/
104 void LightApp_Module::initialize( CAM_Application* app )
105 {
106   CAM_Module::initialize( app );
107
108   SUIT_ResourceMgr* resMgr = app ? app->resourceMgr() : 0;
109   if ( resMgr )
110     resMgr->raiseTranslators( name() );
111 }
112
113 /*!NOT IMPLEMENTED*/
114 void LightApp_Module::windows( QMap<int, int>& ) const
115 {
116 }
117
118 /*!NOT IMPLEMENTED*/
119 void LightApp_Module::viewManagers( QStringList& ) const
120 {
121 }
122
123 /*!Context menu popup.*/
124 void LightApp_Module::contextMenuPopup( const QString& client, QPopupMenu* menu, QString& /*title*/ )
125 {
126   LightApp_Selection* sel = createSelection();
127   sel->init( client, getApp()->selectionMgr() );
128   popupMgr()->updatePopup( menu, sel );
129   delete sel;
130 }
131
132 /*!Update object browser.
133  * For updating model or whole object browser use update() method can be used.
134 */
135 void LightApp_Module::updateObjBrowser( bool theIsUpdateDataModel, 
136                                         SUIT_DataObject* theDataObject )
137 {
138   bool upd = getApp()->objectBrowser()->isAutoUpdate();
139   getApp()->objectBrowser()->setAutoUpdate( false );
140
141   if( theIsUpdateDataModel ){
142     if( CAM_DataModel* aDataModel = dataModel() ){
143       if ( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) ) {
144         SUIT_DataObject* aParent = NULL;
145         if(theDataObject && theDataObject != aDataModel->root())
146           aParent = theDataObject->parent();
147
148         LightApp_DataObject* anObject = dynamic_cast<LightApp_DataObject*>(theDataObject);
149         LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(getApp()->activeStudy());
150         aModel->update( anObject, aStudy );
151       }
152     }
153   }
154   getApp()->objectBrowser()->setAutoUpdate( upd );
155   getApp()->objectBrowser()->updateTree( 0, false );
156 }
157
158 /*!NOT IMPLEMENTED*/
159 void LightApp_Module::selectionChanged()
160 {
161 }
162
163 /*!Activate module.*/
164 bool LightApp_Module::activateModule( SUIT_Study* study )
165 {
166   bool res = CAM_Module::activateModule( study );
167
168   if ( res && application() && application()->resourceMgr() )
169     application()->resourceMgr()->raiseTranslators( name() );
170
171   connect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
172            this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
173   connect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
174            this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
175
176   if ( mySwitchOp == 0 )
177     mySwitchOp = new LightApp_SwitchOp( this );
178
179   return res;
180 }
181
182 /*!Deactivate module.*/
183 bool LightApp_Module::deactivateModule( SUIT_Study* study )
184 {
185   delete mySwitchOp;
186   mySwitchOp = 0;
187
188   disconnect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
189               this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
190   disconnect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
191               this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
192
193   // abort all operations
194   MapOfOperation::const_iterator anIt;
195   for( anIt = myOperations.begin(); anIt != myOperations.end(); anIt++ ) {
196     anIt.data()->abort();
197   }
198
199   return CAM_Module::activateModule( study );
200 }
201
202 /*!NOT IMPLEMENTED*/
203 void LightApp_Module::MenuItem()
204 {
205 }
206
207 /*!NOT IMPLEMENTED*/
208 void LightApp_Module::createPreferences()
209 {
210 }
211
212 /*!NOT IMPLEMENTED*/
213 void LightApp_Module::preferencesChanged( const QString&, const QString& )
214 {
215 }
216
217 /*!Gets application.*/
218 LightApp_Application* LightApp_Module::getApp() const
219 {
220   return (LightApp_Application*)application();
221 }
222
223 /*!
224  * \brief Update something in accordance with update flags
225  * \param theFlags - update flags
226 *
227 * Update viewer or/and object browser etc. in accordance with update flags ( see
228 * LightApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
229 * own purposes
230 */
231 void LightApp_Module::update( const int theFlags )
232 {
233   if ( theFlags & UF_Model )
234   {
235     if( CAM_DataModel* aDataModel = dataModel() )
236       if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
237         aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
238   }
239   if ( theFlags & UF_ObjBrowser )
240     getApp()->objectBrowser()->updateTree( 0 );
241   if ( theFlags & UF_Controls )
242     updateControls();
243   if ( theFlags & UF_Viewer )
244   {
245     if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
246       if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
247       {
248 #ifndef DISABLE_VTKVIEWER
249 #ifndef DISABLE_SALOMEOBJECT
250         if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
251           ( (SVTK_ViewWindow*)viewWnd )->Repaint();
252 #else
253         if ( viewWnd->inherits( "VTKViewer_ViewWindow" ) )
254           ( (VTKViewer_ViewWindow*)viewWnd )->Repaint();
255 #endif
256 #endif
257 #ifndef DISABLE_OCCVIEWER
258         if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
259           ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
260 #endif
261 #ifndef DISABLE_PLOT2DVIEWER
262         if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
263           ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
264 #endif
265 #ifndef DISABLE_GLVIEWER
266         if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
267           ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
268 #endif
269       }
270   }
271 }
272 /*!
273  * \brief Updates controls
274 *
275 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
276 * called from update( UF_Controls ). You may redefine it in concrete module.
277 */
278 void LightApp_Module::updateControls()
279 {
280 }
281
282 /*!Create new instance of data model and return it.*/
283 CAM_DataModel* LightApp_Module::createDataModel()
284 {
285   return new LightApp_DataModel(this);
286 }
287
288 /*!Create and return instance of LightApp_Selection.*/
289 LightApp_Selection* LightApp_Module::createSelection() const
290 {
291   return new LightApp_Selection();
292 }
293
294 /*!NOT IMPLEMENTED*/
295 void LightApp_Module::onModelOpened()
296 {
297 }
298
299 /*!NOT IMPLEMENTED*/
300 void LightApp_Module::onModelSaved()
301 {
302 }
303
304 /*!NOT IMPLEMENTED*/
305 void LightApp_Module::onModelClosed()
306 {
307 }
308
309 /*!Gets popup manager.(create if not exist)*/
310 QtxPopupMgr* LightApp_Module::popupMgr()
311 {
312   if ( !myPopupMgr )
313   {
314     myPopupMgr = new QtxPopupMgr( 0, this );
315
316     QPixmap p;
317     SUIT_Desktop* d = application()->desktop();
318     
319     QAction 
320       *disp = createAction( -1, tr( "TOP_SHOW" ), p, tr( "MEN_SHOW" ), tr( "STB_SHOW" ),
321                             0, d, false, this, SLOT( onShowHide() ) ),
322       *erase = createAction( -1, tr( "TOP_HIDE" ), p, tr( "MEN_HIDE" ), tr( "STB_HIDE" ),
323                              0, d, false, this, SLOT( onShowHide() ) ),
324       *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
325                                 0, d, false, this, SLOT( onShowHide() ) ),
326       *eraseAll = createAction( -1, tr( "TOP_ERASE_ALL" ), p, tr( "MEN_ERASE_ALL" ), tr( "STB_ERASE_ALL" ),
327                                 0, d, false, this, SLOT( onShowHide() ) );
328     myDisplay     = actionId( disp );
329     myErase       = actionId( erase );
330     myDisplayOnly = actionId( dispOnly );
331     myEraseAll    = actionId( eraseAll );
332
333     myPopupMgr->insert( disp, -1, 0 ); 
334     myPopupMgr->insert( erase, -1, 0 );
335     myPopupMgr->insert( dispOnly, -1, 0 );
336     myPopupMgr->insert( eraseAll, -1, 0 );
337     myPopupMgr->insert( separator(), -1, 0 );
338
339     QString oneAndNotActive = "( count( $component ) = 1 ) and ( not( activeModule in $component ) )";
340     QString uniform = "true in $canBeDisplayed and %1 and ( activeModule = '%2' )";
341     uniform = uniform.arg( oneAndNotActive ).arg( name() );
342     myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, true );
343     myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, true );
344     myPopupMgr->setRule( dispOnly, uniform, true );
345
346     QStringList viewers;
347
348 #ifndef DISABLE_OCCVIEWER
349 #ifndef DISABLE_SALOMEOBJECT
350     viewers.append( SOCC_Viewer::Type() );
351 #else
352     viewers.append( OCCViewer_Viewer::Type() );
353 #endif
354 #endif
355 #ifndef DISABLE_VTKVIEWER
356 #ifndef DISABLE_SALOMEOBJECT
357     viewers.append( SVTK_Viewer::Type() );
358 #else
359     viewers.append( VTKViewer_Viewer::Type() );
360 #endif
361 #endif
362 #ifndef DISABLE_PLOT2DVIEWER
363 #ifndef DISABLE_SALOMEOBJECT
364     viewers.append( SPlot2d_Viewer::Type() );
365 #else
366     viewers.append( Plot2d_Viewer::Type() );
367 #endif
368 #endif
369
370     if( !viewers.isEmpty() )
371     {
372       QString strViewers = "{ ", temp = "'%1' ";
373       QStringList::const_iterator anIt = viewers.begin(), aLast = viewers.end();
374       for( ; anIt!=aLast; anIt++ )
375         strViewers+=temp.arg( *anIt );
376       strViewers+="}";
377       myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( strViewers ), true );
378     }
379   }
380   return myPopupMgr;
381 }
382
383 /*!Gets preferences.*/
384 LightApp_Preferences* LightApp_Module::preferences() const
385 {
386   LightApp_Preferences* pref = 0;
387   if ( getApp() )
388     pref = getApp()->preferences();
389   return pref;
390 }
391
392 /*!Add preference to preferences.*/
393 int LightApp_Module::addPreference( const QString& label )
394 {
395   LightApp_Preferences* pref = preferences();
396   if ( !pref )
397     return -1;
398
399   int catId = pref->addPreference( moduleName(), -1 );
400   if ( catId == -1 )
401     return -1;
402
403   return pref->addPreference( label, catId );
404 }
405
406 /*!Add preference to preferences.*/
407 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
408                                     const QString& section, const QString& param )
409 {
410   LightApp_Preferences* pref = preferences();
411   if ( !pref )
412     return -1;
413
414   return pref->addPreference( moduleName(), label, pId, type, section, param );
415 }
416
417 /*!Gets property of preferences.*/
418 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
419 {
420   QVariant var;
421   LightApp_Preferences* pref = preferences();
422   if ( pref )
423     var = pref->itemProperty( id, prop );
424   return var;
425 }
426
427 /*!Set property of preferences.*/
428 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
429 {
430   LightApp_Preferences* pref = preferences();
431   if ( pref )
432     pref->setItemProperty( id, prop, var );
433 }
434
435 /*!
436  * \brief Starts operation with given identifier
437   * \param id - identifier of operation to be started
438 *
439 * Module stores operations in map. This method starts operation by id.
440 * If operation isn't in map, then it will be created by createOperation method
441 * and will be inserted to map
442 */
443 void LightApp_Module::startOperation( const int id )
444 {
445   LightApp_Operation* op = 0;
446   if( myOperations.contains( id ) )
447     op = myOperations[ id ];
448   else
449   {
450     op = createOperation( id );
451     if( op )
452     {
453       myOperations.insert( id, op );
454       op->setModule( this );
455       connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
456       connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
457     }
458   }
459
460   if( op )
461   {
462     // be sure that operation has correct study pointer
463     op->setStudy( application() ? application()->activeStudy() : 0 );
464     op->start();
465   }
466 }
467
468 /*!
469  * \brief Creates operation with given identifier
470   * \param id - identifier of operation to be started
471   * \return Pointer on created operation or NULL if operation is not created
472 *
473 * Creates operation with given id. You should not call this method, it will be called
474 * automatically from startOperation. You may redefine this method in concrete module to
475 * create operations. 
476 */
477 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
478 {
479   if( id==-1 )
480     return 0;
481
482   if( id==myDisplay )
483     return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
484   else if( id==myErase )
485     return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
486   else if( id==myDisplayOnly )
487     return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
488   else if( id==myEraseAll )
489     return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
490   else
491     return 0;
492 }
493
494 /*!
495  * \brief Virtual protected slot called when operation stopped
496  * \param theOp - stopped operation
497 *
498 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
499 * perform actions after stopping operation
500 */
501 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
502 {
503 }
504
505 /*!
506  * \brief Virtual protected slot called when operation destroyed
507   * \param theOp - destroyed operation
508 *
509 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
510 * perform actions after destroying operation. Base implementation removes pointer on
511 * destroyed operation from the map of operations
512 */
513 void LightApp_Module::onOperationDestroyed()
514 {
515   const QObject* s = sender();
516   if( s && s->inherits( "LightApp_Operation" ) )
517   {
518     const LightApp_Operation* op = ( LightApp_Operation* )s;
519     MapOfOperation::const_iterator anIt = myOperations.begin(),
520                                    aLast = myOperations.end();
521     for( ; anIt!=aLast; anIt++ )
522       if( anIt.data()==op )
523       {
524         myOperations.remove( anIt.key() );
525         break;
526       }
527   }
528 }
529
530 /*!
531   Must be redefined in order to use standard displayer mechanism
532   \return displayer of module
533 */
534 LightApp_Displayer* LightApp_Module::displayer()
535 {
536   return 0;
537 }
538
539 /*!
540   SLOT: called on activating of standard operations show/hide
541 */
542 void LightApp_Module::onShowHide()
543 {
544   if( !sender()->inherits( "QAction" ) || !popupMgr() )
545     return;
546
547   QAction* act = ( QAction* )sender();
548   int id = actionId( act );
549   if( id!=-1 )
550     startOperation( id );
551 }
552
553 /*!
554   virtual SLOT: called on view manager adding
555 */
556 void LightApp_Module::onViewManagerAdded( SUIT_ViewManager* )
557 {
558 }
559
560 /*!
561   virtual SLOT: called on view manager removing
562 */
563 void LightApp_Module::onViewManagerRemoved( SUIT_ViewManager* )
564 {
565 }