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