Salome HOME
Merge branch 'BR_H2018_2' of https://codev-tuleap.cea.fr/plugins/git/salome/hydro...
[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_Tool2.h"
34 #include "HYDROGUI_UpdateFlags.h"
35 #include "HYDROGUI_Shape.h"
36 #include "HYDROGUI_VTKPrs.h"
37 #include "HYDROGUI_VTKPrsDisplayer.h"
38 #include "HYDROGUI_AbstractDisplayer.h"
39 #include "HYDROGUI_PolylineOp.h"
40 #include "HYDROGUI_SetColorOp.h"
41 #include "HYDROGUI_ImportGeomObjectOp.h"
42 #include "HYDROGUI_ShowHideOp.h"
43 #include "HYDROGUI_Overview.h"
44 #include <HYDROGUI_ProfileDlg.h>
45 #include <HYDROGUI_SIProgressIndicator.h>
46 #include <HYDROGUI_ZIProgressIndicator.h>
47 #include <HYDROData_Tool.h>
48 #include <HYDROData_Image.h>
49 #include <HYDROData_Stream.h>
50 #include <HYDROData_Profile.h>
51 #include <HYDROData_Lambert93.h>
52 #include <HYDROData_Polyline3D.h>
53 #include <HYDROData_StricklerTable.h>
54 #include <HYDROData_ArtificialObject.h>
55 #include <HYDROData_NaturalObject.h>
56
57 #include <HYDROData_OperationsFactory.h>
58
59 #include <CurveCreator_Utils.hxx>
60
61 #include <GraphicsView_ViewFrame.h>
62 #include <GraphicsView_ViewManager.h>
63 #include <GraphicsView_ViewPort.h>
64 #include <GraphicsView_Viewer.h>
65
66 #include <ImageComposer_CutOperator.h>
67 #include <ImageComposer_CropOperator.h>
68 #include <ImageComposer_FuseOperator.h>
69
70 #include <LightApp_Application.h>
71 #include <LightApp_DataOwner.h>
72 #include <LightApp_GVSelector.h>
73 #include <LightApp_SelectionMgr.h>
74 #include <LightApp_UpdateFlags.h>
75
76 #include <SalomeApp_Study.h>
77
78 #include <OCCViewer_ViewFrame.h>
79 #include <OCCViewer_ViewManager.h>
80 #include <OCCViewer_ViewModel.h>
81
82 #include <SALOME_Event.h>
83
84 #include <SUIT_DataBrowser.h>
85 #include <SUIT_DataObject.h>
86 #include <SUIT_ViewManager.h>
87 #include <SUIT_ResourceMgr.h>
88 #include <SUIT_Desktop.h>
89 #include <SUIT_Study.h>
90 #include <SUIT_Session.h>
91
92 #include <SVTK_ViewManager.h>
93 #include <SVTK_ViewModel.h>
94 #include <SVTK_ViewWindow.h>
95 #include <SVTK_Selector.h>
96
97 #include <OCCViewer_ViewPort3d.h>
98
99 #include <GEOMUtils.hxx>
100 #include <GeometryGUI.h>
101
102 #include <SALOMEDS_wrap.hxx>
103
104 #include <QAction>
105 #include <QApplication>
106 #include <QGraphicsSceneMouseEvent>
107 #include <QMenu>
108 #include <QMouseEvent>
109 #include <QStatusBar>
110 #include <QCursor>
111
112 static int ViewManagerId = 0;
113
114 extern "C" HYDRO_EXPORT CAM_Module* createModule()
115 {
116   return new HYDROGUI_Module();
117 }
118
119 extern "C" HYDRO_EXPORT char* getModuleVersion()
120 {
121   return (char*)HYDRO_VERSION;
122 }
123
124 HYDROGUI_Module::HYDROGUI_Module()
125 : LightApp_Module( "HYDRO" ),
126   myDisplayer( 0 ),
127   myOCCDisplayer( 0 ),
128   myIsUpdateEnabled( true ),
129   myOverview( 0 )
130 {
131 }
132
133 HYDROGUI_Module::~HYDROGUI_Module()
134 {
135 }
136
137 int HYDROGUI_Module::getStudyId() const
138 {
139   LightApp_Application* anApp = getApp();
140   return anApp ? anApp->activeStudy()->id() : 0;
141 }
142
143 void HYDROGUI_Module::initialize( CAM_Application* theApp )
144 {
145   LightApp_Module::initialize( theApp );
146
147   createActions();
148   createUndoRedoActions();
149   createMenus();
150   createPopups();
151   createToolbars();
152
153   setMenuShown( false );
154   setToolShown( false );
155
156   myDisplayer = new HYDROGUI_Displayer( this );
157   myOCCDisplayer = new HYDROGUI_OCCDisplayer( this );
158   myVTKDisplayer = new HYDROGUI_VTKPrsDisplayer( this );
159
160   HYDROData_Tool::SetSIProgress( new HYDROGUI_SIProgressIndicator( theApp->desktop() ) );
161   HYDROData_Tool::SetZIProgress( new HYDROGUI_ZIProgressIndicator( theApp->desktop() ) );
162 }
163
164 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
165 {
166   bool aRes = LightApp_Module::activateModule( theStudy );
167
168   LightApp_Application* anApp = getApp();
169   SUIT_Desktop* aDesktop = anApp->desktop();
170
171   getApp()->setEditEnabled( false ); // hide SalomeApp copy/paste actions
172
173   setMenuShown( true );
174   setToolShown( true );
175
176 #ifndef DISABLE_PYCONSOLE
177   aDesktop->tabifyDockWidget( HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_PyConsole ) ), 
178                               HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_LogWindow ) ) );
179 #endif
180
181   // Remove defunct view managers from the map.
182   // It's essential to do this before "update( UF_All )" call!
183   QList<int> anObsoleteIds;
184   ViewManagerList anAllViewManagers = anApp->viewManagers();
185   ViewManagerList aHydroViewManagers; // view managers created inside the HYDRO module
186   ViewManagerMapIterator anIter( myViewManagerMap );
187   while( anIter.hasNext() ) {
188     int anId = anIter.next().key();
189     const ViewManagerInfo& anInfo = anIter.value();
190   
191     aHydroViewManagers << anInfo.first;
192
193     if ( !anAllViewManagers.contains( anInfo.first ) ) {
194       anObsoleteIds << anId;
195     }
196   }
197   foreach ( const int anId, anObsoleteIds ) {
198     myViewManagerMap.remove( anId );
199     myObjectStateMap.remove( anId );
200     myShapesMap.remove( anId );
201     myVTKPrsMap.remove( anId );
202   }
203   // Replace the default selector for all view managers.
204   // Add view managers created outside of HYDRO module to the map.
205   foreach ( SUIT_ViewManager* aViewManager, anAllViewManagers ) {
206     createSelector( aViewManager ); // replace the default selector
207     if ( !aHydroViewManagers.contains( aViewManager ) ) {
208       ViewManagerInfo anInfo( aViewManager, VMR_General );
209       myViewManagerMap.insert( ViewManagerId++, anInfo );
210     }
211   }
212
213   update( UF_All );
214
215   updateCommandsStatus();
216
217   HYDROGUI_Tool::setOCCActionShown( this, OCCViewer_ViewWindow::MaximizedId, false );
218
219   ViewManagerList anOCCViewManagers;
220   anApp->viewManagers( OCCViewer_Viewer::Type(), anOCCViewManagers );
221
222   foreach ( SUIT_ViewManager* aViewManager, anOCCViewManagers )
223   {
224     connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
225              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
226     connect( aViewManager, SIGNAL( activated( SUIT_ViewManager* ) ), 
227              this, SLOT( onViewActivated( SUIT_ViewManager* ) ) );
228
229     OCCViewer_ViewManager* occ_mgr = dynamic_cast<OCCViewer_ViewManager*>( aViewManager );
230     if( occ_mgr )
231     {
232       occ_mgr->setChainedOperations( true );//TODO: via preferences
233     }
234
235     foreach( SUIT_ViewWindow* aViewWindow, aViewManager->getViews() )
236     {
237       OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aViewWindow );
238       if ( aViewFrame && aViewFrame->getViewPort() )
239       {
240         aViewFrame->getViewPort()->installEventFilter( this );
241       }
242     }
243   }
244
245   preferencesChanged( "HYDRO", "zoom_shutoff" );
246
247   preferencesChanged( "HYDRO", "chained_panning" );
248
249   // Load GEOM data
250   SalomeApp_Study* aStudy = 
251     dynamic_cast<SalomeApp_Study*>( getApp()->activeStudy() );
252   if ( aStudy ) {
253     SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() );
254     GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
255     if ( !aGeomEngine->_is_nil() && !aGeomEngine->_is_nil() ) {
256       SALOMEDS::StudyBuilder_var aStudyBuilder = aDSStudy->NewBuilder();
257       SALOMEDS::SComponent_wrap GEOM_var = aDSStudy->FindComponent( "GEOM" );
258       if( !GEOM_var->_is_nil() ) {
259         aStudyBuilder->LoadWith( GEOM_var, aGeomEngine );
260       }
261     }
262   }
263
264
265 //  SUIT_DataBrowser* ob = getApp()->objectBrowser();
266 //  SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( ob->model() );
267 //  treeModel->setAppropriate( SUIT_DataObject::VisibilityId, Qtx::Toggled );
268
269   return aRes;
270 }
271
272 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
273 {
274   /* Issues ## 68, 88.
275   ViewManagerMapIterator anIter( myViewManagerMap );
276   while( anIter.hasNext() )
277     if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
278       getApp()->removeViewManager( aViewManager );
279   myViewManagerMap.clear();
280   */
281
282   ViewManagerList anOCCViewManagers;
283   getApp()->viewManagers( OCCViewer_Viewer::Type(), anOCCViewManagers );
284   foreach ( SUIT_ViewManager* aViewManager, anOCCViewManagers )
285   {
286     disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
287                 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
288     disconnect( aViewManager, SIGNAL( activated( SUIT_ViewManager* ) ), 
289                 this, SLOT( onViewActivated( SUIT_ViewManager* ) ) );
290
291     OCCViewer_ViewManager* occ_mgr = dynamic_cast<OCCViewer_ViewManager*>( aViewManager );
292     if( occ_mgr )
293     {
294       occ_mgr->setChainedOperations( false );
295       setAutoZoom( occ_mgr, true );
296     }
297   }
298
299   /* Issues ## 68, 88.
300   myObjectStateMap.clear();
301   myShapesMap.clear();
302   myVTKPrsMap.clear();
303   */
304
305   // clear the status bar
306   SUIT_Desktop* aDesktop = getApp()->desktop();
307   if ( aDesktop && aDesktop->statusBar() ) {
308     aDesktop->statusBar()->clearMessage();
309   }
310
311   // clear the data model's list of copying objects
312   HYDROGUI_DataModel::changeCopyingObjects( HYDROData_SequenceOfObjects() );
313
314   setMenuShown( false );
315   setToolShown( false );
316
317   getApp()->setEditEnabled( true ); // show SalomeApp copy/paste actions
318
319   HYDROGUI_Tool::setOCCActionShown( this, OCCViewer_ViewWindow::MaximizedId, true );
320
321   myActiveOperationMap.clear();
322
323   myViewManagerMap.clear();
324
325   return LightApp_Module::deactivateModule( theStudy );
326 }
327
328 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
329 {
330   static bool inWindows = false;
331   if( inWindows )
332     return;
333
334   theMap.clear();
335   theMap.insert( LightApp_Application::WT_LogWindow,     Qt::BottomDockWidgetArea );
336 #ifndef DISABLE_PYCONSOLE
337   theMap.insert( LightApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
338 #endif
339   theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea   );
340   theMap.insert( OverviewWindow, Qt::LeftDockWidgetArea );
341
342   inWindows = true;
343   LightApp_Application* app = getApp();
344   if( app && app->getWindow( OverviewWindow )==0 )
345   {
346     const_cast<HYDROGUI_Module*>( this )->myOverview = 
347       new HYDROGUI_Overview( tr( "OVERVIEW" ), 0, app->desktop() );
348     app->insertDockWindow( OverviewWindow, myOverview );
349     app->placeDockWindow( OverviewWindow, Qt::LeftDockWidgetArea );
350   }
351   inWindows = false;
352 }
353
354 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
355 {
356   theTypesList << GraphicsView_Viewer::Type() << OCCViewer_Viewer::Type();
357 }
358
359 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
360                                         QMenu* theMenu,
361                                         QString& theTitle )
362 {
363   HYDROGUI_DataModel* aModel = getDataModel();
364
365   bool anIsObjectBrowser = theClient == getApp()->objectBrowser()->popupClientType();
366   bool anIsGraphicsView = theClient == GraphicsView_Viewer::Type();
367   bool anIsOCCView = theClient == OCCViewer_Viewer::Type();
368   bool anIsVTKView = theClient == SVTK_Viewer::Type();
369   if( !anIsObjectBrowser && !anIsGraphicsView && !anIsOCCView && !anIsVTKView )
370     return;
371
372   size_t anActiveViewId = HYDROGUI_Tool::GetActiveViewId( this );
373
374   bool anIsSelectedDataObjects = false;
375   bool anIsVisibleInSelection = false;
376   bool anIsHiddenInSelection = false;
377
378   bool anIsImage = false;
379   bool anIsImportedImage = false;
380   bool anIsImageHasRefs = false;
381   bool anIsFusedImage = false;
382   bool anIsCutImage = false;
383   bool anIsSplitImage = false;
384   bool anIsMustObjectBeUpdated = false;
385   bool anIsPolyline = false;
386   bool anIsPolyline3D = false;
387   bool anIsProfile = false;
388   bool anIsValidProfile = false;
389   bool anAllAreProfiles = false;
390   bool anIsBathymetry = false;
391   bool anIsCalculation = false;
392   bool anIsImmersibleZone = false;
393   bool anIsVisualState = false;
394   bool anIsRegion = false;
395   bool anIsZone = false;
396   bool anIsObstacle = false;
397   bool anIsStricklerTable = false;
398   bool anIsLandCoverMap = false;
399   bool anIsStream = false;
400   bool anIsChannel = false;
401   bool anIsDigue = false;
402   bool anIsDummyObject3D = false;
403   bool anIsGroup = false;
404   bool anIsObjectCanBeColored = false;
405   bool isRoot = false;
406   bool isStreamHasBottom = false;
407   bool anIsBCPolygon = false;
408
409   SUIT_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
410   SUIT_DataOwnerPtrList anOwners;
411   aSelectionMgr->selected( anOwners );
412   if( anIsObjectBrowser && anOwners.size()==1 )
413   {
414     QString anEntry = anOwners[0]->keyString();
415     LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( getApp()->activeStudy() );
416     if( aStudy )
417       isRoot = aStudy->isComponent( anEntry );
418   }
419
420   // Check the selected GEOM objects (take into account the Object Browser only)
421   if ( anIsObjectBrowser ) {
422     QList<GEOM::shape_type> anObstacleTypes = 
423       HYDROGUI_ImportGeomObjectOp::getObstacleTypes();
424     QList<GEOM::shape_type> aPolylineTypes = 
425       HYDROGUI_ImportGeomObjectOp::getPolylineTypes();
426
427     bool isCanBeImportedAsObstacle = 
428       !HYDROGUI_Tool::GetSelectedGeomObjects( this, anObstacleTypes ).isEmpty();
429     bool isCanBeImportedAsPolyline = 
430       !HYDROGUI_Tool::GetSelectedGeomObjects( this, aPolylineTypes ).isEmpty();
431
432     // Add import as obstacle action
433     if ( isCanBeImportedAsObstacle ) {
434       theMenu->addAction( action( ImportGeomObjectAsObstacleId ) );
435     }
436     // Add import as polyline action
437     if ( isCanBeImportedAsPolyline ) {
438       theMenu->addAction( action( ImportGeomObjectAsPolylineId ) );
439     }
440     // Add separator
441     if ( isCanBeImportedAsObstacle || isCanBeImportedAsPolyline ) {
442       theMenu->addSeparator();
443     }
444   }
445
446   // check the selected data model objects
447   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
448   int aNbOfSelectedProfiles = 0;
449   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
450   {
451     Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
452     if( !anObject.IsNull() )
453     {
454       anIsSelectedDataObjects = true;
455
456       bool aVisibility = isObjectVisible( anActiveViewId, anObject );
457       anIsVisibleInSelection |= aVisibility;
458       anIsHiddenInSelection |= !aVisibility;
459
460       if ( anObject->CanBeUpdated() && anObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) )
461       {
462         anIsMustObjectBeUpdated = true;
463       }
464
465       ObjectKind anObjectKind = anObject->GetKind();
466       if( anObjectKind == KIND_IMAGE )
467       {
468         anIsImage = true;
469         Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
470         if( !anImage.IsNull() )
471         {
472           anIsImportedImage = anImage->HasLocalPoints();
473           anIsImageHasRefs = anImage->HasReferences();
474           if( HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory() )
475           {
476             if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
477             {
478               QString anOperatorName = anOperator->name();
479               if( anOperatorName == ImageComposer_FuseOperator::Type() )
480                 anIsFusedImage = true;
481               else if( anOperatorName == ImageComposer_CutOperator::Type() )
482                 anIsCutImage = true;
483               else if( anOperatorName == ImageComposer_CropOperator::Type() )
484                 anIsSplitImage = true;
485             }
486           }
487         }
488       }
489       else if( anObjectKind == KIND_POLYLINEXY )
490         anIsPolyline = true;
491       else if( anObjectKind == KIND_POLYLINE )
492         anIsPolyline3D = true;
493       else if( anObjectKind == KIND_PROFILE )
494       {
495         anIsProfile = true;
496         aNbOfSelectedProfiles++;
497
498         Handle(HYDROData_Profile) aProfile = 
499           Handle(HYDROData_Profile)::DownCast( anObject );
500         if( !aProfile.IsNull() && aProfile->IsValid() ) {
501           anIsValidProfile = true;
502         }
503       }
504       else if( anObjectKind == KIND_CALCULATION )
505         anIsCalculation = true;
506       else if( anObjectKind == KIND_IMMERSIBLE_ZONE )
507         anIsImmersibleZone = true;
508       else if( anObjectKind == KIND_VISUAL_STATE )
509         anIsVisualState = true;
510       else if( anObjectKind == KIND_REGION )
511         anIsRegion = true;
512       else if( anObjectKind == KIND_ZONE )
513         anIsZone = true;
514       else if( anObjectKind == KIND_BATHYMETRY )
515         anIsBathymetry = true;
516       else if( anObjectKind == KIND_OBSTACLE )
517         anIsObstacle = true;
518       else if( anObjectKind == KIND_STRICKLER_TABLE )
519         anIsStricklerTable = true;
520       else if( anObjectKind == KIND_LAND_COVER_MAP )
521         anIsLandCoverMap = true;
522       else if (anObjectKind == KIND_BC_POLYGON)
523         anIsBCPolygon = true;
524       else if( anObjectKind == KIND_STREAM )
525       {
526         anIsStream = true;
527         Handle(HYDROData_Stream) aStream = 
528           Handle(HYDROData_Stream)::DownCast( anObject );
529         if ( !aStream.IsNull() )
530           isStreamHasBottom = !aStream->GetBottomPolyline().IsNull();
531       }
532       else if( anObjectKind == KIND_CHANNEL )
533         anIsChannel = true;
534       else if( anObjectKind == KIND_DIGUE )
535         anIsDigue = true;
536       else if( anObjectKind == KIND_DUMMY_3D )
537         anIsDummyObject3D = true;
538       else if( anObjectKind == KIND_SHAPES_GROUP || anObjectKind == KIND_SPLIT_GROUP )
539         anIsGroup = true;
540     }
541
542     if ( !anIsObjectCanBeColored )
543       anIsObjectCanBeColored = HYDROGUI_SetColorOp::CanObjectBeColored( anObject, this );
544   }
545
546   // Check if all selected objects are profiles
547   anAllAreProfiles = ( aNbOfSelectedProfiles > 0 ) &&
548     ( aNbOfSelectedProfiles == aSeq.Length() );
549
550   // check the selected partitions
551   if( !anIsSelectedDataObjects && anIsObjectBrowser )
552   {
553     ObjectKind aSelectedPartition = HYDROGUI_Tool::GetSelectedPartition( this );
554     if( aSelectedPartition != KIND_UNKNOWN )
555     {
556       switch( aSelectedPartition )
557       {
558       case KIND_IMAGE:
559         theMenu->addAction( action( ImportImageId ) );
560         break;
561       case KIND_BATHYMETRY:
562         theMenu->addAction( action( ImportBathymetryId ) );
563         break;
564       case KIND_ARTIFICIAL_OBJECT:
565         theMenu->addAction( action( CreateChannelId ) );
566         theMenu->addAction( action( CreateDigueId ) );
567         break;
568       case KIND_NATURAL_OBJECT:
569         theMenu->addAction( action( CreateImmersibleZoneId ) );
570         theMenu->addAction( action( CreateStreamId ) );
571         break;
572       case KIND_OBSTACLE:
573         theMenu->addAction( action( ImportObstacleFromFileId ) );
574         theMenu->addAction( action( CreateBoxId ) );
575         theMenu->addAction( action( CreateCylinderId ) );
576         break;
577       case KIND_STRICKLER_TABLE:
578         theMenu->addAction( action( ImportStricklerTableFromFileId ) );          
579         break;
580       case KIND_LAND_COVER_MAP:
581         theMenu->addAction( action( CreateLandCoverMapId ) ); 
582         theMenu->addAction( action( ImportLandCoverMapId ) ); 
583         break;
584       case KIND_CALCULATION:
585         theMenu->addAction( action( CreateCalculationId ) );
586         break;
587       case KIND_POLYLINEXY:
588         theMenu->addAction( action( ImportPolylineId ) );
589         theMenu->addAction( action( CreatePolylineId ) );
590         break;
591       case KIND_POLYLINE:
592         theMenu->addAction( action( CreatePolyline3DId ) );
593         break;
594       case KIND_PROFILE:
595         theMenu->addAction( action( CreateProfileId ) );
596         theMenu->addAction( action( ImportProfilesId ) );
597         theMenu->addAction( action( AllGeoreferencementId ) );
598         break;
599       case KIND_VISUAL_STATE:
600         theMenu->addAction( action( SaveVisualStateId ) );
601         break;
602       case KIND_BC_POLYGON:
603         theMenu->addAction( action( ImportBCPolygonId ) );
604         break;
605       }
606       theMenu->addSeparator();
607     }
608     else
609     {
610       Handle(HYDROData_CalculationCase) aCalcCase;
611       QString outStr;
612       HYDROGUI_Tool::IsSelectedPartOfCalcCase(this, aCalcCase, outStr);
613       if (outStr == HYDROGUI_DataModel::partitionName( KIND_REGION ))
614         theMenu->addAction( action( RegenerateRegionColorsId ) );
615     }
616   }
617
618   if( anIsSelectedDataObjects )
619   {
620     if ( anIsMustObjectBeUpdated )
621     {
622       theMenu->addAction( action( UpdateObjectId ) );
623       theMenu->addSeparator();
624     }
625     else
626     {
627       theMenu->addAction( action( ForcedUpdateObjectId ) );
628       theMenu->addSeparator();
629     }
630
631
632     if( aSeq.Length() == 1 )
633     {
634       if( anIsImage )
635       {
636         if( anIsImportedImage )
637           theMenu->addAction( action( EditImportedImageId ) );
638         else if( anIsImageHasRefs )
639         {
640           if( anIsFusedImage )
641             theMenu->addAction( action( EditFusedImageId ) );
642           else if( anIsCutImage )
643             theMenu->addAction( action( EditCutImageId ) );
644           else if( anIsSplitImage )
645             theMenu->addAction( action( EditSplitImageId ) );
646         }
647
648         //RKV: BUG#98: theMenu->addAction( action( ObserveImageId ) );
649         theMenu->addAction( action( ExportImageId ) );
650         theMenu->addSeparator();
651
652         if( anIsImageHasRefs )
653         {
654           theMenu->addAction( action( RemoveImageRefsId ) );
655           theMenu->addSeparator();
656         }
657
658         theMenu->addAction( action( FuseImagesId ) );
659         theMenu->addAction( action( CutImagesId ) );
660         theMenu->addAction( action( SplitImageId ) );
661         theMenu->addSeparator();
662         theMenu->addAction( action( RecognizeContoursId ) );
663         theMenu->addSeparator();
664       }
665       else if( anIsBathymetry )
666       {
667         theMenu->addAction( action( EditImportedBathymetryId ) );
668         theMenu->addAction( action( BathymetryBoundsId ) );
669         theMenu->addSeparator();
670       }
671       else if( anIsPolyline )
672       {
673         theMenu->addAction( action( EditPolylineId ) );
674         theMenu->addSeparator();
675         theMenu->addAction( action( SplitPolylinesId ) );
676         theMenu->addAction( action( MergePolylinesId ) );
677         theMenu->addSeparator();
678         theMenu->addAction( action( ShowAttrPolylinesId ) );
679         theMenu->addSeparator();
680       }
681       else if( anIsPolyline3D )
682       {
683         theMenu->addAction( action( EditPolyline3DId ) );
684         theMenu->addSeparator();
685       }
686       else if( anIsProfile )
687       {
688         theMenu->addAction( action( EditProfileId ) );
689         theMenu->addAction( action( SelectedGeoreferencementId ) );
690         theMenu->addSeparator();
691       }
692       else if( anIsCalculation )
693       {
694         theMenu->addAction( action( EditCalculationId ) );
695         theMenu->addAction( action( ExportCalculationId ) );
696         theMenu->addSeparator();
697       }
698       else if( anIsImmersibleZone )
699       {
700         theMenu->addAction( action( EditImmersibleZoneId ) );
701         theMenu->addSeparator();
702       }
703       else if( anIsStream )
704       {
705         theMenu->addAction( action( EditStreamId ) );
706         if ( action( RiverBottomContextId ) )
707         {
708           theMenu->addAction( action( RiverBottomContextId ) );
709           action( RiverBottomContextId )->setEnabled( !isStreamHasBottom );
710         }
711         theMenu->addAction( action( ProfileInterpolateId ) );
712         theMenu->addSeparator();
713       }
714       else if( anIsChannel )
715       {
716         theMenu->addAction( action( EditChannelId ) );
717         theMenu->addSeparator();
718       }
719       else if( anIsDigue )
720       {
721         theMenu->addAction( action( EditDigueId ) );
722         theMenu->addSeparator();
723       }
724       else if( anIsObstacle )
725       {
726         theMenu->addAction( action( TranslateObstacleId ) );
727         theMenu->addSeparator();
728       }
729       else if( anIsStricklerTable )
730       {
731         theMenu->addAction( action( EditStricklerTableId ) );
732         theMenu->addAction( action( ExportStricklerTableFromFileId ) );
733         theMenu->addAction( action( DuplicateStricklerTableId ) );
734         theMenu->addSeparator();
735
736         Handle(HYDROData_StricklerTable) aTable = 
737           Handle(HYDROData_StricklerTable)::DownCast( aSeq.First() );
738         QString aCurrentTable = 
739           HYDROGUI_DataObject::dataObjectEntry( getLandCoverColoringTable( anActiveViewId ) );
740         bool isUsed = aCurrentTable == HYDROGUI_DataObject::dataObjectEntry( aTable );
741
742         if ( !isUsed && !getObjectShapes( anActiveViewId, KIND_LAND_COVER_MAP ).isEmpty() ) {
743           theMenu->addAction( action( LandCoverScalarMapModeOnId ) );
744           theMenu->addSeparator();
745         }
746       }
747       else if( anIsLandCoverMap )
748       {
749         theMenu->addAction( action( AddLandCoverId ) );
750         theMenu->addAction( action( RemoveLandCoverId ) );
751         theMenu->addSeparator();
752         theMenu->addAction( action( SplitLandCoverId ) );
753         theMenu->addAction( action( MergeLandCoverId ) );
754         theMenu->addAction( action( ChangeLandCoverTypeId ) );
755         theMenu->addSeparator();
756         //
757         theMenu->addAction( action( ExportToShapeFileID ) );
758         theMenu->addSeparator();
759       }      
760       else if( anIsVisualState && anIsObjectBrowser )
761       {
762         theMenu->addAction( action( SaveVisualStateId ) );
763         theMenu->addAction( action( LoadVisualStateId ) );
764         theMenu->addSeparator();
765       }
766       else if (anIsZone)
767         theMenu->addAction( action( ZoneSetColorId ) );
768       else if (anIsBCPolygon)
769         theMenu->addAction( action( SetBoundaryTypePolygonId ) );
770
771       if ( anIsStream || anIsChannel || anIsDigue || anIsObstacle )
772       {
773         theMenu->addAction( action( PolylineExtractionId ) );
774         theMenu->addSeparator();
775       }
776
777       // Add set color action for geometrical objects
778       if ( anIsObjectCanBeColored )
779       {
780         theMenu->addAction( action( SetColorId ) );
781         theMenu->addSeparator();
782       }
783
784       // Add transparency action for land cover map objects
785       if ( anIsLandCoverMap )
786       {
787         theMenu->addAction( action( SetTransparencyId ) );
788         theMenu->addSeparator();
789       }
790     } 
791     else if ( anAllAreProfiles )
792     {
793       theMenu->addAction( action( EditProfileId ) );
794       theMenu->addAction( action( SelectedGeoreferencementId ) );
795       theMenu->addSeparator();
796     }
797
798     bool isPoly = anIsPolyline || anIsPolyline3D;
799     if (isPoly  && !anIsLandCoverMap)
800       theMenu->addAction( action( ExportToShapeFileID ) );
801
802     // Add copy action
803     QAction* aCopyAction = action( CopyId );
804     if( aCopyAction && aCopyAction->isEnabled() ) {
805       theMenu->addAction( action( CopyId ) );
806       theMenu->addSeparator();
807     }
808
809     // Add delete action
810     if( !anIsDummyObject3D )
811       theMenu->addAction( action( DeleteId ) );
812
813     theMenu->addSeparator();
814
815     if( anIsImage || anIsPolyline || anIsPolyline3D || 
816       anIsImmersibleZone || anIsZone || anIsRegion ||
817       anIsBathymetry || anIsObstacle || anIsStream ||
818       anIsChannel || anIsDigue || anIsDummyObject3D ||
819       anIsValidProfile || anIsGroup || anIsLandCoverMap ||
820       anIsBCPolygon)
821     {
822       if( anIsHiddenInSelection )
823         theMenu->addAction( action( ShowId ) );
824       theMenu->addAction( action( ShowOnlyId ) );
825       if( anIsVisibleInSelection )
826         theMenu->addAction( action( HideId ) );
827       theMenu->addSeparator();
828     }
829   }
830
831   if ( anIsOCCView )
832   {
833     SUIT_Operation* anOp = application()->activeStudy()->activeOperation();
834     HYDROGUI_PolylineOp* aPolylineOp = dynamic_cast<HYDROGUI_PolylineOp*>( anOp );
835     if ( aPolylineOp && aPolylineOp->deleteEnabled() )
836       theMenu->addAction( action( DeleteId ) );
837
838     theMenu->addSeparator();
839     theMenu->addAction( action( SetZLevelId ) );
840     theMenu->addSeparator();
841
842     if ( isLandCoversScalarMapModeOn( anActiveViewId ) ) {
843       theMenu->addAction( action( LandCoverScalarMapModeOffId ) );
844       theMenu->addSeparator();
845     }
846   }
847
848   if( anIsObjectBrowser || anIsGraphicsView || anIsOCCView || anIsVTKView )
849   {
850     theMenu->addAction( action( ShowAllId ) );
851     theMenu->addAction( action( HideAllId ) );
852     theMenu->addSeparator();
853   }
854
855   if ( anIsOCCView || anIsVTKView )
856   {
857     theMenu->addSeparator();
858     theMenu->addAction( action( CopyViewerPositionId ) );
859   }
860
861   if( isRoot )
862     theMenu->addAction( action( EditLocalCSId ) );
863
864   if( anIsObjectBrowser && anOwners.size()==1 )
865   {
866     if( aSeq.Size() > 0 )
867     {
868       Handle( HYDROData_Entity ) aFirstEnt = aSeq.First();
869       Handle(HYDROData_Object) anObject;
870       Handle(HYDROData_ArtificialObject) anAObject = Handle( HYDROData_ArtificialObject )::DownCast(aFirstEnt);
871       Handle(HYDROData_NaturalObject) aNObject = Handle( HYDROData_NaturalObject )::DownCast(aFirstEnt);
872
873       if (!anAObject.IsNull())
874         anObject = anAObject;
875       if (!aNObject.IsNull())
876         anObject = aNObject;
877
878       if( !anObject.IsNull() )
879       {
880         theMenu->addSeparator();
881         bool IsSubmersible = anObject->IsSubmersible();
882         if (!IsSubmersible)
883         {
884           theMenu->addAction( action( SubmersibleId ) );
885           action( SubmersibleId )->setCheckable(true);
886           action( SubmersibleId )->setChecked(true);
887         }
888         else
889         {
890           theMenu->addAction( action( UnSubmersibleId ) );
891           action( UnSubmersibleId )->setCheckable(true);
892           action( UnSubmersibleId )->setChecked(true);
893         }
894       }
895     }
896   }
897
898   theMenu->addSeparator();
899   QAction* a = action( ShowHideArrows );
900   a->setText( arrowsVisible() ? tr( "HIDE_ARROWS" ) : tr( "SHOW_ARROWS" ) );
901   theMenu->addAction( a );
902 }
903
904 void HYDROGUI_Module::createPreferences()
905 {
906   int genTab = addPreference( tr( "PREF_TAB_GENERAL" ) );
907   int CursorGroup = addPreference( tr( "PREF_GROUP_CURSOR" ), genTab );
908
909   int typeOfCursor = addPreference( tr( "PREF_TYPE_OF_CURSOR" ), CursorGroup,
910                                     LightApp_Preferences::Selector, "preferences", "type_of_cursor" );
911
912   // Set property cursor type
913   QList<QVariant> aCursorTypeIndicesList;
914   QList<QVariant> aCursorTypeIconsList;
915
916   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
917   for ( int i = CT_ArrowCursor; i < CT_User; i++ ) {
918     QString icoFile = QString( "ICON_CURSOR_%1" ).arg( i+1 );
919     QPixmap pixmap = resMgr->loadPixmap( "HYDRO", tr( qPrintable( icoFile ) ) );
920     aCursorTypeIndicesList << i;
921     aCursorTypeIconsList << pixmap;
922   }
923
924   setPreferenceProperty( typeOfCursor, "indexes", aCursorTypeIndicesList );
925   setPreferenceProperty( typeOfCursor, "icons",   aCursorTypeIconsList );
926
927   int viewerGroup = addPreference( tr( "PREF_GROUP_VIEWER" ), genTab );
928   addPreference( tr( "PREF_VIEWER_AUTO_FITALL" ), viewerGroup,
929                  LightApp_Preferences::Bool, "HYDRO", "auto_fit_all" );
930
931   addPreference( tr( "PREF_VIEWER_ZOOM_SHUTOFF" ), viewerGroup, LightApp_Preferences::Bool, "HYDRO", "zoom_shutoff" );
932
933   addPreference( tr( "PREF_VIEWER_CHAINED_PANNING" ), viewerGroup, LightApp_Preferences::Bool, "HYDRO", "chained_panning" );
934
935   int StricklerTableGroup = addPreference( tr( "PREF_GROUP_STRICKLER_TABLE" ), genTab );
936   int defaultStricklerCoef = addPreference( tr( "PREF_DEFAULT_STRICKLER_COEFFICIENT" ), StricklerTableGroup,
937                                             LightApp_Preferences::DblSpin, "preferences", "default_strickler_coefficient" );
938   setPreferenceProperty( defaultStricklerCoef, "precision", 2 );
939   setPreferenceProperty( defaultStricklerCoef, "min", 0.00 );
940   setPreferenceProperty( defaultStricklerCoef, "max", 1000000.00 );
941   setPreferenceProperty( defaultStricklerCoef, "step", 0.01 );
942
943   int polylinesGroup = addPreference( tr( "PREF_GROUP_POLYLINES" ), genTab );
944   int polylineArrow = addPreference( tr( "PREF_POLYLINE_ARROW" ), polylinesGroup,
945     LightApp_Preferences::Selector, "polyline", "arrow_type" );
946
947   QList<QVariant> arrow_types;
948   arrow_types.append( tr( "No" ) );
949   arrow_types.append( tr( "Triangle" ) );
950   arrow_types.append( tr( "Cone" ) );
951   setPreferenceProperty( polylineArrow, "strings", arrow_types );
952
953   QList<QVariant> indices;
954   indices.append( 0 );
955   indices.append( 1 );
956   indices.append( 2 );
957   setPreferenceProperty( polylineArrow, "indexes", indices );
958   setPreferenceProperty( polylineArrow, "ids", indices );
959
960   int polylineSize = addPreference( tr( "PREF_POLYLINE_ARROW_SIZE" ), polylinesGroup,
961     LightApp_Preferences::IntSpin, "polyline", "arrow_size" );
962 }
963
964 void HYDROGUI_Module::preferencesChanged( const QString& theSection, const QString& thePref )
965 {
966     SUIT_ResourceMgr* resMgr = application()->resourceMgr();
967     if ( theSection == "preferences" && thePref == "default_strickler_coefficient" )
968     {
969         
970         Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
971         if ( resMgr && !aDoc.IsNull() )
972             aDoc->SetDefaultStricklerCoefficient( resMgr->doubleValue( theSection, thePref, 0 ) );
973     }
974     else if( theSection == "polyline" )
975     {
976       int aType = -1;
977       int aSize = -1;
978       if( resMgr )
979       {
980         resMgr->value( "polyline", "arrow_type", aType );
981         resMgr->value( "polyline", "arrow_size", aSize );
982       }
983       //Update polylines
984       ViewManagerMap::const_iterator it = myViewManagerMap.begin(), last = myViewManagerMap.end();
985       for( ; it!=last; it++ )
986       {
987         int aViewerId = it.key();
988         OCCViewer_ViewManager* aMgr = dynamic_cast<OCCViewer_ViewManager*>( it.value().first );
989         if( aMgr )
990           getOCCDisplayer()->UpdatePolylines( aViewerId, aType, aSize );
991       }
992     }
993     else if (theSection == "HYDRO" && thePref == "zoom_shutoff")
994     {
995       bool aZoomShutoff = resMgr->booleanValue( "HYDRO", "zoom_shutoff" );
996       setAutoZoomToAllViewManagers(!aZoomShutoff);
997     }
998     else if (theSection == "HYDRO" && thePref == "chained_panning")
999     {
1000       bool aChainedPan = resMgr->booleanValue( "HYDRO", "chained_panning" );
1001       if (!aChainedPan)
1002         resetViewState();
1003       ViewManagerList aViewManagers = getApp()->viewManagers();
1004       foreach (SUIT_ViewManager* aVMgr, aViewManagers)
1005       {
1006         OCCViewer_ViewManager* anOCCViewMgr = dynamic_cast<OCCViewer_ViewManager*>( aVMgr );
1007         if (anOCCViewMgr)
1008           anOCCViewMgr->setChainedOperations( aChainedPan );
1009       }
1010       
1011       QList<QDockWidget*> docW = getApp()->desktop()->findChildren<QDockWidget*>();
1012       foreach (QDockWidget* qw, docW)
1013       {
1014         HYDROGUI_ProfileDlg* pdlg = dynamic_cast<HYDROGUI_ProfileDlg*>(qw);
1015         if (pdlg)
1016           pdlg->viewManager()->setChainedOperations(aChainedPan);
1017       }
1018     }
1019     else
1020       LightApp_Module::preferencesChanged( theSection, thePref );
1021 }
1022
1023 QCursor HYDROGUI_Module::getPrefEditCursor() const
1024 {
1025   int aCursorType = SUIT_Session::session()->resourceMgr()->integerValue("preferences", "type_of_cursor", (int)CT_CrossCursor );
1026   if ( aCursorType >= Qt::BlankCursor)
1027     aCursorType++;
1028   QCursor aCursor = QCursor( Qt::CursorShape(aCursorType) );
1029   return aCursor;
1030 }
1031
1032 void HYDROGUI_Module::update( const int flags )
1033 {
1034   if ( !isUpdateEnabled() )
1035     return;
1036
1037   QApplication::setOverrideCursor( Qt::WaitCursor );
1038
1039   // To prevent calling this method recursively
1040   // from one of the methods called below
1041   setUpdateEnabled( false );
1042
1043   // store selected objects
1044   QStringList aSelectedEntries = storeSelection();
1045
1046   bool aDoFitAll = flags & UF_FitAll;
1047   if ( aDoFitAll )
1048   {
1049     SUIT_ResourceMgr* aResMgr = getApp()->resourceMgr();
1050     aDoFitAll = aResMgr->booleanValue( "HYDRO", "auto_fit_all", false );
1051   }
1052
1053   if( ( flags & UF_Viewer ) )
1054     updateViewer( getDisplayer(), flags & UF_GV_Init, flags & UF_GV_Forced, aDoFitAll ); 
1055
1056   if( ( flags & UF_OCCViewer ) )
1057     updateViewer( getOCCDisplayer(), flags & UF_OCC_Init, flags & UF_OCC_Forced, aDoFitAll ); 
1058
1059   if( ( flags & UF_VTKViewer ) )
1060     updateViewer( getVTKDisplayer(), flags & UF_VTK_Init, flags & UF_VTK_Forced, aDoFitAll ); 
1061
1062   if( ( flags & UF_Model ) && getDataModel() && getApp() )
1063   {
1064     getDataModel()->update( getStudyId() );
1065
1066     // Temporary workaround to prevent breaking
1067     // the selection in the object browser.
1068     // Note: processEvents() should be called after updateGV(),
1069     // otherwise the application crashes from time to time.
1070     //RKV: qApp->processEvents();
1071     SUIT_ResourceMgr* aResMgr = getApp()->resourceMgr();
1072     bool isResizeOnExpandItem = aResMgr->booleanValue( "ObjectBrowser", "resize_on_expand_item", false );
1073     SUIT_DataBrowser* anObjectBrowser = getApp()->objectBrowser();
1074     if ( isResizeOnExpandItem && anObjectBrowser ) {
1075       anObjectBrowser->setResizeOnExpandItem( false ); // MZN: ISSUE #280 
1076     }
1077     getApp()->updateObjectBrowser( true );
1078     if ( isResizeOnExpandItem && anObjectBrowser ) {
1079       anObjectBrowser->setResizeOnExpandItem( true ); // MZN: ISSUE #280 
1080     }
1081   }
1082
1083   // Object browser is currently updated by using UF_Model flag
1084   if( ( flags & UF_ObjBrowser ) && ((flags & UF_Model) == 0) && getApp() )
1085     getApp()->updateObjectBrowser( true );
1086
1087   if( ( flags & UF_Controls ) && getApp() )
1088     getApp()->updateActions();
1089
1090   // restore selected objects
1091   restoreSelection( aSelectedEntries );
1092
1093   setUpdateEnabled( true );
1094
1095   preferencesChanged( "HYDRO", "zoom_shutoff" );
1096
1097   preferencesChanged( "HYDRO", "chained_panning" );
1098
1099   QApplication::restoreOverrideCursor();
1100 }
1101
1102 void HYDROGUI_Module::updateCommandsStatus()
1103 {
1104   LightApp_Module::updateCommandsStatus();
1105
1106   updateUndoRedoControls();
1107
1108   action( CopyId )->setEnabled( getDataModel()->canCopy() );
1109   action( PasteId )->setEnabled( getDataModel()->canPaste() );
1110 }
1111
1112 void HYDROGUI_Module::selectionChanged()
1113 {
1114   LightApp_Module::selectionChanged();
1115   updateCommandsStatus();
1116 }
1117
1118 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
1119 {
1120   return (HYDROGUI_DataModel*)dataModel();
1121 }
1122
1123 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
1124 {
1125   return myDisplayer;
1126 }
1127
1128 HYDROGUI_OCCDisplayer* HYDROGUI_Module::getOCCDisplayer() const
1129 {
1130   return myOCCDisplayer;
1131 }
1132
1133 HYDROGUI_VTKPrsDisplayer* HYDROGUI_Module::getVTKDisplayer() const
1134 {
1135   return myVTKDisplayer;
1136 }
1137
1138 SUIT_ViewManager* HYDROGUI_Module::getViewManager( const int theId ) const
1139 {
1140   if( myViewManagerMap.contains( theId ) )
1141   {
1142     return myViewManagerMap[ theId ].first;
1143   }
1144   return NULL;
1145 }
1146
1147 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
1148 {
1149   if( myViewManagerMap.contains( theId ) )
1150   {
1151     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
1152     GraphicsView_ViewManager* aViewManager =
1153       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
1154     if( aViewManager )
1155       return aViewManager->getViewer();
1156   }
1157   return NULL;
1158 }
1159
1160 OCCViewer_Viewer* HYDROGUI_Module::getOCCViewer( const int theId ) const
1161 {
1162   if( myViewManagerMap.contains( theId ) )
1163   {
1164     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
1165     OCCViewer_ViewManager* aViewManager =
1166       ::qobject_cast<OCCViewer_ViewManager*>( anInfo.first );
1167     if( aViewManager )
1168       return aViewManager->getOCCViewer();
1169   }
1170   return NULL;
1171 }
1172
1173 SVTK_Viewer* HYDROGUI_Module::getVTKViewer( const int theId ) const
1174 {
1175   if( myViewManagerMap.contains( theId ) )
1176   {
1177     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
1178     SVTK_ViewManager* aViewManager =
1179       ::qobject_cast<SVTK_ViewManager*>( anInfo.first );
1180     if( aViewManager )
1181       return dynamic_cast<SVTK_Viewer*>( aViewManager->getViewModel() );
1182   }
1183   return NULL;
1184 }
1185
1186 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
1187 {
1188   ViewManagerMapIterator anIter( myViewManagerMap );
1189   while( anIter.hasNext() )
1190   {
1191     int anId = anIter.next().key();
1192     const ViewManagerInfo& anInfo = anIter.value();
1193     if( anInfo.first == theViewManager )
1194       return anId;
1195   }
1196   return -1;
1197 }
1198
1199 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
1200 {
1201   int anId = getViewManagerId( theViewManager );
1202   if( anId != -1 )
1203   {
1204     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
1205     return anInfo.second;
1206   }
1207   return VMR_Unknown;
1208 }
1209
1210 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
1211                                           const ViewManagerRole theRole )
1212 {
1213   int anId = getViewManagerId( theViewManager );
1214   if( anId != -1 )
1215   {
1216     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
1217     anInfo.second = theRole;
1218   }
1219 }
1220
1221 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
1222                                        const Handle(HYDROData_Entity)& theObject ) const
1223 {
1224   if( theObject.IsNull() )
1225     return false;
1226
1227   if( theViewId < 0 )
1228   {
1229     //search in all
1230     foreach( int aViewId, myObjectStateMap.keys() )
1231     {
1232       if( isObjectVisible( aViewId, theObject ) )
1233         return true;
1234     }
1235     return false;
1236   }
1237
1238   ViewId2Entry2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
1239   if( anIter1 != myObjectStateMap.end() )
1240   {
1241     const Entry2ObjectStateMap& aEntry2ObjectStateMap = anIter1.value();
1242     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
1243
1244     Entry2ObjectStateMap::const_iterator anIter2 = aEntry2ObjectStateMap.find( anEntry );
1245     if( anIter2 != aEntry2ObjectStateMap.end() )
1246     {
1247       const ObjectState& anObjectState = anIter2.value();
1248       return anObjectState.Visibility;
1249     }
1250   }
1251   return false;
1252 }
1253
1254 void HYDROGUI_Module::setObjectVisible( const int theViewId,
1255                                         const Handle(HYDROData_Entity)& theObject,
1256                                         const bool theState )
1257 {
1258   if( !theObject.IsNull() )
1259   {
1260     Entry2ObjectStateMap& aEntry2ObjectStateMap = myObjectStateMap[ theViewId ];
1261     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
1262
1263     ObjectState& anObjectState = aEntry2ObjectStateMap[ anEntry ];
1264     anObjectState.Visibility = theState;
1265
1266     HYDROGUI_DataObject* hydroObject = getDataModel()->getDataObject( theObject );
1267     if ( hydroObject )
1268     {
1269         SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
1270         if ( treeModel )
1271         {
1272           QString id = hydroObject->text( hydroObject->customData( Qtx::IdType ).toInt() );
1273           Qtx::VisibilityState visState = treeModel->visibilityState( id );
1274           if ( visState != Qtx::UnpresentableState )
1275             treeModel->setVisibilityState( id, theState ? Qtx::ShownState : Qtx::HiddenState );
1276         }
1277     }
1278
1279     if ( theObject->GetKind() == KIND_BATHYMETRY && theState ) {
1280       setLandCoversScalarMapModeOff( theViewId );
1281     } else if ( theObject->GetKind() == KIND_LAND_COVER_MAP && theState ) {
1282       getOCCDisplayer()->SetToUpdateColorScale();
1283     }
1284   }
1285 }
1286
1287 void HYDROGUI_Module::setIsToUpdate( const Handle(HYDROData_Entity)& theObject,
1288                                      const bool theState )
1289 {
1290   if( !theObject.IsNull() )
1291   {
1292     // Process OCC shapes
1293     ViewId2ListOfShapes::const_iterator aShapesMapIter( myShapesMap.begin() );
1294     while( aShapesMapIter != myShapesMap.end() )
1295     {
1296       const ListOfShapes& aShapesList = aShapesMapIter.value();
1297       foreach ( HYDROGUI_Shape* aShape, aShapesList )
1298       {
1299         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
1300         {
1301           aShape->setIsToUpdate( theState );
1302         }
1303       }
1304       aShapesMapIter++;
1305     }
1306     // Process VTK shapes
1307     ViewId2ListOfVTKPrs::const_iterator aVTKPrsMapIter( myVTKPrsMap.begin() );
1308     while( aVTKPrsMapIter != myVTKPrsMap.end() )
1309     {
1310       const ListOfVTKPrs& aShapesList = aVTKPrsMapIter.value();
1311       foreach ( HYDROGUI_VTKPrs* aShape, aShapesList )
1312       {
1313         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
1314         {
1315           aShape->setIsToUpdate( theState );
1316         }
1317       }
1318       aVTKPrsMapIter++;
1319     }
1320   }
1321 }
1322
1323 /////////////////// OCC SHAPES PROCESSING
1324 QList<HYDROGUI_Shape*> HYDROGUI_Module::getObjectShapes( const int  theViewId,
1325                                                          ObjectKind theKind ) const
1326 {
1327   QList<HYDROGUI_Shape*> aResult;
1328
1329   if ( myShapesMap.contains( theViewId ) )
1330   {
1331     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1332     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
1333     {
1334       if( aShape && aShape->getObject()->GetKind()==theKind )
1335         aResult.append( aShape );
1336     }
1337   }
1338   return aResult;
1339 }
1340
1341 HYDROGUI_Shape* HYDROGUI_Module::getObjectShape( const int                       theViewId,
1342                                                  const Handle(HYDROData_Entity)& theObject ) const
1343 {
1344   HYDROGUI_Shape* aResShape = NULL;
1345   if( theObject.IsNull() )
1346     return aResShape;
1347
1348   if ( myShapesMap.contains( theViewId ) )
1349   {
1350     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1351     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
1352     {
1353       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
1354         continue;
1355
1356       aResShape = aShape;
1357       break;
1358     }
1359   }
1360
1361   return aResShape;
1362 }
1363
1364 void HYDROGUI_Module::setObjectShape( const int                       theViewId,
1365                                       const Handle(HYDROData_Entity)& theObject,
1366                                       HYDROGUI_Shape*                 theShape )
1367 {
1368   if( theObject.IsNull() )
1369     return;
1370
1371   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
1372   aViewShapes.append( theShape );
1373 }
1374
1375 void HYDROGUI_Module::removeObjectShape( const int                       theViewId,
1376                                          const Handle(HYDROData_Entity)& theObject )
1377 {
1378   if ( !myShapesMap.contains( theViewId ) )
1379     return;
1380
1381   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
1382   Handle(HYDROData_Entity) anObject;
1383   for ( int i = 0; i < aViewShapes.length(); )
1384   {
1385     HYDROGUI_Shape* aShape = aViewShapes.at( i );
1386     anObject = aShape->getObject();
1387     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1388     {
1389       delete aShape;
1390       aViewShapes.removeAt( i );
1391       continue;
1392     }
1393
1394     ++i;
1395   }
1396 }
1397
1398 void HYDROGUI_Module::removeViewShapes( const int theViewId )
1399 {
1400   if ( !myShapesMap.contains( theViewId ) )
1401     return;
1402
1403   const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1404   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1405   {
1406     HYDROGUI_Shape* aShape = aViewShapes.at( i );
1407     if ( aShape )
1408       delete aShape;
1409   }
1410
1411   myShapesMap.remove( theViewId );
1412 }
1413 /////////////////// END OF OCC SHAPES PROCESSING
1414
1415 /////////////////// VTKPrs PROCESSING
1416 HYDROGUI_VTKPrs* HYDROGUI_Module::getObjectVTKPrs( const int                       theViewId,
1417                                                  const Handle(HYDROData_Entity)& theObject ) const
1418 {
1419   HYDROGUI_VTKPrs* aResShape = NULL;
1420   if( theObject.IsNull() )
1421     return aResShape;
1422
1423   if ( myVTKPrsMap.contains( theViewId ) )
1424   {
1425     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1426     foreach ( HYDROGUI_VTKPrs* aShape, aViewShapes )
1427     {
1428       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
1429         continue;
1430
1431       aResShape = aShape;
1432       break;
1433     }
1434   }
1435
1436   return aResShape;
1437 }
1438
1439 void HYDROGUI_Module::setObjectVTKPrs( const int                       theViewId,
1440                                        const Handle(HYDROData_Entity)& theObject,
1441                                        HYDROGUI_VTKPrs*                 theShape )
1442 {
1443   if( theObject.IsNull() )
1444     return;
1445
1446   if( theShape && theShape->needScalarBar() )
1447   {
1448     // Compute the new global Z range from the added presentation and the old global Z range.
1449     double* aGlobalRange = getVTKDisplayer()->GetZRange( theViewId );
1450     double* aRange = theShape->getInternalZRange();
1451     bool anIsUpdate = false;
1452     if ( aRange[0] < aGlobalRange[0] )
1453     {
1454       aGlobalRange[0] = aRange[0];
1455       anIsUpdate = true;
1456     }
1457     if ( aRange[1] > aGlobalRange[1] )
1458     {
1459       aGlobalRange[1] = aRange[1];
1460       anIsUpdate = true;
1461     }
1462
1463     //if ( anIsUpdate )
1464     //{
1465       updateVTKZRange( theViewId, aGlobalRange );
1466     //}
1467   }
1468
1469   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1470   aViewShapes.append( theShape );
1471 }
1472
1473 void HYDROGUI_Module::removeObjectVTKPrs( const int      theViewId,
1474                                           const QString& theEntry )
1475 {
1476   if ( !myVTKPrsMap.contains( theViewId ) )
1477     return;
1478
1479   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1480   Handle(HYDROData_Entity) anObject;
1481   QString anEntryRef;
1482   for ( int i = 0; i < aViewShapes.length(); )
1483   {
1484     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1485     anObject = aShape->getObject();
1486     anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
1487     if ( aShape && (!anObject.IsNull()) && ( anEntryRef == theEntry ) )
1488     {
1489       delete aShape;
1490       aViewShapes.removeAt( i );
1491       continue;
1492     }
1493
1494     ++i;
1495   }
1496
1497   // Invalidate global Z range
1498   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1499   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1500 }
1501
1502 void HYDROGUI_Module::removeObjectVTKPrs( const int                       theViewId,
1503                                           const Handle(HYDROData_Entity)& theObject )
1504 {
1505   if ( !myVTKPrsMap.contains( theViewId ) )
1506     return;
1507
1508   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1509   Handle(HYDROData_Entity) anObject;
1510   for ( int i = 0; i < aViewShapes.length(); )
1511   {
1512     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1513     anObject = aShape->getObject();
1514     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1515     {
1516       delete aShape;
1517       aViewShapes.removeAt( i );
1518       continue;
1519     }
1520
1521     ++i;
1522   }
1523
1524   // Invalidate global Z range
1525   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1526   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1527 }
1528
1529 void HYDROGUI_Module::removeViewVTKPrs( const int theViewId )
1530 {
1531   if ( !myVTKPrsMap.contains( theViewId ) )
1532     return;
1533
1534   const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1535   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1536   {
1537     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1538     if ( aShape )
1539       delete aShape;
1540   }
1541
1542   myVTKPrsMap.remove( theViewId );
1543 }
1544
1545 void HYDROGUI_Module::updateVTKZRange( const int theViewId, double theRange[] )
1546 {
1547   if ( myVTKPrsMap.contains( theViewId ) )
1548   {
1549     // For the given viewer id update all VTK presentations ...
1550     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1551     HYDROGUI_VTKPrs* aShape;
1552     for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1553     {
1554       aShape = aViewShapes.at( i );
1555       if ( aShape && aShape->needScalarBar() )
1556       {
1557         aShape->setZRange( theRange );
1558       }
1559     }
1560   }
1561   // ... and update the global color legend scalar bar.
1562   getVTKDisplayer()->SetZRange( theViewId, theRange );
1563 }
1564 /////////////////// END OF VTKPrs PROCESSING
1565
1566 void HYDROGUI_Module::clearCache()
1567 {
1568     myObjectStateMap.clear();
1569 }
1570
1571 CAM_DataModel* HYDROGUI_Module::createDataModel()
1572 {
1573   return new HYDROGUI_DataModel( this );
1574 }
1575
1576 void HYDROGUI_Module::customEvent( QEvent* e )
1577 {
1578   int aType = e->type();
1579   if ( aType == NewViewEvent )
1580   {
1581     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
1582     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
1583     {
1584       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
1585       {
1586         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
1587         ViewManagerRole aRole = getViewManagerRole( aViewManager );
1588
1589         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
1590         {
1591           if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1592             aViewPort->scale( 1, -1 ); // invert the Y axis direction from down to up
1593
1594           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
1595           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
1596           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
1597
1598           //ouv: temporarily commented
1599           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
1600         }
1601
1602         if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1603           update( UF_Viewer );
1604
1605         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
1606       }
1607     }
1608   }
1609 }
1610
1611 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
1612 {
1613   QEvent::Type aType = theEvent->type();
1614   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
1615   {
1616     if( aType == QEvent::Show )
1617     {
1618       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
1619       e->setData( theObj );
1620       QApplication::postEvent( this, e );
1621       theObj->removeEventFilter( this );
1622     }
1623   }
1624   else if ( theObj->inherits( "OCCViewer_ViewPort" ) )
1625   {
1626     if( aType == QEvent::Leave )
1627     {
1628       SUIT_Desktop* aDesktop = getApp()->desktop();
1629       if ( aDesktop && aDesktop->statusBar() ) {
1630         aDesktop->statusBar()->clearMessage();
1631       }
1632     }
1633   }
1634   else if ( theObj->inherits( "SVTK_ViewWindow" ) )
1635   {
1636     if( aType == QEvent::Leave )
1637     {
1638       SUIT_Desktop* aDesktop = getApp()->desktop();
1639       if ( aDesktop && aDesktop->statusBar() ) {
1640         aDesktop->statusBar()->clearMessage();
1641       }
1642     }
1643   }
1644
1645   return LightApp_Module::eventFilter( theObj, theEvent );
1646 }
1647
1648 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
1649 {
1650   LightApp_Module::onViewManagerAdded( theViewManager );
1651
1652   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
1653   { 
1654     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1655              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1656   }
1657   else if( theViewManager->getType() == OCCViewer_Viewer::Type() )
1658   {
1659     OCCViewer_ViewManager* mgr = dynamic_cast<OCCViewer_ViewManager*>( theViewManager );
1660     //mgr->setChainedOperations( true );
1661
1662     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1663              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1664     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1665              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1666     connect( theViewManager, SIGNAL( activated( SUIT_ViewManager* ) ), 
1667              this, SLOT( onViewActivated( SUIT_ViewManager* ) ) );
1668   }
1669   else if( theViewManager->getType() == SVTK_Viewer::Type() )
1670   {
1671     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1672              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1673     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1674              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1675   }
1676
1677   createSelector( theViewManager ); // replace the default selector
1678
1679   ViewManagerInfo anInfo( theViewManager, VMR_General );
1680   myViewManagerMap.insert( ViewManagerId++, anInfo );
1681 }
1682
1683 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
1684 {
1685   LightApp_Module::onViewManagerRemoved( theViewManager );
1686
1687   createSelector( theViewManager ); // replace the default selector
1688
1689   int anId = getViewManagerId( theViewManager );
1690   if( anId != -1 )
1691   {
1692     OCCViewer_ViewManager* anOCCViewManager =
1693       ::qobject_cast<OCCViewer_ViewManager*>( myViewManagerMap[ anId ].first );
1694     if ( anOCCViewManager )
1695     {
1696       OCCViewer_Viewer* anOCCViewer = anOCCViewManager->getOCCViewer();
1697       if ( anOCCViewer ) {
1698         int aViewerId = (size_t)anOCCViewer;
1699         removeViewShapes( aViewerId );
1700         setLandCoversScalarMapModeOff( aViewerId );
1701       }
1702     }
1703
1704     if ( getVTKDisplayer()->IsApplicable( theViewManager ) )
1705     {
1706       SVTK_Viewer* aVTKViewer = getVTKViewer( anId );
1707       if ( aVTKViewer )
1708       {
1709         getVTKDisplayer()->EraseScalarBar( anId, true );
1710         removeViewShapes( (size_t)aVTKViewer );
1711       }
1712     }
1713
1714     myViewManagerMap.remove( anId );
1715   }
1716 }
1717
1718 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
1719 {
1720   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
1721   {
1722     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
1723     {
1724       aViewFrame->installEventFilter( this );
1725
1726       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
1727       aViewPort->setInteractionFlag( GraphicsView_ViewPort::GlobalWheelScaling );
1728
1729       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
1730                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
1731     }
1732   }
1733   else if( theViewWindow && theViewWindow->inherits( "OCCViewer_ViewFrame" ) )
1734   {
1735     if( OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theViewWindow ) )
1736     {
1737       aViewFrame->onTopView();
1738
1739       HYDROGUI_Tool::setOCCActionShown( aViewFrame, OCCViewer_ViewWindow::MaximizedId, false );
1740       OCCViewer_ViewPort3d* aViewPort = aViewFrame->getViewPort();
1741       if ( aViewPort ) {
1742         aViewPort->installEventFilter( this );
1743       }
1744     }
1745   }
1746   else if( theViewWindow && theViewWindow->inherits( "SVTK_ViewWindow" ) )
1747   {
1748     if( SVTK_ViewWindow* aViewFrame = dynamic_cast<SVTK_ViewWindow*>( theViewWindow ) )
1749     {
1750       aViewFrame->installEventFilter( this );
1751     }
1752   }
1753 }
1754
1755 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
1756 {
1757   /* ouv: currently unused
1758   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
1759   {
1760     SUIT_ViewManager* aViewManager = 0;
1761
1762     QObject* aParent = aViewPort;
1763     while( aParent = aParent->parent() )
1764     {
1765       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
1766       {
1767         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
1768         {
1769           aViewManager = aViewer->getViewManager();
1770           break;
1771         }
1772       }
1773     }
1774
1775     if( !aViewManager )
1776       return;
1777
1778     double aMouseX = theEvent->scenePos().x();
1779     double aMouseY = theEvent->scenePos().y();
1780
1781     ViewManagerRole aRole = getViewManagerRole( aViewManager );
1782     if( aRole == VMR_General )
1783     {
1784       int aXDeg = 0, aYDeg = 0;
1785       int aXMin = 0, aYMin = 0;
1786       double aXSec = 0, aYSec = 0;
1787       HYDROData_Lambert93::secToDMS( aMouseX, aXDeg, aXMin, aXSec );
1788       HYDROData_Lambert93::secToDMS( aMouseY, aYDeg, aYMin, aYSec );
1789
1790       QString aDegSymbol( QChar( 0x00B0 ) );
1791       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
1792       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
1793
1794       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
1795     }
1796     else if( aRole == VMR_TransformImage )
1797       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
1798   }
1799   */
1800 }
1801
1802 void HYDROGUI_Module::updateViewer( HYDROGUI_AbstractDisplayer* theDisplayer, 
1803                                     const bool theIsInit, 
1804                                     const bool theIsForced, 
1805                                     const bool theDoFitAll )
1806 {
1807   QList<int> aViewManagerIdList;
1808
1809   // currently, all views are updated
1810   ViewManagerMapIterator anIter( myViewManagerMap );
1811   while( anIter.hasNext() )
1812   { 
1813     SUIT_ViewManager* aViewManager = anIter.next().value().first;
1814
1815     if ( theDisplayer->IsApplicable( aViewManager ) )
1816     {
1817       int anId = anIter.key();
1818       aViewManagerIdList.append( anId );
1819     }
1820   }
1821
1822   QListIterator<int> anIdIter( aViewManagerIdList );
1823   while( anIdIter.hasNext() )
1824   {
1825     theDisplayer->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
1826     myOverview->setTopView();
1827   }
1828 }
1829
1830 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
1831 {
1832   if( !theViewManager )
1833     return;
1834
1835   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1836   if( !aSelectionMgr )
1837     return;
1838
1839   QString aViewType = theViewManager->getType();
1840   if( aViewType != GraphicsView_Viewer::Type() &&
1841       aViewType != OCCViewer_Viewer::Type())
1842     return;
1843
1844   QList<SUIT_Selector*> aSelectorList;
1845   aSelectionMgr->selectors( aViewType, aSelectorList );
1846
1847   // disable all alien selectors
1848   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
1849   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
1850   {
1851     SUIT_Selector* aSelector = *anIter;
1852     if( aSelector && ( !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) &&
1853                        !dynamic_cast<SVTK_Selector*>( aSelector ) &&
1854                        !dynamic_cast<HYDROGUI_OCCSelector*>( aSelector ) ) )
1855       aSelector->setEnabled( false );
1856   }
1857
1858   if ( aViewType == GraphicsView_Viewer::Type() )
1859   {
1860     GraphicsView_ViewManager* aViewManager =
1861       ::qobject_cast<GraphicsView_ViewManager*>( theViewManager );
1862     if( aViewManager )
1863       new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
1864   }
1865   else if ( aViewType == OCCViewer_Viewer::Type() )
1866   {
1867     OCCViewer_ViewManager* aViewManager =
1868       ::qobject_cast<OCCViewer_ViewManager*>( theViewManager );
1869     if( aViewManager )
1870       new HYDROGUI_OCCSelector( this, aViewManager->getOCCViewer(), aSelectionMgr );
1871   }
1872 }
1873
1874 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
1875 {
1876   bool aPrevState = myIsUpdateEnabled;
1877   myIsUpdateEnabled = theState;
1878   return aPrevState;
1879 }
1880
1881 bool HYDROGUI_Module::isUpdateEnabled() const
1882 {
1883   return myIsUpdateEnabled;
1884 }
1885
1886 QStringList HYDROGUI_Module::storeSelection() const
1887 {
1888   QStringList anEntryList;
1889   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1890   {
1891     SUIT_DataOwnerPtrList aList( true );
1892     aSelectionMgr->selected( aList );
1893
1894     SUIT_DataOwnerPtrList::iterator anIter;
1895     for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
1896     {
1897       const LightApp_DataOwner* anOwner = 
1898         dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
1899       if( anOwner )
1900         anEntryList.append( anOwner->entry() );
1901     }
1902   }
1903   return anEntryList;
1904 }
1905
1906 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
1907 {
1908   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1909   {
1910     SUIT_DataOwnerPtrList aList( true );
1911     for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
1912       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
1913     aSelectionMgr->setSelected( aList );
1914   }
1915 }
1916
1917 void HYDROGUI_Module::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* )
1918 {
1919   double X, Y, Z;
1920   bool doShow = false;
1921   HYDROGUI_Displayer* aDisplayer = getDisplayer();
1922   if ( aDisplayer )
1923     aDisplayer->SaveCursorViewPosition( theViewWindow );
1924     doShow = aDisplayer->GetCursorViewCoordinates( theViewWindow, X, Y, Z );
1925
1926   if ( doShow )
1927   {
1928     // Show the coordinates in the status bar
1929     SUIT_Desktop* aDesktop = getApp()->desktop();
1930     if ( aDesktop && aDesktop->statusBar() )
1931     {
1932       gp_Pnt aWPnt( X, Y, Z );
1933       int aStudyId = application()->activeStudy()->id();
1934       HYDROData_Document::Document( aStudyId )->Transform( aWPnt, false );
1935       double WX = aWPnt.X(), WY = aWPnt.Y();
1936
1937       QString aXStr = HYDROGUI_Tool::GetCoordinateString( X, true );
1938       QString anYStr = HYDROGUI_Tool::GetCoordinateString( Y, true );
1939       QString aWXStr = HYDROGUI_Tool::GetCoordinateString( WX, true );
1940       QString aWYStr = HYDROGUI_Tool::GetCoordinateString( WY, true );
1941       QString aMsg = tr( "COORDINATES_INFO" );
1942       aMsg = aMsg.arg( aXStr ).arg( anYStr ).arg( aWXStr ).arg( aWYStr );
1943       aDesktop->statusBar()->showMessage( aMsg );
1944     }
1945   }
1946 }
1947
1948 /**
1949  * Returns stack of active operations;
1950  */
1951 QStack<HYDROGUI_Operation*>& HYDROGUI_Module::getActiveOperations()
1952 {
1953   return myActiveOperationMap;
1954 }
1955
1956 /**
1957  * Returns the module active operation. If the active operation is show/hide,
1958  * the method returns the previous operation if it is.
1959  */
1960 HYDROGUI_Operation* HYDROGUI_Module::activeOperation()
1961 {
1962   HYDROGUI_Operation* anOp = !myActiveOperationMap.empty() ? myActiveOperationMap.top() : 0;
1963
1964   if ( dynamic_cast<HYDROGUI_ShowHideOp*>( anOp ) )
1965   {
1966     QVectorIterator<HYDROGUI_Operation*> aVIt( myActiveOperationMap );
1967     aVIt.toBack();
1968     aVIt.previous(); // skip the top show/hide operation
1969     anOp = aVIt.hasPrevious() ? aVIt.previous() : 0;
1970   }
1971
1972   return anOp;
1973 }
1974
1975 /*!
1976  * \brief Virtual public slot
1977  *
1978  * This method is called after the object inserted into data view to update their visibility state
1979  * This is default implementation
1980  */
1981 void HYDROGUI_Module::onObjectClicked( SUIT_DataObject* theObject, int theColumn )
1982 {
1983   if ( !isActiveModule() )
1984       return;
1985
1986   HYDROGUI_DataObject* hydroObject = dynamic_cast<HYDROGUI_DataObject*>( theObject );
1987
1988   // change visibility of object
1989   if ( !hydroObject || theColumn != SUIT_DataObject::VisibilityId )
1990       return;
1991
1992   SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
1993
1994   QString id = theObject->text( theObject->customData( Qtx::IdType ).toInt() );
1995   Qtx::VisibilityState visState = treeModel->visibilityState( id );
1996   if ( visState == Qtx::UnpresentableState )
1997       return;
1998
1999   visState = visState == Qtx::ShownState ? Qtx::HiddenState : Qtx::ShownState;
2000   treeModel->setVisibilityState( id, visState );
2001
2002   bool vis = visState == Qtx::ShownState;
2003   if ( vis == isObjectVisible( HYDROGUI_Tool::GetActiveViewId( this ), hydroObject->modelObject() ) )
2004       return;
2005
2006   setObjectVisible( HYDROGUI_Tool::GetActiveViewId( this ), hydroObject->modelObject(), vis );
2007
2008   update( UF_OCCViewer | UF_VTKViewer | ( visState == Qtx::ShownState ? UF_FitAll : 0 ) );
2009 }
2010
2011 bool HYDROGUI_Module::isDraggable( const SUIT_DataObject* what ) const
2012 {
2013   return true;
2014 }
2015
2016 bool HYDROGUI_Module::isDropAccepted( const SUIT_DataObject* where ) const
2017 {
2018
2019   return true;
2020 }
2021
2022 void HYDROGUI_Module::dropObjects( const DataObjectList& what, SUIT_DataObject* where,
2023                                    const int row, Qt::DropAction action )
2024 {
2025   if ( action != Qt::CopyAction && action != Qt::MoveAction )
2026     return; 
2027
2028   if (row == -1)
2029     return;
2030
2031   if (where->level() < 2 )
2032     return;  
2033
2034   DataObjectList::ConstIterator it = what.constBegin();
2035   for (;it != what.constEnd();++it) 
2036   {
2037     if ((*it)->parent() != where)
2038       return;
2039   }
2040
2041   it = what.constBegin();
2042
2043   int i=0;
2044   for (;it != what.constEnd();++it) 
2045   {
2046     SUIT_DataObject* objWhat = *it;
2047
2048     DataObjectList objInSect = where->children();
2049
2050     //std::list<SUIT_DataObject*> t1 = where->children().toStdList(); //debug
2051     int ind = objInSect.indexOf(objWhat);
2052     if (ind != -1)
2053     {
2054       HYDROGUI_DataModel* aModel = getDataModel();
2055       int pos = -1;
2056       if (ind >= row)
2057       {
2058         pos = row + i;
2059         i++;
2060       }
2061       else
2062         pos = row - 1;
2063       where->moveChildPos(objWhat, pos);
2064       //std::list<SUIT_DataObject*> t2 = where->children().toStdList(); //debug
2065     }  
2066   }
2067
2068   getApp()->updateObjectBrowser(true);
2069   
2070 }
2071
2072 Handle(HYDROData_StricklerTable) HYDROGUI_Module::getLandCoverColoringTable( const int theViewId ) const
2073 {
2074   Handle(HYDROData_StricklerTable) aTable;
2075
2076   if ( myLandCoverColoringMap.contains( theViewId ) ) {
2077     aTable = myLandCoverColoringMap.value( theViewId );
2078   }
2079
2080   return aTable;
2081 }
2082
2083 void HYDROGUI_Module::setLandCoverColoringTable( const int theViewId,
2084                                                  const Handle(HYDROData_StricklerTable)& theTable )
2085 {
2086   if ( !theTable.IsNull() ) {
2087     myLandCoverColoringMap.insert( theViewId, theTable );
2088   }
2089 }
2090
2091 void HYDROGUI_Module::setLandCoversScalarMapModeOff( const int theViewId )
2092 {
2093   myLandCoverColoringMap.remove( theViewId );
2094 }
2095
2096 bool HYDROGUI_Module::isLandCoversScalarMapModeOn( const int theViewId ) const
2097 {
2098   return myLandCoverColoringMap.contains( theViewId );
2099 }
2100
2101 void HYDROGUI_Module::setObjectRemoved( const Handle(HYDROData_Entity)& theObject )
2102 {
2103   if ( theObject.IsNull() || !theObject->IsRemoved() ) {
2104     return;
2105   }
2106
2107   if ( theObject->GetKind() == KIND_STRICKLER_TABLE ) {
2108     Handle(HYDROData_StricklerTable) aTable = 
2109       Handle(HYDROData_StricklerTable)::DownCast( theObject );
2110     QList<int> aViewIds;
2111     QMutableMapIterator<int, Handle(HYDROData_StricklerTable)> anIter( myLandCoverColoringMap );
2112     while ( anIter.hasNext() ) {
2113       if ( HYDROGUI_DataObject::dataObjectEntry( anIter.next().value() ) == 
2114            HYDROGUI_DataObject::dataObjectEntry( aTable ) ) {
2115         anIter.remove();
2116       }
2117     }
2118   }
2119 }
2120
2121 void HYDROGUI_Module::onViewActivated( SUIT_ViewManager* theMgr )
2122 {
2123   if( !theMgr )
2124     return;
2125
2126   SUIT_ViewWindow* wnd = theMgr->getActiveView();
2127   OCCViewer_ViewFrame* occwnd = dynamic_cast<OCCViewer_ViewFrame*>( wnd );
2128   if( !occwnd )
2129     return;
2130
2131   myOverview->setMainView( occwnd );
2132 }
2133
2134 void HYDROGUI_Module::setAutoZoomToAllViewManagers(bool bAutoZoom)
2135 {
2136   ViewManagerList aViewManagers = getApp()->viewManagers();
2137   foreach (SUIT_ViewManager* aVMgr, aViewManagers)
2138     setAutoZoom(aVMgr, bAutoZoom);
2139 }
2140
2141 void HYDROGUI_Module::setAutoZoom(SUIT_ViewManager* aVMgr, bool bAutoZoom)
2142 {
2143   if (!aVMgr)
2144     return;
2145   QVector<SUIT_ViewWindow*> aViews = aVMgr->getViews();
2146   foreach (SUIT_ViewWindow* aView, aViews)
2147   {
2148     if (aView)
2149     {
2150       OCCViewer_ViewFrame* anOCCViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aView );
2151       if (anOCCViewFrame)
2152       {
2153         anOCCViewFrame->setAutomaticZoom(bAutoZoom);
2154         for (int i = OCCViewer_ViewFrame::MAIN_VIEW; i<=OCCViewer_ViewFrame::TOP_RIGHT; i++)
2155         {
2156           OCCViewer_ViewWindow* aV = anOCCViewFrame->getView(i);
2157           if (aV)
2158             aV->setAutomaticZoom(bAutoZoom);
2159         }
2160       }
2161       OCCViewer_ViewWindow* anOCCViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( aView );
2162       if (anOCCViewWindow)
2163         anOCCViewWindow->setAutomaticZoom(bAutoZoom);
2164     }
2165   }
2166 }
2167