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