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