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