Salome HOME
Use LightApp library
[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
12 #include "LightApp_Preferences.h"
13 #include "SalomeApp_UpdateFlags.h"
14 #include "SalomeApp_Operation.h"
15 #include "SalomeApp_SwitchOp.h"
16
17 #include "SUIT_Operation.h"
18 #include "CAM_DataModel.h"
19
20 #include "OB_Browser.h"
21
22 #include <qstring.h>
23 #include <qmap.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 : LightApp_Module( name ),
35 mySwitchOp( 0 )
36 {
37 }
38
39 /*!Destructor.*/
40 SalomeApp_Module::~SalomeApp_Module()
41 {
42   if ( mySwitchOp )
43     delete mySwitchOp;
44 }
45
46 /*!Gets application.*/
47 SalomeApp_Application* SalomeApp_Module::getApp() const
48 {
49   return (SalomeApp_Application*)application();
50 }
51
52 /*!Activate module.*/
53 bool SalomeApp_Module::activateModule( SUIT_Study* study )
54 {
55   bool res = LightApp_Module::activateModule( study );
56
57   if ( mySwitchOp == 0 )
58     mySwitchOp = new SalomeApp_SwitchOp( this );
59
60   return res;
61 }
62
63 /*!Deactivate module.*/
64 bool SalomeApp_Module::deactivateModule( SUIT_Study* study )
65 {
66   bool res = LightApp_Module::deactivateModule( study );
67
68   delete mySwitchOp;
69   mySwitchOp = 0;
70
71   return res;
72 }
73
74 /*!Create new instance of data model and return it.*/
75 CAM_DataModel* SalomeApp_Module::createDataModel()
76 {
77   return new SalomeApp_DataModel(this);
78 }
79
80 /*!
81  * \brief Update something in accordance with update flags
82   * \param theFlags - update flags
83 *
84 * Update viewer or/and object browser etc. in accordance with update flags ( see
85 * SalomeApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
86 * own purposes
87 */
88 void SalomeApp_Module::update( const int theFlags )
89 {
90   if ( theFlags & UF_Model )
91   {
92     if( CAM_DataModel* aDataModel = dataModel() )
93       if( SalomeApp_DataModel* aModel = dynamic_cast<SalomeApp_DataModel*>( aDataModel ) ) {
94         SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( getApp()->activeStudy() );
95         if (aStudy)
96           aModel->update( 0, aStudy );
97       }
98   }
99   if ( theFlags & UF_ObjBrowser )
100     getApp()->objectBrowser()->updateTree( 0 );
101   if ( theFlags & UF_Controls )
102     updateControls();
103   if ( theFlags & UF_Viewer )
104   {
105     if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
106       if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
107       {
108         if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
109           ( (SVTK_ViewWindow*)viewWnd )->Repaint();
110         else if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
111           ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
112         else if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
113           ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
114         else if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
115           ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
116       }
117   }
118 }
119
120 /*!
121  * \brief Updates controls
122 *
123 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
124 * called from update( UF_Controls ). You may redefine it in concrete module.
125 */
126 void SalomeApp_Module::updateControls()
127 {
128 }
129
130 /*!
131  * \brief Starts operation with given identifier
132   * \param id - identifier of operation to be started
133 *
134 * Module stores operations in map. This method starts operation by id.
135 * If operation isn't in map, then it will be created by createOperation method
136 * and will be inserted to map
137 */
138 void SalomeApp_Module::startOperation( const int id )
139 {
140   SalomeApp_Operation* op = 0;
141   if( myOperations.contains( id ) )
142     op = myOperations[ id ];
143   else
144   {
145     op = createOperation( id );
146     if( op )
147     {
148       myOperations.insert( id, op );
149       op->setModule( this );
150       connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
151       connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
152     }
153   }
154
155   if( op )
156     op->start();
157 }
158
159 /*!
160  * \brief Creates operation with given identifier
161   * \param id - identifier of operation to be started
162   * \return Pointer on created operation or NULL if operation is not created
163 *
164 * Creates operation with given id. You should not call this method, it will be called
165 * automatically from startOperation. You may redefine this method in concrete module to
166 * create operations. 
167 */
168 SalomeApp_Operation* SalomeApp_Module::createOperation( const int /*id*/ ) const
169 {
170   return 0;
171 }
172
173 /*!
174  * \brief Virtual protected slot called when operation stopped
175   * \param theOp - stopped operation
176 *
177 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
178 * perform actions after stopping operation
179 */
180 void SalomeApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
181 {
182 }
183
184 /*!
185  * \brief Virtual protected slot called when operation destroyed
186   * \param theOp - destroyed operation
187 *
188 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
189 * perform actions after destroying operation. Base implementation removes pointer on
190 * destroyed operation from the map of operations
191 */
192 void SalomeApp_Module::onOperationDestroyed()
193 {
194   const QObject* s = sender();
195   if( s && s->inherits( "SalomeApp_Operation" ) )
196   {
197     const SalomeApp_Operation* op = ( SalomeApp_Operation* )s;
198     MapOfOperation::const_iterator anIt = myOperations.begin(),
199                                    aLast = myOperations.end();
200     for( ; anIt!=aLast; anIt++ )
201       if( anIt.data()==op )
202       {
203         myOperations.remove( anIt.key() );
204         break;
205       }
206   }
207 }