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