Salome HOME
3a87b3745d61362cfd844290038401ebd291a238
[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_DataOwner.h>
42 #include <LightApp_GVSelector.h>
43 #include <LightApp_SelectionMgr.h>
44 #include <LightApp_UpdateFlags.h>
45
46 #include <SALOME_Event.h>
47
48 #include <SUIT_Study.h>
49 #include <SUIT_ViewManager.h>
50
51 #include <QAction>
52 #include <QApplication>
53 #include <QMenu>
54
55 extern "C" HYDRO_EXPORT CAM_Module* createModule()
56 {
57   return new HYDROGUI_Module();
58 }
59
60 HYDROGUI_Module::HYDROGUI_Module()
61 : LightApp_Module( "HYDRO" ),
62   myDisplayer( 0 ),
63   myIsUpdateEnabled( true )
64 {
65 }
66
67 HYDROGUI_Module::~HYDROGUI_Module()
68 {
69 }
70
71 int HYDROGUI_Module::getStudyId() const
72 {
73   LightApp_Application* anApp = getApp();
74   return anApp ? anApp->activeStudy()->id() : 0;
75 }
76
77 void HYDROGUI_Module::initialize( CAM_Application* theApp )
78 {
79   LightApp_Module::initialize( theApp );
80
81   createActions();
82   createUndoRedoActions();
83   createMenus();
84   createPopups();
85   createToolbars();
86
87   setMenuShown( false );
88   setToolShown( false );
89
90   myDisplayer = new HYDROGUI_Displayer( this );
91 }
92
93 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
94 {
95   bool aRes = LightApp_Module::activateModule( theStudy );
96
97   setMenuShown( true );
98   setToolShown( true );
99
100   update( UF_All );
101
102   updateCommandsStatus();
103
104   return aRes;
105 }
106
107 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
108 {
109   setMenuShown( false );
110   setToolShown( false );
111
112   return LightApp_Module::deactivateModule( theStudy );
113 }
114
115 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
116 {
117   theMap.clear();
118   theMap.insert( LightApp_Application::WT_LogWindow,     Qt::BottomDockWidgetArea );
119   theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea   );
120 }
121
122 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
123 {
124   theTypesList << GraphicsView_Viewer::Type();
125 }
126
127 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
128                                         QMenu* theMenu,
129                                         QString& theTitle )
130 {
131   HYDROGUI_DataModel* aModel = getDataModel();
132
133   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
134   if( !aSelectionMgr )
135     return;
136
137   SUIT_DataOwnerPtrList anOwners;
138   aSelectionMgr->selected( anOwners );
139
140   bool anIsSelection = false;
141   bool anIsVisibleInSelection = false;
142   bool anIsHiddenInSelection = false;
143
144   bool anIsImage = false;
145   bool anIsPolyline = false;
146
147   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
148   {
149     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
150     {
151       Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
152       if( !anObject.IsNull() )
153       {
154         anIsSelection = true;
155
156         bool aVisibility = anObject->GetVisibility();
157         anIsVisibleInSelection |= aVisibility;
158         anIsHiddenInSelection |= !aVisibility;
159
160         if( anObject->GetKind() == KIND_IMAGE )
161           anIsImage = true;
162
163         if( anObject->GetKind() == KIND_POLYLINE )
164           anIsPolyline = true;
165       }
166     }
167   }
168
169   if( anOwners.count() == 1 && anIsImage )
170   {
171     theMenu->addAction( action( EditImageId ) );
172     theMenu->addSeparator();
173   }
174
175   if( anOwners.count() == 1 && anIsPolyline )
176   {
177     theMenu->addAction( action( EditPolylineId ) );
178     theMenu->addSeparator();
179   }
180
181   if( anIsSelection )
182   {
183     theMenu->addAction( action( DeleteId ) );
184     theMenu->addSeparator();
185   }
186
187   if( anIsSelection )
188   {
189     if( anIsHiddenInSelection )
190       theMenu->addAction( action( ShowId ) );
191     theMenu->addAction( action( ShowOnlyId ) );
192     if( anIsVisibleInSelection )
193       theMenu->addAction( action( HideId ) );
194     theMenu->addSeparator();
195   }
196   theMenu->addAction( action( ShowAllId ) );
197   theMenu->addAction( action( HideAllId ) );
198   theMenu->addSeparator();
199 }
200
201 void HYDROGUI_Module::update( const int flags )
202 {
203   if( !isUpdateEnabled() )
204     return;
205
206   QApplication::setOverrideCursor( Qt::WaitCursor );
207
208   // To prevent calling this method recursively
209   // from one of the methods called below
210   setUpdateEnabled( false );
211
212   if( ( flags & UF_Model ) && getDataModel() )
213   {
214     getDataModel()->update( getStudyId() );
215   }
216   else
217   {
218     /* to do
219     if( ( flags & UF_ObjBrowser ) && getObjectBrowser() )
220       updateObjectBrowser();
221     */
222   }
223
224   if( ( flags & UF_Viewer ) )
225     updateGV( flags & UF_GV_Init,
226               flags & UF_GV_Forced );
227
228   if( ( flags & UF_Controls ) && getApp() )
229     getApp()->updateActions();
230
231   setUpdateEnabled( true );
232
233   QApplication::restoreOverrideCursor();
234 }
235
236 void HYDROGUI_Module::updateCommandsStatus()
237 {
238   LightApp_Module::updateCommandsStatus();
239
240   updateUndoRedoControls();
241
242   // to do
243   //action( ... )->setEnabled( ... );
244 }
245
246 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
247 {
248   return (HYDROGUI_DataModel*)dataModel();
249 }
250
251 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
252 {
253   return myDisplayer;
254 }
255
256 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theViewerId ) const
257 {
258   ViewManagerList aViewManagerList;
259   getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewManagerList );
260
261   ViewManagerList::iterator anIter, anIterEnd = aViewManagerList.end();
262   for( anIter = aViewManagerList.begin(); anIter != anIterEnd; anIter++ )
263   {
264     GraphicsView_ViewManager* aViewManager =
265       dynamic_cast<GraphicsView_ViewManager*>( *anIter );
266     if( aViewManager && aViewManager->getId() == theViewerId )
267       return aViewManager->getViewer();
268   }
269   return NULL;
270 }
271
272 CAM_DataModel* HYDROGUI_Module::createDataModel()
273 {
274   return new HYDROGUI_DataModel( this );
275 }
276
277 void HYDROGUI_Module::customEvent( QEvent* e )
278 {
279   int aType = e->type();
280   if ( aType == NewViewEvent )
281   {
282     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
283     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
284     {
285       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
286       {
287         //update( UF_Viewer | UF_GV_Forced ); // ouv: to do
288         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
289
290         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
291         {
292           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
293           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
294           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
295
296           // ouv: tmp
297           aViewPort->setMousePositionEnabled( true );
298         }
299       }
300     }
301   }
302 }
303
304 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
305 {
306   QEvent::Type aType = theEvent->type();
307   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
308   {
309     if( aType == QEvent::Show )
310     {
311       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
312       e->setData( theObj );
313       QApplication::postEvent( this, e );
314       theObj->removeEventFilter( this );
315     }
316   }
317   return LightApp_Module::eventFilter( theObj, theEvent );
318 }
319
320 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
321 {
322   LightApp_Module::onViewManagerAdded( theViewManager );
323
324   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
325   { 
326     createSelector( theViewManager ); // replace the default selector
327
328     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
329              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
330   }
331 }
332
333 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
334 {
335   LightApp_Module::onViewManagerRemoved( theViewManager );
336
337   createSelector( theViewManager ); // replace the default selector
338 }
339
340 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
341 {
342   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
343   {
344     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
345     {
346       aViewFrame->installEventFilter( this );
347     }
348   }
349 }
350
351 void HYDROGUI_Module::updateGV( const bool theIsInit,
352                                 const bool theIsForced )
353 {
354   if( !getDisplayer() )
355     return;
356
357   ViewManagerList aViewManagerList;
358   getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewManagerList );
359
360   ViewManagerList::iterator anIter, anIterEnd = aViewManagerList.end();
361   for( anIter = aViewManagerList.begin(); anIter != anIterEnd; anIter++ )
362   {
363     GraphicsView_ViewManager* aViewManager =
364       dynamic_cast<GraphicsView_ViewManager*>( *anIter );
365     if( aViewManager )
366       getDisplayer()->UpdateAll( aViewManager->getId(), theIsInit, theIsForced );
367   }
368 }
369
370 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
371 {
372   if( !theViewManager )
373     return;
374
375   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
376   if( !aSelectionMgr )
377     return;
378
379   QString aViewType = theViewManager->getType();
380   if( aViewType != GraphicsView_Viewer::Type() )
381     return;
382
383   GraphicsView_ViewManager* aViewManager =
384     dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
385   if( !aViewManager )
386     return;
387
388   QList<SUIT_Selector*> aSelectorList;
389   aSelectionMgr->selectors( aViewType, aSelectorList );
390
391   // disable all alien selectors
392   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
393   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
394   {
395     SUIT_Selector* aSelector = *anIter;
396     if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
397       aSelector->setEnabled( false );
398   }
399
400   new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
401 }
402
403 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
404 {
405   bool aPrevState = myIsUpdateEnabled;
406   myIsUpdateEnabled = theState;
407   return aPrevState;
408 }
409
410 bool HYDROGUI_Module::isUpdateEnabled() const
411 {
412   return myIsUpdateEnabled;
413 }