]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_Module.cxx
Salome HOME
refs #526: objects excluded from the final geometry
[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   if( anIsObjectBrowser && anOwners.size()==1 )
705   {
706     Handle( HYDROData_Object ) anObject = Handle( HYDROData_Object )::DownCast( aSeq.First() );
707     if( !anObject.IsNull() )
708     {
709       theMenu->addSeparator();
710       theMenu->addAction( action( SubmersibleId ) );
711       action( SubmersibleId )->setCheckable( true );
712       action( SubmersibleId )->setChecked( anObject->IsSubmersible() );
713     }
714   }
715 }
716
717 void HYDROGUI_Module::update( const int flags )
718 {
719   if( !isUpdateEnabled() )
720     return;
721
722   QApplication::setOverrideCursor( Qt::WaitCursor );
723
724   // To prevent calling this method recursively
725   // from one of the methods called below
726   setUpdateEnabled( false );
727
728   // store selected objects
729   QStringList aSelectedEntries = storeSelection();
730
731   bool aDoFitAll = flags & UF_FitAll;
732   if( ( flags & UF_Viewer ) )
733     updateViewer( getDisplayer(), flags & UF_GV_Init, flags & UF_GV_Forced, aDoFitAll ); 
734
735   if( ( flags & UF_OCCViewer ) )
736     updateViewer( getOCCDisplayer(), flags & UF_OCC_Init, flags & UF_OCC_Forced, aDoFitAll ); 
737
738   if( ( flags & UF_VTKViewer ) )
739     updateViewer( getVTKDisplayer(), flags & UF_VTK_Init, flags & UF_VTK_Forced, aDoFitAll ); 
740
741   if( ( flags & UF_Model ) && getDataModel() && getApp() )
742   {
743     getDataModel()->update( getStudyId() );
744
745     // Temporary workaround to prevent breaking
746     // the selection in the object browser.
747     // Note: processEvents() should be called after updateGV(),
748     // otherwise the application crashes from time to time.
749     //RKV: qApp->processEvents();
750     SUIT_ResourceMgr* aResMgr = getApp()->resourceMgr();
751     bool isResizeOnExpandItem = aResMgr->booleanValue( "ObjectBrowser", "resize_on_expand_item", false );
752     SUIT_DataBrowser* anObjectBrowser = getApp()->objectBrowser();
753     if ( isResizeOnExpandItem && anObjectBrowser ) {
754       anObjectBrowser->setResizeOnExpandItem( false ); // MZN: ISSUE #280 
755     }
756     getApp()->updateObjectBrowser( true );
757     if ( isResizeOnExpandItem && anObjectBrowser ) {
758       anObjectBrowser->setResizeOnExpandItem( true ); // MZN: ISSUE #280 
759     }
760   }
761
762   // Object browser is currently updated by using UF_Model flag
763   if( ( flags & UF_ObjBrowser ) && ((flags & UF_Model) == 0) && getApp() )
764     getApp()->updateObjectBrowser( true );
765
766   if( ( flags & UF_Controls ) && getApp() )
767     getApp()->updateActions();
768
769   // restore selected objects
770   restoreSelection( aSelectedEntries );
771
772   setUpdateEnabled( true );
773
774   QApplication::restoreOverrideCursor();
775 }
776
777 void HYDROGUI_Module::updateCommandsStatus()
778 {
779   LightApp_Module::updateCommandsStatus();
780
781   updateUndoRedoControls();
782
783   action( CopyId )->setEnabled( getDataModel()->canCopy() );
784   action( PasteId )->setEnabled( getDataModel()->canPaste() );
785 }
786
787 void HYDROGUI_Module::selectionChanged()
788 {
789   LightApp_Module::selectionChanged();
790   updateCommandsStatus();
791 }
792
793 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
794 {
795   return (HYDROGUI_DataModel*)dataModel();
796 }
797
798 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
799 {
800   return myDisplayer;
801 }
802
803 HYDROGUI_OCCDisplayer* HYDROGUI_Module::getOCCDisplayer() const
804 {
805   return myOCCDisplayer;
806 }
807
808 HYDROGUI_VTKPrsDisplayer* HYDROGUI_Module::getVTKDisplayer() const
809 {
810   return myVTKDisplayer;
811 }
812
813 SUIT_ViewManager* HYDROGUI_Module::getViewManager( const int theId ) const
814 {
815   if( myViewManagerMap.contains( theId ) )
816   {
817     return myViewManagerMap[ theId ].first;
818   }
819   return NULL;
820 }
821
822 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
823 {
824   if( myViewManagerMap.contains( theId ) )
825   {
826     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
827     GraphicsView_ViewManager* aViewManager =
828       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
829     if( aViewManager )
830       return aViewManager->getViewer();
831   }
832   return NULL;
833 }
834
835 OCCViewer_Viewer* HYDROGUI_Module::getOCCViewer( const int theId ) const
836 {
837   if( myViewManagerMap.contains( theId ) )
838   {
839     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
840     OCCViewer_ViewManager* aViewManager =
841       ::qobject_cast<OCCViewer_ViewManager*>( anInfo.first );
842     if( aViewManager )
843       return aViewManager->getOCCViewer();
844   }
845   return NULL;
846 }
847
848 SVTK_Viewer* HYDROGUI_Module::getVTKViewer( const int theId ) const
849 {
850   if( myViewManagerMap.contains( theId ) )
851   {
852     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
853     SVTK_ViewManager* aViewManager =
854       ::qobject_cast<SVTK_ViewManager*>( anInfo.first );
855     if( aViewManager )
856       return dynamic_cast<SVTK_Viewer*>( aViewManager->getViewModel() );
857   }
858   return NULL;
859 }
860
861 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
862 {
863   ViewManagerMapIterator anIter( myViewManagerMap );
864   while( anIter.hasNext() )
865   {
866     int anId = anIter.next().key();
867     const ViewManagerInfo& anInfo = anIter.value();
868     if( anInfo.first == theViewManager )
869       return anId;
870   }
871   return -1;
872 }
873
874 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
875 {
876   int anId = getViewManagerId( theViewManager );
877   if( anId != -1 )
878   {
879     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
880     return anInfo.second;
881   }
882   return VMR_Unknown;
883 }
884
885 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
886                                           const ViewManagerRole theRole )
887 {
888   int anId = getViewManagerId( theViewManager );
889   if( anId != -1 )
890   {
891     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
892     anInfo.second = theRole;
893   }
894 }
895
896 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
897                                        const Handle(HYDROData_Entity)& theObject ) const
898 {
899   if( theObject.IsNull() )
900     return false;
901
902   ViewId2Entry2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
903   if( anIter1 != myObjectStateMap.end() )
904   {
905     const Entry2ObjectStateMap& aEntry2ObjectStateMap = anIter1.value();
906     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
907
908     Entry2ObjectStateMap::const_iterator anIter2 = aEntry2ObjectStateMap.find( anEntry );
909     if( anIter2 != aEntry2ObjectStateMap.end() )
910     {
911       const ObjectState& anObjectState = anIter2.value();
912       return anObjectState.Visibility;
913     }
914   }
915   return false;
916 }
917
918 void HYDROGUI_Module::setObjectVisible( const int theViewId,
919                                         const Handle(HYDROData_Entity)& theObject,
920                                         const bool theState )
921 {
922   if( !theObject.IsNull() )
923   {
924     Entry2ObjectStateMap& aEntry2ObjectStateMap = myObjectStateMap[ theViewId ];
925     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
926
927     ObjectState& anObjectState = aEntry2ObjectStateMap[ anEntry ];
928     anObjectState.Visibility = theState;
929   }
930 }
931
932 void HYDROGUI_Module::setIsToUpdate( const Handle(HYDROData_Entity)& theObject,
933                                      const bool theState )
934 {
935   if( !theObject.IsNull() )
936   {
937     // Process OCC shapes
938     ViewId2ListOfShapes::const_iterator aShapesMapIter( myShapesMap.begin() );
939     while( aShapesMapIter != myShapesMap.end() )
940     {
941       const ListOfShapes& aShapesList = aShapesMapIter.value();
942       foreach ( HYDROGUI_Shape* aShape, aShapesList )
943       {
944         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
945         {
946           aShape->setIsToUpdate( theState );
947         }
948       }
949       aShapesMapIter++;
950     }
951     // Process VTK shapes
952     ViewId2ListOfVTKPrs::const_iterator aVTKPrsMapIter( myVTKPrsMap.begin() );
953     while( aVTKPrsMapIter != myVTKPrsMap.end() )
954     {
955       const ListOfVTKPrs& aShapesList = aVTKPrsMapIter.value();
956       foreach ( HYDROGUI_VTKPrs* aShape, aShapesList )
957       {
958         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
959         {
960           aShape->setIsToUpdate( theState );
961         }
962       }
963       aVTKPrsMapIter++;
964     }
965   }
966 }
967
968 /////////////////// OCC SHAPES PROCESSING
969 QList<HYDROGUI_Shape*> HYDROGUI_Module::getObjectShapes( const int  theViewId,
970                                                          ObjectKind theKind ) const
971 {
972   QList<HYDROGUI_Shape*> aResult;
973
974   if ( myShapesMap.contains( theViewId ) )
975   {
976     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
977     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
978     {
979       if( aShape && aShape->getObject()->GetKind()==theKind )
980         aResult.append( aShape );
981     }
982   }
983   return aResult;
984 }
985
986 HYDROGUI_Shape* HYDROGUI_Module::getObjectShape( const int                       theViewId,
987                                                  const Handle(HYDROData_Entity)& theObject ) const
988 {
989   HYDROGUI_Shape* aResShape = NULL;
990   if( theObject.IsNull() )
991     return aResShape;
992
993   if ( myShapesMap.contains( theViewId ) )
994   {
995     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
996     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
997     {
998       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
999         continue;
1000
1001       aResShape = aShape;
1002       break;
1003     }
1004   }
1005
1006   return aResShape;
1007 }
1008
1009 void HYDROGUI_Module::setObjectShape( const int                       theViewId,
1010                                       const Handle(HYDROData_Entity)& theObject,
1011                                       HYDROGUI_Shape*                 theShape )
1012 {
1013   if( theObject.IsNull() )
1014     return;
1015
1016   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
1017   aViewShapes.append( theShape );
1018 }
1019
1020 void HYDROGUI_Module::removeObjectShape( const int                       theViewId,
1021                                          const Handle(HYDROData_Entity)& theObject )
1022 {
1023   if ( !myShapesMap.contains( theViewId ) )
1024     return;
1025
1026   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
1027   Handle(HYDROData_Entity) anObject;
1028   for ( int i = 0; i < aViewShapes.length(); )
1029   {
1030     HYDROGUI_Shape* aShape = aViewShapes.at( i );
1031     anObject = aShape->getObject();
1032     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1033     {
1034       delete aShape;
1035       aViewShapes.removeAt( i );
1036       continue;
1037     }
1038
1039     ++i;
1040   }
1041 }
1042
1043 void HYDROGUI_Module::removeViewShapes( const int theViewId )
1044 {
1045   if ( !myShapesMap.contains( theViewId ) )
1046     return;
1047
1048   const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1049   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1050   {
1051     HYDROGUI_Shape* aShape = aViewShapes.at( i );
1052     if ( aShape )
1053       delete aShape;
1054   }
1055
1056   myShapesMap.remove( theViewId );
1057 }
1058 /////////////////// END OF OCC SHAPES PROCESSING
1059
1060 /////////////////// VTKPrs PROCESSING
1061 HYDROGUI_VTKPrs* HYDROGUI_Module::getObjectVTKPrs( const int                       theViewId,
1062                                                  const Handle(HYDROData_Entity)& theObject ) const
1063 {
1064   HYDROGUI_VTKPrs* aResShape = NULL;
1065   if( theObject.IsNull() )
1066     return aResShape;
1067
1068   if ( myVTKPrsMap.contains( theViewId ) )
1069   {
1070     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1071     foreach ( HYDROGUI_VTKPrs* aShape, aViewShapes )
1072     {
1073       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
1074         continue;
1075
1076       aResShape = aShape;
1077       break;
1078     }
1079   }
1080
1081   return aResShape;
1082 }
1083
1084 void HYDROGUI_Module::setObjectVTKPrs( const int                       theViewId,
1085                                        const Handle(HYDROData_Entity)& theObject,
1086                                        HYDROGUI_VTKPrs*                 theShape )
1087 {
1088   if( theObject.IsNull() )
1089     return;
1090
1091   if( theShape && theShape->needScalarBar() )
1092   {
1093     // Compute the new global Z range from the added presentation and the old global Z range.
1094     double* aGlobalRange = getVTKDisplayer()->GetZRange( theViewId );
1095     double* aRange = theShape->getInternalZRange();
1096     bool anIsUpdate = false;
1097     if ( aRange[0] < aGlobalRange[0] )
1098     {
1099       aGlobalRange[0] = aRange[0];
1100       anIsUpdate = true;
1101     }
1102     if ( aRange[1] > aGlobalRange[1] )
1103     {
1104       aGlobalRange[1] = aRange[1];
1105       anIsUpdate = true;
1106     }
1107
1108     //if ( anIsUpdate )
1109     //{
1110       updateVTKZRange( theViewId, aGlobalRange );
1111     //}
1112   }
1113
1114   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1115   aViewShapes.append( theShape );
1116 }
1117
1118 void HYDROGUI_Module::removeObjectVTKPrs( const int      theViewId,
1119                                           const QString& theEntry )
1120 {
1121   if ( !myVTKPrsMap.contains( theViewId ) )
1122     return;
1123
1124   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1125   Handle(HYDROData_Entity) anObject;
1126   QString anEntryRef;
1127   for ( int i = 0; i < aViewShapes.length(); )
1128   {
1129     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1130     anObject = aShape->getObject();
1131     anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
1132     if ( aShape && (!anObject.IsNull()) && ( anEntryRef == theEntry ) )
1133     {
1134       delete aShape;
1135       aViewShapes.removeAt( i );
1136       continue;
1137     }
1138
1139     ++i;
1140   }
1141
1142   // Invalidate global Z range
1143   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1144   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1145 }
1146
1147 void HYDROGUI_Module::removeObjectVTKPrs( const int                       theViewId,
1148                                           const Handle(HYDROData_Entity)& theObject )
1149 {
1150   if ( !myVTKPrsMap.contains( theViewId ) )
1151     return;
1152
1153   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1154   Handle(HYDROData_Entity) anObject;
1155   for ( int i = 0; i < aViewShapes.length(); )
1156   {
1157     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1158     anObject = aShape->getObject();
1159     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1160     {
1161       delete aShape;
1162       aViewShapes.removeAt( i );
1163       continue;
1164     }
1165
1166     ++i;
1167   }
1168
1169   // Invalidate global Z range
1170   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1171   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1172 }
1173
1174 void HYDROGUI_Module::removeViewVTKPrs( const int theViewId )
1175 {
1176   if ( !myVTKPrsMap.contains( theViewId ) )
1177     return;
1178
1179   const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1180   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1181   {
1182     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1183     if ( aShape )
1184       delete aShape;
1185   }
1186
1187   myVTKPrsMap.remove( theViewId );
1188 }
1189
1190 void HYDROGUI_Module::updateVTKZRange( const int theViewId, double theRange[] )
1191 {
1192   if ( myVTKPrsMap.contains( theViewId ) )
1193   {
1194     // For the given viewer id update all VTK presentations ...
1195     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1196     HYDROGUI_VTKPrs* aShape;
1197     for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1198     {
1199       aShape = aViewShapes.at( i );
1200       if ( aShape && aShape->needScalarBar() )
1201       {
1202         aShape->setZRange( theRange );
1203       }
1204     }
1205   }
1206   // ... and update the global color legend scalar bar.
1207   getVTKDisplayer()->SetZRange( theViewId, theRange );
1208 }
1209 /////////////////// END OF VTKPrs PROCESSING
1210
1211 CAM_DataModel* HYDROGUI_Module::createDataModel()
1212 {
1213   return new HYDROGUI_DataModel( this );
1214 }
1215
1216 void HYDROGUI_Module::customEvent( QEvent* e )
1217 {
1218   int aType = e->type();
1219   if ( aType == NewViewEvent )
1220   {
1221     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
1222     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
1223     {
1224       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
1225       {
1226         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
1227         ViewManagerRole aRole = getViewManagerRole( aViewManager );
1228
1229         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
1230         {
1231           if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1232             aViewPort->scale( 1, -1 ); // invert the Y axis direction from down to up
1233
1234           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
1235           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
1236           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
1237
1238           //ouv: temporarily commented
1239           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
1240         }
1241
1242         if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1243           update( UF_Viewer );
1244
1245         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
1246       }
1247     }
1248   }
1249 }
1250
1251 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
1252 {
1253   QEvent::Type aType = theEvent->type();
1254   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
1255   {
1256     if( aType == QEvent::Show )
1257     {
1258       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
1259       e->setData( theObj );
1260       QApplication::postEvent( this, e );
1261       theObj->removeEventFilter( this );
1262     }
1263   }
1264   else if ( theObj->inherits( "OCCViewer_ViewPort" ) )
1265   {
1266     if( aType == QEvent::Leave )
1267     {
1268       SUIT_Desktop* aDesktop = getApp()->desktop();
1269       if ( aDesktop && aDesktop->statusBar() ) {
1270         aDesktop->statusBar()->clearMessage();
1271       }
1272     }
1273   }
1274   else if ( theObj->inherits( "SVTK_ViewWindow" ) )
1275   {
1276     if( aType == QEvent::Leave )
1277     {
1278       SUIT_Desktop* aDesktop = getApp()->desktop();
1279       if ( aDesktop && aDesktop->statusBar() ) {
1280         aDesktop->statusBar()->clearMessage();
1281       }
1282     }
1283   }
1284
1285   return LightApp_Module::eventFilter( theObj, theEvent );
1286 }
1287
1288 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
1289 {
1290   LightApp_Module::onViewManagerAdded( theViewManager );
1291
1292   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
1293   { 
1294     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1295              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1296   }
1297   else if( theViewManager->getType() == OCCViewer_Viewer::Type() )
1298   {
1299     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1300              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1301     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1302              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1303   }
1304   else if( theViewManager->getType() == SVTK_Viewer::Type() )
1305   {
1306     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1307              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1308     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1309              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1310   }
1311
1312   createSelector( theViewManager ); // replace the default selector
1313
1314   ViewManagerInfo anInfo( theViewManager, VMR_General );
1315   myViewManagerMap.insert( ViewManagerId++, anInfo );
1316 }
1317
1318 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
1319 {
1320   LightApp_Module::onViewManagerRemoved( theViewManager );
1321
1322   createSelector( theViewManager ); // replace the default selector
1323
1324   int anId = getViewManagerId( theViewManager );
1325   if( anId != -1 )
1326   {
1327     OCCViewer_ViewManager* anOCCViewManager =
1328       ::qobject_cast<OCCViewer_ViewManager*>( myViewManagerMap[ anId ].first );
1329     if ( anOCCViewManager )
1330     {
1331       OCCViewer_Viewer* anOCCViewer = anOCCViewManager->getOCCViewer();
1332       if ( anOCCViewer )
1333         removeViewShapes( (size_t)anOCCViewer );
1334     }
1335
1336     if ( getVTKDisplayer()->IsApplicable( theViewManager ) )
1337     {
1338       SVTK_Viewer* aVTKViewer = getVTKViewer( anId );
1339       if ( aVTKViewer )
1340       {
1341         getVTKDisplayer()->EraseScalarBar( anId, true );
1342         removeViewShapes( (size_t)aVTKViewer );
1343       }
1344     }
1345
1346     myViewManagerMap.remove( anId );
1347   }
1348 }
1349
1350 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
1351 {
1352   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
1353   {
1354     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
1355     {
1356       aViewFrame->installEventFilter( this );
1357
1358       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
1359
1360       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
1361                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
1362     }
1363   }
1364   else if( theViewWindow && theViewWindow->inherits( "OCCViewer_ViewFrame" ) )
1365   {
1366     if( OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theViewWindow ) )
1367     {
1368       aViewFrame->onTopView();
1369
1370       HYDROGUI_Tool::setOCCActionShown( aViewFrame, OCCViewer_ViewWindow::MaximizedId, false );
1371
1372       OCCViewer_ViewPort3d* aViewPort = aViewFrame->getViewPort();
1373       if ( aViewPort ) {
1374         aViewPort->installEventFilter( this );
1375       }
1376     }
1377   }
1378   else if( theViewWindow && theViewWindow->inherits( "SVTK_ViewWindow" ) )
1379   {
1380     if( SVTK_ViewWindow* aViewFrame = dynamic_cast<SVTK_ViewWindow*>( theViewWindow ) )
1381     {
1382       aViewFrame->installEventFilter( this );
1383     }
1384   }
1385 }
1386
1387 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
1388 {
1389   /* ouv: currently unused
1390   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
1391   {
1392     SUIT_ViewManager* aViewManager = 0;
1393
1394     QObject* aParent = aViewPort;
1395     while( aParent = aParent->parent() )
1396     {
1397       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
1398       {
1399         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
1400         {
1401           aViewManager = aViewer->getViewManager();
1402           break;
1403         }
1404       }
1405     }
1406
1407     if( !aViewManager )
1408       return;
1409
1410     double aMouseX = theEvent->scenePos().x();
1411     double aMouseY = theEvent->scenePos().y();
1412
1413     ViewManagerRole aRole = getViewManagerRole( aViewManager );
1414     if( aRole == VMR_General )
1415     {
1416       int aXDeg = 0, aYDeg = 0;
1417       int aXMin = 0, aYMin = 0;
1418       double aXSec = 0, aYSec = 0;
1419       HYDROData_Lambert93::secToDMS( aMouseX, aXDeg, aXMin, aXSec );
1420       HYDROData_Lambert93::secToDMS( aMouseY, aYDeg, aYMin, aYSec );
1421
1422       QString aDegSymbol( QChar( 0x00B0 ) );
1423       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
1424       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
1425
1426       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
1427     }
1428     else if( aRole == VMR_TransformImage )
1429       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
1430   }
1431   */
1432 }
1433
1434 void HYDROGUI_Module::updateViewer( HYDROGUI_AbstractDisplayer* theDisplayer, 
1435                                     const bool theIsInit, 
1436                                     const bool theIsForced, 
1437                                     const bool theDoFitAll )
1438 {
1439   QList<int> aViewManagerIdList;
1440
1441   // currently, all views are updated
1442   ViewManagerMapIterator anIter( myViewManagerMap );
1443   while( anIter.hasNext() )
1444   { 
1445     SUIT_ViewManager* aViewManager = anIter.next().value().first;
1446
1447     if ( theDisplayer->IsApplicable( aViewManager ) )
1448     {
1449       int anId = anIter.key();
1450       aViewManagerIdList.append( anId );
1451     }
1452   }
1453
1454   QListIterator<int> anIdIter( aViewManagerIdList );
1455   while( anIdIter.hasNext() )
1456   {
1457     theDisplayer->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
1458   }
1459 }
1460
1461 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
1462 {
1463   if( !theViewManager )
1464     return;
1465
1466   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1467   if( !aSelectionMgr )
1468     return;
1469
1470   QString aViewType = theViewManager->getType();
1471   if( aViewType != GraphicsView_Viewer::Type() &&
1472       aViewType != OCCViewer_Viewer::Type())
1473     return;
1474
1475   QList<SUIT_Selector*> aSelectorList;
1476   aSelectionMgr->selectors( aViewType, aSelectorList );
1477
1478   // disable all alien selectors
1479   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
1480   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
1481   {
1482     SUIT_Selector* aSelector = *anIter;
1483     if( aSelector && ( !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) &&
1484                        !dynamic_cast<SVTK_Selector*>( aSelector ) &&
1485                        !dynamic_cast<HYDROGUI_OCCSelector*>( aSelector ) ) )
1486       aSelector->setEnabled( false );
1487   }
1488
1489   if ( aViewType == GraphicsView_Viewer::Type() )
1490   {
1491     GraphicsView_ViewManager* aViewManager =
1492       ::qobject_cast<GraphicsView_ViewManager*>( theViewManager );
1493     if( aViewManager )
1494       new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
1495   }
1496   else if ( aViewType == OCCViewer_Viewer::Type() )
1497   {
1498     OCCViewer_ViewManager* aViewManager =
1499       ::qobject_cast<OCCViewer_ViewManager*>( theViewManager );
1500     if( aViewManager )
1501       new HYDROGUI_OCCSelector( this, aViewManager->getOCCViewer(), aSelectionMgr );
1502   }
1503 }
1504
1505 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
1506 {
1507   bool aPrevState = myIsUpdateEnabled;
1508   myIsUpdateEnabled = theState;
1509   return aPrevState;
1510 }
1511
1512 bool HYDROGUI_Module::isUpdateEnabled() const
1513 {
1514   return myIsUpdateEnabled;
1515 }
1516
1517 QStringList HYDROGUI_Module::storeSelection() const
1518 {
1519   QStringList anEntryList;
1520   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1521   {
1522     SUIT_DataOwnerPtrList aList( true );
1523     aSelectionMgr->selected( aList );
1524
1525     SUIT_DataOwnerPtrList::iterator anIter;
1526     for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
1527     {
1528       const LightApp_DataOwner* anOwner = 
1529         dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
1530       if( anOwner )
1531         anEntryList.append( anOwner->entry() );
1532     }
1533   }
1534   return anEntryList;
1535 }
1536
1537 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
1538 {
1539   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1540   {
1541     SUIT_DataOwnerPtrList aList( true );
1542     for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
1543       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
1544     aSelectionMgr->setSelected( aList );
1545   }
1546 }
1547
1548 void HYDROGUI_Module::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* )
1549 {
1550   double X, Y, Z;
1551   bool doShow = false;
1552   HYDROGUI_Displayer* aDisplayer = getDisplayer();
1553   if ( aDisplayer )
1554     aDisplayer->SaveCursorViewPosition( theViewWindow );
1555     doShow = aDisplayer->GetCursorViewCoordinates( theViewWindow, X, Y, Z );
1556
1557   if ( doShow )
1558   {
1559     // Show the coordinates in the status bar
1560     SUIT_Desktop* aDesktop = getApp()->desktop();
1561     if ( aDesktop && aDesktop->statusBar() )
1562     {
1563       gp_Pnt aWPnt( X, Y, Z );
1564       int aStudyId = application()->activeStudy()->id();
1565       HYDROData_Document::Document( aStudyId )->Transform( aWPnt, false );
1566       double WX = aWPnt.X(), WY = aWPnt.Y();
1567
1568       QString aXStr = HYDROGUI_Tool::GetCoordinateString( X, true );
1569       QString anYStr = HYDROGUI_Tool::GetCoordinateString( Y, true );
1570       QString aWXStr = HYDROGUI_Tool::GetCoordinateString( WX, true );
1571       QString aWYStr = HYDROGUI_Tool::GetCoordinateString( WY, true );
1572       QString aMsg = tr( "COORDINATES_INFO" );
1573       aMsg = aMsg.arg( aXStr ).arg( anYStr ).arg( aWXStr ).arg( aWYStr );
1574       aDesktop->statusBar()->showMessage( aMsg );
1575     }
1576   }
1577 }
1578
1579 /**
1580  * Returns stack of active operations;
1581  */
1582 QStack<HYDROGUI_Operation*>& HYDROGUI_Module::getActiveOperations()
1583 {
1584   return myActiveOperationMap;
1585 }
1586
1587 /**
1588  * Returns the module active operation. If the active operation is show/hide,
1589  * the method returns the previous operation if it is.
1590  */
1591 HYDROGUI_Operation* HYDROGUI_Module::activeOperation()
1592 {
1593   HYDROGUI_Operation* anOp = !myActiveOperationMap.empty() ? myActiveOperationMap.top() : 0;
1594
1595   if ( dynamic_cast<HYDROGUI_ShowHideOp*>( anOp ) )
1596   {
1597     QVectorIterator<HYDROGUI_Operation*> aVIt( myActiveOperationMap );
1598     aVIt.toBack();
1599     aVIt.previous(); // skip the top show/hide operation
1600     anOp = aVIt.hasPrevious() ? aVIt.previous() : 0;
1601   }
1602
1603   return anOp;
1604 }