Salome HOME
Minor fix.
[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_Tool.h"
34 #include "HYDROGUI_UpdateFlags.h"
35
36 #include <GraphicsView_ViewFrame.h>
37 #include <GraphicsView_ViewManager.h>
38 #include <GraphicsView_ViewPort.h>
39 #include <GraphicsView_Viewer.h>
40
41 #include <LightApp_Application.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 <QGraphicsSceneMouseEvent>
54 #include <QMenu>
55
56 static int ViewManagerId = 0;
57
58 extern "C" HYDRO_EXPORT CAM_Module* createModule()
59 {
60   return new HYDROGUI_Module();
61 }
62
63 HYDROGUI_Module::HYDROGUI_Module()
64 : LightApp_Module( "HYDRO" ),
65   myDisplayer( 0 ),
66   myIsUpdateEnabled( true )
67 {
68 }
69
70 HYDROGUI_Module::~HYDROGUI_Module()
71 {
72 }
73
74 int HYDROGUI_Module::getStudyId() const
75 {
76   LightApp_Application* anApp = getApp();
77   return anApp ? anApp->activeStudy()->id() : 0;
78 }
79
80 void HYDROGUI_Module::initialize( CAM_Application* theApp )
81 {
82   LightApp_Module::initialize( theApp );
83
84   createActions();
85   createUndoRedoActions();
86   createMenus();
87   createPopups();
88   createToolbars();
89
90   setMenuShown( false );
91   setToolShown( false );
92
93   myDisplayer = new HYDROGUI_Displayer( this );
94 }
95
96 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
97 {
98   bool aRes = LightApp_Module::activateModule( theStudy );
99
100   setMenuShown( true );
101   setToolShown( true );
102
103   update( UF_All );
104
105   updateCommandsStatus();
106
107   return aRes;
108 }
109
110 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
111 {
112   ViewManagerMapIterator anIter( myViewManagerMap );
113   while( anIter.hasNext() )
114     if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
115       getApp()->removeViewManager( aViewManager );
116   myViewManagerMap.clear();
117
118   setMenuShown( false );
119   setToolShown( false );
120
121   return LightApp_Module::deactivateModule( theStudy );
122 }
123
124 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
125 {
126   theMap.clear();
127   theMap.insert( LightApp_Application::WT_LogWindow,     Qt::BottomDockWidgetArea );
128   theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea   );
129 }
130
131 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
132 {
133   theTypesList << GraphicsView_Viewer::Type();
134 }
135
136 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
137                                         QMenu* theMenu,
138                                         QString& theTitle )
139 {
140   HYDROGUI_DataModel* aModel = getDataModel();
141
142   bool anIsSelection = false;
143   bool anIsVisibleInSelection = false;
144   bool anIsHiddenInSelection = false;
145
146   bool anIsImage = false;
147   bool anIsPolyline = false;
148
149   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
150   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
151   {
152     Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
153     if( !anObject.IsNull() )
154     {
155       anIsSelection = true;
156
157       bool aVisibility = anObject->GetVisibility();
158       anIsVisibleInSelection |= aVisibility;
159       anIsHiddenInSelection |= !aVisibility;
160
161       if( anObject->GetKind() == KIND_IMAGE )
162         anIsImage = true;
163
164       if( anObject->GetKind() == KIND_POLYLINE )
165         anIsPolyline = true;
166     }
167   }
168
169   if( aSeq.Length() == 1 )
170   {
171     if( anIsImage )
172     {
173       theMenu->addAction( action( EditImageId ) );
174       theMenu->addAction( action( ObserveImageId ) );
175       theMenu->addAction( action( ExportImageId ) );
176       theMenu->addSeparator();
177     }
178     else if( anIsPolyline )
179     {
180       theMenu->addAction( action( EditPolylineId ) );
181       theMenu->addSeparator();
182     }
183   }
184
185   if( anIsSelection )
186   {
187     theMenu->addAction( action( DeleteId ) );
188     theMenu->addSeparator();
189   }
190
191   if( anIsSelection )
192   {
193     if( anIsHiddenInSelection )
194       theMenu->addAction( action( ShowId ) );
195     theMenu->addAction( action( ShowOnlyId ) );
196     if( anIsVisibleInSelection )
197       theMenu->addAction( action( HideId ) );
198     theMenu->addSeparator();
199   }
200   theMenu->addAction( action( ShowAllId ) );
201   theMenu->addAction( action( HideAllId ) );
202   theMenu->addSeparator();
203 }
204
205 void HYDROGUI_Module::update( const int flags )
206 {
207   if( !isUpdateEnabled() )
208     return;
209
210   QApplication::setOverrideCursor( Qt::WaitCursor );
211
212   // To prevent calling this method recursively
213   // from one of the methods called below
214   setUpdateEnabled( false );
215
216   if( ( flags & UF_Model ) && getDataModel() )
217     getDataModel()->update( getStudyId() );
218
219   if( ( flags & UF_ObjBrowser ) && getApp() )
220     getApp()->updateObjectBrowser( true );
221
222   if( ( flags & UF_Viewer ) )
223     updateGV( flags & UF_GV_Init,
224               flags & UF_GV_Forced );
225
226   if( ( flags & UF_Controls ) && getApp() )
227     getApp()->updateActions();
228
229   setUpdateEnabled( true );
230
231   QApplication::restoreOverrideCursor();
232 }
233
234 void HYDROGUI_Module::updateCommandsStatus()
235 {
236   LightApp_Module::updateCommandsStatus();
237
238   updateUndoRedoControls();
239
240   // to do
241   //action( ... )->setEnabled( ... );
242 }
243
244 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
245 {
246   return (HYDROGUI_DataModel*)dataModel();
247 }
248
249 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
250 {
251   return myDisplayer;
252 }
253
254 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
255 {
256   if( myViewManagerMap.contains( theId ) )
257   {
258     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
259     GraphicsView_ViewManager* aViewManager =
260       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
261     if( aViewManager )
262       return aViewManager->getViewer();
263   }
264   return NULL;
265 }
266
267 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
268 {
269   ViewManagerMapIterator anIter( myViewManagerMap );
270   while( anIter.hasNext() )
271   {
272     int anId = anIter.next().key();
273     const ViewManagerInfo& anInfo = anIter.value();
274     if( anInfo.first == theViewManager )
275       return anId;
276   }
277   return -1;
278 }
279
280 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
281 {
282   int anId = getViewManagerId( theViewManager );
283   if( anId != -1 )
284   {
285     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
286     return anInfo.second;
287   }
288   return VMR_Unknown;
289 }
290
291 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
292                                           const ViewManagerRole theRole )
293 {
294   int anId = getViewManagerId( theViewManager );
295   if( anId != -1 )
296   {
297     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
298     anInfo.second = theRole;
299   }
300 }
301
302 CAM_DataModel* HYDROGUI_Module::createDataModel()
303 {
304   return new HYDROGUI_DataModel( this );
305 }
306
307 void HYDROGUI_Module::customEvent( QEvent* e )
308 {
309   int aType = e->type();
310   if ( aType == NewViewEvent )
311   {
312     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
313     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
314     {
315       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
316       {
317         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
318         ViewManagerRole aRole = getViewManagerRole( aViewManager );
319         if( aRole == VMR_General )
320           update( UF_Viewer );
321
322         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
323
324         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
325         {
326           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
327           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
328           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
329
330           // ouv: tmp
331           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
332         }
333       }
334     }
335   }
336 }
337
338 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
339 {
340   QEvent::Type aType = theEvent->type();
341   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
342   {
343     if( aType == QEvent::Show )
344     {
345       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
346       e->setData( theObj );
347       QApplication::postEvent( this, e );
348       theObj->removeEventFilter( this );
349     }
350   }
351   return LightApp_Module::eventFilter( theObj, theEvent );
352 }
353
354 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
355 {
356   LightApp_Module::onViewManagerAdded( theViewManager );
357
358   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
359   { 
360     createSelector( theViewManager ); // replace the default selector
361
362     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
363              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
364   }
365
366   ViewManagerInfo anInfo( theViewManager, VMR_General );
367   myViewManagerMap.insert( ViewManagerId++, anInfo );
368 }
369
370 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
371 {
372   LightApp_Module::onViewManagerRemoved( theViewManager );
373
374   createSelector( theViewManager ); // replace the default selector
375
376   int anId = getViewManagerId( theViewManager );
377   if( anId != -1 )
378     myViewManagerMap.remove( anId );
379 }
380
381 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
382 {
383   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
384   {
385     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
386     {
387       aViewFrame->installEventFilter( this );
388
389       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
390
391       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
392                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
393       return;
394     }
395   }
396 }
397
398 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
399 {
400   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
401   {
402     SUIT_ViewManager* aViewManager = 0;
403
404     QObject* aParent = aViewPort;
405     while( aParent = aParent->parent() )
406     {
407       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
408       {
409         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
410         {
411           aViewManager = aViewer->getViewManager();
412           break;
413         }
414       }
415     }
416
417     if( !aViewManager )
418       return;
419
420     double aMouseX = theEvent->scenePos().x();
421     double aMouseY = theEvent->scenePos().y();
422
423     ViewManagerRole aRole = getViewManagerRole( aViewManager );
424     if( aRole == VMR_General )
425     {
426       int aXDeg = 0, aYDeg = 0;
427       int aXMin = 0, aYMin = 0;
428       double aXSec = 0, aYSec = 0;
429       HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
430       HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
431
432       QString aDegSymbol( QChar( 0x00B0 ) );
433       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
434       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
435
436       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
437     }
438     else if( aRole == VMR_TransformImage )
439       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
440   }
441 }
442
443 void HYDROGUI_Module::updateGV( const bool theIsInit,
444                                 const bool theIsForced )
445 {
446   if( !getDisplayer() )
447     return;
448
449   QList<int> aViewManagerIdList;
450
451   // currently, all views are updated
452   ViewManagerMapIterator anIter( myViewManagerMap );
453   while( anIter.hasNext() )
454   {
455     int anId = anIter.next().key();
456     aViewManagerIdList.append( anId );
457   }
458
459   QListIterator<int> anIdIter( aViewManagerIdList );
460   while( anIdIter.hasNext() )
461     getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
462 }
463
464 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
465 {
466   if( !theViewManager )
467     return;
468
469   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
470   if( !aSelectionMgr )
471     return;
472
473   QString aViewType = theViewManager->getType();
474   if( aViewType != GraphicsView_Viewer::Type() )
475     return;
476
477   GraphicsView_ViewManager* aViewManager =
478     dynamic_cast<GraphicsView_ViewManager*>( theViewManager );
479   if( !aViewManager )
480     return;
481
482   QList<SUIT_Selector*> aSelectorList;
483   aSelectionMgr->selectors( aViewType, aSelectorList );
484
485   // disable all alien selectors
486   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
487   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
488   {
489     SUIT_Selector* aSelector = *anIter;
490     if( aSelector && !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) )
491       aSelector->setEnabled( false );
492   }
493
494   new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
495 }
496
497 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
498 {
499   bool aPrevState = myIsUpdateEnabled;
500   myIsUpdateEnabled = theState;
501   return aPrevState;
502 }
503
504 bool HYDROGUI_Module::isUpdateEnabled() const
505 {
506   return myIsUpdateEnabled;
507 }