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