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