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