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