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