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