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