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