Salome HOME
Refs #279 - Behavior of polyline object is strange after changing the name
[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_DataObject.h"
28 #include "HYDROGUI_Displayer.h"
29 #include "HYDROGUI_GVSelector.h"
30 #include "HYDROGUI_InputPanel.h"
31 #include "HYDROGUI_ObjSelector.h"
32 #include "HYDROGUI_OCCDisplayer.h"
33 #include "HYDROGUI_OCCSelector.h"
34 #include "HYDROGUI_Operations.h"
35 #include "HYDROGUI_PrsImage.h"
36 #include "HYDROGUI_Tool.h"
37 #include "HYDROGUI_UpdateFlags.h"
38 #include "HYDROGUI_Shape.h"
39 #include "HYDROGUI_VTKPrs.h"
40 #include "HYDROGUI_VTKPrsDisplayer.h"
41 #include "HYDROGUI_AbstractDisplayer.h"
42 #include "HYDROGUI_PolylineOp.h"
43 #include "HYDROGUI_SetColorOp.h"
44 #include "HYDROGUI_ImportGeomObjectOp.h"
45
46 #include <HYDROData_Image.h>
47 #include <HYDROData_Profile.h>
48 #include <HYDROData_Lambert93.h>
49 #include <HYDROData_Tool.h>
50
51 #include <HYDROData_OperationsFactory.h>
52
53 #include <CurveCreator_Utils.h>
54
55 #include <GraphicsView_ViewFrame.h>
56 #include <GraphicsView_ViewManager.h>
57 #include <GraphicsView_ViewPort.h>
58 #include <GraphicsView_Viewer.h>
59
60 #include <ImageComposer_CutOperator.h>
61 #include <ImageComposer_CropOperator.h>
62 #include <ImageComposer_FuseOperator.h>
63
64 #include <LightApp_Application.h>
65 #include <LightApp_DataOwner.h>
66 #include <LightApp_GVSelector.h>
67 #include <LightApp_SelectionMgr.h>
68 #include <LightApp_UpdateFlags.h>
69
70 #include <SalomeApp_Study.h>
71
72 #include <OCCViewer_ViewFrame.h>
73 #include <OCCViewer_ViewManager.h>
74 #include <OCCViewer_ViewModel.h>
75
76 #include <SALOME_Event.h>
77
78 #include <SUIT_DataBrowser.h>
79 #include <SUIT_Desktop.h>
80 #include <SUIT_Study.h>
81 #include <SUIT_ViewManager.h>
82 #include <SUIT_ResourceMgr.h>
83
84 #include <SVTK_ViewManager.h>
85 #include <SVTK_ViewModel.h>
86 #include <SVTK_ViewWindow.h>
87 #include <SVTK_Selector.h>
88 #include <SVTK_RenderWindowInteractor.h>
89
90 #include <OCCViewer_ViewPort3d.h>
91
92 #include <GEOMUtils.hxx>
93 #include <GeometryGUI.h>
94
95 #include <SALOMEDS_wrap.hxx>
96
97 #include <QAction>
98 #include <QApplication>
99 #include <QGraphicsSceneMouseEvent>
100 #include <QMenu>
101 #include <QMouseEvent>
102 #include <QStatusBar>
103
104 static int ViewManagerId = 0;
105
106 extern "C" HYDRO_EXPORT CAM_Module* createModule()
107 {
108   return new HYDROGUI_Module();
109 }
110
111 extern "C" HYDRO_EXPORT char* getModuleVersion()
112 {
113   return (char*)HYDRO_VERSION;
114 }
115
116 HYDROGUI_Module::HYDROGUI_Module()
117 : LightApp_Module( "HYDRO" ),
118   myDisplayer( 0 ),
119   myOCCDisplayer( 0 ),
120   myIsUpdateEnabled( true )
121 {
122 }
123
124 HYDROGUI_Module::~HYDROGUI_Module()
125 {
126 }
127
128 int HYDROGUI_Module::getStudyId() const
129 {
130   LightApp_Application* anApp = getApp();
131   return anApp ? anApp->activeStudy()->id() : 0;
132 }
133
134 void HYDROGUI_Module::initialize( CAM_Application* theApp )
135 {
136   LightApp_Module::initialize( theApp );
137
138   createActions();
139   createUndoRedoActions();
140   createMenus();
141   createPopups();
142   createToolbars();
143
144   setMenuShown( false );
145   setToolShown( false );
146
147   myDisplayer = new HYDROGUI_Displayer( this );
148   myOCCDisplayer = new HYDROGUI_OCCDisplayer( this );
149   myVTKDisplayer = new HYDROGUI_VTKPrsDisplayer( this );
150 }
151
152 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
153 {
154   bool aRes = LightApp_Module::activateModule( theStudy );
155
156   LightApp_Application* anApp = getApp();
157   SUIT_Desktop* aDesktop = anApp->desktop();
158
159   getApp()->setEditEnabled( false ); // hide SalomeApp copy/paste actions
160
161   setMenuShown( true );
162   setToolShown( true );
163
164 #ifndef DISABLE_PYCONSOLE
165   aDesktop->tabifyDockWidget( HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_PyConsole ) ), 
166                               HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_LogWindow ) ) );
167 #endif
168
169   // Remove defunct view managers from the map.
170   // It's essential to do this before "update( UF_All )" call!
171   QList<int> anObsoleteIds;
172   ViewManagerList anAllViewManagers = anApp->viewManagers();
173   ViewManagerList aHydroViewManagers; // view managers created inside the HYDRO module
174   ViewManagerMapIterator anIter( myViewManagerMap );
175   while( anIter.hasNext() ) {
176     int anId = anIter.next().key();
177     const ViewManagerInfo& anInfo = anIter.value();
178   
179     aHydroViewManagers << anInfo.first;
180
181     if ( !anAllViewManagers.contains( anInfo.first ) ) {
182       anObsoleteIds << anId;
183     }
184   }
185   foreach ( const int anId, anObsoleteIds ) {
186     myViewManagerMap.remove( anId );
187     myObjectStateMap.remove( anId );
188     myShapesMap.remove( anId );
189     myVTKPrsMap.remove( anId );
190   }
191   // Replace the default selector for all view managers.
192   // Add view managers created outside of HYDRO module to the map.
193   foreach ( SUIT_ViewManager* aViewManager, anAllViewManagers ) {
194     createSelector( aViewManager ); // replace the default selector
195     if ( !aHydroViewManagers.contains( aViewManager ) ) {
196       ViewManagerInfo anInfo( aViewManager, VMR_General );
197       myViewManagerMap.insert( ViewManagerId++, anInfo );
198     }
199   }
200
201   update( UF_All );
202
203   updateCommandsStatus();
204
205   HYDROGUI_Tool::setOCCActionShown( this, OCCViewer_ViewWindow::MaximizedId, false );
206
207   ViewManagerList anOCCViewManagers;
208   anApp->viewManagers( OCCViewer_Viewer::Type(), anOCCViewManagers );
209   foreach ( const SUIT_ViewManager* aViewManager, anOCCViewManagers ) {
210     connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
211              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
212     foreach( SUIT_ViewWindow* aViewWindow, aViewManager->getViews() ) {
213       OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aViewWindow );
214       if ( aViewFrame && aViewFrame->getViewPort() ) {
215         aViewFrame->getViewPort()->installEventFilter( this );
216       }
217     }
218   }
219
220   // Load GEOM data
221   SalomeApp_Study* aStudy = 
222     dynamic_cast<SalomeApp_Study*>( getApp()->activeStudy() );
223   if ( aStudy ) {
224     SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() );
225     GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
226     if ( !aGeomEngine->_is_nil() && !aGeomEngine->_is_nil() ) {
227       SALOMEDS::StudyBuilder_var aStudyBuilder = aDSStudy->NewBuilder();
228       SALOMEDS::SComponent_wrap GEOM_var = aDSStudy->FindComponent( "GEOM" );
229       if( !GEOM_var->_is_nil() ) {
230         aStudyBuilder->LoadWith( GEOM_var, aGeomEngine );
231       }
232     }
233   }
234
235   return aRes;
236 }
237
238 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
239 {
240   /* Issues ## 68, 88.
241   ViewManagerMapIterator anIter( myViewManagerMap );
242   while( anIter.hasNext() )
243     if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
244       getApp()->removeViewManager( aViewManager );
245   myViewManagerMap.clear();
246   */
247
248   ViewManagerList anOCCViewManagers;
249   getApp()->viewManagers( OCCViewer_Viewer::Type(), anOCCViewManagers );
250   foreach ( const SUIT_ViewManager* aViewManager, anOCCViewManagers ) {
251     disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
252                 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
253   }
254
255   /* Issues ## 68, 88.
256   myObjectStateMap.clear();
257   myShapesMap.clear();
258   myVTKPrsMap.clear();
259   */
260
261   // clear the data model's list of copying objects
262   HYDROGUI_DataModel::changeCopyingObjects( HYDROData_SequenceOfObjects() );
263
264   setMenuShown( false );
265   setToolShown( false );
266
267   getApp()->setEditEnabled( true ); // show SalomeApp copy/paste actions
268
269   HYDROGUI_Tool::setOCCActionShown( this, OCCViewer_ViewWindow::MaximizedId, true );
270
271   return LightApp_Module::deactivateModule( theStudy );
272 }
273
274 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
275 {
276   theMap.clear();
277   theMap.insert( LightApp_Application::WT_LogWindow,     Qt::BottomDockWidgetArea );
278 #ifndef DISABLE_PYCONSOLE
279   theMap.insert( LightApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
280 #endif
281   theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea   );
282 }
283
284 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
285 {
286   theTypesList << GraphicsView_Viewer::Type() << OCCViewer_Viewer::Type();
287 }
288
289 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
290                                         QMenu* theMenu,
291                                         QString& theTitle )
292 {
293   HYDROGUI_DataModel* aModel = getDataModel();
294
295   bool anIsObjectBrowser = theClient == getApp()->objectBrowser()->popupClientType();
296   bool anIsGraphicsView = theClient == GraphicsView_Viewer::Type();
297   bool anIsOCCView = theClient == OCCViewer_Viewer::Type();
298   bool anIsVTKView = theClient == SVTK_Viewer::Type();
299   if( !anIsObjectBrowser && !anIsGraphicsView && !anIsOCCView && !anIsVTKView )
300     return;
301
302   size_t anActiveViewId = HYDROGUI_Tool::GetActiveViewId( this );
303
304   bool anIsSelectedDataObjects = false;
305   bool anIsVisibleInSelection = false;
306   bool anIsHiddenInSelection = false;
307
308   bool anIsImage = false;
309   bool anIsImportedImage = false;
310   bool anIsImageHasRefs = false;
311   bool anIsFusedImage = false;
312   bool anIsCutImage = false;
313   bool anIsSplittedImage = false;
314   bool anIsMustObjectBeUpdated = false;
315   bool anIsPolyline = false;
316   bool anIsPolyline3D = false;
317   bool anIsProfile = false;
318   bool anIsValidProfile = false;
319   bool anAllAreProfiles = false;
320   bool anIsBathymetry = false;
321   bool anIsCalculation = false;
322   bool anIsImmersibleZone = false;
323   bool anIsVisualState = false;
324   bool anIsRegion = false;
325   bool anIsZone = false;
326   bool anIsObstacle = false;
327   bool anIsStream = false;
328   bool anIsChannel = false;
329   bool anIsDigue = false;
330   bool anIsDummyObject3D = false;
331   bool anIsGroup = false;
332   bool anIsObjectCanBeColored = false;
333
334   // Check the selected GEOM objects (take into account the Object Browser only)
335   if ( anIsObjectBrowser ) {
336     QList<GEOM::shape_type> anObstacleTypes = 
337       HYDROGUI_ImportGeomObjectOp::getObstacleTypes();
338     QList<GEOM::shape_type> aPolylineTypes = 
339       HYDROGUI_ImportGeomObjectOp::getPolylineTypes();
340
341     bool isCanBeImportedAsObstacle = 
342       !HYDROGUI_Tool::GetSelectedGeomObjects( this, anObstacleTypes ).isEmpty();
343     bool isCanBeImportedAsPolyline = 
344       !HYDROGUI_Tool::GetSelectedGeomObjects( this, aPolylineTypes ).isEmpty();
345
346     // Add import as obstacle action
347     if ( isCanBeImportedAsObstacle ) {
348       theMenu->addAction( action( ImportGeomObjectAsObstacleId ) );
349     }
350     // Add import as polyline action
351     if ( isCanBeImportedAsPolyline ) {
352       theMenu->addAction( action( ImportGeomObjectAsPolylineId ) );
353     }
354     // Add separator
355     if ( isCanBeImportedAsObstacle || isCanBeImportedAsPolyline ) {
356       theMenu->addSeparator();
357     }
358   }
359
360   // check the selected data model objects
361   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
362   int aNbOfSelectedProfiles = 0;
363   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
364   {
365     Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
366     if( !anObject.IsNull() )
367     {
368       anIsSelectedDataObjects = true;
369
370       bool aVisibility = isObjectVisible( anActiveViewId, anObject );
371       anIsVisibleInSelection |= aVisibility;
372       anIsHiddenInSelection |= !aVisibility;
373
374       if ( anObject->CanBeUpdated() && anObject->IsMustBeUpdated() )
375       {
376         anIsMustObjectBeUpdated = true;
377       }
378
379       ObjectKind anObjectKind = anObject->GetKind();
380       if( anObjectKind == KIND_IMAGE )
381       {
382         anIsImage = true;
383         Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
384         if( !anImage.IsNull() )
385         {
386           anIsImportedImage = anImage->HasLocalPoints();
387           anIsImageHasRefs = anImage->HasReferences();
388           if( HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory() )
389           {
390             if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
391             {
392               QString anOperatorName = anOperator->name();
393               if( anOperatorName == ImageComposer_FuseOperator::Type() )
394                 anIsFusedImage = true;
395               else if( anOperatorName == ImageComposer_CutOperator::Type() )
396                 anIsCutImage = true;
397               else if( anOperatorName == ImageComposer_CropOperator::Type() )
398                 anIsSplittedImage = true;
399             }
400           }
401         }
402       }
403       else if( anObjectKind == KIND_POLYLINEXY )
404         anIsPolyline = true;
405       else if( anObjectKind == KIND_POLYLINE )
406         anIsPolyline3D = true;
407       else if( anObjectKind == KIND_PROFILE )
408       {
409         anIsProfile = true;
410         aNbOfSelectedProfiles++;
411
412         Handle(HYDROData_Profile) aProfile = 
413           Handle(HYDROData_Profile)::DownCast( anObject );
414         if( !aProfile.IsNull() && aProfile->IsValid() ) {
415           anIsValidProfile = true;
416         }
417       }
418       else if( anObjectKind == KIND_CALCULATION )
419         anIsCalculation = true;
420       else if( anObjectKind == KIND_IMMERSIBLE_ZONE )
421         anIsImmersibleZone = true;
422       else if( anObjectKind == KIND_VISUAL_STATE )
423         anIsVisualState = true;
424       else if( anObjectKind == KIND_REGION )
425         anIsRegion = true;
426       else if( anObjectKind == KIND_ZONE )
427         anIsZone = true;
428       else if( anObjectKind == KIND_BATHYMETRY )
429         anIsBathymetry = true;
430       else if( anObjectKind == KIND_OBSTACLE )
431         anIsObstacle = true;
432       else if( anObjectKind == KIND_STREAM )
433         anIsStream = true;
434       else if( anObjectKind == KIND_CHANNEL )
435         anIsChannel = true;
436       else if( anObjectKind == KIND_DIGUE )
437         anIsDigue = true;
438       else if( anObjectKind == KIND_DUMMY_3D )
439         anIsDummyObject3D = true;
440       else if( anObjectKind == KIND_SHAPES_GROUP || anObjectKind == KIND_SPLITTED_GROUP )
441         anIsGroup = true;
442     }
443
444     if ( !anIsObjectCanBeColored )
445       anIsObjectCanBeColored = HYDROGUI_SetColorOp::CanObjectBeColored( anObject );
446   }
447
448   // Check if all selected objects are profiles
449   anAllAreProfiles = ( aNbOfSelectedProfiles > 0 ) &&
450                      ( aNbOfSelectedProfiles == aSeq.Length() );
451
452   // check the selected partitions
453   if( !anIsSelectedDataObjects && anIsObjectBrowser )
454   {
455     ObjectKind aSelectedPartition = HYDROGUI_Tool::GetSelectedPartition( this );
456     if( aSelectedPartition != KIND_UNKNOWN )
457     {
458       switch( aSelectedPartition )
459       {
460         case KIND_IMAGE:
461           theMenu->addAction( action( ImportImageId ) );
462           break;
463         case KIND_BATHYMETRY:
464           theMenu->addAction( action( ImportBathymetryId ) );
465           break;
466         case KIND_ARTIFICIAL_OBJECT:
467           theMenu->addAction( action( CreateChannelId ) );
468           theMenu->addAction( action( CreateDigueId ) );
469           break;
470         case KIND_NATURAL_OBJECT:
471           theMenu->addAction( action( CreateImmersibleZoneId ) );
472           theMenu->addAction( action( CreateStreamId ) );
473           break;
474         case KIND_OBSTACLE:
475           theMenu->addAction( action( ImportObstacleFromFileId ) );
476           theMenu->addAction( action( CreateBoxId ) );
477           theMenu->addAction( action( CreateCylinderId ) );
478           break;
479         case KIND_CALCULATION:
480           theMenu->addAction( action( CreateCalculationId ) );
481           break;
482         case KIND_POLYLINEXY:
483           theMenu->addAction( action( CreatePolylineId ) );
484           break;
485         case KIND_POLYLINE:
486           theMenu->addAction( action( CreatePolyline3DId ) );
487           break;
488         case KIND_PROFILE:
489           theMenu->addAction( action( CreateProfileId ) );
490           theMenu->addAction( action( ImportProfilesId ) );
491           theMenu->addAction( action( AllGeoreferencementId ) );
492           break;
493         case KIND_VISUAL_STATE:
494           theMenu->addAction( action( SaveVisualStateId ) );
495           break;
496       }
497       theMenu->addSeparator();
498     }
499   }
500
501   if( anIsSelectedDataObjects )
502   {
503     if ( anIsMustObjectBeUpdated )
504     {
505       theMenu->addAction( action( UpdateObjectId ) );
506       theMenu->addSeparator();
507     }
508     else
509     {
510       theMenu->addAction( action( ForcedUpdateObjectId ) );
511       theMenu->addSeparator();
512     }
513
514
515     if( aSeq.Length() == 1 )
516     {
517       if( anIsImage )
518       {
519         if( anIsImportedImage )
520           theMenu->addAction( action( EditImportedImageId ) );
521         else if( anIsImageHasRefs )
522         {
523           if( anIsFusedImage )
524             theMenu->addAction( action( EditFusedImageId ) );
525           else if( anIsCutImage )
526             theMenu->addAction( action( EditCutImageId ) );
527           else if( anIsSplittedImage )
528             theMenu->addAction( action( EditSplittedImageId ) );
529         }
530
531         //RKV: BUG#98: theMenu->addAction( action( ObserveImageId ) );
532         theMenu->addAction( action( ExportImageId ) );
533         theMenu->addSeparator();
534
535         if( anIsImageHasRefs )
536         {
537           theMenu->addAction( action( RemoveImageRefsId ) );
538           theMenu->addSeparator();
539         }
540
541         theMenu->addAction( action( FuseImagesId ) );
542         theMenu->addAction( action( CutImagesId ) );
543         theMenu->addAction( action( SplitImageId ) );
544         theMenu->addSeparator();
545       }
546       else if( anIsBathymetry )
547       {
548         theMenu->addAction( action( EditImportedBathymetryId ) );
549         theMenu->addAction( action( BathymetryBoundsId ) );
550         theMenu->addSeparator();
551       }
552       else if( anIsPolyline )
553       {
554         theMenu->addAction( action( EditPolylineId ) );
555         theMenu->addSeparator();
556       }
557       else if( anIsPolyline3D )
558       {
559         theMenu->addAction( action( EditPolyline3DId ) );
560         theMenu->addSeparator();
561       }
562       else if( anIsProfile )
563       {
564         theMenu->addAction( action( EditProfileId ) );
565         theMenu->addAction( action( SelectedGeoreferencementId ) );
566         theMenu->addSeparator();
567       }
568       else if( anIsCalculation )
569       {
570         theMenu->addAction( action( EditCalculationId ) );
571         theMenu->addAction( action( ExportCalculationId ) );
572         theMenu->addSeparator();
573       }
574       else if( anIsImmersibleZone )
575       {
576         theMenu->addAction( action( EditImmersibleZoneId ) );
577         theMenu->addSeparator();
578       }
579       else if( anIsStream )
580       {
581         theMenu->addAction( action( EditStreamId ) );
582         theMenu->addSeparator();
583       }
584       else if( anIsChannel )
585       {
586         theMenu->addAction( action( EditChannelId ) );
587         theMenu->addSeparator();
588       }
589       else if( anIsDigue )
590       {
591         theMenu->addAction( action( EditDigueId ) );
592         theMenu->addSeparator();
593       }
594       else if( anIsVisualState && anIsObjectBrowser )
595       {
596         theMenu->addAction( action( SaveVisualStateId ) );
597         theMenu->addAction( action( LoadVisualStateId ) );
598         theMenu->addSeparator();
599       }
600
601       // Add set color action for geometrical objects
602       if ( anIsObjectCanBeColored )
603       {
604         theMenu->addAction( action( SetColorId ) );
605         theMenu->addSeparator();
606       }
607
608       // Add copy action
609       if( aModel->canCopy() ) {
610         theMenu->addAction( action( CopyId ) );
611         theMenu->addSeparator();
612       }
613     } else if ( anAllAreProfiles ) {
614       theMenu->addAction( action( SelectedGeoreferencementId ) );
615       theMenu->addSeparator();
616     }
617
618     if( !anIsDummyObject3D )
619       theMenu->addAction( action( DeleteId ) );
620     theMenu->addSeparator();
621
622     if( anIsImage || anIsPolyline || anIsPolyline3D || 
623         anIsImmersibleZone || anIsZone || anIsRegion ||
624         anIsBathymetry || anIsObstacle || anIsStream ||
625         anIsChannel || anIsDigue || anIsDummyObject3D ||
626         anIsValidProfile || anIsGroup )
627     {
628       if( anIsHiddenInSelection )
629         theMenu->addAction( action( ShowId ) );
630       theMenu->addAction( action( ShowOnlyId ) );
631       if( anIsVisibleInSelection )
632         theMenu->addAction( action( HideId ) );
633       theMenu->addSeparator();
634     }
635   }
636
637   if ( anIsOCCView )
638   {
639     SUIT_Operation* anOp = application()->activeStudy()->activeOperation();
640     HYDROGUI_PolylineOp* aPolylineOp = dynamic_cast<HYDROGUI_PolylineOp*>( anOp );
641     if ( aPolylineOp && aPolylineOp->deleteEnabled() )
642       theMenu->addAction( action( DeleteId ) );
643   }
644
645   if( anIsObjectBrowser || anIsGraphicsView || anIsOCCView || anIsVTKView )
646   {
647     theMenu->addAction( action( ShowAllId ) );
648     theMenu->addAction( action( HideAllId ) );
649     theMenu->addSeparator();
650   }
651 }
652
653 void HYDROGUI_Module::update( const int flags )
654 {
655   if( !isUpdateEnabled() )
656     return;
657
658   QApplication::setOverrideCursor( Qt::WaitCursor );
659
660   // To prevent calling this method recursively
661   // from one of the methods called below
662   setUpdateEnabled( false );
663
664   // store selected objects
665   QStringList aSelectedEntries = storeSelection();
666
667   bool aDoFitAll = flags & UF_FitAll;
668   if( ( flags & UF_Viewer ) )
669     updateViewer( getDisplayer(), flags & UF_GV_Init, flags & UF_GV_Forced, aDoFitAll ); 
670
671   if( ( flags & UF_OCCViewer ) )
672     updateViewer( getOCCDisplayer(), flags & UF_OCC_Init, flags & UF_OCC_Forced, aDoFitAll ); 
673
674   if( ( flags & UF_VTKViewer ) )
675     updateViewer( getVTKDisplayer(), flags & UF_VTK_Init, flags & UF_VTK_Forced, aDoFitAll ); 
676
677   if( ( flags & UF_Model ) && getDataModel() && getApp() )
678   {
679     getDataModel()->update( getStudyId() );
680
681     // Temporary workaround to prevent breaking
682     // the selection in the object browser.
683     // Note: processEvents() should be called after updateGV(),
684     // otherwise the application crashes from time to time.
685     //RKV: qApp->processEvents();
686     SUIT_ResourceMgr* aResMgr = getApp()->resourceMgr();
687     bool isResizeOnExpandItem = aResMgr->booleanValue( "ObjectBrowser", "resize_on_expand_item", false );
688     SUIT_DataBrowser* anObjectBrowser = getApp()->objectBrowser();
689     if ( isResizeOnExpandItem && anObjectBrowser ) {
690       anObjectBrowser->setResizeOnExpandItem( false ); // MZN: ISSUE #280 
691     }
692     getApp()->updateObjectBrowser( true );
693     if ( isResizeOnExpandItem && anObjectBrowser ) {
694       anObjectBrowser->setResizeOnExpandItem( true ); // MZN: ISSUE #280 
695     }
696   }
697
698   // Object browser is currently updated by using UF_Model flag
699   if( ( flags & UF_ObjBrowser ) && ((flags & UF_Model) == 0) && getApp() )
700     getApp()->updateObjectBrowser( true );
701
702   if( ( flags & UF_Controls ) && getApp() )
703     getApp()->updateActions();
704
705   // restore selected objects
706   restoreSelection( aSelectedEntries );
707
708   setUpdateEnabled( true );
709
710   QApplication::restoreOverrideCursor();
711 }
712
713 void HYDROGUI_Module::updateCommandsStatus()
714 {
715   LightApp_Module::updateCommandsStatus();
716
717   updateUndoRedoControls();
718
719   action( CopyId )->setEnabled( getDataModel()->canCopy() );
720   action( PasteId )->setEnabled( getDataModel()->canPaste() );
721 }
722
723 void HYDROGUI_Module::selectionChanged()
724 {
725   LightApp_Module::selectionChanged();
726   updateCommandsStatus();
727 }
728
729 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
730 {
731   return (HYDROGUI_DataModel*)dataModel();
732 }
733
734 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
735 {
736   return myDisplayer;
737 }
738
739 HYDROGUI_OCCDisplayer* HYDROGUI_Module::getOCCDisplayer() const
740 {
741   return myOCCDisplayer;
742 }
743
744 HYDROGUI_VTKPrsDisplayer* HYDROGUI_Module::getVTKDisplayer() const
745 {
746   return myVTKDisplayer;
747 }
748
749 SUIT_ViewManager* HYDROGUI_Module::getViewManager( const int theId ) const
750 {
751   if( myViewManagerMap.contains( theId ) )
752   {
753     return myViewManagerMap[ theId ].first;
754   }
755   return NULL;
756 }
757
758 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
759 {
760   if( myViewManagerMap.contains( theId ) )
761   {
762     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
763     GraphicsView_ViewManager* aViewManager =
764       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
765     if( aViewManager )
766       return aViewManager->getViewer();
767   }
768   return NULL;
769 }
770
771 OCCViewer_Viewer* HYDROGUI_Module::getOCCViewer( const int theId ) const
772 {
773   if( myViewManagerMap.contains( theId ) )
774   {
775     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
776     OCCViewer_ViewManager* aViewManager =
777       ::qobject_cast<OCCViewer_ViewManager*>( anInfo.first );
778     if( aViewManager )
779       return aViewManager->getOCCViewer();
780   }
781   return NULL;
782 }
783
784 SVTK_Viewer* HYDROGUI_Module::getVTKViewer( const int theId ) const
785 {
786   if( myViewManagerMap.contains( theId ) )
787   {
788     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
789     SVTK_ViewManager* aViewManager =
790       ::qobject_cast<SVTK_ViewManager*>( anInfo.first );
791     if( aViewManager )
792       return dynamic_cast<SVTK_Viewer*>( aViewManager->getViewModel() );
793   }
794   return NULL;
795 }
796
797 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
798 {
799   ViewManagerMapIterator anIter( myViewManagerMap );
800   while( anIter.hasNext() )
801   {
802     int anId = anIter.next().key();
803     const ViewManagerInfo& anInfo = anIter.value();
804     if( anInfo.first == theViewManager )
805       return anId;
806   }
807   return -1;
808 }
809
810 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
811 {
812   int anId = getViewManagerId( theViewManager );
813   if( anId != -1 )
814   {
815     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
816     return anInfo.second;
817   }
818   return VMR_Unknown;
819 }
820
821 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
822                                           const ViewManagerRole theRole )
823 {
824   int anId = getViewManagerId( theViewManager );
825   if( anId != -1 )
826   {
827     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
828     anInfo.second = theRole;
829   }
830 }
831
832 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
833                                        const Handle(HYDROData_Entity)& theObject ) const
834 {
835   if( theObject.IsNull() )
836     return false;
837
838   ViewId2Entry2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
839   if( anIter1 != myObjectStateMap.end() )
840   {
841     const Entry2ObjectStateMap& aEntry2ObjectStateMap = anIter1.value();
842     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
843
844     Entry2ObjectStateMap::const_iterator anIter2 = aEntry2ObjectStateMap.find( anEntry );
845     if( anIter2 != aEntry2ObjectStateMap.end() )
846     {
847       const ObjectState& anObjectState = anIter2.value();
848       return anObjectState.Visibility;
849     }
850   }
851   return false;
852 }
853
854 void HYDROGUI_Module::setObjectVisible( const int theViewId,
855                                         const Handle(HYDROData_Entity)& theObject,
856                                         const bool theState )
857 {
858   if( !theObject.IsNull() )
859   {
860     Entry2ObjectStateMap& aEntry2ObjectStateMap = myObjectStateMap[ theViewId ];
861     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
862
863     ObjectState& anObjectState = aEntry2ObjectStateMap[ anEntry ];
864     anObjectState.Visibility = theState;
865   }
866 }
867
868 void HYDROGUI_Module::setIsToUpdate( const Handle(HYDROData_Entity)& theObject,
869                                      const bool theState )
870 {
871   if( !theObject.IsNull() )
872   {
873     // Process OCC shapes
874     ViewId2ListOfShapes::const_iterator aShapesMapIter( myShapesMap.begin() );
875     while( aShapesMapIter != myShapesMap.end() )
876     {
877       const ListOfShapes& aShapesList = aShapesMapIter.value();
878       foreach ( HYDROGUI_Shape* aShape, aShapesList )
879       {
880         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
881         {
882           aShape->setIsToUpdate( theState );
883         }
884       }
885       aShapesMapIter++;
886     }
887     // Process VTK shapes
888     ViewId2ListOfVTKPrs::const_iterator aVTKPrsMapIter( myVTKPrsMap.begin() );
889     while( aVTKPrsMapIter != myVTKPrsMap.end() )
890     {
891       const ListOfVTKPrs& aShapesList = aVTKPrsMapIter.value();
892       foreach ( HYDROGUI_VTKPrs* aShape, aShapesList )
893       {
894         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
895         {
896           aShape->setIsToUpdate( theState );
897         }
898       }
899       aVTKPrsMapIter++;
900     }
901   }
902 }
903
904 /////////////////// OCC SHAPES PROCESSING
905 HYDROGUI_Shape* HYDROGUI_Module::getObjectShape( const int                       theViewId,
906                                                  const Handle(HYDROData_Entity)& theObject ) const
907 {
908   HYDROGUI_Shape* aResShape = NULL;
909   if( theObject.IsNull() )
910     return aResShape;
911
912   if ( myShapesMap.contains( theViewId ) )
913   {
914     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
915     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
916     {
917       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
918         continue;
919
920       aResShape = aShape;
921       break;
922     }
923   }
924
925   return aResShape;
926 }
927
928 void HYDROGUI_Module::setObjectShape( const int                       theViewId,
929                                       const Handle(HYDROData_Entity)& theObject,
930                                       HYDROGUI_Shape*                 theShape )
931 {
932   if( theObject.IsNull() )
933     return;
934
935   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
936   aViewShapes.append( theShape );
937 }
938
939 void HYDROGUI_Module::removeObjectShape( const int                       theViewId,
940                                          const Handle(HYDROData_Entity)& theObject )
941 {
942   if ( !myShapesMap.contains( theViewId ) )
943     return;
944
945   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
946   Handle(HYDROData_Entity) anObject;
947   for ( int i = 0; i < aViewShapes.length(); )
948   {
949     HYDROGUI_Shape* aShape = aViewShapes.at( i );
950     anObject = aShape->getObject();
951     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
952     {
953       delete aShape;
954       aViewShapes.removeAt( i );
955       continue;
956     }
957
958     ++i;
959   }
960 }
961
962 void HYDROGUI_Module::removeViewShapes( const int theViewId )
963 {
964   if ( !myShapesMap.contains( theViewId ) )
965     return;
966
967   const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
968   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
969   {
970     HYDROGUI_Shape* aShape = aViewShapes.at( i );
971     if ( aShape )
972       delete aShape;
973   }
974
975   myShapesMap.remove( theViewId );
976 }
977 /////////////////// END OF OCC SHAPES PROCESSING
978
979 /////////////////// VTKPrs PROCESSING
980 HYDROGUI_VTKPrs* HYDROGUI_Module::getObjectVTKPrs( const int                       theViewId,
981                                                  const Handle(HYDROData_Entity)& theObject ) const
982 {
983   HYDROGUI_VTKPrs* aResShape = NULL;
984   if( theObject.IsNull() )
985     return aResShape;
986
987   if ( myVTKPrsMap.contains( theViewId ) )
988   {
989     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
990     foreach ( HYDROGUI_VTKPrs* aShape, aViewShapes )
991     {
992       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
993         continue;
994
995       aResShape = aShape;
996       break;
997     }
998   }
999
1000   return aResShape;
1001 }
1002
1003 void HYDROGUI_Module::setObjectVTKPrs( const int                       theViewId,
1004                                        const Handle(HYDROData_Entity)& theObject,
1005                                        HYDROGUI_VTKPrs*                 theShape )
1006 {
1007   if( theObject.IsNull() )
1008     return;
1009
1010   if( theShape && theShape->needScalarBar() )
1011   {
1012     // Compute the new global Z range from the added presentation and the old global Z range.
1013     double* aGlobalRange = getVTKDisplayer()->GetZRange( theViewId );
1014     double* aRange = theShape->getInternalZRange();
1015     bool anIsUpdate = false;
1016     if ( aRange[0] < aGlobalRange[0] )
1017     {
1018       aGlobalRange[0] = aRange[0];
1019       anIsUpdate = true;
1020     }
1021     if ( aRange[1] > aGlobalRange[1] )
1022     {
1023       aGlobalRange[1] = aRange[1];
1024       anIsUpdate = true;
1025     }
1026
1027     //if ( anIsUpdate )
1028     //{
1029       updateVTKZRange( theViewId, aGlobalRange );
1030     //}
1031   }
1032
1033   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1034   aViewShapes.append( theShape );
1035 }
1036
1037 void HYDROGUI_Module::removeObjectVTKPrs( const int                       theViewId,
1038                                           const Handle(HYDROData_Entity)& theObject )
1039 {
1040   if ( !myVTKPrsMap.contains( theViewId ) )
1041     return;
1042
1043   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1044   Handle(HYDROData_Entity) anObject;
1045   for ( int i = 0; i < aViewShapes.length(); )
1046   {
1047     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1048     anObject = aShape->getObject();
1049     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1050     {
1051       delete aShape;
1052       aViewShapes.removeAt( i );
1053       continue;
1054     }
1055
1056     ++i;
1057   }
1058
1059   // Invalidate global Z range
1060   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1061   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1062 }
1063
1064 void HYDROGUI_Module::removeViewVTKPrs( const int theViewId )
1065 {
1066   if ( !myVTKPrsMap.contains( theViewId ) )
1067     return;
1068
1069   const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1070   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1071   {
1072     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1073     if ( aShape )
1074       delete aShape;
1075   }
1076
1077   myVTKPrsMap.remove( theViewId );
1078 }
1079
1080 void HYDROGUI_Module::updateVTKZRange( const int theViewId, double theRange[] )
1081 {
1082   if ( myVTKPrsMap.contains( theViewId ) )
1083   {
1084     // For the given viewer id update all VTK presentations ...
1085     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1086     HYDROGUI_VTKPrs* aShape;
1087     for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1088     {
1089       aShape = aViewShapes.at( i );
1090       if ( aShape && aShape->needScalarBar() )
1091       {
1092         aShape->setZRange( theRange );
1093       }
1094     }
1095   }
1096   // ... and update the global color legend scalar bar.
1097   getVTKDisplayer()->SetZRange( theViewId, theRange );
1098 }
1099 /////////////////// END OF VTKPrs PROCESSING
1100
1101 CAM_DataModel* HYDROGUI_Module::createDataModel()
1102 {
1103   return new HYDROGUI_DataModel( this );
1104 }
1105
1106 void HYDROGUI_Module::customEvent( QEvent* e )
1107 {
1108   int aType = e->type();
1109   if ( aType == NewViewEvent )
1110   {
1111     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
1112     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
1113     {
1114       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
1115       {
1116         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
1117         ViewManagerRole aRole = getViewManagerRole( aViewManager );
1118
1119         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
1120         {
1121           if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1122             aViewPort->scale( 1, -1 ); // invert the Y axis direction from down to up
1123
1124           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
1125           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
1126           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
1127
1128           //ouv: temporarily commented
1129           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
1130         }
1131
1132         if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1133           update( UF_Viewer );
1134
1135         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
1136       }
1137     }
1138   }
1139 }
1140
1141 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
1142 {
1143   QEvent::Type aType = theEvent->type();
1144   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
1145   {
1146     if( aType == QEvent::Show )
1147     {
1148       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
1149       e->setData( theObj );
1150       QApplication::postEvent( this, e );
1151       theObj->removeEventFilter( this );
1152     }
1153   }
1154   else if ( theObj->inherits( "OCCViewer_ViewPort" ) )
1155   {
1156     if( aType == QEvent::Leave )
1157     {
1158       SUIT_Desktop* aDesktop = getApp()->desktop();
1159       if ( aDesktop && aDesktop->statusBar() ) {
1160         aDesktop->statusBar()->clearMessage();
1161       }
1162     }
1163   }
1164   else if ( theObj->inherits( "SVTK_ViewWindow" ) )
1165   {
1166     if( aType == QEvent::Leave )
1167     {
1168       SUIT_Desktop* aDesktop = getApp()->desktop();
1169       if ( aDesktop && aDesktop->statusBar() ) {
1170         aDesktop->statusBar()->clearMessage();
1171       }
1172     }
1173   }
1174
1175   return LightApp_Module::eventFilter( theObj, theEvent );
1176 }
1177
1178 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
1179 {
1180   LightApp_Module::onViewManagerAdded( theViewManager );
1181
1182   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
1183   { 
1184     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1185              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1186   }
1187   else if( theViewManager->getType() == OCCViewer_Viewer::Type() )
1188   {
1189     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1190              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1191     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1192              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1193   }
1194   else if( theViewManager->getType() == SVTK_Viewer::Type() )
1195   {
1196     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1197              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1198     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1199              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1200   }
1201
1202   createSelector( theViewManager ); // replace the default selector
1203
1204   ViewManagerInfo anInfo( theViewManager, VMR_General );
1205   myViewManagerMap.insert( ViewManagerId++, anInfo );
1206 }
1207
1208 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
1209 {
1210   LightApp_Module::onViewManagerRemoved( theViewManager );
1211
1212   createSelector( theViewManager ); // replace the default selector
1213
1214   int anId = getViewManagerId( theViewManager );
1215   if( anId != -1 )
1216   {
1217     OCCViewer_ViewManager* anOCCViewManager =
1218       ::qobject_cast<OCCViewer_ViewManager*>( myViewManagerMap[ anId ].first );
1219     if ( anOCCViewManager )
1220     {
1221       OCCViewer_Viewer* anOCCViewer = anOCCViewManager->getOCCViewer();
1222       if ( anOCCViewer )
1223         removeViewShapes( (size_t)anOCCViewer );
1224     }
1225
1226     if ( getVTKDisplayer()->IsApplicable( theViewManager ) )
1227     {
1228       SVTK_Viewer* aVTKViewer = getVTKViewer( anId );
1229       if ( aVTKViewer )
1230       {
1231         getVTKDisplayer()->EraseScalarBar( anId, true );
1232         removeViewShapes( (size_t)aVTKViewer );
1233       }
1234     }
1235
1236     myViewManagerMap.remove( anId );
1237   }
1238 }
1239
1240 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
1241 {
1242   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
1243   {
1244     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
1245     {
1246       aViewFrame->installEventFilter( this );
1247
1248       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
1249
1250       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
1251                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
1252     }
1253   }
1254   else if( theViewWindow && theViewWindow->inherits( "OCCViewer_ViewFrame" ) )
1255   {
1256     if( OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theViewWindow ) )
1257     {
1258       aViewFrame->onTopView();
1259
1260       HYDROGUI_Tool::setOCCActionShown( aViewFrame, OCCViewer_ViewWindow::MaximizedId, false );
1261
1262       OCCViewer_ViewPort3d* aViewPort = aViewFrame->getViewPort();
1263       if ( aViewPort ) {
1264         aViewPort->installEventFilter( this );
1265       }
1266     }
1267   }
1268   else if( theViewWindow && theViewWindow->inherits( "SVTK_ViewWindow" ) )
1269   {
1270     if( SVTK_ViewWindow* aViewFrame = dynamic_cast<SVTK_ViewWindow*>( theViewWindow ) )
1271     {
1272       aViewFrame->installEventFilter( this );
1273     }
1274   }
1275 }
1276
1277 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
1278 {
1279   /* ouv: currently unused
1280   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
1281   {
1282     SUIT_ViewManager* aViewManager = 0;
1283
1284     QObject* aParent = aViewPort;
1285     while( aParent = aParent->parent() )
1286     {
1287       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
1288       {
1289         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
1290         {
1291           aViewManager = aViewer->getViewManager();
1292           break;
1293         }
1294       }
1295     }
1296
1297     if( !aViewManager )
1298       return;
1299
1300     double aMouseX = theEvent->scenePos().x();
1301     double aMouseY = theEvent->scenePos().y();
1302
1303     ViewManagerRole aRole = getViewManagerRole( aViewManager );
1304     if( aRole == VMR_General )
1305     {
1306       int aXDeg = 0, aYDeg = 0;
1307       int aXMin = 0, aYMin = 0;
1308       double aXSec = 0, aYSec = 0;
1309       HYDROData_Lambert93::secToDMS( aMouseX, aXDeg, aXMin, aXSec );
1310       HYDROData_Lambert93::secToDMS( aMouseY, aYDeg, aYMin, aYSec );
1311
1312       QString aDegSymbol( QChar( 0x00B0 ) );
1313       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
1314       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
1315
1316       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
1317     }
1318     else if( aRole == VMR_TransformImage )
1319       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
1320   }
1321   */
1322 }
1323
1324 void HYDROGUI_Module::updateViewer( HYDROGUI_AbstractDisplayer* theDisplayer, 
1325                                     const bool theIsInit, 
1326                                     const bool theIsForced, 
1327                                     const bool theDoFitAll )
1328 {
1329   QList<int> aViewManagerIdList;
1330
1331   // currently, all views are updated
1332   ViewManagerMapIterator anIter( myViewManagerMap );
1333   while( anIter.hasNext() )
1334   { 
1335     SUIT_ViewManager* aViewManager = anIter.next().value().first;
1336
1337     if ( theDisplayer->IsApplicable( aViewManager ) )
1338     {
1339       int anId = anIter.key();
1340       aViewManagerIdList.append( anId );
1341     }
1342   }
1343
1344   QListIterator<int> anIdIter( aViewManagerIdList );
1345   while( anIdIter.hasNext() )
1346   {
1347     theDisplayer->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
1348   }
1349 }
1350
1351 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
1352 {
1353   if( !theViewManager )
1354     return;
1355
1356   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1357   if( !aSelectionMgr )
1358     return;
1359
1360   QString aViewType = theViewManager->getType();
1361   if( aViewType != GraphicsView_Viewer::Type() &&
1362       aViewType != OCCViewer_Viewer::Type())
1363     return;
1364
1365   QList<SUIT_Selector*> aSelectorList;
1366   aSelectionMgr->selectors( aViewType, aSelectorList );
1367
1368   // disable all alien selectors
1369   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
1370   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
1371   {
1372     SUIT_Selector* aSelector = *anIter;
1373     if( aSelector && ( !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) &&
1374                        !dynamic_cast<SVTK_Selector*>( aSelector ) &&
1375                        !dynamic_cast<HYDROGUI_OCCSelector*>( aSelector ) ) )
1376       aSelector->setEnabled( false );
1377   }
1378
1379   if ( aViewType == GraphicsView_Viewer::Type() )
1380   {
1381     GraphicsView_ViewManager* aViewManager =
1382       ::qobject_cast<GraphicsView_ViewManager*>( theViewManager );
1383     if( aViewManager )
1384       new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
1385   }
1386   else if ( aViewType == OCCViewer_Viewer::Type() )
1387   {
1388     OCCViewer_ViewManager* aViewManager =
1389       ::qobject_cast<OCCViewer_ViewManager*>( theViewManager );
1390     if( aViewManager )
1391       new HYDROGUI_OCCSelector( this, aViewManager->getOCCViewer(), aSelectionMgr );
1392   }
1393 }
1394
1395 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
1396 {
1397   bool aPrevState = myIsUpdateEnabled;
1398   myIsUpdateEnabled = theState;
1399   return aPrevState;
1400 }
1401
1402 bool HYDROGUI_Module::isUpdateEnabled() const
1403 {
1404   return myIsUpdateEnabled;
1405 }
1406
1407 QStringList HYDROGUI_Module::storeSelection() const
1408 {
1409   QStringList anEntryList;
1410   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1411   {
1412     SUIT_DataOwnerPtrList aList( true );
1413     aSelectionMgr->selected( aList );
1414
1415     SUIT_DataOwnerPtrList::iterator anIter;
1416     for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
1417     {
1418       const LightApp_DataOwner* anOwner = 
1419         dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
1420       if( anOwner )
1421         anEntryList.append( anOwner->entry() );
1422     }
1423   }
1424   return anEntryList;
1425 }
1426
1427 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
1428 {
1429   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1430   {
1431     SUIT_DataOwnerPtrList aList( true );
1432     for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
1433       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
1434     aSelectionMgr->setSelected( aList );
1435   }
1436 }
1437
1438 void HYDROGUI_Module::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
1439 {
1440   OCCViewer_ViewWindow* anOCCViewWindow = 
1441     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
1442   if ( anOCCViewWindow ) {
1443     // Get the selected point coordinates
1444     OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
1445     if ( !aViewPort ) {
1446       return;
1447     }
1448
1449     gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(), 
1450                                                            aViewPort->getView() );
1451     // Show the coordinates in the status bar
1452     SUIT_Desktop* aDesktop = getApp()->desktop();
1453     if ( aDesktop && aDesktop->statusBar() ) {
1454       QString aX = HYDROGUI_Tool::GetCoordinateString( aPnt.X() );
1455       QString anY = HYDROGUI_Tool::GetCoordinateString( aPnt.Y() );
1456       aDesktop->statusBar()->showMessage( tr("COORDINATES_INFO").arg( aX ).arg( anY ) );
1457     }
1458   } 
1459   else
1460   {
1461     SVTK_ViewWindow* aViewWindow = 
1462       dynamic_cast<SVTK_ViewWindow*>(theViewWindow);
1463     if ( aViewWindow ) {
1464       // Get the selected point coordinates
1465       double aCoords[3];
1466       SVTK_RenderWindowInteractor* anInteractor = aViewWindow->GetInteractor();
1467       if ( anInteractor )
1468       {
1469         //TODO: Use a WorldPicker to get current coords
1470         //anInteractorStyle->ComputeDisplayToWorld( theEvent->x(), theEvent->y(), 0, aCoords );
1471         aCoords[0] = theEvent->x();
1472         aCoords[1] = theEvent->y();
1473         // Show the coordinates in the status bar
1474         SUIT_Desktop* aDesktop = getApp()->desktop();
1475         if ( aDesktop && aDesktop->statusBar() ) {
1476           QString aX = HYDROGUI_Tool::GetCoordinateString( aCoords[0] );
1477           QString anY = HYDROGUI_Tool::GetCoordinateString( aCoords[1] );
1478           aDesktop->statusBar()->showMessage( tr("COORDINATES_INFO").arg( aX ).arg( anY ) );
1479         }
1480       }
1481     } 
1482   }
1483 }