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