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