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