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