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