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