]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_Module.cxx
Salome HOME
ea883c19cd8fece6b7c1e179426dfe52f26b81c3
[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
614       // Add copy action
615       if( aModel->canCopy() ) {
616         theMenu->addAction( action( CopyId ) );
617         theMenu->addSeparator();
618       }
619     } else if ( anAllAreProfiles ) {
620       theMenu->addAction( action( SelectedGeoreferencementId ) );
621       theMenu->addSeparator();
622     }
623
624     if( !anIsDummyObject3D )
625       theMenu->addAction( action( DeleteId ) );
626     theMenu->addSeparator();
627
628     if( anIsImage || anIsPolyline || anIsPolyline3D || 
629         anIsImmersibleZone || anIsZone || anIsRegion ||
630         anIsBathymetry || anIsObstacle || anIsStream ||
631         anIsChannel || anIsDigue || anIsDummyObject3D ||
632         anIsValidProfile || anIsGroup )
633     {
634       if( anIsHiddenInSelection )
635         theMenu->addAction( action( ShowId ) );
636       theMenu->addAction( action( ShowOnlyId ) );
637       if( anIsVisibleInSelection )
638         theMenu->addAction( action( HideId ) );
639       theMenu->addSeparator();
640     }
641   }
642
643   if ( anIsOCCView )
644   {
645     SUIT_Operation* anOp = application()->activeStudy()->activeOperation();
646     HYDROGUI_PolylineOp* aPolylineOp = dynamic_cast<HYDROGUI_PolylineOp*>( anOp );
647     if ( aPolylineOp && aPolylineOp->deleteEnabled() )
648       theMenu->addAction( action( DeleteId ) );
649   }
650
651   if( anIsObjectBrowser || anIsGraphicsView || anIsOCCView || anIsVTKView )
652   {
653     theMenu->addAction( action( ShowAllId ) );
654     theMenu->addAction( action( HideAllId ) );
655     theMenu->addSeparator();
656   }
657   if ( anIsOCCView ) {
658     theMenu->addSeparator();
659     theMenu->addAction( action( CopyViewerPositionId ) );
660   }
661 }
662
663 void HYDROGUI_Module::update( const int flags )
664 {
665   if( !isUpdateEnabled() )
666     return;
667
668   QApplication::setOverrideCursor( Qt::WaitCursor );
669
670   // To prevent calling this method recursively
671   // from one of the methods called below
672   setUpdateEnabled( false );
673
674   // store selected objects
675   QStringList aSelectedEntries = storeSelection();
676
677   bool aDoFitAll = flags & UF_FitAll;
678   if( ( flags & UF_Viewer ) )
679     updateViewer( getDisplayer(), flags & UF_GV_Init, flags & UF_GV_Forced, aDoFitAll ); 
680
681   if( ( flags & UF_OCCViewer ) )
682     updateViewer( getOCCDisplayer(), flags & UF_OCC_Init, flags & UF_OCC_Forced, aDoFitAll ); 
683
684   if( ( flags & UF_VTKViewer ) )
685     updateViewer( getVTKDisplayer(), flags & UF_VTK_Init, flags & UF_VTK_Forced, aDoFitAll ); 
686
687   if( ( flags & UF_Model ) && getDataModel() && getApp() )
688   {
689     getDataModel()->update( getStudyId() );
690
691     // Temporary workaround to prevent breaking
692     // the selection in the object browser.
693     // Note: processEvents() should be called after updateGV(),
694     // otherwise the application crashes from time to time.
695     //RKV: qApp->processEvents();
696     SUIT_ResourceMgr* aResMgr = getApp()->resourceMgr();
697     bool isResizeOnExpandItem = aResMgr->booleanValue( "ObjectBrowser", "resize_on_expand_item", false );
698     SUIT_DataBrowser* anObjectBrowser = getApp()->objectBrowser();
699     if ( isResizeOnExpandItem && anObjectBrowser ) {
700       anObjectBrowser->setResizeOnExpandItem( false ); // MZN: ISSUE #280 
701     }
702     getApp()->updateObjectBrowser( true );
703     if ( isResizeOnExpandItem && anObjectBrowser ) {
704       anObjectBrowser->setResizeOnExpandItem( true ); // MZN: ISSUE #280 
705     }
706   }
707
708   // Object browser is currently updated by using UF_Model flag
709   if( ( flags & UF_ObjBrowser ) && ((flags & UF_Model) == 0) && getApp() )
710     getApp()->updateObjectBrowser( true );
711
712   if( ( flags & UF_Controls ) && getApp() )
713     getApp()->updateActions();
714
715   // restore selected objects
716   restoreSelection( aSelectedEntries );
717
718   setUpdateEnabled( true );
719
720   QApplication::restoreOverrideCursor();
721 }
722
723 void HYDROGUI_Module::updateCommandsStatus()
724 {
725   LightApp_Module::updateCommandsStatus();
726
727   updateUndoRedoControls();
728
729   action( CopyId )->setEnabled( getDataModel()->canCopy() );
730   action( PasteId )->setEnabled( getDataModel()->canPaste() );
731 }
732
733 void HYDROGUI_Module::selectionChanged()
734 {
735   LightApp_Module::selectionChanged();
736   updateCommandsStatus();
737 }
738
739 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
740 {
741   return (HYDROGUI_DataModel*)dataModel();
742 }
743
744 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
745 {
746   return myDisplayer;
747 }
748
749 HYDROGUI_OCCDisplayer* HYDROGUI_Module::getOCCDisplayer() const
750 {
751   return myOCCDisplayer;
752 }
753
754 HYDROGUI_VTKPrsDisplayer* HYDROGUI_Module::getVTKDisplayer() const
755 {
756   return myVTKDisplayer;
757 }
758
759 SUIT_ViewManager* HYDROGUI_Module::getViewManager( const int theId ) const
760 {
761   if( myViewManagerMap.contains( theId ) )
762   {
763     return myViewManagerMap[ theId ].first;
764   }
765   return NULL;
766 }
767
768 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
769 {
770   if( myViewManagerMap.contains( theId ) )
771   {
772     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
773     GraphicsView_ViewManager* aViewManager =
774       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
775     if( aViewManager )
776       return aViewManager->getViewer();
777   }
778   return NULL;
779 }
780
781 OCCViewer_Viewer* HYDROGUI_Module::getOCCViewer( const int theId ) const
782 {
783   if( myViewManagerMap.contains( theId ) )
784   {
785     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
786     OCCViewer_ViewManager* aViewManager =
787       ::qobject_cast<OCCViewer_ViewManager*>( anInfo.first );
788     if( aViewManager )
789       return aViewManager->getOCCViewer();
790   }
791   return NULL;
792 }
793
794 SVTK_Viewer* HYDROGUI_Module::getVTKViewer( const int theId ) const
795 {
796   if( myViewManagerMap.contains( theId ) )
797   {
798     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
799     SVTK_ViewManager* aViewManager =
800       ::qobject_cast<SVTK_ViewManager*>( anInfo.first );
801     if( aViewManager )
802       return dynamic_cast<SVTK_Viewer*>( aViewManager->getViewModel() );
803   }
804   return NULL;
805 }
806
807 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
808 {
809   ViewManagerMapIterator anIter( myViewManagerMap );
810   while( anIter.hasNext() )
811   {
812     int anId = anIter.next().key();
813     const ViewManagerInfo& anInfo = anIter.value();
814     if( anInfo.first == theViewManager )
815       return anId;
816   }
817   return -1;
818 }
819
820 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
821 {
822   int anId = getViewManagerId( theViewManager );
823   if( anId != -1 )
824   {
825     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
826     return anInfo.second;
827   }
828   return VMR_Unknown;
829 }
830
831 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
832                                           const ViewManagerRole theRole )
833 {
834   int anId = getViewManagerId( theViewManager );
835   if( anId != -1 )
836   {
837     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
838     anInfo.second = theRole;
839   }
840 }
841
842 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
843                                        const Handle(HYDROData_Entity)& theObject ) const
844 {
845   if( theObject.IsNull() )
846     return false;
847
848   ViewId2Entry2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
849   if( anIter1 != myObjectStateMap.end() )
850   {
851     const Entry2ObjectStateMap& aEntry2ObjectStateMap = anIter1.value();
852     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
853
854     Entry2ObjectStateMap::const_iterator anIter2 = aEntry2ObjectStateMap.find( anEntry );
855     if( anIter2 != aEntry2ObjectStateMap.end() )
856     {
857       const ObjectState& anObjectState = anIter2.value();
858       return anObjectState.Visibility;
859     }
860   }
861   return false;
862 }
863
864 void HYDROGUI_Module::setObjectVisible( const int theViewId,
865                                         const Handle(HYDROData_Entity)& theObject,
866                                         const bool theState )
867 {
868   if( !theObject.IsNull() )
869   {
870     Entry2ObjectStateMap& aEntry2ObjectStateMap = myObjectStateMap[ theViewId ];
871     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
872
873     ObjectState& anObjectState = aEntry2ObjectStateMap[ anEntry ];
874     anObjectState.Visibility = theState;
875
876     // Remember the display order ( needed for Z layers assignment only )
877     QStringList& anObjectEntries = myObjectDisplayOrderMap[ theViewId ];
878     anObjectEntries.removeAll( anEntry );
879     if ( theState ) {
880       anObjectEntries.append( anEntry );
881     }
882   }
883 }
884
885 void HYDROGUI_Module::setIsToUpdate( const Handle(HYDROData_Entity)& theObject,
886                                      const bool theState )
887 {
888   if( !theObject.IsNull() )
889   {
890     // Process OCC shapes
891     ViewId2ListOfShapes::const_iterator aShapesMapIter( myShapesMap.begin() );
892     while( aShapesMapIter != myShapesMap.end() )
893     {
894       const ListOfShapes& aShapesList = aShapesMapIter.value();
895       foreach ( HYDROGUI_Shape* aShape, aShapesList )
896       {
897         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
898         {
899           aShape->setIsToUpdate( theState );
900         }
901       }
902       aShapesMapIter++;
903     }
904     // Process VTK shapes
905     ViewId2ListOfVTKPrs::const_iterator aVTKPrsMapIter( myVTKPrsMap.begin() );
906     while( aVTKPrsMapIter != myVTKPrsMap.end() )
907     {
908       const ListOfVTKPrs& aShapesList = aVTKPrsMapIter.value();
909       foreach ( HYDROGUI_VTKPrs* aShape, aShapesList )
910       {
911         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
912         {
913           aShape->setIsToUpdate( theState );
914         }
915       }
916       aVTKPrsMapIter++;
917     }
918   }
919 }
920
921 /////////////////// OCC SHAPES PROCESSING
922 HYDROGUI_Shape* HYDROGUI_Module::getObjectShape( const int                       theViewId,
923                                                  const Handle(HYDROData_Entity)& theObject ) const
924 {
925   HYDROGUI_Shape* aResShape = NULL;
926   if( theObject.IsNull() )
927     return aResShape;
928
929   if ( myShapesMap.contains( theViewId ) )
930   {
931     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
932     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
933     {
934       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
935         continue;
936
937       aResShape = aShape;
938       break;
939     }
940   }
941
942   return aResShape;
943 }
944
945 void HYDROGUI_Module::setObjectShape( const int                       theViewId,
946                                       const Handle(HYDROData_Entity)& theObject,
947                                       HYDROGUI_Shape*                 theShape )
948 {
949   if( theObject.IsNull() )
950     return;
951
952   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
953   aViewShapes.append( theShape );
954 }
955
956 void HYDROGUI_Module::removeObjectShape( const int                       theViewId,
957                                          const Handle(HYDROData_Entity)& theObject )
958 {
959   if ( !myShapesMap.contains( theViewId ) )
960     return;
961
962   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
963   Handle(HYDROData_Entity) anObject;
964   for ( int i = 0; i < aViewShapes.length(); )
965   {
966     HYDROGUI_Shape* aShape = aViewShapes.at( i );
967     anObject = aShape->getObject();
968     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
969     {
970       delete aShape;
971       aViewShapes.removeAt( i );
972       continue;
973     }
974
975     ++i;
976   }
977 }
978
979 void HYDROGUI_Module::removeViewShapes( const int theViewId )
980 {
981   if ( !myShapesMap.contains( theViewId ) )
982     return;
983
984   const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
985   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
986   {
987     HYDROGUI_Shape* aShape = aViewShapes.at( i );
988     if ( aShape )
989       delete aShape;
990   }
991
992   myShapesMap.remove( theViewId );
993 }
994 /////////////////// END OF OCC SHAPES PROCESSING
995
996 /////////////////// VTKPrs PROCESSING
997 HYDROGUI_VTKPrs* HYDROGUI_Module::getObjectVTKPrs( const int                       theViewId,
998                                                  const Handle(HYDROData_Entity)& theObject ) const
999 {
1000   HYDROGUI_VTKPrs* aResShape = NULL;
1001   if( theObject.IsNull() )
1002     return aResShape;
1003
1004   if ( myVTKPrsMap.contains( theViewId ) )
1005   {
1006     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1007     foreach ( HYDROGUI_VTKPrs* aShape, aViewShapes )
1008     {
1009       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
1010         continue;
1011
1012       aResShape = aShape;
1013       break;
1014     }
1015   }
1016
1017   return aResShape;
1018 }
1019
1020 void HYDROGUI_Module::setObjectVTKPrs( const int                       theViewId,
1021                                        const Handle(HYDROData_Entity)& theObject,
1022                                        HYDROGUI_VTKPrs*                 theShape )
1023 {
1024   if( theObject.IsNull() )
1025     return;
1026
1027   if( theShape && theShape->needScalarBar() )
1028   {
1029     // Compute the new global Z range from the added presentation and the old global Z range.
1030     double* aGlobalRange = getVTKDisplayer()->GetZRange( theViewId );
1031     double* aRange = theShape->getInternalZRange();
1032     bool anIsUpdate = false;
1033     if ( aRange[0] < aGlobalRange[0] )
1034     {
1035       aGlobalRange[0] = aRange[0];
1036       anIsUpdate = true;
1037     }
1038     if ( aRange[1] > aGlobalRange[1] )
1039     {
1040       aGlobalRange[1] = aRange[1];
1041       anIsUpdate = true;
1042     }
1043
1044     //if ( anIsUpdate )
1045     //{
1046       updateVTKZRange( theViewId, aGlobalRange );
1047     //}
1048   }
1049
1050   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1051   aViewShapes.append( theShape );
1052 }
1053
1054 void HYDROGUI_Module::removeObjectVTKPrs( const int                       theViewId,
1055                                           const Handle(HYDROData_Entity)& theObject )
1056 {
1057   if ( !myVTKPrsMap.contains( theViewId ) )
1058     return;
1059
1060   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1061   Handle(HYDROData_Entity) anObject;
1062   for ( int i = 0; i < aViewShapes.length(); )
1063   {
1064     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1065     anObject = aShape->getObject();
1066     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1067     {
1068       delete aShape;
1069       aViewShapes.removeAt( i );
1070       continue;
1071     }
1072
1073     ++i;
1074   }
1075
1076   // Invalidate global Z range
1077   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1078   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1079 }
1080
1081 void HYDROGUI_Module::removeViewVTKPrs( const int theViewId )
1082 {
1083   if ( !myVTKPrsMap.contains( theViewId ) )
1084     return;
1085
1086   const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1087   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1088   {
1089     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1090     if ( aShape )
1091       delete aShape;
1092   }
1093
1094   myVTKPrsMap.remove( theViewId );
1095 }
1096
1097 void HYDROGUI_Module::updateVTKZRange( const int theViewId, double theRange[] )
1098 {
1099   if ( myVTKPrsMap.contains( theViewId ) )
1100   {
1101     // For the given viewer id update all VTK presentations ...
1102     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1103     HYDROGUI_VTKPrs* aShape;
1104     for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1105     {
1106       aShape = aViewShapes.at( i );
1107       if ( aShape && aShape->needScalarBar() )
1108       {
1109         aShape->setZRange( theRange );
1110       }
1111     }
1112   }
1113   // ... and update the global color legend scalar bar.
1114   getVTKDisplayer()->SetZRange( theViewId, theRange );
1115 }
1116 /////////////////// END OF VTKPrs PROCESSING
1117
1118 CAM_DataModel* HYDROGUI_Module::createDataModel()
1119 {
1120   return new HYDROGUI_DataModel( this );
1121 }
1122
1123 void HYDROGUI_Module::customEvent( QEvent* e )
1124 {
1125   int aType = e->type();
1126   if ( aType == NewViewEvent )
1127   {
1128     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
1129     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
1130     {
1131       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
1132       {
1133         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
1134         ViewManagerRole aRole = getViewManagerRole( aViewManager );
1135
1136         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
1137         {
1138           if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1139             aViewPort->scale( 1, -1 ); // invert the Y axis direction from down to up
1140
1141           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
1142           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
1143           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
1144
1145           //ouv: temporarily commented
1146           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
1147         }
1148
1149         if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1150           update( UF_Viewer );
1151
1152         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
1153       }
1154     }
1155   }
1156 }
1157
1158 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
1159 {
1160   QEvent::Type aType = theEvent->type();
1161   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
1162   {
1163     if( aType == QEvent::Show )
1164     {
1165       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
1166       e->setData( theObj );
1167       QApplication::postEvent( this, e );
1168       theObj->removeEventFilter( this );
1169     }
1170   }
1171   else if ( theObj->inherits( "OCCViewer_ViewPort" ) )
1172   {
1173     if( aType == QEvent::Leave )
1174     {
1175       SUIT_Desktop* aDesktop = getApp()->desktop();
1176       if ( aDesktop && aDesktop->statusBar() ) {
1177         aDesktop->statusBar()->clearMessage();
1178       }
1179     }
1180   }
1181   else if ( theObj->inherits( "SVTK_ViewWindow" ) )
1182   {
1183     if( aType == QEvent::Leave )
1184     {
1185       SUIT_Desktop* aDesktop = getApp()->desktop();
1186       if ( aDesktop && aDesktop->statusBar() ) {
1187         aDesktop->statusBar()->clearMessage();
1188       }
1189     }
1190   }
1191
1192   return LightApp_Module::eventFilter( theObj, theEvent );
1193 }
1194
1195 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
1196 {
1197   LightApp_Module::onViewManagerAdded( theViewManager );
1198
1199   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
1200   { 
1201     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1202              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1203   }
1204   else if( theViewManager->getType() == OCCViewer_Viewer::Type() )
1205   {
1206     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1207              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1208     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1209              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1210   }
1211   else if( theViewManager->getType() == SVTK_Viewer::Type() )
1212   {
1213     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1214              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1215     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1216              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1217   }
1218
1219   createSelector( theViewManager ); // replace the default selector
1220
1221   ViewManagerInfo anInfo( theViewManager, VMR_General );
1222   myViewManagerMap.insert( ViewManagerId++, anInfo );
1223 }
1224
1225 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
1226 {
1227   LightApp_Module::onViewManagerRemoved( theViewManager );
1228
1229   createSelector( theViewManager ); // replace the default selector
1230
1231   int anId = getViewManagerId( theViewManager );
1232   if( anId != -1 )
1233   {
1234     OCCViewer_ViewManager* anOCCViewManager =
1235       ::qobject_cast<OCCViewer_ViewManager*>( myViewManagerMap[ anId ].first );
1236     if ( anOCCViewManager )
1237     {
1238       OCCViewer_Viewer* anOCCViewer = anOCCViewManager->getOCCViewer();
1239       if ( anOCCViewer )
1240         removeViewShapes( (size_t)anOCCViewer );
1241     }
1242
1243     if ( getVTKDisplayer()->IsApplicable( theViewManager ) )
1244     {
1245       SVTK_Viewer* aVTKViewer = getVTKViewer( anId );
1246       if ( aVTKViewer )
1247       {
1248         getVTKDisplayer()->EraseScalarBar( anId, true );
1249         removeViewShapes( (size_t)aVTKViewer );
1250       }
1251     }
1252
1253     myViewManagerMap.remove( anId );
1254   }
1255 }
1256
1257 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
1258 {
1259   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
1260   {
1261     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
1262     {
1263       aViewFrame->installEventFilter( this );
1264
1265       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
1266
1267       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
1268                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
1269     }
1270   }
1271   else if( theViewWindow && theViewWindow->inherits( "OCCViewer_ViewFrame" ) )
1272   {
1273     if( OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theViewWindow ) )
1274     {
1275       aViewFrame->onTopView();
1276
1277       HYDROGUI_Tool::setOCCActionShown( aViewFrame, OCCViewer_ViewWindow::MaximizedId, false );
1278
1279       OCCViewer_ViewPort3d* aViewPort = aViewFrame->getViewPort();
1280       if ( aViewPort ) {
1281         aViewPort->installEventFilter( this );
1282       }
1283     }
1284   }
1285   else if( theViewWindow && theViewWindow->inherits( "SVTK_ViewWindow" ) )
1286   {
1287     if( SVTK_ViewWindow* aViewFrame = dynamic_cast<SVTK_ViewWindow*>( theViewWindow ) )
1288     {
1289       aViewFrame->installEventFilter( this );
1290     }
1291   }
1292 }
1293
1294 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
1295 {
1296   /* ouv: currently unused
1297   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
1298   {
1299     SUIT_ViewManager* aViewManager = 0;
1300
1301     QObject* aParent = aViewPort;
1302     while( aParent = aParent->parent() )
1303     {
1304       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
1305       {
1306         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
1307         {
1308           aViewManager = aViewer->getViewManager();
1309           break;
1310         }
1311       }
1312     }
1313
1314     if( !aViewManager )
1315       return;
1316
1317     double aMouseX = theEvent->scenePos().x();
1318     double aMouseY = theEvent->scenePos().y();
1319
1320     ViewManagerRole aRole = getViewManagerRole( aViewManager );
1321     if( aRole == VMR_General )
1322     {
1323       int aXDeg = 0, aYDeg = 0;
1324       int aXMin = 0, aYMin = 0;
1325       double aXSec = 0, aYSec = 0;
1326       HYDROData_Lambert93::secToDMS( aMouseX, aXDeg, aXMin, aXSec );
1327       HYDROData_Lambert93::secToDMS( aMouseY, aYDeg, aYMin, aYSec );
1328
1329       QString aDegSymbol( QChar( 0x00B0 ) );
1330       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
1331       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
1332
1333       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
1334     }
1335     else if( aRole == VMR_TransformImage )
1336       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
1337   }
1338   */
1339 }
1340
1341 void HYDROGUI_Module::updateViewer( HYDROGUI_AbstractDisplayer* theDisplayer, 
1342                                     const bool theIsInit, 
1343                                     const bool theIsForced, 
1344                                     const bool theDoFitAll )
1345 {
1346   QList<int> aViewManagerIdList;
1347
1348   // currently, all views are updated
1349   ViewManagerMapIterator anIter( myViewManagerMap );
1350   while( anIter.hasNext() )
1351   { 
1352     SUIT_ViewManager* aViewManager = anIter.next().value().first;
1353
1354     if ( theDisplayer->IsApplicable( aViewManager ) )
1355     {
1356       int anId = anIter.key();
1357       aViewManagerIdList.append( anId );
1358     }
1359   }
1360
1361   QListIterator<int> anIdIter( aViewManagerIdList );
1362   while( anIdIter.hasNext() )
1363   {
1364     theDisplayer->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
1365   }
1366 }
1367
1368 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
1369 {
1370   if( !theViewManager )
1371     return;
1372
1373   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1374   if( !aSelectionMgr )
1375     return;
1376
1377   QString aViewType = theViewManager->getType();
1378   if( aViewType != GraphicsView_Viewer::Type() &&
1379       aViewType != OCCViewer_Viewer::Type())
1380     return;
1381
1382   QList<SUIT_Selector*> aSelectorList;
1383   aSelectionMgr->selectors( aViewType, aSelectorList );
1384
1385   // disable all alien selectors
1386   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
1387   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
1388   {
1389     SUIT_Selector* aSelector = *anIter;
1390     if( aSelector && ( !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) &&
1391                        !dynamic_cast<SVTK_Selector*>( aSelector ) &&
1392                        !dynamic_cast<HYDROGUI_OCCSelector*>( aSelector ) ) )
1393       aSelector->setEnabled( false );
1394   }
1395
1396   if ( aViewType == GraphicsView_Viewer::Type() )
1397   {
1398     GraphicsView_ViewManager* aViewManager =
1399       ::qobject_cast<GraphicsView_ViewManager*>( theViewManager );
1400     if( aViewManager )
1401       new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
1402   }
1403   else if ( aViewType == OCCViewer_Viewer::Type() )
1404   {
1405     OCCViewer_ViewManager* aViewManager =
1406       ::qobject_cast<OCCViewer_ViewManager*>( theViewManager );
1407     if( aViewManager )
1408       new HYDROGUI_OCCSelector( this, aViewManager->getOCCViewer(), aSelectionMgr );
1409   }
1410 }
1411
1412 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
1413 {
1414   bool aPrevState = myIsUpdateEnabled;
1415   myIsUpdateEnabled = theState;
1416   return aPrevState;
1417 }
1418
1419 bool HYDROGUI_Module::isUpdateEnabled() const
1420 {
1421   return myIsUpdateEnabled;
1422 }
1423
1424 QStringList HYDROGUI_Module::storeSelection() const
1425 {
1426   QStringList anEntryList;
1427   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1428   {
1429     SUIT_DataOwnerPtrList aList( true );
1430     aSelectionMgr->selected( aList );
1431
1432     SUIT_DataOwnerPtrList::iterator anIter;
1433     for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
1434     {
1435       const LightApp_DataOwner* anOwner = 
1436         dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
1437       if( anOwner )
1438         anEntryList.append( anOwner->entry() );
1439     }
1440   }
1441   return anEntryList;
1442 }
1443
1444 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
1445 {
1446   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1447   {
1448     SUIT_DataOwnerPtrList aList( true );
1449     for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
1450       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
1451     aSelectionMgr->setSelected( aList );
1452   }
1453 }
1454
1455 void HYDROGUI_Module::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
1456 {
1457   OCCViewer_ViewWindow* anOCCViewWindow = 
1458     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
1459   bool doShow = false;
1460   gp_Pnt aPnt;
1461   if ( anOCCViewWindow ) {
1462     // Get the selected point coordinates
1463     OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
1464     if ( !aViewPort ) {
1465       return;
1466     }
1467
1468     aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(), 
1469                                                            aViewPort->getView() );
1470     doShow = true;
1471   } 
1472   else
1473   {
1474     SVTK_ViewWindow* aViewWindow = 
1475       dynamic_cast<SVTK_ViewWindow*>(theViewWindow);
1476     if ( aViewWindow ) {
1477       vtkRenderer* aRen = aViewWindow->getRenderer();
1478       if ( aRen )
1479       {
1480         vtkCamera* aCamera = aRen->GetActiveCamera();
1481         double* aNormal = aCamera->GetViewPlaneNormal();
1482         int event_x, event_y;
1483         vtkRenderWindowInteractor* anInteractor = aViewWindow->getInteractor();
1484         if ( anInteractor )
1485         {
1486           anInteractor->GetLastEventPosition(event_x, event_y);
1487           // Use a WorldPicker to get current coords
1488           myPicker->Pick( event_x, event_y, 0, aRen );
1489           double* aCoords = myPicker->GetPickPosition();
1490           /////////////////////// Use the same algorithm as for OCC
1491           double X, Y, Z;
1492           double aXp, aYp, aZp;
1493           double Vx, Vy, Vz;
1494           X = aCoords[0];
1495           Y = aCoords[1];
1496           Z = aCoords[2];
1497           Vx = aNormal[0];
1498           Vy = aNormal[1];
1499           Vz = aNormal[2];
1500           Standard_Real aPrec = LOCAL_SELECTION_TOLERANCE;
1501           if ( fabs( Vz ) > aPrec ) {
1502             double aT = -Z/Vz;
1503             aXp = X + aT*Vx;
1504             aYp = Y + aT*Vy;
1505             aZp = Z + aT*Vz;
1506           }
1507           else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
1508             aXp = aYp = aZp = 0;
1509             if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
1510               aYp = Y;
1511             else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
1512               aXp = X;
1513           }
1514           /////////////////////////
1515           doShow = true;
1516           aPnt.SetX( aXp );
1517           aPnt.SetY( aYp );
1518         }
1519       }
1520     } 
1521   }
1522   if ( doShow )
1523   {
1524     // Show the coordinates in the status bar
1525     SUIT_Desktop* aDesktop = getApp()->desktop();
1526     if ( aDesktop && aDesktop->statusBar() ) {
1527       QString aX = HYDROGUI_Tool::GetCoordinateString( aPnt.X() );
1528       QString anY = HYDROGUI_Tool::GetCoordinateString( aPnt.Y() );
1529       aDesktop->statusBar()->showMessage( tr("COORDINATES_INFO").arg( aX ).arg( anY ) );
1530     }
1531   }
1532 }
1533
1534 /**
1535  * Get the object display order. Needed for Z layers assignment only.
1536  */
1537 int HYDROGUI_Module::getObjectDisplayOrder( 
1538   const int theViewId, const Handle(HYDROData_Entity)& theObject) const
1539 {
1540   if( theObject.IsNull() )
1541     return -1;
1542
1543   QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
1544   QStringList anObjectEntries = myObjectDisplayOrderMap.value( theViewId );
1545
1546   return anObjectEntries.indexOf( anEntry );
1547 }