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