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