Salome HOME
670246bed1438d7f2f234464732e1e069b6cf39e
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Module.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Module.h"
24
25 #include "HYDROGUI.h"
26 #include "HYDROGUI_DataModel.h"
27 #include "HYDROGUI_Displayer.h"
28 #include "HYDROGUI_GVSelector.h"
29 #include "HYDROGUI_InputPanel.h"
30 #include "HYDROGUI_ObjSelector.h"
31 #include "HYDROGUI_Operations.h"
32 #include "HYDROGUI_PrsImage.h"
33 #include "HYDROGUI_UpdateFlags.h"
34
35 #include <GraphicsView_ViewFrame.h>
36 #include <GraphicsView_ViewManager.h>
37 #include <GraphicsView_ViewPort.h>
38 #include <GraphicsView_Viewer.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_GVSelector.h>
42 #include <LightApp_SelectionMgr.h>
43 #include <LightApp_UpdateFlags.h>
44
45 #include <SUIT_Study.h>
46 #include <SUIT_ViewManager.h>
47
48 #include <QApplication>
49
50 extern "C" HYDRO_EXPORT CAM_Module* createModule()
51 {
52   return new HYDROGUI_Module();
53 }
54
55 HYDROGUI_Module::HYDROGUI_Module()
56 : LightApp_Module( "HYDRO" ),
57   myDisplayer( 0 ),
58   myIsUpdateEnabled( true )
59 {
60 }
61
62 HYDROGUI_Module::~HYDROGUI_Module()
63 {
64 }
65
66 int HYDROGUI_Module::getStudyId() const
67 {
68   LightApp_Application* anApp = getApp();
69   return anApp ? anApp->activeStudy()->id() : 0;
70 }
71
72 void HYDROGUI_Module::initialize( CAM_Application* theApp )
73 {
74   LightApp_Module::initialize( theApp );
75
76   CreateActions();
77   CreateMenus();
78   CreatePopups();
79   CreateToolbars();
80
81   setMenuShown( false );
82
83   myDisplayer = new HYDROGUI_Displayer( this );
84 }
85
86 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
87 {
88   setMenuShown( true );
89   return LightApp_Module::activateModule( theStudy );
90 }
91
92 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
93 {
94   theMap.clear();
95   theMap.insert( LightApp_Application::WT_LogWindow,     Qt::BottomDockWidgetArea );
96   theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea   );
97 }
98
99 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
100 {
101   theTypesList << GraphicsView_Viewer::Type();
102 }
103
104 void HYDROGUI_Module::update( const int flags )
105 {
106   if( !isUpdateEnabled() )
107     return;
108
109   QApplication::setOverrideCursor( Qt::WaitCursor );
110
111   // To prevent calling this method recursively
112   // from one of the methods called below
113   setUpdateEnabled( false );
114
115   if( ( flags & UF_Model ) && getDataModel() )
116   {
117     getDataModel()->update( getStudyId() );
118   }
119   else
120   {
121     /* to do
122     if( ( flags & UF_ObjBrowser ) && getObjectBrowser() )
123       updateObjectBrowser();
124     */
125   }
126
127   if( ( flags & UF_Viewer ) )
128     updateGV( flags & UF_GV_Init,
129               flags & UF_GV_Forced );
130
131   if( ( flags & UF_Controls ) && getApp() )
132     getApp()->updateActions();
133
134   setUpdateEnabled( true );
135
136   QApplication::restoreOverrideCursor();
137 }
138
139 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
140 {
141   return (HYDROGUI_DataModel*)dataModel();
142 }
143
144 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
145 {
146   return myDisplayer;
147 }
148
149 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theViewerId ) const
150 {
151   ViewManagerList aViewManagerList;
152   getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewManagerList );
153
154   ViewManagerList::iterator anIter, anIterEnd = aViewManagerList.end();
155   for( anIter = aViewManagerList.begin(); anIter != anIterEnd; anIter++ )
156   {
157     GraphicsView_ViewManager* aViewManager =
158       dynamic_cast<GraphicsView_ViewManager*>( *anIter );
159     if( aViewManager && aViewManager->getId() == theViewerId )
160       return aViewManager->getViewer();
161   }
162   return NULL;
163 }
164
165 CAM_DataModel* HYDROGUI_Module::createDataModel()
166 {
167   return new HYDROGUI_DataModel( this );
168 }
169
170 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
171 {
172   LightApp_Module::onViewManagerAdded( theViewManager );
173
174   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
175   { 
176     createSelector( theViewManager ); // replace the default selector
177
178     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
179              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
180   }
181 }
182
183 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
184 {
185   LightApp_Module::onViewManagerRemoved( theViewManager );
186
187   createSelector( theViewManager ); // replace the default selector
188 }
189
190 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theWnd )
191 {
192 }
193
194 void HYDROGUI_Module::updateGV( const bool theIsInit,
195                                 const bool theIsForced )
196 {
197   if( !getDisplayer() )
198     return;
199
200   ViewManagerList aViewManagerList;
201   getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewManagerList );
202
203   ViewManagerList::iterator anIter, anIterEnd = aViewManagerList.end();
204   for( anIter = aViewManagerList.begin(); anIter != anIterEnd; anIter++ )
205   {
206     GraphicsView_ViewManager* aViewManager =
207       dynamic_cast<GraphicsView_ViewManager*>( *anIter );
208     if( aViewManager )
209       getDisplayer()->UpdateAll( aViewManager->getId(), theIsInit, theIsForced );
210   }
211 }
212
213 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
214 {
215   if( !theViewManager )
216     return;
217
218   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
219   if( !aSelectionMgr )
220     return;
221
222   QString aViewType = theViewManager->getType();
223   if( aViewType != GraphicsView_Viewer::Type() )
224     return;
225
226   GraphicsView_ViewManager* aViewManager =
227     dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
228   if( !aViewManager )
229     return;
230
231   QList<SUIT_Selector*> aSelectorList;
232   aSelectionMgr->selectors( aViewType, aSelectorList );
233
234   // disable all alien selectors
235   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
236   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
237   {
238     SUIT_Selector* aSelector = *anIter;
239     if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
240       aSelector->setEnabled( false );
241   }
242
243   new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
244 }
245
246 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
247 {
248   bool aPrevState = myIsUpdateEnabled;
249   myIsUpdateEnabled = theState;
250   return aPrevState;
251 }
252
253 bool HYDROGUI_Module::isUpdateEnabled() const
254 {
255   return myIsUpdateEnabled;
256 }