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