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