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