Salome HOME
test linear interpolation of a stream
[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 <Python.h>
20
21 #include "HYDROGUI_Module.h"
22
23 #include "HYDROGUI.h"
24 #include "HYDROGUI_DataModel.h"
25 #include "HYDROGUI_DataObject.h"
26 #include "HYDROGUI_Displayer.h"
27 #include "HYDROGUI_GVSelector.h"
28 #include "HYDROGUI_InputPanel.h"
29 #include "HYDROGUI_ObjSelector.h"
30 #include "HYDROGUI_OCCDisplayer.h"
31 #include "HYDROGUI_OCCSelector.h"
32 #include "HYDROGUI_Operations.h"
33 #include "HYDROGUI_PrsImage.h"
34 #include "HYDROGUI_Tool.h"
35 #include "HYDROGUI_Tool2.h"
36 #include "HYDROGUI_UpdateFlags.h"
37 #include "HYDROGUI_Shape.h"
38 #include "HYDROGUI_VTKPrs.h"
39 #include "HYDROGUI_VTKPrsDisplayer.h"
40 #include "HYDROGUI_AbstractDisplayer.h"
41 #include "HYDROGUI_PolylineOp.h"
42 #include "HYDROGUI_SetColorOp.h"
43 #include "HYDROGUI_ImportGeomObjectOp.h"
44 #include "HYDROGUI_ShowHideOp.h"
45 #include "HYDROGUI_Overview.h"
46 #include <HYDROGUI_ProfileDlg.h>
47 #include <HYDROGUI_SIProgressIndicator.h>
48 #include <HYDROGUI_ZIProgressIndicator.h>
49 #include <HYDROData_Tool.h>
50 #include <HYDROData_Image.h>
51 #include <HYDROData_Stream.h>
52 #include <HYDROData_Profile.h>
53 #include <HYDROData_Lambert93.h>
54 #include <HYDROData_Polyline3D.h>
55 #include <HYDROData_StricklerTable.h>
56 #include <HYDROData_ArtificialObject.h>
57 #include <HYDROData_NaturalObject.h>
58
59 #include <HYDROData_OperationsFactory.h>
60
61 #include <CurveCreator_Utils.hxx>
62
63 #include <GraphicsView_ViewFrame.h>
64 #include <GraphicsView_ViewManager.h>
65 #include <GraphicsView_ViewPort.h>
66 #include <GraphicsView_Viewer.h>
67
68 #include <ImageComposer_CutOperator.h>
69 #include <ImageComposer_CropOperator.h>
70 #include <ImageComposer_FuseOperator.h>
71
72 #include <LightApp_Application.h>
73 #include <LightApp_DataOwner.h>
74 #include <LightApp_GVSelector.h>
75 #include <LightApp_SelectionMgr.h>
76 #include <LightApp_UpdateFlags.h>
77
78 #include <SalomeApp_Study.h>
79
80 #include <OCCViewer_ViewFrame.h>
81 #include <OCCViewer_ViewManager.h>
82 #include <OCCViewer_ViewModel.h>
83
84 #include <SALOME_Event.h>
85
86 #include <SUIT_DataBrowser.h>
87 #include <SUIT_DataObject.h>
88 #include <SUIT_ViewManager.h>
89 #include <SUIT_ResourceMgr.h>
90 #include <SUIT_Desktop.h>
91 #include <SUIT_Study.h>
92 #include <SUIT_Session.h>
93
94 #include <SVTK_ViewManager.h>
95 #include <SVTK_ViewModel.h>
96 #include <SVTK_ViewWindow.h>
97 #include <SVTK_Selector.h>
98
99 #include <OCCViewer_ViewPort3d.h>
100
101 #include <GEOMUtils.hxx>
102 #include <GeometryGUI.h>
103
104 #include <SALOMEDS_wrap.hxx>
105
106 #include <QAction>
107 #include <QApplication>
108 #include <QGraphicsSceneMouseEvent>
109 #include <QMenu>
110 #include <QMouseEvent>
111 #include <QStatusBar>
112 #include <QCursor>
113
114 #define _DEVDEBUG_
115 #include "HYDRO_trace.hxx"
116
117 static int ViewManagerId = 0;
118
119 extern "C" HYDRO_EXPORT CAM_Module* createModule()
120 {
121   return new HYDROGUI_Module();
122 }
123
124 extern "C" HYDRO_EXPORT char* getModuleVersion()
125 {
126   return (char*)HYDRO_VERSION;
127 }
128
129 HYDROGUI_Module::HYDROGUI_Module()
130 : LightApp_Module( "HYDRO" ),
131   myDisplayer( 0 ),
132   myOCCDisplayer( 0 ),
133   myIsUpdateEnabled( true ),
134   myOverview( 0 )
135 {
136   DEBTRACE("HYDROGUI_Module::HYDROGUI_Module()");
137 }
138
139 HYDROGUI_Module::~HYDROGUI_Module()
140 {
141   DEBTRACE("HYDROGUI_Module::~HYDROGUI_Module()");
142 }
143
144 int HYDROGUI_Module::getStudyId() const
145 {
146   DEBTRACE("HYDROGUI_Module::getStudyId()");
147   LightApp_Application* anApp = getApp();
148   return anApp ? anApp->activeStudy()->id() : 0;
149 }
150
151 void HYDROGUI_Module::initialize( CAM_Application* theApp )
152 {
153   DEBTRACE("HYDROGUI_Module::initialize");
154   LightApp_Module::initialize( theApp );
155
156   createActions();
157   createUndoRedoActions();
158   createMenus();
159   createPopups();
160   createToolbars();
161
162   setMenuShown( false );
163   setToolShown( false );
164
165   myDisplayer = new HYDROGUI_Displayer( this );
166   myOCCDisplayer = new HYDROGUI_OCCDisplayer( this );
167   myVTKDisplayer = new HYDROGUI_VTKPrsDisplayer( this );
168
169   HYDROData_Tool::SetSIProgress( new HYDROGUI_SIProgressIndicator( theApp->desktop() ) );
170   HYDROData_Tool::SetZIProgress( new HYDROGUI_ZIProgressIndicator( theApp->desktop() ) );
171 }
172
173 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
174 {
175   DEBTRACE("HYDROGUI_Module::activateModule");
176   bool aRes = LightApp_Module::activateModule( theStudy );
177
178   LightApp_Application* anApp = getApp();
179   SUIT_Desktop* aDesktop = anApp->desktop();
180
181   getApp()->setEditEnabled( false ); // hide SalomeApp copy/paste actions
182
183   setMenuShown( true );
184   setToolShown( true );
185
186   // import Python module that manages HYDRO plugins (need to be here because SalomePyQt API uses active module)
187   PyGILState_STATE gstate = PyGILState_Ensure();
188   PyObject* pluginsmanager = PyImport_ImportModuleNoBlock((char*)"salome_pluginsmanager");
189   if ( !pluginsmanager ) {
190     PyErr_Print();
191   }
192   else {
193     PyObject* result =
194       PyObject_CallMethod(pluginsmanager, (char*)"initialize", (char*)"isss", 1, "hydro",
195                           tr("MEN_DESK_HYDRO").toUtf8().data(),
196                           tr("Python plugins").toUtf8().data());
197     if ( !result )
198       PyErr_Print();
199     Py_XDECREF(result);
200   }
201   PyGILState_Release(gstate);
202   // end of GEOM plugins loading
203
204 #ifndef DISABLE_PYCONSOLE
205   aDesktop->tabifyDockWidget( HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_PyConsole ) ), 
206                               HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_LogWindow ) ) );
207 #endif
208
209   // Remove defunct view managers from the map.
210   // It's essential to do this before "update( UF_All )" call!
211   QList<size_t> anObsoleteIds;
212   ViewManagerList anAllViewManagers = anApp->viewManagers();
213   ViewManagerList aHydroViewManagers; // view managers created inside the HYDRO module
214   ViewManagerMapIterator anIter( myViewManagerMap );
215   while( anIter.hasNext() ) {
216     size_t anId = anIter.next().key();
217     const ViewManagerInfo& anInfo = anIter.value();
218   
219     aHydroViewManagers << anInfo.first;
220
221     if ( !anAllViewManagers.contains( anInfo.first ) ) {
222       anObsoleteIds << anId;
223     }
224   }
225   foreach ( const size_t anId, anObsoleteIds ) {
226     myViewManagerMap.remove( anId );
227     myObjectStateMap.remove( anId );
228     myShapesMap.remove( anId );
229     myVTKPrsMap.remove( anId );
230   }
231   // Replace the default selector for all view managers.
232   // Add view managers created outside of HYDRO module to the map.
233   foreach ( SUIT_ViewManager* aViewManager, anAllViewManagers ) {
234     createSelector( aViewManager ); // replace the default selector
235     if ( !aHydroViewManagers.contains( aViewManager ) ) {
236       ViewManagerInfo anInfo( aViewManager, VMR_General );
237       myViewManagerMap.insert( ViewManagerId++, anInfo );
238     }
239   }
240
241   update( UF_All );
242
243   updateCommandsStatus();
244
245   HYDROGUI_Tool::setOCCActionShown( this, OCCViewer_ViewWindow::MaximizedId, false );
246
247   ViewManagerList anOCCViewManagers;
248   anApp->viewManagers( OCCViewer_Viewer::Type(), anOCCViewManagers );
249
250   foreach ( SUIT_ViewManager* aViewManager, anOCCViewManagers )
251   {
252     connect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
253              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
254     connect( aViewManager, SIGNAL( activated( SUIT_ViewManager* ) ), 
255              this, SLOT( onViewActivated( SUIT_ViewManager* ) ) );
256
257     OCCViewer_ViewManager* occ_mgr = dynamic_cast<OCCViewer_ViewManager*>( aViewManager );
258     if( occ_mgr )
259     {
260       occ_mgr->setChainedOperations( true );//TODO: via preferences
261     }
262
263     foreach( SUIT_ViewWindow* aViewWindow, aViewManager->getViews() )
264     {
265       OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aViewWindow );
266       if ( aViewFrame && aViewFrame->getViewPort() )
267       {
268         aViewFrame->getViewPort()->installEventFilter( this );
269       }
270     }
271   }
272
273   preferencesChanged( "HYDRO", "zoom_shutoff" );
274
275   preferencesChanged( "HYDRO", "chained_panning" );
276
277   // Load GEOM data
278   SalomeApp_Study* aStudy = 
279     dynamic_cast<SalomeApp_Study*>( getApp()->activeStudy() );
280   if ( aStudy ) {
281     SALOMEDS::Study_var aDSStudy = GeometryGUI::getStudyServant();
282     GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
283     if ( !aGeomEngine->_is_nil() && !aGeomEngine->_is_nil() ) {
284       SALOMEDS::StudyBuilder_var aStudyBuilder = aDSStudy->NewBuilder();
285       SALOMEDS::SComponent_wrap GEOM_var = aDSStudy->FindComponent( "GEOM" );
286       if( !GEOM_var->_is_nil() ) {
287         aStudyBuilder->LoadWith( GEOM_var, aGeomEngine );
288       }
289     }
290   }
291
292
293 //  SUIT_DataBrowser* ob = getApp()->objectBrowser();
294 //  SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( ob->model() );
295 //  treeModel->setAppropriate( SUIT_DataObject::VisibilityId, Qtx::Toggled );
296
297   return aRes;
298 }
299
300 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
301 {
302   DEBTRACE("HYDROGUI_Module::deactivateModule");
303   /* Issues ## 68, 88.
304   ViewManagerMapIterator anIter( myViewManagerMap );
305   while( anIter.hasNext() )
306     if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
307       getApp()->removeViewManager( aViewManager );
308   myViewManagerMap.clear();
309   */
310
311   ViewManagerList anOCCViewManagers;
312   getApp()->viewManagers( OCCViewer_Viewer::Type(), anOCCViewManagers );
313   foreach ( SUIT_ViewManager* aViewManager, anOCCViewManagers )
314   {
315     disconnect( aViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
316                 this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
317     disconnect( aViewManager, SIGNAL( activated( SUIT_ViewManager* ) ), 
318                 this, SLOT( onViewActivated( SUIT_ViewManager* ) ) );
319
320     OCCViewer_ViewManager* occ_mgr = dynamic_cast<OCCViewer_ViewManager*>( aViewManager );
321     if( occ_mgr )
322     {
323       occ_mgr->setChainedOperations( false );
324       setAutoZoom( occ_mgr, true );
325     }
326   }
327
328   /* Issues ## 68, 88.
329   myObjectStateMap.clear();
330   myShapesMap.clear();
331   myVTKPrsMap.clear();
332   */
333
334   // clear the status bar
335   SUIT_Desktop* aDesktop = getApp()->desktop();
336   if ( aDesktop && aDesktop->statusBar() ) {
337     aDesktop->statusBar()->clearMessage();
338   }
339
340   // clear the data model's list of copying objects
341   HYDROGUI_DataModel::changeCopyingObjects( HYDROData_SequenceOfObjects() );
342
343   setMenuShown( false );
344   setToolShown( false );
345
346   getApp()->setEditEnabled( true ); // show SalomeApp copy/paste actions
347
348   HYDROGUI_Tool::setOCCActionShown( this, OCCViewer_ViewWindow::MaximizedId, true );
349
350   myActiveOperationMap.clear();
351
352   myViewManagerMap.clear();
353
354   bool ret = LightApp_Module::deactivateModule( theStudy );
355   return ret;
356 }
357
358 void HYDROGUI_Module::studyClosed(SUIT_Study* theStudy)
359 {
360     DEBTRACE("HYDROGUI_Module::studyClosed");
361     Handle(HYDROData_Document) aDoc = HYDROData_Document::Document();
362     if (!aDoc.IsNull())
363       aDoc->Close();
364 }
365
366 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
367 {
368   DEBTRACE("HYDROGUI_Module::windows");
369   static bool inWindows = false;
370   if( inWindows )
371     return;
372
373   theMap.clear();
374   theMap.insert( LightApp_Application::WT_LogWindow,     Qt::BottomDockWidgetArea );
375 #ifndef DISABLE_PYCONSOLE
376   theMap.insert( LightApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
377 #endif
378   theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea   );
379   theMap.insert( OverviewWindow, Qt::LeftDockWidgetArea );
380
381   inWindows = true;
382   LightApp_Application* app = getApp();
383   if( app && app->getWindow( OverviewWindow )==0 )
384   {
385     const_cast<HYDROGUI_Module*>( this )->myOverview = 
386       new HYDROGUI_Overview( tr( "OVERVIEW" ), 0, app->desktop() );
387     app->insertDockWindow( OverviewWindow, myOverview );
388     app->placeDockWindow( OverviewWindow, Qt::LeftDockWidgetArea );
389   }
390   inWindows = false;
391 }
392
393 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
394 {
395   DEBTRACE("HYDROGUI_Module::viewManagers");
396   theTypesList << GraphicsView_Viewer::Type() << OCCViewer_Viewer::Type();
397 }
398
399 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
400                                         QMenu* theMenu,
401                                         QString& theTitle )
402 {
403   HYDROGUI_DataModel* aModel = getDataModel();
404
405   bool anIsObjectBrowser = theClient == getApp()->objectBrowser()->popupClientType();
406   bool anIsGraphicsView = theClient == GraphicsView_Viewer::Type();
407   bool anIsOCCView = theClient == OCCViewer_Viewer::Type();
408   bool anIsVTKView = theClient == SVTK_Viewer::Type();
409   if( !anIsObjectBrowser && !anIsGraphicsView && !anIsOCCView && !anIsVTKView )
410     return;
411
412   size_t anActiveViewId = HYDROGUI_Tool::GetActiveViewId( this );
413
414   bool anIsSelectedDataObjects = false;
415   bool anIsVisibleInSelection = false;
416   bool anIsHiddenInSelection = false;
417
418   bool anIsImage = false;
419   bool anIsImportedImage = false;
420   bool anIsImageHasRefs = false;
421   bool anIsFusedImage = false;
422   bool anIsCutImage = false;
423   bool anIsSplitImage = false;
424   bool anIsMustObjectBeUpdated = false;
425   bool anIsPolyline = false;
426   bool anIsPolyline3D = false;
427   bool anIsProfile = false;
428   bool anIsValidProfile = false;
429   bool anAllAreProfiles = false;
430   bool anIsBathymetry = false;
431   bool anIsCalculation = false;
432   bool anIsImmersibleZone = false;
433   bool anIsVisualState = false;
434   bool anIsRegion = false;
435   bool anIsZone = false;
436   bool anIsObstacle = false;
437   bool anIsStricklerTable = false;
438   bool anIsLandCoverMap = false;
439   bool anIsStream = false;
440   bool anIsChannel = false;
441   bool anIsDigue = false;
442   bool anIsDummyObject3D = false;
443   bool anIsGroup = false;
444   bool anIsObjectCanBeColored = false;
445   bool isRoot = false;
446   bool isStreamHasBottom = false;
447   bool anIsBCPolygon = false;
448
449   SUIT_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
450   SUIT_DataOwnerPtrList anOwners;
451   aSelectionMgr->selected( anOwners );
452   if( anIsObjectBrowser && anOwners.size()==1 )
453   {
454     QString anEntry = anOwners[0]->keyString();
455     LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( getApp()->activeStudy() );
456     if( aStudy )
457       isRoot = aStudy->isComponent( anEntry );
458   }
459
460   // Check the selected GEOM objects (take into account the Object Browser only)
461   if ( anIsObjectBrowser ) {
462     QList<GEOM::shape_type> anObstacleTypes = 
463       HYDROGUI_ImportGeomObjectOp::getObstacleTypes();
464     QList<GEOM::shape_type> aPolylineTypes = 
465       HYDROGUI_ImportGeomObjectOp::getPolylineTypes();
466
467     bool isCanBeImportedAsObstacle = 
468       !HYDROGUI_Tool::GetSelectedGeomObjects( this, anObstacleTypes ).isEmpty();
469     bool isCanBeImportedAsPolyline = 
470       !HYDROGUI_Tool::GetSelectedGeomObjects( this, aPolylineTypes ).isEmpty();
471
472     // Add import as obstacle action
473     if ( isCanBeImportedAsObstacle ) {
474       theMenu->addAction( action( ImportGeomObjectAsObstacleId ) );
475     }
476     // Add import as polyline action
477     if ( isCanBeImportedAsPolyline ) {
478       theMenu->addAction( action( ImportGeomObjectAsPolylineId ) );
479     }
480     // Add separator
481     if ( isCanBeImportedAsObstacle || isCanBeImportedAsPolyline ) {
482       theMenu->addSeparator();
483     }
484   }
485
486   // check the selected data model objects
487   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
488   int aNbOfSelectedProfiles = 0;
489   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
490   {
491     Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
492     if( !anObject.IsNull() )
493     {
494       anIsSelectedDataObjects = true;
495
496       bool aVisibility = isObjectVisible( anActiveViewId, anObject );
497       anIsVisibleInSelection |= aVisibility;
498       anIsHiddenInSelection |= !aVisibility;
499
500       if ( anObject->CanBeUpdated() && anObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) )
501       {
502         anIsMustObjectBeUpdated = true;
503       }
504
505       ObjectKind anObjectKind = anObject->GetKind();
506       if( anObjectKind == KIND_IMAGE )
507       {
508         anIsImage = true;
509         Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
510         if( !anImage.IsNull() )
511         {
512           anIsImportedImage = anImage->HasLocalPoints();
513           anIsImageHasRefs = anImage->HasReferences();
514           if( HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory() )
515           {
516             if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
517             {
518               QString anOperatorName = anOperator->name();
519               if( anOperatorName == ImageComposer_FuseOperator::Type() )
520                 anIsFusedImage = true;
521               else if( anOperatorName == ImageComposer_CutOperator::Type() )
522                 anIsCutImage = true;
523               else if( anOperatorName == ImageComposer_CropOperator::Type() )
524                 anIsSplitImage = true;
525             }
526           }
527         }
528       }
529       else if( anObjectKind == KIND_POLYLINEXY )
530         anIsPolyline = true;
531       else if( anObjectKind == KIND_POLYLINE )
532         anIsPolyline3D = true;
533       else if( anObjectKind == KIND_PROFILE )
534       {
535         anIsProfile = true;
536         aNbOfSelectedProfiles++;
537
538         Handle(HYDROData_Profile) aProfile = 
539           Handle(HYDROData_Profile)::DownCast( anObject );
540         if( !aProfile.IsNull() && aProfile->IsValid() ) {
541           anIsValidProfile = true;
542         }
543       }
544       else if( anObjectKind == KIND_CALCULATION )
545         anIsCalculation = true;
546       else if( anObjectKind == KIND_IMMERSIBLE_ZONE )
547         anIsImmersibleZone = true;
548       else if( anObjectKind == KIND_VISUAL_STATE )
549         anIsVisualState = true;
550       else if( anObjectKind == KIND_REGION )
551         anIsRegion = true;
552       else if( anObjectKind == KIND_ZONE )
553         anIsZone = true;
554       else if( anObjectKind == KIND_BATHYMETRY )
555         anIsBathymetry = true;
556       else if( anObjectKind == KIND_OBSTACLE )
557         anIsObstacle = true;
558       else if( anObjectKind == KIND_STRICKLER_TABLE )
559         anIsStricklerTable = true;
560       else if( anObjectKind == KIND_LAND_COVER_MAP )
561         anIsLandCoverMap = true;
562       else if (anObjectKind == KIND_BC_POLYGON)
563         anIsBCPolygon = true;
564       else if( anObjectKind == KIND_STREAM )
565       {
566         anIsStream = true;
567         Handle(HYDROData_Stream) aStream = 
568           Handle(HYDROData_Stream)::DownCast( anObject );
569         if ( !aStream.IsNull() )
570           isStreamHasBottom = !aStream->GetBottomPolyline().IsNull();
571       }
572       else if( anObjectKind == KIND_CHANNEL )
573         anIsChannel = true;
574       else if( anObjectKind == KIND_DIGUE )
575         anIsDigue = true;
576       else if( anObjectKind == KIND_DUMMY_3D )
577         anIsDummyObject3D = true;
578       else if( anObjectKind == KIND_SHAPES_GROUP || anObjectKind == KIND_SPLIT_GROUP )
579         anIsGroup = true;
580     }
581
582     if ( !anIsObjectCanBeColored )
583       anIsObjectCanBeColored = HYDROGUI_SetColorOp::CanObjectBeColored( anObject, this );
584   }
585
586   // Check if all selected objects are profiles
587   anAllAreProfiles = ( aNbOfSelectedProfiles > 0 ) &&
588     ( aNbOfSelectedProfiles == aSeq.Length() );
589
590   // check the selected partitions
591   if( !anIsSelectedDataObjects && anIsObjectBrowser )
592   {
593     ObjectKind aSelectedPartition = HYDROGUI_Tool::GetSelectedPartition( this );
594     if( aSelectedPartition != KIND_UNKNOWN )
595     {
596       switch( aSelectedPartition )
597       {
598       case KIND_IMAGE:
599         theMenu->addAction( action( ImportImageId ) );
600         break;
601       case KIND_BATHYMETRY:
602         theMenu->addAction( action( ImportBathymetryId ) );
603         break;
604       case KIND_ARTIFICIAL_OBJECT:
605         theMenu->addAction( action( CreateChannelId ) );
606         theMenu->addAction( action( CreateDigueId ) );
607         break;
608       case KIND_NATURAL_OBJECT:
609         theMenu->addAction( action( CreateImmersibleZoneId ) );
610         theMenu->addAction( action( CreateStreamId ) );
611         break;
612       case KIND_OBSTACLE:
613         theMenu->addAction( action( ImportObstacleFromFileId ) );
614         theMenu->addAction( action( CreateBoxId ) );
615         theMenu->addAction( action( CreateCylinderId ) );
616         break;
617       case KIND_STRICKLER_TABLE:
618         theMenu->addAction( action( ImportStricklerTableFromFileId ) );          
619         break;
620       case KIND_LAND_COVER_MAP:
621         theMenu->addAction( action( CreateLandCoverMapId ) ); 
622         theMenu->addAction( action( ImportLandCoverMapId ) ); 
623         break;
624       case KIND_CALCULATION:
625         theMenu->addAction( action( CreateCalculationId ) );
626         break;
627       case KIND_POLYLINEXY:
628         theMenu->addAction( action( ImportPolylineId ) );
629         theMenu->addAction( action( CreatePolylineId ) );
630         break;
631       case KIND_POLYLINE:
632         theMenu->addAction( action( CreatePolyline3DId ) );
633         theMenu->addAction( action( ImportPolylineId ) );
634         break;
635       case KIND_PROFILE:
636         theMenu->addAction( action( CreateProfileId ) );
637         theMenu->addAction( action( ImportProfilesId ) );
638         theMenu->addAction( action( AllGeoreferencementId ) );
639         break;
640       case KIND_VISUAL_STATE:
641         theMenu->addAction( action( SaveVisualStateId ) );
642         break;
643       case KIND_BC_POLYGON:
644         theMenu->addAction( action( ImportBCPolygonId ) );
645         break;
646       }
647       theMenu->addSeparator();
648     }
649     else
650     {
651       Handle(HYDROData_CalculationCase) aCalcCase;
652       QString outStr;
653       HYDROGUI_Tool::IsSelectedPartOfCalcCase(this, aCalcCase, outStr);
654       if (outStr == HYDROGUI_DataModel::partitionName( KIND_REGION ))
655         theMenu->addAction( action( RegenerateRegionColorsId ) );
656     }
657   }
658
659   if( anIsSelectedDataObjects )
660   {
661     if ( anIsMustObjectBeUpdated )
662     {
663       theMenu->addAction( action( UpdateObjectId ) );
664       theMenu->addSeparator();
665     }
666     else
667     {
668       theMenu->addAction( action( ForcedUpdateObjectId ) );
669       theMenu->addSeparator();
670     }
671
672
673     if( aSeq.Length() == 1 )
674     {
675       if( anIsImage )
676       {
677         if( anIsImportedImage )
678           theMenu->addAction( action( EditImportedImageId ) );
679         else if( anIsImageHasRefs )
680         {
681           if( anIsFusedImage )
682             theMenu->addAction( action( EditFusedImageId ) );
683           else if( anIsCutImage )
684             theMenu->addAction( action( EditCutImageId ) );
685           else if( anIsSplitImage )
686             theMenu->addAction( action( EditSplitImageId ) );
687         }
688
689         //RKV: BUG#98: theMenu->addAction( action( ObserveImageId ) );
690         theMenu->addAction( action( ExportImageId ) );
691         theMenu->addSeparator();
692
693         if( anIsImageHasRefs )
694         {
695           theMenu->addAction( action( RemoveImageRefsId ) );
696           theMenu->addSeparator();
697         }
698
699         theMenu->addAction( action( FuseImagesId ) );
700         theMenu->addAction( action( CutImagesId ) );
701         theMenu->addAction( action( SplitImageId ) );
702         theMenu->addSeparator();
703         theMenu->addAction( action( RecognizeContoursId ) );
704         theMenu->addSeparator();
705       }
706       else if( anIsBathymetry )
707       {
708         theMenu->addAction( action( EditImportedBathymetryId ) );
709         theMenu->addAction( action( BathymetryBoundsId ) );
710         theMenu->addSeparator();
711       }
712       else if( anIsPolyline )
713       {
714         theMenu->addAction( action( EditPolylineId ) );
715         theMenu->addSeparator();
716         theMenu->addAction( action( SplitPolylinesId ) );
717         theMenu->addAction( action( MergePolylinesId ) );
718         theMenu->addSeparator();
719         theMenu->addAction( action( ShowAttrPolylinesId ) );
720         theMenu->addSeparator();
721       }
722       else if( anIsPolyline3D )
723       {
724         theMenu->addAction( action( EditPolyline3DId ) );
725         theMenu->addSeparator();
726       }
727       else if( anIsProfile )
728       {
729         theMenu->addAction( action( EditProfileId ) );
730         theMenu->addAction( action( SelectedGeoreferencementId ) );
731         theMenu->addSeparator();
732       }
733       else if( anIsCalculation )
734       {
735         theMenu->addAction( action( EditCalculationId ) );
736         theMenu->addAction( action( ExportCalculationId ) );
737         theMenu->addAction( action( CompleteCalculationId ) );
738         theMenu->addSeparator();
739       }
740       else if( anIsImmersibleZone )
741       {
742         theMenu->addAction( action( EditImmersibleZoneId ) );
743         theMenu->addSeparator();
744       }
745       else if( anIsStream )
746       {
747         theMenu->addAction( action( EditStreamId ) );
748         if ( action( RiverBottomContextId ) )
749         {
750           theMenu->addAction( action( RiverBottomContextId ) );
751           action( RiverBottomContextId )->setEnabled( !isStreamHasBottom );
752         }
753         theMenu->addAction( action( ProfileInterpolateId ) );
754         theMenu->addSeparator();
755       }
756       else if( anIsChannel )
757       {
758         theMenu->addAction( action( EditChannelId ) );
759         theMenu->addSeparator();
760       }
761       else if( anIsDigue )
762       {
763         theMenu->addAction( action( EditDigueId ) );
764         theMenu->addSeparator();
765       }
766       else if( anIsObstacle )
767       {
768         theMenu->addAction( action( TranslateObstacleId ) );
769         theMenu->addSeparator();
770       }
771       else if( anIsStricklerTable )
772       {
773         theMenu->addAction( action( EditStricklerTableId ) );
774         theMenu->addAction( action( ExportStricklerTableFromFileId ) );
775         theMenu->addAction( action( DuplicateStricklerTableId ) );
776         theMenu->addSeparator();
777
778         Handle(HYDROData_StricklerTable) aTable = 
779           Handle(HYDROData_StricklerTable)::DownCast( aSeq.First() );
780         QString aCurrentTable = 
781           HYDROGUI_DataObject::dataObjectEntry( getLandCoverColoringTable( anActiveViewId ) );
782         bool isUsed = aCurrentTable == HYDROGUI_DataObject::dataObjectEntry( aTable );
783
784         if ( !isUsed && !getObjectShapes( anActiveViewId, KIND_LAND_COVER_MAP ).isEmpty() ) {
785           theMenu->addAction( action( LandCoverScalarMapModeOnId ) );
786           theMenu->addSeparator();
787         }
788       }
789       else if( anIsLandCoverMap )
790       {
791         theMenu->addAction( action( AddLandCoverId ) );
792         theMenu->addAction( action( RemoveLandCoverId ) );
793         theMenu->addSeparator();
794         theMenu->addAction( action( SplitLandCoverId ) );
795         theMenu->addAction( action( MergeLandCoverId ) );
796         theMenu->addAction( action( ChangeLandCoverTypeId ) );
797         theMenu->addSeparator();
798         //
799         theMenu->addAction( action( ExportToShapeFileID ) );
800         theMenu->addSeparator();
801       }      
802       else if( anIsVisualState && anIsObjectBrowser )
803       {
804         theMenu->addAction( action( SaveVisualStateId ) );
805         theMenu->addAction( action( LoadVisualStateId ) );
806         theMenu->addSeparator();
807       }
808       else if (anIsZone)
809         theMenu->addAction( action( ZoneSetColorId ) );
810       else if (anIsBCPolygon)
811         theMenu->addAction( action( SetBoundaryTypePolygonId ) );
812
813       if ( anIsStream || anIsChannel || anIsDigue || anIsObstacle )
814       {
815         theMenu->addAction( action( PolylineExtractionId ) );
816         theMenu->addSeparator();
817       }
818
819       // Add set color action for geometrical objects
820       if ( anIsObjectCanBeColored )
821       {
822         theMenu->addAction( action( SetColorId ) );
823         theMenu->addSeparator();
824       }
825
826       // Add transparency action for land cover map objects
827       if ( anIsLandCoverMap )
828       {
829         theMenu->addAction( action( SetTransparencyId ) );
830         theMenu->addSeparator();
831       }
832     } 
833     else if ( anAllAreProfiles )
834     {
835       theMenu->addAction( action( EditProfileId ) );
836       theMenu->addAction( action( SelectedGeoreferencementId ) );
837       theMenu->addSeparator();
838     }
839
840     bool isPoly = anIsPolyline || anIsPolyline3D;
841     if (isPoly  && !anIsLandCoverMap)
842       theMenu->addAction( action( ExportToShapeFileID ) );
843
844     // Add copy action
845     QAction* aCopyAction = action( CopyId );
846     if( aCopyAction && aCopyAction->isEnabled() ) {
847       theMenu->addAction( action( CopyId ) );
848       theMenu->addSeparator();
849     }
850
851     // Add delete action
852     if( !anIsDummyObject3D )
853       theMenu->addAction( action( DeleteId ) );
854
855     theMenu->addSeparator();
856
857     if( anIsImage || anIsPolyline || anIsPolyline3D || 
858       anIsImmersibleZone || anIsZone || anIsRegion ||
859       anIsBathymetry || anIsObstacle || anIsStream ||
860       anIsChannel || anIsDigue || anIsDummyObject3D ||
861       anIsValidProfile || anIsGroup || anIsLandCoverMap ||
862       anIsBCPolygon)
863     {
864       if( anIsHiddenInSelection )
865         theMenu->addAction( action( ShowId ) );
866       theMenu->addAction( action( ShowOnlyId ) );
867       if( anIsVisibleInSelection )
868         theMenu->addAction( action( HideId ) );
869       theMenu->addSeparator();
870     }
871   }
872
873   if ( anIsOCCView )
874   {
875     SUIT_Operation* anOp = application()->activeStudy()->activeOperation();
876     HYDROGUI_PolylineOp* aPolylineOp = dynamic_cast<HYDROGUI_PolylineOp*>( anOp );
877     if ( aPolylineOp && aPolylineOp->deleteEnabled() )
878       theMenu->addAction( action( DeleteId ) );
879
880     theMenu->addSeparator();
881     theMenu->addAction( action( SetZLevelId ) );
882     theMenu->addSeparator();
883
884     if ( isLandCoversScalarMapModeOn( anActiveViewId ) ) {
885       theMenu->addAction( action( LandCoverScalarMapModeOffId ) );
886       theMenu->addSeparator();
887     }
888   }
889
890   if( anIsObjectBrowser || anIsGraphicsView || anIsOCCView || anIsVTKView )
891   {
892     theMenu->addAction( action( ShowAllId ) );
893     theMenu->addAction( action( HideAllId ) );
894     theMenu->addSeparator();
895   }
896
897   if ( anIsOCCView || anIsVTKView )
898   {
899     theMenu->addSeparator();
900     theMenu->addAction( action( CopyViewerPositionId ) );
901   }
902
903   if( isRoot )
904     theMenu->addAction( action( EditLocalCSId ) );
905
906   if( anIsObjectBrowser && anOwners.size()==1 )
907   {
908     if( aSeq.Size() > 0 )
909     {
910       Handle( HYDROData_Entity ) aFirstEnt = aSeq.First();
911       Handle(HYDROData_Object) anObject;
912       Handle(HYDROData_ArtificialObject) anAObject = Handle( HYDROData_ArtificialObject )::DownCast(aFirstEnt);
913       Handle(HYDROData_NaturalObject) aNObject = Handle( HYDROData_NaturalObject )::DownCast(aFirstEnt);
914
915       if (!anAObject.IsNull())
916         anObject = anAObject;
917       if (!aNObject.IsNull())
918         anObject = aNObject;
919
920       if( !anObject.IsNull() )
921       {
922         theMenu->addSeparator();
923         bool IsSubmersible = anObject->IsSubmersible();
924         if (!IsSubmersible)
925         {
926           theMenu->addAction( action( SubmersibleId ) );
927           action( SubmersibleId )->setCheckable(true);
928           action( SubmersibleId )->setChecked(true);
929         }
930         else
931         {
932           theMenu->addAction( action( UnSubmersibleId ) );
933           action( UnSubmersibleId )->setCheckable(true);
934           action( UnSubmersibleId )->setChecked(true);
935         }
936       }
937     }
938   }
939
940   theMenu->addSeparator();
941   QAction* a = action( ShowHideArrows );
942   a->setText( arrowsVisible() ? tr( "HIDE_ARROWS" ) : tr( "SHOW_ARROWS" ) );
943   theMenu->addAction( a );
944 }
945
946 void HYDROGUI_Module::createPreferences()
947 {
948   DEBTRACE("HYDROGUI_Module::createPreferences");
949   int genTab = addPreference( tr( "PREF_TAB_GENERAL" ) );
950   int CursorGroup = addPreference( tr( "PREF_GROUP_CURSOR" ), genTab );
951
952   int typeOfCursor = addPreference( tr( "PREF_TYPE_OF_CURSOR" ), CursorGroup,
953                                     LightApp_Preferences::Selector, "preferences", "type_of_cursor" );
954
955   // Set property cursor type
956   QList<QVariant> aCursorTypeIndicesList;
957   QList<QVariant> aCursorTypeIconsList;
958
959   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
960   for ( int i = CT_ArrowCursor; i < CT_User; i++ ) {
961     QString icoFile = QString( "ICON_CURSOR_%1" ).arg( i+1 );
962     QPixmap pixmap = resMgr->loadPixmap( "HYDRO", tr( qPrintable( icoFile ) ) );
963     aCursorTypeIndicesList << i;
964     aCursorTypeIconsList << pixmap;
965   }
966
967   setPreferenceProperty( typeOfCursor, "indexes", aCursorTypeIndicesList );
968   setPreferenceProperty( typeOfCursor, "icons",   aCursorTypeIconsList );
969
970   int viewerGroup = addPreference( tr( "PREF_GROUP_VIEWER" ), genTab );
971   addPreference( tr( "PREF_VIEWER_AUTO_FITALL" ), viewerGroup,
972                  LightApp_Preferences::Bool, "HYDRO", "auto_fit_all" );
973
974   addPreference( tr( "PREF_VIEWER_ZOOM_SHUTOFF" ), viewerGroup, LightApp_Preferences::Bool, "HYDRO", "zoom_shutoff" );
975
976   addPreference( tr( "PREF_VIEWER_CHAINED_PANNING" ), viewerGroup, LightApp_Preferences::Bool, "HYDRO", "chained_panning" );
977
978   int StricklerTableGroup = addPreference( tr( "PREF_GROUP_STRICKLER_TABLE" ), genTab );
979   int defaultStricklerCoef = addPreference( tr( "PREF_DEFAULT_STRICKLER_COEFFICIENT" ), StricklerTableGroup,
980                                             LightApp_Preferences::DblSpin, "preferences", "default_strickler_coefficient" );
981   setPreferenceProperty( defaultStricklerCoef, "precision", 2 );
982   setPreferenceProperty( defaultStricklerCoef, "min", 0.00 );
983   setPreferenceProperty( defaultStricklerCoef, "max", 1000000.00 );
984   setPreferenceProperty( defaultStricklerCoef, "step", 0.01 );
985
986   int polylinesGroup = addPreference( tr( "PREF_GROUP_POLYLINES" ), genTab );
987   int polylineArrow = addPreference( tr( "PREF_POLYLINE_ARROW" ), polylinesGroup,
988     LightApp_Preferences::Selector, "polyline", "arrow_type" );
989
990   QList<QVariant> arrow_types;
991   arrow_types.append( tr( "No" ) );
992   arrow_types.append( tr( "Triangle" ) );
993   arrow_types.append( tr( "Cone" ) );
994   setPreferenceProperty( polylineArrow, "strings", arrow_types );
995
996   QList<QVariant> indices;
997   indices.append( 0 );
998   indices.append( 1 );
999   indices.append( 2 );
1000   setPreferenceProperty( polylineArrow, "indexes", indices );
1001   setPreferenceProperty( polylineArrow, "ids", indices );
1002
1003   int polylineSize = addPreference( tr( "PREF_POLYLINE_ARROW_SIZE" ), polylinesGroup,
1004     LightApp_Preferences::IntSpin, "polyline", "arrow_size" );
1005 }
1006
1007 void HYDROGUI_Module::preferencesChanged( const QString& theSection, const QString& thePref )
1008 {
1009     DEBTRACE("HYDROGUI_Module::preferencesChanged");
1010     SUIT_ResourceMgr* resMgr = application()->resourceMgr();
1011     if ( theSection == "preferences" && thePref == "default_strickler_coefficient" )
1012     {
1013         
1014         Handle(HYDROData_Document) aDoc = HYDROData_Document::Document();
1015         if ( resMgr && !aDoc.IsNull() )
1016             aDoc->SetDefaultStricklerCoefficient( resMgr->doubleValue( theSection, thePref, 0 ) );
1017     }
1018     else if( theSection == "polyline" )
1019     {
1020       int aType = -1;
1021       int aSize = -1;
1022       if( resMgr )
1023       {
1024         resMgr->value( "polyline", "arrow_type", aType );
1025         resMgr->value( "polyline", "arrow_size", aSize );
1026       }
1027       //Update polylines
1028       ViewManagerMap::const_iterator it = myViewManagerMap.begin(), last = myViewManagerMap.end();
1029       for( ; it!=last; it++ )
1030       {
1031         int aViewerId = it.key();
1032         OCCViewer_ViewManager* aMgr = dynamic_cast<OCCViewer_ViewManager*>( it.value().first );
1033         if( aMgr )
1034           getOCCDisplayer()->UpdatePolylines( aViewerId, aType, aSize );
1035       }
1036     }
1037     else if (theSection == "HYDRO" && thePref == "zoom_shutoff")
1038     {
1039       bool aZoomShutoff = resMgr->booleanValue( "HYDRO", "zoom_shutoff" );
1040       setAutoZoomToAllViewManagers(!aZoomShutoff);
1041     }
1042     else if (theSection == "HYDRO" && thePref == "chained_panning")
1043     {
1044       bool aChainedPan = resMgr->booleanValue( "HYDRO", "chained_panning" );
1045       if (!aChainedPan)
1046         resetViewState();
1047       ViewManagerList aViewManagers = getApp()->viewManagers();
1048       foreach (SUIT_ViewManager* aVMgr, aViewManagers)
1049       {
1050         OCCViewer_ViewManager* anOCCViewMgr = dynamic_cast<OCCViewer_ViewManager*>( aVMgr );
1051         if (anOCCViewMgr)
1052           anOCCViewMgr->setChainedOperations( aChainedPan );
1053       }
1054       
1055       QList<QDockWidget*> docW = getApp()->desktop()->findChildren<QDockWidget*>();
1056       foreach (QDockWidget* qw, docW)
1057       {
1058         HYDROGUI_ProfileDlg* pdlg = dynamic_cast<HYDROGUI_ProfileDlg*>(qw);
1059         if (pdlg)
1060           pdlg->viewManager()->setChainedOperations(aChainedPan);
1061       }
1062     }
1063     else
1064       LightApp_Module::preferencesChanged( theSection, thePref );
1065 }
1066
1067 QCursor HYDROGUI_Module::getPrefEditCursor() const
1068 {
1069   int aCursorType = SUIT_Session::session()->resourceMgr()->integerValue("preferences", "type_of_cursor", (int)CT_CrossCursor );
1070   if ( aCursorType >= Qt::BlankCursor)
1071     aCursorType++;
1072   QCursor aCursor = QCursor( Qt::CursorShape(aCursorType) );
1073   return aCursor;
1074 }
1075
1076 void HYDROGUI_Module::update( const int flags )
1077 {
1078   DEBTRACE("HYDROGUI_Module::update");
1079   if ( !isUpdateEnabled() )
1080     return;
1081
1082   QApplication::setOverrideCursor( Qt::WaitCursor );
1083
1084   // To prevent calling this method recursively
1085   // from one of the methods called below
1086   setUpdateEnabled( false );
1087
1088   // store selected objects
1089   QStringList aSelectedEntries = storeSelection();
1090
1091   bool aDoFitAll = flags & UF_FitAll;
1092   if ( aDoFitAll )
1093   {
1094     SUIT_ResourceMgr* aResMgr = getApp()->resourceMgr();
1095     aDoFitAll = aResMgr->booleanValue( "HYDRO", "auto_fit_all", false );
1096   }
1097
1098   if( ( flags & UF_Viewer ) )
1099     updateViewer( getDisplayer(), flags & UF_GV_Init, flags & UF_GV_Forced, aDoFitAll ); 
1100
1101   if( ( flags & UF_OCCViewer ) )
1102     updateViewer( getOCCDisplayer(), flags & UF_OCC_Init, flags & UF_OCC_Forced, aDoFitAll ); 
1103
1104   if( ( flags & UF_VTKViewer ) )
1105     updateViewer( getVTKDisplayer(), flags & UF_VTK_Init, flags & UF_VTK_Forced, aDoFitAll ); 
1106
1107   if( ( flags & UF_Model ) && getDataModel() && getApp() )
1108   {
1109     getDataModel()->update( getStudyId() );
1110
1111     // Temporary workaround to prevent breaking
1112     // the selection in the object browser.
1113     // Note: processEvents() should be called after updateGV(),
1114     // otherwise the application crashes from time to time.
1115     //RKV: qApp->processEvents();
1116     SUIT_ResourceMgr* aResMgr = getApp()->resourceMgr();
1117     bool isResizeOnExpandItem = aResMgr->booleanValue( "ObjectBrowser", "resize_on_expand_item", false );
1118     SUIT_DataBrowser* anObjectBrowser = getApp()->objectBrowser();
1119     if ( isResizeOnExpandItem && anObjectBrowser ) {
1120       anObjectBrowser->setResizeOnExpandItem( false ); // MZN: ISSUE #280 
1121     }
1122     getApp()->updateObjectBrowser( true );
1123     if ( isResizeOnExpandItem && anObjectBrowser ) {
1124       anObjectBrowser->setResizeOnExpandItem( true ); // MZN: ISSUE #280 
1125     }
1126   }
1127
1128   // Object browser is currently updated by using UF_Model flag
1129   if( ( flags & UF_ObjBrowser ) && ((flags & UF_Model) == 0) && getApp() )
1130     getApp()->updateObjectBrowser( true );
1131
1132   if( ( flags & UF_Controls ) && getApp() )
1133     getApp()->updateActions();
1134
1135   // restore selected objects
1136   restoreSelection( aSelectedEntries );
1137
1138   setUpdateEnabled( true );
1139
1140   preferencesChanged( "HYDRO", "zoom_shutoff" );
1141
1142   preferencesChanged( "HYDRO", "chained_panning" );
1143
1144   QApplication::restoreOverrideCursor();
1145 }
1146
1147 void HYDROGUI_Module::updateCommandsStatus()
1148 {
1149   DEBTRACE("HYDROGUI_Module::updateCommandsStatus");
1150   LightApp_Module::updateCommandsStatus();
1151
1152   updateUndoRedoControls();
1153
1154   action( CopyId )->setEnabled( getDataModel()->canCopy() );
1155   action( PasteId )->setEnabled( getDataModel()->canPaste() );
1156 }
1157
1158 void HYDROGUI_Module::selectionChanged()
1159 {
1160   LightApp_Module::selectionChanged();
1161   updateCommandsStatus();
1162 }
1163
1164 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
1165 {
1166   //DEBTRACE("HYDROGUI_Module::getDataModel()");
1167   return (HYDROGUI_DataModel*)dataModel();
1168 }
1169
1170 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
1171 {
1172   //DEBTRACE("HYDROGUI_Module::getDisplayer()");
1173   return myDisplayer;
1174 }
1175
1176 HYDROGUI_OCCDisplayer* HYDROGUI_Module::getOCCDisplayer() const
1177 {
1178   DEBTRACE("HYDROGUI_Module::getOCCDisplayer");
1179   return myOCCDisplayer;
1180 }
1181
1182 HYDROGUI_VTKPrsDisplayer* HYDROGUI_Module::getVTKDisplayer() const
1183 {
1184   DEBTRACE("HYDROGUI_Module::getVTKDisplayer");
1185   return myVTKDisplayer;
1186 }
1187
1188 SUIT_ViewManager* HYDROGUI_Module::getViewManager( const int theId ) const
1189 {
1190   DEBTRACE("HYDROGUI_Module::getViewManager");
1191   if( myViewManagerMap.contains( theId ) )
1192   {
1193     return myViewManagerMap[ theId ].first;
1194   }
1195   return NULL;
1196 }
1197
1198 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
1199 {
1200   DEBTRACE("HYDROGUI_Module::getViewer");
1201   if( myViewManagerMap.contains( theId ) )
1202   {
1203     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
1204     GraphicsView_ViewManager* aViewManager =
1205       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
1206     if( aViewManager )
1207       return aViewManager->getViewer();
1208   }
1209   return NULL;
1210 }
1211
1212 OCCViewer_Viewer* HYDROGUI_Module::getOCCViewer( const int theId ) const
1213 {
1214   DEBTRACE("HYDROGUI_Module::getOCCViewer");
1215   if( myViewManagerMap.contains( theId ) )
1216   {
1217     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
1218     OCCViewer_ViewManager* aViewManager =
1219       ::qobject_cast<OCCViewer_ViewManager*>( anInfo.first );
1220     if( aViewManager )
1221       return aViewManager->getOCCViewer();
1222   }
1223   return NULL;
1224 }
1225
1226 SVTK_Viewer* HYDROGUI_Module::getVTKViewer( const int theId ) const
1227 {
1228   DEBTRACE("HYDROGUI_Module::getVTKViewer");
1229   if( myViewManagerMap.contains( theId ) )
1230   {
1231     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
1232     SVTK_ViewManager* aViewManager =
1233       ::qobject_cast<SVTK_ViewManager*>( anInfo.first );
1234     if( aViewManager )
1235       return dynamic_cast<SVTK_Viewer*>( aViewManager->getViewModel() );
1236   }
1237   return NULL;
1238 }
1239
1240 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
1241 {
1242   DEBTRACE("HYDROGUI_Module::getViewManagerId");
1243   ViewManagerMapIterator anIter( myViewManagerMap );
1244   while( anIter.hasNext() )
1245   {
1246     int anId = anIter.next().key();
1247     const ViewManagerInfo& anInfo = anIter.value();
1248     if( anInfo.first == theViewManager )
1249       return anId;
1250   }
1251   return -1;
1252 }
1253
1254 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
1255 {
1256   DEBTRACE("HYDROGUI_Module::getViewManagerRole");
1257   int anId = getViewManagerId( theViewManager );
1258   if( anId != -1 )
1259   {
1260     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
1261     return anInfo.second;
1262   }
1263   return VMR_Unknown;
1264 }
1265
1266 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
1267                                           const ViewManagerRole theRole )
1268 {
1269   DEBTRACE("HYDROGUI_Module::setViewManagerRole");
1270   int anId = getViewManagerId( theViewManager );
1271   if( anId != -1 )
1272   {
1273     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
1274     anInfo.second = theRole;
1275   }
1276 }
1277
1278 bool HYDROGUI_Module::isObjectVisible( const size_t theViewId,
1279                                        const Handle(HYDROData_Entity)& theObject ) const
1280 {
1281   if( theObject.IsNull() )
1282     return false;
1283
1284   if( theViewId == 0 )
1285   {
1286     //search in all
1287     foreach( size_t aViewId, myObjectStateMap.keys() )
1288     {
1289       if( isObjectVisible( aViewId, theObject ) )
1290         return true;
1291     }
1292     return false;
1293   }
1294
1295   ViewId2Entry2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
1296   if( anIter1 != myObjectStateMap.end() )
1297   {
1298     const Entry2ObjectStateMap& aEntry2ObjectStateMap = anIter1.value();
1299     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
1300
1301     Entry2ObjectStateMap::const_iterator anIter2 = aEntry2ObjectStateMap.find( anEntry );
1302     if( anIter2 != aEntry2ObjectStateMap.end() )
1303     {
1304       const ObjectState& anObjectState = anIter2.value();
1305       return anObjectState.Visibility;
1306     }
1307   }
1308   return false;
1309 }
1310
1311 void HYDROGUI_Module::setObjectVisible( const size_t theViewId,
1312                                         const Handle(HYDROData_Entity)& theObject,
1313                                         const bool theState )
1314 {
1315   DEBTRACE("setObjectVisible, theViewId: " << theViewId);
1316   if( !theObject.IsNull() )
1317   {
1318         DEBTRACE("myObjectStateMap.size: " << myObjectStateMap.size());
1319         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
1320         DEBTRACE("anEntry: " << anEntry.toStdString());
1321         if (myObjectStateMap.find(theViewId) == myObjectStateMap.end())
1322         {
1323                 DEBTRACE("theViewId is not a valid key for myObjectStateMap, created");
1324         }
1325     Entry2ObjectStateMap& aEntry2ObjectStateMap = myObjectStateMap[ theViewId ]; // created OK if it does not exist
1326
1327     ObjectState& anObjectState = aEntry2ObjectStateMap[ anEntry ];
1328     anObjectState.Visibility = theState;
1329
1330     HYDROGUI_DataObject* hydroObject = getDataModel()->getDataObject( theObject );
1331     if ( hydroObject )
1332     {
1333         SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
1334         if ( treeModel )
1335         {
1336           QString id = hydroObject->text( hydroObject->customData( Qtx::IdType ).toInt() );
1337           Qtx::VisibilityState visState = treeModel->visibilityState( id );
1338           if ( visState != Qtx::UnpresentableState )
1339             treeModel->setVisibilityState( id, theState ? Qtx::ShownState : Qtx::HiddenState );
1340         }
1341     }
1342
1343     if ( theObject->GetKind() == KIND_BATHYMETRY && theState ) {
1344       setLandCoversScalarMapModeOff( theViewId );
1345     } else if ( theObject->GetKind() == KIND_LAND_COVER_MAP && theState ) {
1346       getOCCDisplayer()->SetToUpdateColorScale();
1347     }
1348   }
1349 }
1350
1351 void HYDROGUI_Module::setIsToUpdate( const Handle(HYDROData_Entity)& theObject,
1352                                      const bool theState )
1353 {
1354   DEBTRACE("HYDROGUI_Module::setIsToUpdate");
1355   if( !theObject.IsNull() )
1356   {
1357     // Process OCC shapes
1358     ViewId2ListOfShapes::const_iterator aShapesMapIter( myShapesMap.begin() );
1359     while( aShapesMapIter != myShapesMap.end() )
1360     {
1361       const ListOfShapes& aShapesList = aShapesMapIter.value();
1362       foreach ( HYDROGUI_Shape* aShape, aShapesList )
1363       {
1364         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
1365         {
1366           aShape->setIsToUpdate( theState );
1367         }
1368       }
1369       aShapesMapIter++;
1370     }
1371     // Process VTK shapes
1372     ViewId2ListOfVTKPrs::const_iterator aVTKPrsMapIter( myVTKPrsMap.begin() );
1373     while( aVTKPrsMapIter != myVTKPrsMap.end() )
1374     {
1375       const ListOfVTKPrs& aShapesList = aVTKPrsMapIter.value();
1376       foreach ( HYDROGUI_VTKPrs* aShape, aShapesList )
1377       {
1378         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
1379         {
1380           aShape->setIsToUpdate( theState );
1381         }
1382       }
1383       aVTKPrsMapIter++;
1384     }
1385   }
1386 }
1387
1388 /////////////////// OCC SHAPES PROCESSING
1389 QList<HYDROGUI_Shape*> HYDROGUI_Module::getObjectShapes( const int  theViewId,
1390                                                          ObjectKind theKind ) const
1391 {
1392   QList<HYDROGUI_Shape*> aResult;
1393
1394   if ( myShapesMap.contains( theViewId ) )
1395   {
1396     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1397     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
1398     {
1399       if( aShape && aShape->getObject()->GetKind()==theKind )
1400         aResult.append( aShape );
1401     }
1402   }
1403   return aResult;
1404 }
1405
1406 HYDROGUI_Shape* HYDROGUI_Module::getObjectShape( const int                       theViewId,
1407                                                  const Handle(HYDROData_Entity)& theObject ) const
1408 {
1409   HYDROGUI_Shape* aResShape = NULL;
1410   if( theObject.IsNull() )
1411     return aResShape;
1412
1413   if ( myShapesMap.contains( theViewId ) )
1414   {
1415     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1416     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
1417     {
1418       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
1419         continue;
1420
1421       aResShape = aShape;
1422       break;
1423     }
1424   }
1425
1426   return aResShape;
1427 }
1428
1429 void HYDROGUI_Module::setObjectShape( const int                       theViewId,
1430                                       const Handle(HYDROData_Entity)& theObject,
1431                                       HYDROGUI_Shape*                 theShape )
1432 {
1433   if( theObject.IsNull() )
1434     return;
1435
1436   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
1437   aViewShapes.append( theShape );
1438 }
1439
1440 void HYDROGUI_Module::removeObjectShape( const int                       theViewId,
1441                                          const Handle(HYDROData_Entity)& theObject )
1442 {
1443   if ( !myShapesMap.contains( theViewId ) )
1444     return;
1445
1446   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
1447   Handle(HYDROData_Entity) anObject;
1448   for ( int i = 0; i < aViewShapes.length(); )
1449   {
1450     HYDROGUI_Shape* aShape = aViewShapes.at( i );
1451     anObject = aShape->getObject();
1452     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1453     {
1454       delete aShape;
1455       aViewShapes.removeAt( i );
1456       continue;
1457     }
1458
1459     ++i;
1460   }
1461 }
1462
1463 void HYDROGUI_Module::removeViewShapes( const int theViewId )
1464 {
1465   if ( !myShapesMap.contains( theViewId ) )
1466     return;
1467
1468   const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1469   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1470   {
1471     HYDROGUI_Shape* aShape = aViewShapes.at( i );
1472     if ( aShape )
1473       delete aShape;
1474   }
1475
1476   myShapesMap.remove( theViewId );
1477 }
1478 /////////////////// END OF OCC SHAPES PROCESSING
1479
1480 /////////////////// VTKPrs PROCESSING
1481 HYDROGUI_VTKPrs* HYDROGUI_Module::getObjectVTKPrs( const int                       theViewId,
1482                                                  const Handle(HYDROData_Entity)& theObject ) const
1483 {
1484   HYDROGUI_VTKPrs* aResShape = NULL;
1485   if( theObject.IsNull() )
1486     return aResShape;
1487
1488   if ( myVTKPrsMap.contains( theViewId ) )
1489   {
1490     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1491     foreach ( HYDROGUI_VTKPrs* aShape, aViewShapes )
1492     {
1493       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
1494         continue;
1495
1496       aResShape = aShape;
1497       break;
1498     }
1499   }
1500
1501   return aResShape;
1502 }
1503
1504 void HYDROGUI_Module::setObjectVTKPrs( const int                       theViewId,
1505                                        const Handle(HYDROData_Entity)& theObject,
1506                                        HYDROGUI_VTKPrs*                 theShape )
1507 {
1508   if( theObject.IsNull() )
1509     return;
1510
1511   if( theShape && theShape->needScalarBar() )
1512   {
1513     // Compute the new global Z range from the added presentation and the old global Z range.
1514     double* aGlobalRange = getVTKDisplayer()->GetZRange( theViewId );
1515     double* aRange = theShape->getInternalZRange();
1516     bool anIsUpdate = false;
1517     if ( aRange[0] < aGlobalRange[0] )
1518     {
1519       aGlobalRange[0] = aRange[0];
1520       anIsUpdate = true;
1521     }
1522     if ( aRange[1] > aGlobalRange[1] )
1523     {
1524       aGlobalRange[1] = aRange[1];
1525       anIsUpdate = true;
1526     }
1527
1528     //if ( anIsUpdate )
1529     //{
1530       updateVTKZRange( theViewId, aGlobalRange );
1531     //}
1532   }
1533
1534   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1535   aViewShapes.append( theShape );
1536 }
1537
1538 void HYDROGUI_Module::removeObjectVTKPrs( const int      theViewId,
1539                                           const QString& theEntry )
1540 {
1541   if ( !myVTKPrsMap.contains( theViewId ) )
1542     return;
1543
1544   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1545   Handle(HYDROData_Entity) anObject;
1546   QString anEntryRef;
1547   for ( int i = 0; i < aViewShapes.length(); )
1548   {
1549     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1550     anObject = aShape->getObject();
1551     anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
1552     if ( aShape && (!anObject.IsNull()) && ( anEntryRef == theEntry ) )
1553     {
1554       delete aShape;
1555       aViewShapes.removeAt( i );
1556       continue;
1557     }
1558
1559     ++i;
1560   }
1561
1562   // Invalidate global Z range
1563   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1564   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1565 }
1566
1567 void HYDROGUI_Module::removeObjectVTKPrs( const int                       theViewId,
1568                                           const Handle(HYDROData_Entity)& theObject )
1569 {
1570   if ( !myVTKPrsMap.contains( theViewId ) )
1571     return;
1572
1573   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1574   Handle(HYDROData_Entity) anObject;
1575   for ( int i = 0; i < aViewShapes.length(); )
1576   {
1577     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1578     anObject = aShape->getObject();
1579     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1580     {
1581       delete aShape;
1582       aViewShapes.removeAt( i );
1583       continue;
1584     }
1585
1586     ++i;
1587   }
1588
1589   // Invalidate global Z range
1590   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1591   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1592 }
1593
1594 void HYDROGUI_Module::removeViewVTKPrs( const int theViewId )
1595 {
1596   if ( !myVTKPrsMap.contains( theViewId ) )
1597     return;
1598
1599   const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1600   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1601   {
1602     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1603     if ( aShape )
1604       delete aShape;
1605   }
1606
1607   myVTKPrsMap.remove( theViewId );
1608 }
1609
1610 void HYDROGUI_Module::updateVTKZRange( const int theViewId, double theRange[] )
1611 {
1612   if ( myVTKPrsMap.contains( theViewId ) )
1613   {
1614     // For the given viewer id update all VTK presentations ...
1615     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1616     HYDROGUI_VTKPrs* aShape;
1617     for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1618     {
1619       aShape = aViewShapes.at( i );
1620       if ( aShape && aShape->needScalarBar() )
1621       {
1622         aShape->setZRange( theRange );
1623       }
1624     }
1625   }
1626   // ... and update the global color legend scalar bar.
1627   getVTKDisplayer()->SetZRange( theViewId, theRange );
1628 }
1629 /////////////////// END OF VTKPrs PROCESSING
1630
1631 void HYDROGUI_Module::clearCache()
1632 {
1633     DEBTRACE("HYDROGUI_Module::clearCache");
1634     myObjectStateMap.clear();
1635 }
1636
1637 CAM_DataModel* HYDROGUI_Module::createDataModel()
1638 {
1639   DEBTRACE("HYDROGUI_Module::createDataModel");
1640   return new HYDROGUI_DataModel( this );
1641 }
1642
1643 void HYDROGUI_Module::customEvent( QEvent* e )
1644 {
1645   int aType = e->type();
1646   if ( aType == NewViewEvent )
1647   {
1648     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
1649     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
1650     {
1651       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
1652       {
1653         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
1654         ViewManagerRole aRole = getViewManagerRole( aViewManager );
1655
1656         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
1657         {
1658           if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1659             aViewPort->scale( 1, -1 ); // invert the Y axis direction from down to up
1660
1661           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
1662           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
1663           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
1664
1665           //ouv: temporarily commented
1666           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
1667         }
1668
1669         if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1670           update( UF_Viewer );
1671
1672         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
1673       }
1674     }
1675   }
1676 }
1677
1678 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
1679 {
1680   QEvent::Type aType = theEvent->type();
1681   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
1682   {
1683     if( aType == QEvent::Show )
1684     {
1685       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
1686       e->setData( theObj );
1687       QApplication::postEvent( this, e );
1688       theObj->removeEventFilter( this );
1689     }
1690   }
1691   else if ( theObj->inherits( "OCCViewer_ViewPort" ) )
1692   {
1693     if( aType == QEvent::Leave )
1694     {
1695       SUIT_Desktop* aDesktop = getApp()->desktop();
1696       if ( aDesktop && aDesktop->statusBar() ) {
1697         aDesktop->statusBar()->clearMessage();
1698       }
1699     }
1700   }
1701   else if ( theObj->inherits( "SVTK_ViewWindow" ) )
1702   {
1703     if( aType == QEvent::Leave )
1704     {
1705       SUIT_Desktop* aDesktop = getApp()->desktop();
1706       if ( aDesktop && aDesktop->statusBar() ) {
1707         aDesktop->statusBar()->clearMessage();
1708       }
1709     }
1710   }
1711
1712   return LightApp_Module::eventFilter( theObj, theEvent );
1713 }
1714
1715 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
1716 {
1717   DEBTRACE("HYDROGUI_Module::onViewManagerAdded");
1718   LightApp_Module::onViewManagerAdded( theViewManager );
1719
1720   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
1721   { 
1722     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1723              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1724   }
1725   else if( theViewManager->getType() == OCCViewer_Viewer::Type() )
1726   {
1727     OCCViewer_ViewManager* mgr = dynamic_cast<OCCViewer_ViewManager*>( theViewManager );
1728     //mgr->setChainedOperations( true );
1729
1730     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1731              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1732     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1733              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1734     connect( theViewManager, SIGNAL( activated( SUIT_ViewManager* ) ), 
1735              this, SLOT( onViewActivated( SUIT_ViewManager* ) ) );
1736   }
1737   else if( theViewManager->getType() == SVTK_Viewer::Type() )
1738   {
1739     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1740              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1741     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1742              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1743   }
1744
1745   createSelector( theViewManager ); // replace the default selector
1746
1747   ViewManagerInfo anInfo( theViewManager, VMR_General );
1748   myViewManagerMap.insert( ViewManagerId++, anInfo );
1749 }
1750
1751 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
1752 {
1753   DEBTRACE("HYDROGUI_Module::onViewManagerRemoved");
1754   LightApp_Module::onViewManagerRemoved( theViewManager );
1755
1756   createSelector( theViewManager ); // replace the default selector
1757
1758   int anId = getViewManagerId( theViewManager );
1759   if( anId != -1 )
1760   {
1761     OCCViewer_ViewManager* anOCCViewManager =
1762       ::qobject_cast<OCCViewer_ViewManager*>( myViewManagerMap[ anId ].first );
1763     if ( anOCCViewManager )
1764     {
1765       OCCViewer_Viewer* anOCCViewer = anOCCViewManager->getOCCViewer();
1766       if ( anOCCViewer ) {
1767         int aViewerId = (size_t)anOCCViewer;
1768         removeViewShapes( aViewerId );
1769         setLandCoversScalarMapModeOff( aViewerId );
1770       }
1771     }
1772
1773     if ( getVTKDisplayer()->IsApplicable( theViewManager ) )
1774     {
1775       SVTK_Viewer* aVTKViewer = getVTKViewer( anId );
1776       if ( aVTKViewer )
1777       {
1778         getVTKDisplayer()->EraseScalarBar( anId, true );
1779         removeViewShapes( (size_t)aVTKViewer );
1780       }
1781     }
1782
1783     myViewManagerMap.remove( anId );
1784   }
1785 }
1786
1787 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
1788 {
1789   DEBTRACE("HYDROGUI_Module::onViewCreated");
1790   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
1791   {
1792     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
1793     {
1794       aViewFrame->installEventFilter( this );
1795
1796       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
1797       aViewPort->setInteractionFlag( GraphicsView_ViewPort::GlobalWheelScaling );
1798
1799       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
1800                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
1801     }
1802   }
1803   else if( theViewWindow && theViewWindow->inherits( "OCCViewer_ViewFrame" ) )
1804   {
1805     if( OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theViewWindow ) )
1806     {
1807       aViewFrame->onTopView();
1808
1809       HYDROGUI_Tool::setOCCActionShown( aViewFrame, OCCViewer_ViewWindow::MaximizedId, false );
1810       OCCViewer_ViewPort3d* aViewPort = aViewFrame->getViewPort();
1811       if ( aViewPort ) {
1812         aViewPort->installEventFilter( this );
1813       }
1814     }
1815   }
1816   else if( theViewWindow && theViewWindow->inherits( "SVTK_ViewWindow" ) )
1817   {
1818     if( SVTK_ViewWindow* aViewFrame = dynamic_cast<SVTK_ViewWindow*>( theViewWindow ) )
1819     {
1820       aViewFrame->installEventFilter( this );
1821     }
1822   }
1823 }
1824
1825 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
1826 {
1827   /* ouv: currently unused
1828   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
1829   {
1830     SUIT_ViewManager* aViewManager = 0;
1831
1832     QObject* aParent = aViewPort;
1833     while( aParent = aParent->parent() )
1834     {
1835       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
1836       {
1837         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
1838         {
1839           aViewManager = aViewer->getViewManager();
1840           break;
1841         }
1842       }
1843     }
1844
1845     if( !aViewManager )
1846       return;
1847
1848     double aMouseX = theEvent->scenePos().x();
1849     double aMouseY = theEvent->scenePos().y();
1850
1851     ViewManagerRole aRole = getViewManagerRole( aViewManager );
1852     if( aRole == VMR_General )
1853     {
1854       int aXDeg = 0, aYDeg = 0;
1855       int aXMin = 0, aYMin = 0;
1856       double aXSec = 0, aYSec = 0;
1857       HYDROData_Lambert93::secToDMS( aMouseX, aXDeg, aXMin, aXSec );
1858       HYDROData_Lambert93::secToDMS( aMouseY, aYDeg, aYMin, aYSec );
1859
1860       QString aDegSymbol( QChar( 0x00B0 ) );
1861       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
1862       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
1863
1864       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
1865     }
1866     else if( aRole == VMR_TransformImage )
1867       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
1868   }
1869   */
1870 }
1871
1872 void HYDROGUI_Module::updateViewer( HYDROGUI_AbstractDisplayer* theDisplayer, 
1873                                     const bool theIsInit, 
1874                                     const bool theIsForced, 
1875                                     const bool theDoFitAll )
1876 {
1877   DEBTRACE("HYDROGUI_Module::updateViewer");
1878   QList<int> aViewManagerIdList;
1879
1880   // currently, all views are updated
1881   ViewManagerMapIterator anIter( myViewManagerMap );
1882   while( anIter.hasNext() )
1883   { 
1884     SUIT_ViewManager* aViewManager = anIter.next().value().first;
1885
1886     if ( theDisplayer->IsApplicable( aViewManager ) )
1887     {
1888       int anId = anIter.key();
1889       aViewManagerIdList.append( anId );
1890     }
1891   }
1892
1893   QListIterator<int> anIdIter( aViewManagerIdList );
1894   while( anIdIter.hasNext() )
1895   {
1896     theDisplayer->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
1897     myOverview->setTopView();
1898   }
1899 }
1900
1901 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
1902 {
1903   DEBTRACE("HYDROGUI_Module::createSelector");
1904   if( !theViewManager )
1905     return;
1906
1907   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1908   if( !aSelectionMgr )
1909     return;
1910
1911   QString aViewType = theViewManager->getType();
1912   if( aViewType != GraphicsView_Viewer::Type() &&
1913       aViewType != OCCViewer_Viewer::Type())
1914     return;
1915
1916   QList<SUIT_Selector*> aSelectorList;
1917   aSelectionMgr->selectors( aViewType, aSelectorList );
1918
1919   // disable all alien selectors
1920   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
1921   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
1922   {
1923     SUIT_Selector* aSelector = *anIter;
1924     if( aSelector && ( !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) &&
1925                        !dynamic_cast<SVTK_Selector*>( aSelector ) &&
1926                        !dynamic_cast<HYDROGUI_OCCSelector*>( aSelector ) ) )
1927       aSelector->setEnabled( false );
1928   }
1929
1930   if ( aViewType == GraphicsView_Viewer::Type() )
1931   {
1932     GraphicsView_ViewManager* aViewManager =
1933       ::qobject_cast<GraphicsView_ViewManager*>( theViewManager );
1934     if( aViewManager )
1935       new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
1936   }
1937   else if ( aViewType == OCCViewer_Viewer::Type() )
1938   {
1939     OCCViewer_ViewManager* aViewManager =
1940       ::qobject_cast<OCCViewer_ViewManager*>( theViewManager );
1941     if( aViewManager )
1942       new HYDROGUI_OCCSelector( this, aViewManager->getOCCViewer(), aSelectionMgr );
1943   }
1944 }
1945
1946 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
1947 {
1948   DEBTRACE("HYDROGUI_Module::setUpdateEnabled");
1949   bool aPrevState = myIsUpdateEnabled;
1950   myIsUpdateEnabled = theState;
1951   return aPrevState;
1952 }
1953
1954 bool HYDROGUI_Module::isUpdateEnabled() const
1955 {
1956   return myIsUpdateEnabled;
1957 }
1958
1959 QStringList HYDROGUI_Module::storeSelection() const
1960 {
1961   DEBTRACE("HYDROGUI_Module::storeSelection");
1962   QStringList anEntryList;
1963   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1964   {
1965     SUIT_DataOwnerPtrList aList( true );
1966     aSelectionMgr->selected( aList );
1967
1968     SUIT_DataOwnerPtrList::iterator anIter;
1969     for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
1970     {
1971       const LightApp_DataOwner* anOwner = 
1972         dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
1973       if( anOwner )
1974         anEntryList.append( anOwner->entry() );
1975     }
1976   }
1977   return anEntryList;
1978 }
1979
1980 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
1981 {
1982   DEBTRACE("HYDROGUI_Module::restoreSelection");
1983   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1984   {
1985     SUIT_DataOwnerPtrList aList( true );
1986     for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
1987       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
1988     aSelectionMgr->setSelected( aList );
1989   }
1990 }
1991
1992 void HYDROGUI_Module::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* )
1993 {
1994   double X, Y, Z;
1995   bool doShow = false;
1996   HYDROGUI_Displayer* aDisplayer = getDisplayer();
1997   if ( aDisplayer )
1998     aDisplayer->SaveCursorViewPosition( theViewWindow );
1999     doShow = aDisplayer->GetCursorViewCoordinates( theViewWindow, X, Y, Z );
2000
2001   if ( doShow )
2002   {
2003     // Show the coordinates in the status bar
2004     SUIT_Desktop* aDesktop = getApp()->desktop();
2005     if ( aDesktop && aDesktop->statusBar() )
2006     {
2007       gp_Pnt aWPnt( X, Y, Z );
2008       HYDROData_Document::Document()->Transform( aWPnt, false );
2009       double WX = aWPnt.X(), WY = aWPnt.Y();
2010
2011       QString aXStr = HYDROGUI_Tool::GetCoordinateString( X, true );
2012       QString anYStr = HYDROGUI_Tool::GetCoordinateString( Y, true );
2013       QString aWXStr = HYDROGUI_Tool::GetCoordinateString( WX, true );
2014       QString aWYStr = HYDROGUI_Tool::GetCoordinateString( WY, true );
2015       QString aMsg = tr( "COORDINATES_INFO" );
2016       aMsg = aMsg.arg( aXStr ).arg( anYStr ).arg( aWXStr ).arg( aWYStr );
2017       aDesktop->statusBar()->showMessage( aMsg );
2018     }
2019   }
2020 }
2021
2022 /**
2023  * Returns stack of active operations;
2024  */
2025 QStack<HYDROGUI_Operation*>& HYDROGUI_Module::getActiveOperations()
2026 {
2027   DEBTRACE("HYDROGUI_Module::getActiveOperations");
2028   return myActiveOperationMap;
2029 }
2030
2031 /**
2032  * Returns the module active operation. If the active operation is show/hide,
2033  * the method returns the previous operation if it is.
2034  */
2035 HYDROGUI_Operation* HYDROGUI_Module::activeOperation()
2036 {
2037   HYDROGUI_Operation* anOp = !myActiveOperationMap.empty() ? myActiveOperationMap.top() : 0;
2038
2039   if ( dynamic_cast<HYDROGUI_ShowHideOp*>( anOp ) )
2040   {
2041     QVectorIterator<HYDROGUI_Operation*> aVIt( myActiveOperationMap );
2042     aVIt.toBack();
2043     aVIt.previous(); // skip the top show/hide operation
2044     anOp = aVIt.hasPrevious() ? aVIt.previous() : 0;
2045   }
2046
2047   return anOp;
2048 }
2049
2050 /*!
2051  * \brief Virtual public slot
2052  *
2053  * This method is called after the object inserted into data view to update their visibility state
2054  * This is default implementation
2055  */
2056 void HYDROGUI_Module::onObjectClicked( SUIT_DataObject* theObject, int theColumn )
2057 {
2058   if ( !isActiveModule() )
2059       return;
2060
2061   HYDROGUI_DataObject* hydroObject = dynamic_cast<HYDROGUI_DataObject*>( theObject );
2062
2063   // change visibility of object
2064   if ( !hydroObject || theColumn != SUIT_DataObject::VisibilityId )
2065       return;
2066
2067   SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
2068
2069   QString id = theObject->text( theObject->customData( Qtx::IdType ).toInt() );
2070   Qtx::VisibilityState visState = treeModel->visibilityState( id );
2071   if ( visState == Qtx::UnpresentableState )
2072       return;
2073
2074   visState = visState == Qtx::ShownState ? Qtx::HiddenState : Qtx::ShownState;
2075   treeModel->setVisibilityState( id, visState );
2076
2077   bool vis = visState == Qtx::ShownState;
2078   if ( vis == isObjectVisible( HYDROGUI_Tool::GetActiveViewId( this ), hydroObject->modelObject() ) )
2079       return;
2080
2081   setObjectVisible( HYDROGUI_Tool::GetActiveViewId( this ), hydroObject->modelObject(), vis );
2082
2083   update( UF_OCCViewer | UF_VTKViewer | ( visState == Qtx::ShownState ? UF_FitAll : 0 ) );
2084 }
2085
2086 bool HYDROGUI_Module::isDraggable( const SUIT_DataObject* what ) const
2087 {
2088   return true;
2089 }
2090
2091 bool HYDROGUI_Module::isDropAccepted( const SUIT_DataObject* where ) const
2092 {
2093
2094   return true;
2095 }
2096
2097 void HYDROGUI_Module::dropObjects( const DataObjectList& what, SUIT_DataObject* where,
2098                                    const int row, Qt::DropAction action )
2099 {
2100   if ( action != Qt::CopyAction && action != Qt::MoveAction )
2101     return; 
2102
2103   if (row == -1)
2104     return;
2105
2106   if (where->level() < 2 )
2107     return;  
2108
2109   DataObjectList::ConstIterator it = what.constBegin();
2110   for (;it != what.constEnd();++it) 
2111   {
2112     if ((*it)->parent() != where)
2113       return;
2114   }
2115
2116   it = what.constBegin();
2117
2118   int i=0;
2119   for (;it != what.constEnd();++it) 
2120   {
2121     SUIT_DataObject* objWhat = *it;
2122
2123     DataObjectList objInSect = where->children();
2124
2125     //std::list<SUIT_DataObject*> t1 = where->children().toStdList(); //debug
2126     int ind = objInSect.indexOf(objWhat);
2127     if (ind != -1)
2128     {
2129       HYDROGUI_DataModel* aModel = getDataModel();
2130       int pos = -1;
2131       if (ind >= row)
2132       {
2133         pos = row + i;
2134         i++;
2135       }
2136       else
2137         pos = row - 1;
2138       where->moveChildPos(objWhat, pos);
2139       //std::list<SUIT_DataObject*> t2 = where->children().toStdList(); //debug
2140     }  
2141   }
2142
2143   getApp()->updateObjectBrowser(true);
2144   
2145 }
2146
2147 Handle(HYDROData_StricklerTable) HYDROGUI_Module::getLandCoverColoringTable( const int theViewId ) const
2148 {
2149   Handle(HYDROData_StricklerTable) aTable;
2150
2151   if ( myLandCoverColoringMap.contains( theViewId ) ) {
2152     aTable = myLandCoverColoringMap.value( theViewId );
2153   }
2154
2155   return aTable;
2156 }
2157
2158 void HYDROGUI_Module::setLandCoverColoringTable( const int theViewId,
2159                                                  const Handle(HYDROData_StricklerTable)& theTable )
2160 {
2161   if ( !theTable.IsNull() ) {
2162     myLandCoverColoringMap.insert( theViewId, theTable );
2163   }
2164 }
2165
2166 void HYDROGUI_Module::setLandCoversScalarMapModeOff( const int theViewId )
2167 {
2168   myLandCoverColoringMap.remove( theViewId );
2169 }
2170
2171 bool HYDROGUI_Module::isLandCoversScalarMapModeOn( const int theViewId ) const
2172 {
2173   return myLandCoverColoringMap.contains( theViewId );
2174 }
2175
2176 void HYDROGUI_Module::setObjectRemoved( const Handle(HYDROData_Entity)& theObject )
2177 {
2178   if ( theObject.IsNull() || !theObject->IsRemoved() ) {
2179     return;
2180   }
2181
2182   if ( theObject->GetKind() == KIND_STRICKLER_TABLE ) {
2183     Handle(HYDROData_StricklerTable) aTable = 
2184       Handle(HYDROData_StricklerTable)::DownCast( theObject );
2185     QList<int> aViewIds;
2186     QMutableMapIterator<int, Handle(HYDROData_StricklerTable)> anIter( myLandCoverColoringMap );
2187     while ( anIter.hasNext() ) {
2188       if ( HYDROGUI_DataObject::dataObjectEntry( anIter.next().value() ) == 
2189            HYDROGUI_DataObject::dataObjectEntry( aTable ) ) {
2190         anIter.remove();
2191       }
2192     }
2193   }
2194 }
2195
2196 void HYDROGUI_Module::onViewActivated( SUIT_ViewManager* theMgr )
2197 {
2198   DEBTRACE("HYDROGUI_Module::onViewActivated");
2199   if( !theMgr )
2200     return;
2201
2202   SUIT_ViewWindow* wnd = theMgr->getActiveView();
2203   OCCViewer_ViewFrame* occwnd = dynamic_cast<OCCViewer_ViewFrame*>( wnd );
2204   if( !occwnd )
2205     return;
2206
2207   myOverview->setMainView( occwnd );
2208 }
2209
2210 void HYDROGUI_Module::setAutoZoomToAllViewManagers(bool bAutoZoom)
2211 {
2212   ViewManagerList aViewManagers = getApp()->viewManagers();
2213   foreach (SUIT_ViewManager* aVMgr, aViewManagers)
2214     setAutoZoom(aVMgr, bAutoZoom);
2215 }
2216
2217 void HYDROGUI_Module::setAutoZoom(SUIT_ViewManager* aVMgr, bool bAutoZoom)
2218 {
2219   if (!aVMgr)
2220     return;
2221   QVector<SUIT_ViewWindow*> aViews = aVMgr->getViews();
2222   foreach (SUIT_ViewWindow* aView, aViews)
2223   {
2224     if (aView)
2225     {
2226       OCCViewer_ViewFrame* anOCCViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aView );
2227       if (anOCCViewFrame)
2228       {
2229         anOCCViewFrame->setAutomaticZoom(bAutoZoom);
2230         for (int i = OCCViewer_ViewFrame::MAIN_VIEW; i<=OCCViewer_ViewFrame::TOP_RIGHT; i++)
2231         {
2232           OCCViewer_ViewWindow* aV = anOCCViewFrame->getView(i);
2233           if (aV)
2234             aV->setAutomaticZoom(bAutoZoom);
2235         }
2236       }
2237       OCCViewer_ViewWindow* anOCCViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( aView );
2238       if (anOCCViewWindow)
2239         anOCCViewWindow->setAutomaticZoom(bAutoZoom);
2240     }
2241   }
2242 }
2243