]> SALOME platform Git repositories - modules/gui.git/blob - src/SalomeApp/SalomeApp_Module.cxx
Salome HOME
Rolling back incorrect integration
[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
16 #include <OB_Browser.h>
17
18 #include <CAM_Study.h>
19
20 #include <SUIT_MessageBox.h>
21 #include <SUIT_ResourceMgr.h>
22
23 #include <QtxPopupMgr.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
32 /*!Constructor.*/
33 SalomeApp_Module::SalomeApp_Module( const QString& name )
34 : CAM_Module( name ),
35 myPopupMgr( 0 ),
36 mySwitchOp( 0 )
37 {
38 }
39
40 /*!Destructor.*/
41 SalomeApp_Module::~SalomeApp_Module()
42 {
43   if ( mySwitchOp )
44     delete mySwitchOp;
45 }
46
47 /*!Initialize module.*/
48 void SalomeApp_Module::initialize( CAM_Application* app )
49 {
50   CAM_Module::initialize( app );
51
52   SUIT_ResourceMgr* resMgr = app ? app->resourceMgr() : 0;
53   if ( resMgr )
54     resMgr->raiseTranslators( name() );
55 }
56
57 /*!Activate module.*/
58 bool SalomeApp_Module::activateModule( SUIT_Study* study )
59 {
60   bool res = CAM_Module::activateModule( study );
61
62   if ( res && application() && application()->resourceMgr() )
63     application()->resourceMgr()->raiseTranslators( name() );
64
65   if ( mySwitchOp == 0 )
66     mySwitchOp = new SalomeApp_SwitchOp( this );
67     
68   return res;
69 }
70
71 /*!Deactivate module.*/
72 bool SalomeApp_Module::deactivateModule( SUIT_Study* )
73 {
74   delete mySwitchOp;
75   mySwitchOp = 0;
76   
77   return true;
78 }
79
80 /*!NOT IMPLEMENTED*/
81 void SalomeApp_Module::selectionChanged()
82 {
83 }
84
85 /*!NOT IMPLEMENTED*/
86 void SalomeApp_Module::MenuItem()
87 {
88 }
89
90 /*!NOT IMPLEMENTED*/
91 void SalomeApp_Module::windows( QMap<int, int>& ) const
92 {
93 }
94
95 /*!NOT IMPLEMENTED*/
96 void SalomeApp_Module::viewManagers( QStringList& ) const
97 {
98 }
99
100 /*!NOT IMPLEMENTED*/
101 void SalomeApp_Module::createPreferences()
102 {
103 }
104
105 /*!NOT IMPLEMENTED*/
106 void SalomeApp_Module::preferencesChanged( const QString&, const QString& )
107 {
108 }
109
110 /*!Gets application.*/
111 SalomeApp_Application* SalomeApp_Module::getApp() const
112 {
113   return (SalomeApp_Application*)application();
114 }
115
116 /*!NOT IMPLEMENTED*/
117 void SalomeApp_Module::onModelOpened()
118 {
119 }
120
121 /*!NOT IMPLEMENTED*/
122 void SalomeApp_Module::onModelSaved()
123 {
124 }
125
126 /*!NOT IMPLEMENTED*/
127 void SalomeApp_Module::onModelClosed()
128 {
129 }
130
131 /*!Gets popup manager.(create if not exist)*/
132 QtxPopupMgr* SalomeApp_Module::popupMgr()
133 {
134   if ( !myPopupMgr )
135     myPopupMgr = new QtxPopupMgr( 0, this );
136   return myPopupMgr;
137 }
138
139 /*!Gets preferences.*/
140 SalomeApp_Preferences* SalomeApp_Module::preferences() const
141 {
142   SalomeApp_Preferences* pref = 0;
143   if ( getApp() )
144     pref = getApp()->preferences();
145   return pref;
146 }
147
148 /*!Create new instance of data model and return it.*/
149 CAM_DataModel* SalomeApp_Module::createDataModel()
150 {
151   return new SalomeApp_DataModel(this);
152 }
153
154 /*!Update object browser.*/
155 void SalomeApp_Module::updateObjBrowser( bool updateDataModel, SUIT_DataObject* root )
156 {
157   if( updateDataModel )
158     if( CAM_DataModel* aDataModel = dataModel() )
159       if( SalomeApp_DataModel* aModel = dynamic_cast<SalomeApp_DataModel*>( aDataModel ) )
160         aModel->update( 0, dynamic_cast<SalomeApp_Study*>( getApp()->activeStudy() ) );
161   getApp()->objectBrowser()->updateTree( root );
162 }
163
164 /*!Context menu popup.*/
165 void SalomeApp_Module::contextMenuPopup( const QString& client, QPopupMenu* menu, QString& /*title*/ )
166 {
167   SalomeApp_Selection* sel = createSelection();
168   sel->init( client, getApp()->selectionMgr() );
169   popupMgr()->updatePopup( menu, sel );
170   delete sel;
171 }
172
173 /*!Create and return instance of SalomeApp_Selection.*/
174 SalomeApp_Selection* SalomeApp_Module::createSelection() const
175 {
176   return new SalomeApp_Selection();
177 }
178
179 /*!Add preference to preferences.*/
180 int SalomeApp_Module::addPreference( const QString& label )
181 {
182   SalomeApp_Preferences* pref = preferences();
183   if ( !pref )
184     return -1;
185
186   int catId = pref->addPreference( moduleName(), -1 );
187   if ( catId == -1 )
188     return -1;
189                              
190   return pref->addPreference( label, catId );
191 }
192
193 /*!Add preference to preferences.*/
194 int SalomeApp_Module::addPreference( const QString& label, const int pId, const int type,
195                                      const QString& section, const QString& param )
196 {
197   SalomeApp_Preferences* pref = preferences();
198   if ( !pref )
199     return -1;
200
201   return pref->addPreference( moduleName(), label, pId, type, section, param );
202 }
203
204 /*!Gets property of preferences.*/
205 QVariant SalomeApp_Module::preferenceProperty( const int id, const QString& prop ) const
206 {
207   QVariant var;
208   SalomeApp_Preferences* pref = preferences();
209   if ( pref )
210     var = pref->itemProperty( id, prop );
211   return var;
212 }
213
214
215 /*!Set property of preferences.*/
216 void SalomeApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
217 {
218   SalomeApp_Preferences* pref = preferences();
219   if ( pref )
220     pref->setItemProperty( id, prop, var );
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 * SalomeApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
229 * own purposes
230 */
231 void SalomeApp_Module::update( const int theFlags )
232 {
233   if ( theFlags & UF_Model )
234   {
235     if( CAM_DataModel* aDataModel = dataModel() )
236       if( SalomeApp_DataModel* aModel = dynamic_cast<SalomeApp_DataModel*>( aDataModel ) )
237         aModel->update( 0, dynamic_cast<SalomeApp_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         if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
249           ( (SVTK_ViewWindow*)viewWnd )->Repaint();
250         else if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
251           ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
252         else if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
253           ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
254         else if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
255           ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
256       }
257   }
258 }
259
260 /*!
261  * \brief Updates controls
262 *
263 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
264 * called from update( UF_Controls ). You may redefine it in concrete module.
265 */
266 void SalomeApp_Module::updateControls()
267 {
268 }
269
270 /*!
271  * \brief Starts operation with given identifier
272   * \param id - identifier of operation to be started
273 *
274 * Module stores operations in map. This method starts operation by id.
275 * If operation isn't in map, then it will be created by createOperation method
276 * and will be inserted to map
277 */
278 void SalomeApp_Module::startOperation( const int id )
279 {
280   SalomeApp_Operation* op = 0;
281   if( myOperations.contains( id ) )
282     op = myOperations[ id ];
283   else
284   {
285     op = createOperation( id );
286     if( op )
287     {
288       myOperations.insert( id, op );
289       op->setModule( this );
290       connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
291       connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
292     }
293   }
294
295   if( op )
296     op->start();
297 }
298
299 /*!
300  * \brief Creates operation with given identifier
301   * \param id - identifier of operation to be started
302   * \return Pointer on created operation or NULL if operation is not created
303 *
304 * Creates operation with given id. You should not call this method, it will be called
305 * automatically from startOperation. You may redefine this method in concrete module to
306 * create operations. 
307 */
308 SalomeApp_Operation* SalomeApp_Module::createOperation( const int /*id*/ ) const
309 {
310   return 0;
311 }
312
313 /*!
314  * \brief Virtual protected slot called when operation stopped
315   * \param theOp - stopped operation
316 *
317 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
318 * perform actions after stopping operation
319 */
320 void SalomeApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
321 {
322 }
323
324 /*!
325  * \brief Virtual protected slot called when operation destroyed
326   * \param theOp - destroyed operation
327 *
328 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
329 * perform actions after destroying operation. Base implementation removes pointer on
330 * destroyed operation from the map of operations
331 */
332 void SalomeApp_Module::onOperationDestroyed()
333 {
334   const QObject* s = sender();
335   if( s && s->inherits( "SalomeApp_Operation" ) )
336   {
337     const SalomeApp_Operation* op = ( SalomeApp_Operation* )s;
338     MapOfOperation::const_iterator anIt = myOperations.begin(),
339                                    aLast = myOperations.end();
340     for( ; anIt!=aLast; anIt++ )
341       if( anIt.data()==op )
342       {
343         myOperations.remove( anIt.key() );
344         break;
345       }
346   }
347 }