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