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