Salome HOME
analyse HYDRO_tests
[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<int> 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     int 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 int 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 int 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( int 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 int theViewId,
1312                                         const Handle(HYDROData_Entity)& theObject,
1313                                         const bool theState )
1314 {
1315   if( !theObject.IsNull() )
1316   {
1317     Entry2ObjectStateMap& aEntry2ObjectStateMap = myObjectStateMap[ theViewId ];
1318     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theObject );
1319
1320     ObjectState& anObjectState = aEntry2ObjectStateMap[ anEntry ];
1321     anObjectState.Visibility = theState;
1322
1323     HYDROGUI_DataObject* hydroObject = getDataModel()->getDataObject( theObject );
1324     if ( hydroObject )
1325     {
1326         SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
1327         if ( treeModel )
1328         {
1329           QString id = hydroObject->text( hydroObject->customData( Qtx::IdType ).toInt() );
1330           Qtx::VisibilityState visState = treeModel->visibilityState( id );
1331           if ( visState != Qtx::UnpresentableState )
1332             treeModel->setVisibilityState( id, theState ? Qtx::ShownState : Qtx::HiddenState );
1333         }
1334     }
1335
1336     if ( theObject->GetKind() == KIND_BATHYMETRY && theState ) {
1337       setLandCoversScalarMapModeOff( theViewId );
1338     } else if ( theObject->GetKind() == KIND_LAND_COVER_MAP && theState ) {
1339       getOCCDisplayer()->SetToUpdateColorScale();
1340     }
1341   }
1342 }
1343
1344 void HYDROGUI_Module::setIsToUpdate( const Handle(HYDROData_Entity)& theObject,
1345                                      const bool theState )
1346 {
1347   DEBTRACE("HYDROGUI_Module::setIsToUpdate");
1348   if( !theObject.IsNull() )
1349   {
1350     // Process OCC shapes
1351     ViewId2ListOfShapes::const_iterator aShapesMapIter( myShapesMap.begin() );
1352     while( aShapesMapIter != myShapesMap.end() )
1353     {
1354       const ListOfShapes& aShapesList = aShapesMapIter.value();
1355       foreach ( HYDROGUI_Shape* aShape, aShapesList )
1356       {
1357         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
1358         {
1359           aShape->setIsToUpdate( theState );
1360         }
1361       }
1362       aShapesMapIter++;
1363     }
1364     // Process VTK shapes
1365     ViewId2ListOfVTKPrs::const_iterator aVTKPrsMapIter( myVTKPrsMap.begin() );
1366     while( aVTKPrsMapIter != myVTKPrsMap.end() )
1367     {
1368       const ListOfVTKPrs& aShapesList = aVTKPrsMapIter.value();
1369       foreach ( HYDROGUI_VTKPrs* aShape, aShapesList )
1370       {
1371         if ( aShape && IsEqual( aShape->getObject(), theObject ) )
1372         {
1373           aShape->setIsToUpdate( theState );
1374         }
1375       }
1376       aVTKPrsMapIter++;
1377     }
1378   }
1379 }
1380
1381 /////////////////// OCC SHAPES PROCESSING
1382 QList<HYDROGUI_Shape*> HYDROGUI_Module::getObjectShapes( const int  theViewId,
1383                                                          ObjectKind theKind ) const
1384 {
1385   QList<HYDROGUI_Shape*> aResult;
1386
1387   if ( myShapesMap.contains( theViewId ) )
1388   {
1389     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1390     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
1391     {
1392       if( aShape && aShape->getObject()->GetKind()==theKind )
1393         aResult.append( aShape );
1394     }
1395   }
1396   return aResult;
1397 }
1398
1399 HYDROGUI_Shape* HYDROGUI_Module::getObjectShape( const int                       theViewId,
1400                                                  const Handle(HYDROData_Entity)& theObject ) const
1401 {
1402   HYDROGUI_Shape* aResShape = NULL;
1403   if( theObject.IsNull() )
1404     return aResShape;
1405
1406   if ( myShapesMap.contains( theViewId ) )
1407   {
1408     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1409     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
1410     {
1411       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
1412         continue;
1413
1414       aResShape = aShape;
1415       break;
1416     }
1417   }
1418
1419   return aResShape;
1420 }
1421
1422 void HYDROGUI_Module::setObjectShape( const int                       theViewId,
1423                                       const Handle(HYDROData_Entity)& theObject,
1424                                       HYDROGUI_Shape*                 theShape )
1425 {
1426   if( theObject.IsNull() )
1427     return;
1428
1429   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
1430   aViewShapes.append( theShape );
1431 }
1432
1433 void HYDROGUI_Module::removeObjectShape( const int                       theViewId,
1434                                          const Handle(HYDROData_Entity)& theObject )
1435 {
1436   if ( !myShapesMap.contains( theViewId ) )
1437     return;
1438
1439   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
1440   Handle(HYDROData_Entity) anObject;
1441   for ( int i = 0; i < aViewShapes.length(); )
1442   {
1443     HYDROGUI_Shape* aShape = aViewShapes.at( i );
1444     anObject = aShape->getObject();
1445     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1446     {
1447       delete aShape;
1448       aViewShapes.removeAt( i );
1449       continue;
1450     }
1451
1452     ++i;
1453   }
1454 }
1455
1456 void HYDROGUI_Module::removeViewShapes( const int theViewId )
1457 {
1458   if ( !myShapesMap.contains( theViewId ) )
1459     return;
1460
1461   const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
1462   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1463   {
1464     HYDROGUI_Shape* aShape = aViewShapes.at( i );
1465     if ( aShape )
1466       delete aShape;
1467   }
1468
1469   myShapesMap.remove( theViewId );
1470 }
1471 /////////////////// END OF OCC SHAPES PROCESSING
1472
1473 /////////////////// VTKPrs PROCESSING
1474 HYDROGUI_VTKPrs* HYDROGUI_Module::getObjectVTKPrs( const int                       theViewId,
1475                                                  const Handle(HYDROData_Entity)& theObject ) const
1476 {
1477   HYDROGUI_VTKPrs* aResShape = NULL;
1478   if( theObject.IsNull() )
1479     return aResShape;
1480
1481   if ( myVTKPrsMap.contains( theViewId ) )
1482   {
1483     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1484     foreach ( HYDROGUI_VTKPrs* aShape, aViewShapes )
1485     {
1486       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
1487         continue;
1488
1489       aResShape = aShape;
1490       break;
1491     }
1492   }
1493
1494   return aResShape;
1495 }
1496
1497 void HYDROGUI_Module::setObjectVTKPrs( const int                       theViewId,
1498                                        const Handle(HYDROData_Entity)& theObject,
1499                                        HYDROGUI_VTKPrs*                 theShape )
1500 {
1501   if( theObject.IsNull() )
1502     return;
1503
1504   if( theShape && theShape->needScalarBar() )
1505   {
1506     // Compute the new global Z range from the added presentation and the old global Z range.
1507     double* aGlobalRange = getVTKDisplayer()->GetZRange( theViewId );
1508     double* aRange = theShape->getInternalZRange();
1509     bool anIsUpdate = false;
1510     if ( aRange[0] < aGlobalRange[0] )
1511     {
1512       aGlobalRange[0] = aRange[0];
1513       anIsUpdate = true;
1514     }
1515     if ( aRange[1] > aGlobalRange[1] )
1516     {
1517       aGlobalRange[1] = aRange[1];
1518       anIsUpdate = true;
1519     }
1520
1521     //if ( anIsUpdate )
1522     //{
1523       updateVTKZRange( theViewId, aGlobalRange );
1524     //}
1525   }
1526
1527   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1528   aViewShapes.append( theShape );
1529 }
1530
1531 void HYDROGUI_Module::removeObjectVTKPrs( const int      theViewId,
1532                                           const QString& theEntry )
1533 {
1534   if ( !myVTKPrsMap.contains( theViewId ) )
1535     return;
1536
1537   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1538   Handle(HYDROData_Entity) anObject;
1539   QString anEntryRef;
1540   for ( int i = 0; i < aViewShapes.length(); )
1541   {
1542     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1543     anObject = aShape->getObject();
1544     anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
1545     if ( aShape && (!anObject.IsNull()) && ( anEntryRef == theEntry ) )
1546     {
1547       delete aShape;
1548       aViewShapes.removeAt( i );
1549       continue;
1550     }
1551
1552     ++i;
1553   }
1554
1555   // Invalidate global Z range
1556   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1557   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1558 }
1559
1560 void HYDROGUI_Module::removeObjectVTKPrs( const int                       theViewId,
1561                                           const Handle(HYDROData_Entity)& theObject )
1562 {
1563   if ( !myVTKPrsMap.contains( theViewId ) )
1564     return;
1565
1566   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
1567   Handle(HYDROData_Entity) anObject;
1568   for ( int i = 0; i < aViewShapes.length(); )
1569   {
1570     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1571     anObject = aShape->getObject();
1572     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
1573     {
1574       delete aShape;
1575       aViewShapes.removeAt( i );
1576       continue;
1577     }
1578
1579     ++i;
1580   }
1581
1582   // Invalidate global Z range
1583   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
1584   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
1585 }
1586
1587 void HYDROGUI_Module::removeViewVTKPrs( const int theViewId )
1588 {
1589   if ( !myVTKPrsMap.contains( theViewId ) )
1590     return;
1591
1592   const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1593   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1594   {
1595     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
1596     if ( aShape )
1597       delete aShape;
1598   }
1599
1600   myVTKPrsMap.remove( theViewId );
1601 }
1602
1603 void HYDROGUI_Module::updateVTKZRange( const int theViewId, double theRange[] )
1604 {
1605   if ( myVTKPrsMap.contains( theViewId ) )
1606   {
1607     // For the given viewer id update all VTK presentations ...
1608     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
1609     HYDROGUI_VTKPrs* aShape;
1610     for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
1611     {
1612       aShape = aViewShapes.at( i );
1613       if ( aShape && aShape->needScalarBar() )
1614       {
1615         aShape->setZRange( theRange );
1616       }
1617     }
1618   }
1619   // ... and update the global color legend scalar bar.
1620   getVTKDisplayer()->SetZRange( theViewId, theRange );
1621 }
1622 /////////////////// END OF VTKPrs PROCESSING
1623
1624 void HYDROGUI_Module::clearCache()
1625 {
1626     DEBTRACE("HYDROGUI_Module::clearCache");
1627     myObjectStateMap.clear();
1628 }
1629
1630 CAM_DataModel* HYDROGUI_Module::createDataModel()
1631 {
1632   DEBTRACE("HYDROGUI_Module::createDataModel");
1633   return new HYDROGUI_DataModel( this );
1634 }
1635
1636 void HYDROGUI_Module::customEvent( QEvent* e )
1637 {
1638   int aType = e->type();
1639   if ( aType == NewViewEvent )
1640   {
1641     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
1642     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
1643     {
1644       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
1645       {
1646         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
1647         ViewManagerRole aRole = getViewManagerRole( aViewManager );
1648
1649         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
1650         {
1651           if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1652             aViewPort->scale( 1, -1 ); // invert the Y axis direction from down to up
1653
1654           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
1655           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
1656           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
1657
1658           //ouv: temporarily commented
1659           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
1660         }
1661
1662         if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
1663           update( UF_Viewer );
1664
1665         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
1666       }
1667     }
1668   }
1669 }
1670
1671 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
1672 {
1673   QEvent::Type aType = theEvent->type();
1674   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
1675   {
1676     if( aType == QEvent::Show )
1677     {
1678       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
1679       e->setData( theObj );
1680       QApplication::postEvent( this, e );
1681       theObj->removeEventFilter( this );
1682     }
1683   }
1684   else if ( theObj->inherits( "OCCViewer_ViewPort" ) )
1685   {
1686     if( aType == QEvent::Leave )
1687     {
1688       SUIT_Desktop* aDesktop = getApp()->desktop();
1689       if ( aDesktop && aDesktop->statusBar() ) {
1690         aDesktop->statusBar()->clearMessage();
1691       }
1692     }
1693   }
1694   else if ( theObj->inherits( "SVTK_ViewWindow" ) )
1695   {
1696     if( aType == QEvent::Leave )
1697     {
1698       SUIT_Desktop* aDesktop = getApp()->desktop();
1699       if ( aDesktop && aDesktop->statusBar() ) {
1700         aDesktop->statusBar()->clearMessage();
1701       }
1702     }
1703   }
1704
1705   return LightApp_Module::eventFilter( theObj, theEvent );
1706 }
1707
1708 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
1709 {
1710   DEBTRACE("HYDROGUI_Module::onViewManagerAdded");
1711   LightApp_Module::onViewManagerAdded( theViewManager );
1712
1713   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
1714   { 
1715     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1716              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1717   }
1718   else if( theViewManager->getType() == OCCViewer_Viewer::Type() )
1719   {
1720     OCCViewer_ViewManager* mgr = dynamic_cast<OCCViewer_ViewManager*>( theViewManager );
1721     //mgr->setChainedOperations( true );
1722
1723     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1724              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1725     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1726              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1727     connect( theViewManager, SIGNAL( activated( SUIT_ViewManager* ) ), 
1728              this, SLOT( onViewActivated( SUIT_ViewManager* ) ) );
1729   }
1730   else if( theViewManager->getType() == SVTK_Viewer::Type() )
1731   {
1732     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
1733              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
1734     connect( theViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
1735              this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
1736   }
1737
1738   createSelector( theViewManager ); // replace the default selector
1739
1740   ViewManagerInfo anInfo( theViewManager, VMR_General );
1741   myViewManagerMap.insert( ViewManagerId++, anInfo );
1742 }
1743
1744 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
1745 {
1746   DEBTRACE("HYDROGUI_Module::onViewManagerRemoved");
1747   LightApp_Module::onViewManagerRemoved( theViewManager );
1748
1749   createSelector( theViewManager ); // replace the default selector
1750
1751   int anId = getViewManagerId( theViewManager );
1752   if( anId != -1 )
1753   {
1754     OCCViewer_ViewManager* anOCCViewManager =
1755       ::qobject_cast<OCCViewer_ViewManager*>( myViewManagerMap[ anId ].first );
1756     if ( anOCCViewManager )
1757     {
1758       OCCViewer_Viewer* anOCCViewer = anOCCViewManager->getOCCViewer();
1759       if ( anOCCViewer ) {
1760         int aViewerId = (size_t)anOCCViewer;
1761         removeViewShapes( aViewerId );
1762         setLandCoversScalarMapModeOff( aViewerId );
1763       }
1764     }
1765
1766     if ( getVTKDisplayer()->IsApplicable( theViewManager ) )
1767     {
1768       SVTK_Viewer* aVTKViewer = getVTKViewer( anId );
1769       if ( aVTKViewer )
1770       {
1771         getVTKDisplayer()->EraseScalarBar( anId, true );
1772         removeViewShapes( (size_t)aVTKViewer );
1773       }
1774     }
1775
1776     myViewManagerMap.remove( anId );
1777   }
1778 }
1779
1780 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
1781 {
1782   DEBTRACE("HYDROGUI_Module::onViewCreated");
1783   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
1784   {
1785     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
1786     {
1787       aViewFrame->installEventFilter( this );
1788
1789       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
1790       aViewPort->setInteractionFlag( GraphicsView_ViewPort::GlobalWheelScaling );
1791
1792       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
1793                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
1794     }
1795   }
1796   else if( theViewWindow && theViewWindow->inherits( "OCCViewer_ViewFrame" ) )
1797   {
1798     if( OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theViewWindow ) )
1799     {
1800       aViewFrame->onTopView();
1801
1802       HYDROGUI_Tool::setOCCActionShown( aViewFrame, OCCViewer_ViewWindow::MaximizedId, false );
1803       OCCViewer_ViewPort3d* aViewPort = aViewFrame->getViewPort();
1804       if ( aViewPort ) {
1805         aViewPort->installEventFilter( this );
1806       }
1807     }
1808   }
1809   else if( theViewWindow && theViewWindow->inherits( "SVTK_ViewWindow" ) )
1810   {
1811     if( SVTK_ViewWindow* aViewFrame = dynamic_cast<SVTK_ViewWindow*>( theViewWindow ) )
1812     {
1813       aViewFrame->installEventFilter( this );
1814     }
1815   }
1816 }
1817
1818 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
1819 {
1820   /* ouv: currently unused
1821   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
1822   {
1823     SUIT_ViewManager* aViewManager = 0;
1824
1825     QObject* aParent = aViewPort;
1826     while( aParent = aParent->parent() )
1827     {
1828       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
1829       {
1830         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
1831         {
1832           aViewManager = aViewer->getViewManager();
1833           break;
1834         }
1835       }
1836     }
1837
1838     if( !aViewManager )
1839       return;
1840
1841     double aMouseX = theEvent->scenePos().x();
1842     double aMouseY = theEvent->scenePos().y();
1843
1844     ViewManagerRole aRole = getViewManagerRole( aViewManager );
1845     if( aRole == VMR_General )
1846     {
1847       int aXDeg = 0, aYDeg = 0;
1848       int aXMin = 0, aYMin = 0;
1849       double aXSec = 0, aYSec = 0;
1850       HYDROData_Lambert93::secToDMS( aMouseX, aXDeg, aXMin, aXSec );
1851       HYDROData_Lambert93::secToDMS( aMouseY, aYDeg, aYMin, aYSec );
1852
1853       QString aDegSymbol( QChar( 0x00B0 ) );
1854       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
1855       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
1856
1857       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
1858     }
1859     else if( aRole == VMR_TransformImage )
1860       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
1861   }
1862   */
1863 }
1864
1865 void HYDROGUI_Module::updateViewer( HYDROGUI_AbstractDisplayer* theDisplayer, 
1866                                     const bool theIsInit, 
1867                                     const bool theIsForced, 
1868                                     const bool theDoFitAll )
1869 {
1870   DEBTRACE("HYDROGUI_Module::updateViewer");
1871   QList<int> aViewManagerIdList;
1872
1873   // currently, all views are updated
1874   ViewManagerMapIterator anIter( myViewManagerMap );
1875   while( anIter.hasNext() )
1876   { 
1877     SUIT_ViewManager* aViewManager = anIter.next().value().first;
1878
1879     if ( theDisplayer->IsApplicable( aViewManager ) )
1880     {
1881       int anId = anIter.key();
1882       aViewManagerIdList.append( anId );
1883     }
1884   }
1885
1886   QListIterator<int> anIdIter( aViewManagerIdList );
1887   while( anIdIter.hasNext() )
1888   {
1889     theDisplayer->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
1890     myOverview->setTopView();
1891   }
1892 }
1893
1894 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
1895 {
1896   DEBTRACE("HYDROGUI_Module::createSelector");
1897   if( !theViewManager )
1898     return;
1899
1900   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1901   if( !aSelectionMgr )
1902     return;
1903
1904   QString aViewType = theViewManager->getType();
1905   if( aViewType != GraphicsView_Viewer::Type() &&
1906       aViewType != OCCViewer_Viewer::Type())
1907     return;
1908
1909   QList<SUIT_Selector*> aSelectorList;
1910   aSelectionMgr->selectors( aViewType, aSelectorList );
1911
1912   // disable all alien selectors
1913   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
1914   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
1915   {
1916     SUIT_Selector* aSelector = *anIter;
1917     if( aSelector && ( !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) &&
1918                        !dynamic_cast<SVTK_Selector*>( aSelector ) &&
1919                        !dynamic_cast<HYDROGUI_OCCSelector*>( aSelector ) ) )
1920       aSelector->setEnabled( false );
1921   }
1922
1923   if ( aViewType == GraphicsView_Viewer::Type() )
1924   {
1925     GraphicsView_ViewManager* aViewManager =
1926       ::qobject_cast<GraphicsView_ViewManager*>( theViewManager );
1927     if( aViewManager )
1928       new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
1929   }
1930   else if ( aViewType == OCCViewer_Viewer::Type() )
1931   {
1932     OCCViewer_ViewManager* aViewManager =
1933       ::qobject_cast<OCCViewer_ViewManager*>( theViewManager );
1934     if( aViewManager )
1935       new HYDROGUI_OCCSelector( this, aViewManager->getOCCViewer(), aSelectionMgr );
1936   }
1937 }
1938
1939 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
1940 {
1941   DEBTRACE("HYDROGUI_Module::setUpdateEnabled");
1942   bool aPrevState = myIsUpdateEnabled;
1943   myIsUpdateEnabled = theState;
1944   return aPrevState;
1945 }
1946
1947 bool HYDROGUI_Module::isUpdateEnabled() const
1948 {
1949   return myIsUpdateEnabled;
1950 }
1951
1952 QStringList HYDROGUI_Module::storeSelection() const
1953 {
1954   DEBTRACE("HYDROGUI_Module::storeSelection");
1955   QStringList anEntryList;
1956   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1957   {
1958     SUIT_DataOwnerPtrList aList( true );
1959     aSelectionMgr->selected( aList );
1960
1961     SUIT_DataOwnerPtrList::iterator anIter;
1962     for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
1963     {
1964       const LightApp_DataOwner* anOwner = 
1965         dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
1966       if( anOwner )
1967         anEntryList.append( anOwner->entry() );
1968     }
1969   }
1970   return anEntryList;
1971 }
1972
1973 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
1974 {
1975   DEBTRACE("HYDROGUI_Module::restoreSelection");
1976   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1977   {
1978     SUIT_DataOwnerPtrList aList( true );
1979     for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
1980       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
1981     aSelectionMgr->setSelected( aList );
1982   }
1983 }
1984
1985 void HYDROGUI_Module::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* )
1986 {
1987   double X, Y, Z;
1988   bool doShow = false;
1989   HYDROGUI_Displayer* aDisplayer = getDisplayer();
1990   if ( aDisplayer )
1991     aDisplayer->SaveCursorViewPosition( theViewWindow );
1992     doShow = aDisplayer->GetCursorViewCoordinates( theViewWindow, X, Y, Z );
1993
1994   if ( doShow )
1995   {
1996     // Show the coordinates in the status bar
1997     SUIT_Desktop* aDesktop = getApp()->desktop();
1998     if ( aDesktop && aDesktop->statusBar() )
1999     {
2000       gp_Pnt aWPnt( X, Y, Z );
2001       HYDROData_Document::Document()->Transform( aWPnt, false );
2002       double WX = aWPnt.X(), WY = aWPnt.Y();
2003
2004       QString aXStr = HYDROGUI_Tool::GetCoordinateString( X, true );
2005       QString anYStr = HYDROGUI_Tool::GetCoordinateString( Y, true );
2006       QString aWXStr = HYDROGUI_Tool::GetCoordinateString( WX, true );
2007       QString aWYStr = HYDROGUI_Tool::GetCoordinateString( WY, true );
2008       QString aMsg = tr( "COORDINATES_INFO" );
2009       aMsg = aMsg.arg( aXStr ).arg( anYStr ).arg( aWXStr ).arg( aWYStr );
2010       aDesktop->statusBar()->showMessage( aMsg );
2011     }
2012   }
2013 }
2014
2015 /**
2016  * Returns stack of active operations;
2017  */
2018 QStack<HYDROGUI_Operation*>& HYDROGUI_Module::getActiveOperations()
2019 {
2020   DEBTRACE("HYDROGUI_Module::getActiveOperations");
2021   return myActiveOperationMap;
2022 }
2023
2024 /**
2025  * Returns the module active operation. If the active operation is show/hide,
2026  * the method returns the previous operation if it is.
2027  */
2028 HYDROGUI_Operation* HYDROGUI_Module::activeOperation()
2029 {
2030   HYDROGUI_Operation* anOp = !myActiveOperationMap.empty() ? myActiveOperationMap.top() : 0;
2031
2032   if ( dynamic_cast<HYDROGUI_ShowHideOp*>( anOp ) )
2033   {
2034     QVectorIterator<HYDROGUI_Operation*> aVIt( myActiveOperationMap );
2035     aVIt.toBack();
2036     aVIt.previous(); // skip the top show/hide operation
2037     anOp = aVIt.hasPrevious() ? aVIt.previous() : 0;
2038   }
2039
2040   return anOp;
2041 }
2042
2043 /*!
2044  * \brief Virtual public slot
2045  *
2046  * This method is called after the object inserted into data view to update their visibility state
2047  * This is default implementation
2048  */
2049 void HYDROGUI_Module::onObjectClicked( SUIT_DataObject* theObject, int theColumn )
2050 {
2051   if ( !isActiveModule() )
2052       return;
2053
2054   HYDROGUI_DataObject* hydroObject = dynamic_cast<HYDROGUI_DataObject*>( theObject );
2055
2056   // change visibility of object
2057   if ( !hydroObject || theColumn != SUIT_DataObject::VisibilityId )
2058       return;
2059
2060   SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
2061
2062   QString id = theObject->text( theObject->customData( Qtx::IdType ).toInt() );
2063   Qtx::VisibilityState visState = treeModel->visibilityState( id );
2064   if ( visState == Qtx::UnpresentableState )
2065       return;
2066
2067   visState = visState == Qtx::ShownState ? Qtx::HiddenState : Qtx::ShownState;
2068   treeModel->setVisibilityState( id, visState );
2069
2070   bool vis = visState == Qtx::ShownState;
2071   if ( vis == isObjectVisible( HYDROGUI_Tool::GetActiveViewId( this ), hydroObject->modelObject() ) )
2072       return;
2073
2074   setObjectVisible( HYDROGUI_Tool::GetActiveViewId( this ), hydroObject->modelObject(), vis );
2075
2076   update( UF_OCCViewer | UF_VTKViewer | ( visState == Qtx::ShownState ? UF_FitAll : 0 ) );
2077 }
2078
2079 bool HYDROGUI_Module::isDraggable( const SUIT_DataObject* what ) const
2080 {
2081   return true;
2082 }
2083
2084 bool HYDROGUI_Module::isDropAccepted( const SUIT_DataObject* where ) const
2085 {
2086
2087   return true;
2088 }
2089
2090 void HYDROGUI_Module::dropObjects( const DataObjectList& what, SUIT_DataObject* where,
2091                                    const int row, Qt::DropAction action )
2092 {
2093   if ( action != Qt::CopyAction && action != Qt::MoveAction )
2094     return; 
2095
2096   if (row == -1)
2097     return;
2098
2099   if (where->level() < 2 )
2100     return;  
2101
2102   DataObjectList::ConstIterator it = what.constBegin();
2103   for (;it != what.constEnd();++it) 
2104   {
2105     if ((*it)->parent() != where)
2106       return;
2107   }
2108
2109   it = what.constBegin();
2110
2111   int i=0;
2112   for (;it != what.constEnd();++it) 
2113   {
2114     SUIT_DataObject* objWhat = *it;
2115
2116     DataObjectList objInSect = where->children();
2117
2118     //std::list<SUIT_DataObject*> t1 = where->children().toStdList(); //debug
2119     int ind = objInSect.indexOf(objWhat);
2120     if (ind != -1)
2121     {
2122       HYDROGUI_DataModel* aModel = getDataModel();
2123       int pos = -1;
2124       if (ind >= row)
2125       {
2126         pos = row + i;
2127         i++;
2128       }
2129       else
2130         pos = row - 1;
2131       where->moveChildPos(objWhat, pos);
2132       //std::list<SUIT_DataObject*> t2 = where->children().toStdList(); //debug
2133     }  
2134   }
2135
2136   getApp()->updateObjectBrowser(true);
2137   
2138 }
2139
2140 Handle(HYDROData_StricklerTable) HYDROGUI_Module::getLandCoverColoringTable( const int theViewId ) const
2141 {
2142   Handle(HYDROData_StricklerTable) aTable;
2143
2144   if ( myLandCoverColoringMap.contains( theViewId ) ) {
2145     aTable = myLandCoverColoringMap.value( theViewId );
2146   }
2147
2148   return aTable;
2149 }
2150
2151 void HYDROGUI_Module::setLandCoverColoringTable( const int theViewId,
2152                                                  const Handle(HYDROData_StricklerTable)& theTable )
2153 {
2154   if ( !theTable.IsNull() ) {
2155     myLandCoverColoringMap.insert( theViewId, theTable );
2156   }
2157 }
2158
2159 void HYDROGUI_Module::setLandCoversScalarMapModeOff( const int theViewId )
2160 {
2161   myLandCoverColoringMap.remove( theViewId );
2162 }
2163
2164 bool HYDROGUI_Module::isLandCoversScalarMapModeOn( const int theViewId ) const
2165 {
2166   return myLandCoverColoringMap.contains( theViewId );
2167 }
2168
2169 void HYDROGUI_Module::setObjectRemoved( const Handle(HYDROData_Entity)& theObject )
2170 {
2171   if ( theObject.IsNull() || !theObject->IsRemoved() ) {
2172     return;
2173   }
2174
2175   if ( theObject->GetKind() == KIND_STRICKLER_TABLE ) {
2176     Handle(HYDROData_StricklerTable) aTable = 
2177       Handle(HYDROData_StricklerTable)::DownCast( theObject );
2178     QList<int> aViewIds;
2179     QMutableMapIterator<int, Handle(HYDROData_StricklerTable)> anIter( myLandCoverColoringMap );
2180     while ( anIter.hasNext() ) {
2181       if ( HYDROGUI_DataObject::dataObjectEntry( anIter.next().value() ) == 
2182            HYDROGUI_DataObject::dataObjectEntry( aTable ) ) {
2183         anIter.remove();
2184       }
2185     }
2186   }
2187 }
2188
2189 void HYDROGUI_Module::onViewActivated( SUIT_ViewManager* theMgr )
2190 {
2191   DEBTRACE("HYDROGUI_Module::onViewActivated");
2192   if( !theMgr )
2193     return;
2194
2195   SUIT_ViewWindow* wnd = theMgr->getActiveView();
2196   OCCViewer_ViewFrame* occwnd = dynamic_cast<OCCViewer_ViewFrame*>( wnd );
2197   if( !occwnd )
2198     return;
2199
2200   myOverview->setMainView( occwnd );
2201 }
2202
2203 void HYDROGUI_Module::setAutoZoomToAllViewManagers(bool bAutoZoom)
2204 {
2205   ViewManagerList aViewManagers = getApp()->viewManagers();
2206   foreach (SUIT_ViewManager* aVMgr, aViewManagers)
2207     setAutoZoom(aVMgr, bAutoZoom);
2208 }
2209
2210 void HYDROGUI_Module::setAutoZoom(SUIT_ViewManager* aVMgr, bool bAutoZoom)
2211 {
2212   if (!aVMgr)
2213     return;
2214   QVector<SUIT_ViewWindow*> aViews = aVMgr->getViews();
2215   foreach (SUIT_ViewWindow* aView, aViews)
2216   {
2217     if (aView)
2218     {
2219       OCCViewer_ViewFrame* anOCCViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aView );
2220       if (anOCCViewFrame)
2221       {
2222         anOCCViewFrame->setAutomaticZoom(bAutoZoom);
2223         for (int i = OCCViewer_ViewFrame::MAIN_VIEW; i<=OCCViewer_ViewFrame::TOP_RIGHT; i++)
2224         {
2225           OCCViewer_ViewWindow* aV = anOCCViewFrame->getView(i);
2226           if (aV)
2227             aV->setAutomaticZoom(bAutoZoom);
2228         }
2229       }
2230       OCCViewer_ViewWindow* anOCCViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( aView );
2231       if (anOCCViewWindow)
2232         anOCCViewWindow->setAutomaticZoom(bAutoZoom);
2233     }
2234   }
2235 }
2236