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