Salome HOME
remove traces
[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 << " " << theState);
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         DEBTRACE("hydroObject " << hydroObject);
1341         SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
1342         if ( treeModel )
1343         {
1344           DEBTRACE("treeModel " << treeModel);
1345           QString id = hydroObject->text( hydroObject->customData( Qtx::IdType ).toInt() );
1346           Qtx::VisibilityState visState = treeModel->visibilityState( id );
1347           DEBTRACE("id " << id << " visState "<< visState);
1348           if ( visState != Qtx::UnpresentableState )
1349             treeModel->setVisibilityState( id, theState ? Qtx::ShownState : Qtx::HiddenState );
1350         }
1351     }
1352
1353     if ( theObject->GetKind() == KIND_BATHYMETRY && theState ) {
1354       setLandCoversScalarMapModeOff( theViewId );
1355     } else if ( theObject->GetKind() == KIND_LAND_COVER_MAP && theState ) {
1356       getOCCDisplayer()->SetToUpdateColorScale();
1357     }
1358   }
1359 }
1360
1361 void HYDROGUI_Module::setIsToUpdate( const Handle(HYDROData_Entity)& theObject,
1362                                      const bool theState )
1363 {
1364   DEBTRACE("HYDROGUI_Module::setIsToUpdate");
1365   if( !theObject.IsNull() )
1366   {
1367     // Process OCC shapes
1368     ViewId2ListOfShapes::const_iterator aShapesMapIter( myShapesMap.begin() );
1369     while( aShapesMapIter != myShapesMap.end() )
1370     {
1371       const ListOfShapes& aShapesList = aShapesMapIter.value();
1372       foreach ( HYDROGUI_Shape* aShape, aShapesList )
1373       {
1374         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
1375         {
1376           aShape->setIsToUpdate( theState );
1377         }
1378       }
1379       aShapesMapIter++;
1380     }
1381     // Process VTK shapes
1382     ViewId2ListOfVTKPrs::const_iterator aVTKPrsMapIter( myVTKPrsMap.begin() );
1383     while( aVTKPrsMapIter != myVTKPrsMap.end() )
1384     {
1385       const ListOfVTKPrs& aShapesList = aVTKPrsMapIter.value();
1386       foreach ( HYDROGUI_VTKPrs* aShape, aShapesList )
1387       {
1388         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
1389         {
1390           aShape->setIsToUpdate( theState );
1391         }
1392       }
1393       aVTKPrsMapIter++;
1394     }
1395   }
1396 }
1397
1398 /////////////////// OCC SHAPES PROCESSING
1399 QList<HYDROGUI_Shape*> HYDROGUI_Module::getObjectShapes( const size_t  theViewId,
1400                                                          ObjectKind theKind ) const
1401 {
1402   QList<HYDROGUI_Shape*> aResult;
1403
1404   if ( myShapesMap.contains( theViewId ) )
1405   {
1406     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1407     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
1408     {
1409       if( aShape && aShape->getObject()->GetKind()==theKind )
1410         aResult.append( aShape );
1411     }
1412   }
1413   return aResult;
1414 }
1415
1416 HYDROGUI_Shape* HYDROGUI_Module::getObjectShape( const size_t                    theViewId,
1417                                                  const Handle(HYDROData_Entity)& theObject ) const
1418 {
1419   HYDROGUI_Shape* aResShape = NULL;
1420   if( theObject.IsNull() )
1421     return aResShape;
1422
1423   if ( myShapesMap.contains( theViewId ) )
1424   {
1425     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1426     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
1427     {
1428       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
1429         continue;
1430
1431       aResShape = aShape;
1432       break;
1433     }
1434   }
1435
1436   return aResShape;
1437 }
1438
1439 void HYDROGUI_Module::setObjectShape( const size_t                    theViewId,
1440                                       const Handle(HYDROData_Entity)& theObject,
1441                                       HYDROGUI_Shape*                 theShape )
1442 {
1443   if( theObject.IsNull() )
1444     return;
1445
1446   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
1447   aViewShapes.append( theShape );
1448 }
1449
1450 void HYDROGUI_Module::removeObjectShape( const size_t                    theViewId,
1451                                          const Handle(HYDROData_Entity)& theObject )
1452 {
1453   if ( !myShapesMap.contains( theViewId ) )
1454     return;
1455
1456   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
1457   Handle(HYDROData_Entity) anObject;
1458   for ( int i = 0; i < aViewShapes.length(); )
1459   {
1460     HYDROGUI_Shape* aShape = aViewShapes.at( i );
1461     anObject = aShape->getObject();
1462     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1463     {
1464       DEBTRACE("delete shape :" << aShape->getObject()->GetName().toStdString());
1465       delete aShape;
1466       aViewShapes.removeAt( i );
1467       continue;
1468     }
1469
1470     ++i;
1471   }
1472 }
1473
1474 void HYDROGUI_Module::removeViewShapes( const size_t theViewId )
1475 {
1476   if ( !myShapesMap.contains( theViewId ) )
1477     return;
1478
1479   const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1480   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1481   {
1482     HYDROGUI_Shape* aShape = aViewShapes.at( i );
1483     if ( aShape )
1484       delete aShape;
1485   }
1486
1487   myShapesMap.remove( theViewId );
1488 }
1489 /////////////////// END OF OCC SHAPES PROCESSING
1490
1491 /////////////////// VTKPrs PROCESSING
1492 HYDROGUI_VTKPrs* HYDROGUI_Module::getObjectVTKPrs( const size_t                  theViewId,
1493                                                  const Handle(HYDROData_Entity)& theObject ) const
1494 {
1495   HYDROGUI_VTKPrs* aResShape = NULL;
1496   if( theObject.IsNull() )
1497     return aResShape;
1498
1499   if ( myVTKPrsMap.contains( theViewId ) )
1500   {
1501     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1502     foreach ( HYDROGUI_VTKPrs* aShape, aViewShapes )
1503     {
1504       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
1505         continue;
1506
1507       aResShape = aShape;
1508       break;
1509     }
1510   }
1511
1512   return aResShape;
1513 }
1514
1515 void HYDROGUI_Module::setObjectVTKPrs( const size_t                    theViewId,
1516                                        const Handle(HYDROData_Entity)& theObject,
1517                                        HYDROGUI_VTKPrs*                 theShape )
1518 {
1519   if( theObject.IsNull() )
1520     return;
1521
1522   if( theShape && theShape->needScalarBar() )
1523   {
1524     // Compute the new global Z range from the added presentation and the old global Z range.
1525     double* aGlobalRange = getVTKDisplayer()->GetZRange( theViewId );
1526     double* aRange = theShape->getInternalZRange();
1527     bool anIsUpdate = false;
1528     if ( aRange[0] < aGlobalRange[0] )
1529     {
1530       aGlobalRange[0] = aRange[0];
1531       anIsUpdate = true;
1532     }
1533     if ( aRange[1] > aGlobalRange[1] )
1534     {
1535       aGlobalRange[1] = aRange[1];
1536       anIsUpdate = true;
1537     }
1538
1539     //if ( anIsUpdate )
1540     //{
1541       updateVTKZRange( theViewId, aGlobalRange );
1542     //}
1543   }
1544
1545   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1546   aViewShapes.append( theShape );
1547 }
1548
1549 void HYDROGUI_Module::removeObjectVTKPrs( const size_t   theViewId,
1550                                           const QString& theEntry )
1551 {
1552   if ( !myVTKPrsMap.contains( theViewId ) )
1553     return;
1554
1555   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1556   Handle(HYDROData_Entity) anObject;
1557   QString anEntryRef;
1558   for ( int i = 0; i < aViewShapes.length(); )
1559   {
1560     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1561     anObject = aShape->getObject();
1562     anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
1563     if ( aShape && (!anObject.IsNull()) && ( anEntryRef == theEntry ) )
1564     {
1565       delete aShape;
1566       aViewShapes.removeAt( i );
1567       continue;
1568     }
1569
1570     ++i;
1571   }
1572
1573   // Invalidate global Z range
1574   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1575   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1576 }
1577
1578 void HYDROGUI_Module::removeObjectVTKPrs( const size_t                    theViewId,
1579                                           const Handle(HYDROData_Entity)& theObject )
1580 {
1581   if ( !myVTKPrsMap.contains( theViewId ) )
1582     return;
1583
1584   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1585   Handle(HYDROData_Entity) anObject;
1586   for ( int i = 0; i < aViewShapes.length(); )
1587   {
1588     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1589     anObject = aShape->getObject();
1590     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1591     {
1592       delete aShape;
1593       aViewShapes.removeAt( i );
1594       continue;
1595     }
1596
1597     ++i;
1598   }
1599
1600   // Invalidate global Z range
1601   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1602   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1603 }
1604
1605 void HYDROGUI_Module::removeViewVTKPrs( const size_t theViewId )
1606 {
1607   if ( !myVTKPrsMap.contains( theViewId ) )
1608     return;
1609
1610   const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1611   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1612   {
1613     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1614     if ( aShape )
1615       delete aShape;
1616   }
1617
1618   myVTKPrsMap.remove( theViewId );
1619 }
1620
1621 void HYDROGUI_Module::updateVTKZRange( const size_t theViewId, double theRange[] )
1622 {
1623   if ( myVTKPrsMap.contains( theViewId ) )
1624   {
1625     // For the given viewer id update all VTK presentations ...
1626     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1627     HYDROGUI_VTKPrs* aShape;
1628     for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1629     {
1630       aShape = aViewShapes.at( i );
1631       if ( aShape && aShape->needScalarBar() )
1632       {
1633         aShape->setZRange( theRange );
1634       }
1635     }
1636   }
1637   // ... and update the global color legend scalar bar.
1638   getVTKDisplayer()->SetZRange( theViewId, theRange );
1639 }
1640 /////////////////// END OF VTKPrs PROCESSING
1641
1642 void HYDROGUI_Module::clearCache()
1643 {
1644     DEBTRACE("HYDROGUI_Module::clearCache");
1645     myObjectStateMap.clear();
1646 }
1647
1648 CAM_DataModel* HYDROGUI_Module::createDataModel()
1649 {
1650   DEBTRACE("HYDROGUI_Module::createDataModel");
1651   return new HYDROGUI_DataModel( this );
1652 }
1653
1654 void HYDROGUI_Module::customEvent( QEvent* e )
1655 {
1656   int aType = e->type();
1657   if ( aType == NewViewEvent )
1658   {
1659     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
1660     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
1661     {
1662       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
1663       {
1664         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
1665         ViewManagerRole aRole = getViewManagerRole( aViewManager );
1666
1667         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
1668         {
1669           if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1670             aViewPort->scale( 1, -1 ); // invert the Y axis direction from down to up
1671
1672           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
1673           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
1674           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
1675
1676           //ouv: temporarily commented
1677           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
1678         }
1679
1680         if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1681           update( UF_Viewer );
1682
1683         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
1684       }
1685     }
1686   }
1687 }
1688
1689 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
1690 {
1691   QEvent::Type aType = theEvent->type();
1692   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
1693   {
1694     if( aType == QEvent::Show )
1695     {
1696       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
1697       e->setData( theObj );
1698       QApplication::postEvent( this, e );
1699       theObj->removeEventFilter( this );
1700     }
1701   }
1702   else if ( theObj->inherits( "OCCViewer_ViewPort" ) )
1703   {
1704     if( aType == QEvent::Leave )
1705     {
1706       SUIT_Desktop* aDesktop = getApp()->desktop();
1707       if ( aDesktop && aDesktop->statusBar() ) {
1708         aDesktop->statusBar()->clearMessage();
1709       }
1710     }
1711   }
1712   else if ( theObj->inherits( "SVTK_ViewWindow" ) )
1713   {
1714     if( aType == QEvent::Leave )
1715     {
1716       SUIT_Desktop* aDesktop = getApp()->desktop();
1717       if ( aDesktop && aDesktop->statusBar() ) {
1718         aDesktop->statusBar()->clearMessage();
1719       }
1720     }
1721   }
1722
1723   return LightApp_Module::eventFilter( theObj, theEvent );
1724 }
1725
1726 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
1727 {
1728   DEBTRACE("HYDROGUI_Module::onViewManagerAdded");
1729   LightApp_Module::onViewManagerAdded( theViewManager );
1730
1731   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
1732   { 
1733     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1734              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1735   }
1736   else if( theViewManager->getType() == OCCViewer_Viewer::Type() )
1737   {
1738     OCCViewer_ViewManager* mgr = dynamic_cast<OCCViewer_ViewManager*>( theViewManager );
1739     //mgr->setChainedOperations( true );
1740
1741     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1742              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1743     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1744              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1745     connect( theViewManager, SIGNAL( activated( SUIT_ViewManager* ) ), 
1746              this, SLOT( onViewActivated( SUIT_ViewManager* ) ) );
1747   }
1748   else if( theViewManager->getType() == SVTK_Viewer::Type() )
1749   {
1750     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1751              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1752     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1753              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1754   }
1755
1756   createSelector( theViewManager ); // replace the default selector
1757
1758   ViewManagerInfo anInfo( theViewManager, VMR_General );
1759   myViewManagerMap.insert( ViewManagerId++, anInfo );
1760 }
1761
1762 void HYDROGUI_Module::removeViewManager( SUIT_ViewManager* theViewManager)
1763 {
1764   DEBTRACE("removeViewManager")
1765
1766   size_t anId = getViewManagerId( theViewManager );
1767   if( anId != 0 )
1768   {
1769     bool isDisableGVSelector = theViewManager->getType() == GraphicsView_Viewer::Type();
1770     createSelector( theViewManager, isDisableGVSelector ); // replace the default selector
1771
1772     OCCViewer_ViewManager* anOCCViewManager =
1773       ::qobject_cast<OCCViewer_ViewManager*>( myViewManagerMap[ anId ].first );
1774     if ( anOCCViewManager )
1775     {
1776       OCCViewer_Viewer* anOCCViewer = anOCCViewManager->getOCCViewer();
1777       if ( anOCCViewer ) {
1778         size_t aViewerId = (size_t)anOCCViewer;
1779         removeViewShapes( aViewerId );
1780         setLandCoversScalarMapModeOff( aViewerId );
1781       }
1782     }
1783
1784     if ( getVTKDisplayer()->IsApplicable( theViewManager ) )
1785     {
1786       SVTK_Viewer* aVTKViewer = getVTKViewer( anId );
1787       if ( aVTKViewer )
1788       {
1789         getVTKDisplayer()->EraseScalarBar( anId, true );
1790         removeViewShapes( (size_t)aVTKViewer );
1791       }
1792     }
1793
1794     myViewManagerMap.remove( anId );
1795   }
1796 }
1797
1798 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
1799 {
1800   DEBTRACE("HYDROGUI_Module::onViewManagerRemoved");
1801   removeViewManager(theViewManager);
1802   LightApp_Module::onViewManagerRemoved( theViewManager );
1803
1804 }
1805
1806 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
1807 {
1808   DEBTRACE("HYDROGUI_Module::onViewCreated");
1809   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
1810   {
1811         DEBTRACE("theViewWindow->inherits( 'GraphicsView_ViewFrame' )");
1812     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
1813     {
1814       DEBTRACE("---");
1815       aViewFrame->installEventFilter( this );
1816
1817       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
1818       aViewPort->setInteractionFlag( GraphicsView_ViewPort::GlobalWheelScaling );
1819
1820       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
1821                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
1822     }
1823   }
1824   else if( theViewWindow && theViewWindow->inherits( "OCCViewer_ViewFrame" ) )
1825   {
1826         DEBTRACE("theViewWindow->inherits( 'OCCViewer_ViewFrame' )");
1827     if( OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theViewWindow ) )
1828     {
1829       DEBTRACE("---");
1830       aViewFrame->onTopView();
1831
1832       HYDROGUI_Tool::setOCCActionShown( aViewFrame, OCCViewer_ViewWindow::MaximizedId, false );
1833       OCCViewer_ViewPort3d* aViewPort = aViewFrame->getViewPort();
1834       if ( aViewPort ) {
1835         aViewPort->installEventFilter( this );
1836       }
1837     }
1838   }
1839   else if( theViewWindow && theViewWindow->inherits( "SVTK_ViewWindow" ) )
1840   {
1841         DEBTRACE("theViewWindow->inherits( 'SVTK_ViewWindow' )");
1842     if( SVTK_ViewWindow* aViewFrame = dynamic_cast<SVTK_ViewWindow*>( theViewWindow ) )
1843     {
1844       DEBTRACE("---");
1845       aViewFrame->installEventFilter( this );
1846     }
1847   }
1848 }
1849
1850 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
1851 {
1852   /* ouv: currently unused
1853   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
1854   {
1855     SUIT_ViewManager* aViewManager = 0;
1856
1857     QObject* aParent = aViewPort;
1858     while( aParent = aParent->parent() )
1859     {
1860       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
1861       {
1862         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
1863         {
1864           aViewManager = aViewer->getViewManager();
1865           break;
1866         }
1867       }
1868     }
1869
1870     if( !aViewManager )
1871       return;
1872
1873     double aMouseX = theEvent->scenePos().x();
1874     double aMouseY = theEvent->scenePos().y();
1875
1876     ViewManagerRole aRole = getViewManagerRole( aViewManager );
1877     if( aRole == VMR_General )
1878     {
1879       int aXDeg = 0, aYDeg = 0;
1880       int aXMin = 0, aYMin = 0;
1881       double aXSec = 0, aYSec = 0;
1882       HYDROData_Lambert93::secToDMS( aMouseX, aXDeg, aXMin, aXSec );
1883       HYDROData_Lambert93::secToDMS( aMouseY, aYDeg, aYMin, aYSec );
1884
1885       QString aDegSymbol( QChar( 0x00B0 ) );
1886       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
1887       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
1888
1889       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
1890     }
1891     else if( aRole == VMR_TransformImage )
1892       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
1893   }
1894   */
1895 }
1896
1897 void HYDROGUI_Module::updateViewer( HYDROGUI_AbstractDisplayer* theDisplayer, 
1898                                     const bool theIsInit, 
1899                                     const bool theIsForced, 
1900                                     const bool theDoFitAll )
1901 {
1902   DEBTRACE("HYDROGUI_Module::updateViewer");
1903   QList<size_t> aViewManagerIdList;
1904
1905   // currently, all views are updated
1906   ViewManagerMapIterator anIter( myViewManagerMap );
1907   while( anIter.hasNext() )
1908   { 
1909     SUIT_ViewManager* aViewManager = anIter.next().value().first;
1910
1911     if ( theDisplayer->IsApplicable( aViewManager ) )
1912     {
1913       int anId = anIter.key();
1914       aViewManagerIdList.append( anId );
1915     }
1916   }
1917
1918   QListIterator<size_t> anIdIter( aViewManagerIdList );
1919   while( anIdIter.hasNext() )
1920   {
1921     theDisplayer->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
1922     myOverview->setTopView();
1923   }
1924 }
1925
1926 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager, bool isDisableGV )
1927 {
1928   DEBTRACE("HYDROGUI_Module::createSelector " << isDisableGV);
1929   if( !theViewManager )
1930     return;
1931
1932   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1933   if( !aSelectionMgr )
1934     return;
1935
1936   QString aViewType = theViewManager->getType();
1937   DEBTRACE("aViewType " << aViewType.toStdString());
1938   if( aViewType != GraphicsView_Viewer::Type() &&
1939       aViewType != OCCViewer_Viewer::Type())
1940     return;
1941
1942   QList<SUIT_Selector*> aSelectorList;
1943   aSelectionMgr->selectors( aViewType, aSelectorList );
1944
1945   // disable all alien selectors
1946   bool isGV = false;
1947   bool isOCC = false;
1948   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
1949   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
1950   {
1951     SUIT_Selector* aSelector = *anIter;
1952     bool isOk = false;
1953     if (aSelector)
1954     {
1955         DEBTRACE("Selector " << aSelector->type().toStdString());
1956         if (dynamic_cast<HYDROGUI_GVSelector*>( aSelector ))
1957         {
1958                 isOk = !isDisableGV;
1959                 isGV = true;
1960                 DEBTRACE("HYDROGUI_GVSelector " << isOk);
1961         }
1962         else if (dynamic_cast<SVTK_Selector*>( aSelector ))
1963         {
1964                 isOk = true;
1965                 DEBTRACE("SVTK_Selector");
1966         }
1967         else if (dynamic_cast<HYDROGUI_OCCSelector*>( aSelector ))
1968         {
1969                 isOk = true;
1970                 isOCC = true;
1971                 DEBTRACE("HYDROGUI_OCCSelector");
1972         }
1973         if (isOk)
1974                 {
1975                   DEBTRACE("Selector enabled " << aSelector->type().toStdString());
1976                   aSelector->setEnabled( true );
1977                 }
1978         else
1979                 {
1980                   DEBTRACE("Selector disabled " << aSelector->type().toStdString());
1981                   aSelector->setEnabled( false );
1982                 }
1983     }
1984   }
1985
1986   if ( aViewType == GraphicsView_Viewer::Type() )
1987   {
1988     GraphicsView_ViewManager* aViewManager =
1989       ::qobject_cast<GraphicsView_ViewManager*>( theViewManager );
1990     if( aViewManager )
1991     {
1992       HYDROGUI_GVSelector* sel = new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
1993       DEBTRACE("new HYDROGUI_GVSelector " << sel << " " << sel->type().toStdString());
1994       sel->setEnabled( true );
1995     }
1996   }
1997   else if ( aViewType == OCCViewer_Viewer::Type() )
1998   {
1999     OCCViewer_ViewManager* aViewManager =
2000       ::qobject_cast<OCCViewer_ViewManager*>( theViewManager );
2001     if( aViewManager )
2002     {
2003       HYDROGUI_OCCSelector* sel =  new HYDROGUI_OCCSelector( this, aViewManager->getOCCViewer(), aSelectionMgr );
2004       DEBTRACE("new HYDROGUI_OCCSelector " << sel << " " << sel->type().toStdString());
2005       sel->setEnabled( true );
2006     }
2007   }
2008 }
2009
2010 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
2011 {
2012   DEBTRACE("HYDROGUI_Module::setUpdateEnabled");
2013   bool aPrevState = myIsUpdateEnabled;
2014   myIsUpdateEnabled = theState;
2015   return aPrevState;
2016 }
2017
2018 bool HYDROGUI_Module::isUpdateEnabled() const
2019 {
2020   return myIsUpdateEnabled;
2021 }
2022
2023 QStringList HYDROGUI_Module::storeSelection() const
2024 {
2025   DEBTRACE("HYDROGUI_Module::storeSelection");
2026   QStringList anEntryList;
2027   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
2028   {
2029     SUIT_DataOwnerPtrList aList( true );
2030     aSelectionMgr->selected( aList );
2031
2032     SUIT_DataOwnerPtrList::iterator anIter;
2033     for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
2034     {
2035       const LightApp_DataOwner* anOwner = 
2036         dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
2037       if( anOwner )
2038         anEntryList.append( anOwner->entry() );
2039     }
2040   }
2041   return anEntryList;
2042 }
2043
2044 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
2045 {
2046   DEBTRACE("HYDROGUI_Module::restoreSelection");
2047   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
2048   {
2049     SUIT_DataOwnerPtrList aList( true );
2050     for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
2051       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
2052     aSelectionMgr->setSelected( aList );
2053   }
2054 }
2055
2056 void HYDROGUI_Module::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* )
2057 {
2058   double X, Y, Z;
2059   bool doShow = false;
2060   HYDROGUI_Displayer* aDisplayer = getDisplayer();
2061   if ( aDisplayer )
2062     aDisplayer->SaveCursorViewPosition( theViewWindow );
2063     doShow = aDisplayer->GetCursorViewCoordinates( theViewWindow, X, Y, Z );
2064
2065   if ( doShow )
2066   {
2067     // Show the coordinates in the status bar
2068     SUIT_Desktop* aDesktop = getApp()->desktop();
2069     if ( aDesktop && aDesktop->statusBar() )
2070     {
2071       gp_Pnt aWPnt( X, Y, Z );
2072       HYDROData_Document::Document()->Transform( aWPnt, false );
2073       double WX = aWPnt.X(), WY = aWPnt.Y();
2074
2075       QString aXStr = HYDROGUI_Tool::GetCoordinateString( X, true );
2076       QString anYStr = HYDROGUI_Tool::GetCoordinateString( Y, true );
2077       QString aWXStr = HYDROGUI_Tool::GetCoordinateString( WX, true );
2078       QString aWYStr = HYDROGUI_Tool::GetCoordinateString( WY, true );
2079       QString aMsg = tr( "COORDINATES_INFO" );
2080       aMsg = aMsg.arg( aXStr ).arg( anYStr ).arg( aWXStr ).arg( aWYStr );
2081       aDesktop->statusBar()->showMessage( aMsg );
2082     }
2083   }
2084 }
2085
2086 /**
2087  * Returns stack of active operations;
2088  */
2089 QStack<HYDROGUI_Operation*>& HYDROGUI_Module::getActiveOperations()
2090 {
2091   DEBTRACE("HYDROGUI_Module::getActiveOperations");
2092   return myActiveOperationMap;
2093 }
2094
2095 /**
2096  * Returns the module active operation. If the active operation is show/hide,
2097  * the method returns the previous operation if it is.
2098  */
2099 HYDROGUI_Operation* HYDROGUI_Module::activeOperation()
2100 {
2101   HYDROGUI_Operation* anOp = !myActiveOperationMap.empty() ? myActiveOperationMap.top() : 0;
2102
2103   if ( dynamic_cast<HYDROGUI_ShowHideOp*>( anOp ) )
2104   {
2105     QVectorIterator<HYDROGUI_Operation*> aVIt( myActiveOperationMap );
2106     aVIt.toBack();
2107     aVIt.previous(); // skip the top show/hide operation
2108     anOp = aVIt.hasPrevious() ? aVIt.previous() : 0;
2109   }
2110
2111   return anOp;
2112 }
2113
2114 /*!
2115  * \brief Virtual public slot
2116  *
2117  * This method is called after the object inserted into data view to update their visibility state
2118  * This is default implementation
2119  */
2120 void HYDROGUI_Module::onObjectClicked( SUIT_DataObject* theObject, int theColumn )
2121 {
2122   if ( !isActiveModule() )
2123       return;
2124
2125   HYDROGUI_DataObject* hydroObject = dynamic_cast<HYDROGUI_DataObject*>( theObject );
2126
2127   // change visibility of object
2128   if ( !hydroObject || theColumn != SUIT_DataObject::VisibilityId )
2129       return;
2130
2131   SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
2132
2133   QString id = theObject->text( theObject->customData( Qtx::IdType ).toInt() );
2134   Qtx::VisibilityState visState = treeModel->visibilityState( id );
2135   if ( visState == Qtx::UnpresentableState )
2136       return;
2137
2138   visState = visState == Qtx::ShownState ? Qtx::HiddenState : Qtx::ShownState;
2139   treeModel->setVisibilityState( id, visState );
2140
2141   bool vis = visState == Qtx::ShownState;
2142   if ( vis == isObjectVisible( HYDROGUI_Tool::GetActiveViewId( this ), hydroObject->modelObject() ) )
2143       return;
2144
2145   setObjectVisible( HYDROGUI_Tool::GetActiveViewId( this ), hydroObject->modelObject(), vis );
2146
2147   update( UF_OCCViewer | UF_VTKViewer | ( visState == Qtx::ShownState ? UF_FitAll : 0 ) );
2148 }
2149
2150 bool HYDROGUI_Module::isDraggable( const SUIT_DataObject* what ) const
2151 {
2152   return true;
2153 }
2154
2155 bool HYDROGUI_Module::isDropAccepted( const SUIT_DataObject* where ) const
2156 {
2157
2158   return true;
2159 }
2160
2161 void HYDROGUI_Module::dropObjects( const DataObjectList& what, SUIT_DataObject* where,
2162                                    const int row, Qt::DropAction action )
2163 {
2164   if ( action != Qt::CopyAction && action != Qt::MoveAction )
2165     return; 
2166
2167   if (row == -1)
2168     return;
2169
2170   if (where->level() < 2 )
2171     return;  
2172
2173   DataObjectList::ConstIterator it = what.constBegin();
2174   for (;it != what.constEnd();++it) 
2175   {
2176     if ((*it)->parent() != where)
2177       return;
2178   }
2179
2180   it = what.constBegin();
2181
2182   int i=0;
2183   for (;it != what.constEnd();++it) 
2184   {
2185     SUIT_DataObject* objWhat = *it;
2186
2187     DataObjectList objInSect = where->children();
2188
2189     //std::list<SUIT_DataObject*> t1 = where->children().toStdList(); //debug
2190     int ind = objInSect.indexOf(objWhat);
2191     if (ind != -1)
2192     {
2193       HYDROGUI_DataModel* aModel = getDataModel();
2194       int pos = -1;
2195       if (ind >= row)
2196       {
2197         pos = row + i;
2198         i++;
2199       }
2200       else
2201         pos = row - 1;
2202       where->moveChildPos(objWhat, pos);
2203       //std::list<SUIT_DataObject*> t2 = where->children().toStdList(); //debug
2204     }  
2205   }
2206
2207   getApp()->updateObjectBrowser(true);
2208   
2209 }
2210
2211 Handle(HYDROData_StricklerTable) HYDROGUI_Module::getLandCoverColoringTable( const size_t theViewId ) const
2212 {
2213   Handle(HYDROData_StricklerTable) aTable;
2214
2215   if ( myLandCoverColoringMap.contains( theViewId ) ) {
2216     aTable = myLandCoverColoringMap.value( theViewId );
2217   }
2218
2219   return aTable;
2220 }
2221
2222 void HYDROGUI_Module::setLandCoverColoringTable( const size_t theViewId,
2223                                                  const Handle(HYDROData_StricklerTable)& theTable )
2224 {
2225   if ( !theTable.IsNull() && theViewId ) {
2226     myLandCoverColoringMap.insert( theViewId, theTable );
2227   }
2228 }
2229
2230 void HYDROGUI_Module::setLandCoversScalarMapModeOff( const size_t theViewId )
2231 {
2232   myLandCoverColoringMap.remove( theViewId );
2233 }
2234
2235 bool HYDROGUI_Module::isLandCoversScalarMapModeOn( const size_t theViewId ) const
2236 {
2237   return myLandCoverColoringMap.contains( theViewId );
2238 }
2239
2240 void HYDROGUI_Module::setObjectRemoved( const Handle(HYDROData_Entity)& theObject )
2241 {
2242   if ( theObject.IsNull() || !theObject->IsRemoved() ) {
2243     return;
2244   }
2245
2246   if ( theObject->GetKind() == KIND_STRICKLER_TABLE ) {
2247     Handle(HYDROData_StricklerTable) aTable = 
2248       Handle(HYDROData_StricklerTable)::DownCast( theObject );
2249     QMutableMapIterator<size_t, Handle(HYDROData_StricklerTable)> anIter( myLandCoverColoringMap );
2250     while ( anIter.hasNext() ) {
2251       if ( HYDROGUI_DataObject::dataObjectEntry( anIter.next().value() ) == 
2252            HYDROGUI_DataObject::dataObjectEntry( aTable ) ) {
2253         anIter.remove();
2254       }
2255     }
2256   }
2257 }
2258
2259 void HYDROGUI_Module::onViewActivated( SUIT_ViewManager* theMgr )
2260 {
2261   DEBTRACE("HYDROGUI_Module::onViewActivated");
2262   if( !theMgr )
2263     return;
2264
2265   SUIT_ViewWindow* wnd = theMgr->getActiveView();
2266   OCCViewer_ViewFrame* occwnd = dynamic_cast<OCCViewer_ViewFrame*>( wnd );
2267   if( !occwnd )
2268     return;
2269
2270   myOverview->setMainView( occwnd );
2271 }
2272
2273 void HYDROGUI_Module::setAutoZoomToAllViewManagers(bool bAutoZoom)
2274 {
2275   ViewManagerList aViewManagers = getApp()->viewManagers();
2276   foreach (SUIT_ViewManager* aVMgr, aViewManagers)
2277     setAutoZoom(aVMgr, bAutoZoom);
2278 }
2279
2280 void HYDROGUI_Module::setAutoZoom(SUIT_ViewManager* aVMgr, bool bAutoZoom)
2281 {
2282   if (!aVMgr)
2283     return;
2284   QVector<SUIT_ViewWindow*> aViews = aVMgr->getViews();
2285   foreach (SUIT_ViewWindow* aView, aViews)
2286   {
2287     if (aView)
2288     {
2289       OCCViewer_ViewFrame* anOCCViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aView );
2290       if (anOCCViewFrame)
2291       {
2292         anOCCViewFrame->setAutomaticZoom(bAutoZoom);
2293         for (int i = OCCViewer_ViewFrame::MAIN_VIEW; i<=OCCViewer_ViewFrame::TOP_RIGHT; i++)
2294         {
2295           OCCViewer_ViewWindow* aV = anOCCViewFrame->getView(i);
2296           if (aV)
2297             aV->setAutomaticZoom(bAutoZoom);
2298         }
2299       }
2300       OCCViewer_ViewWindow* anOCCViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( aView );
2301       if (anOCCViewWindow)
2302         anOCCViewWindow->setAutomaticZoom(bAutoZoom);
2303     }
2304   }
2305 }
2306