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