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