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