Salome HOME
c92f41b4718c3bb54e845a6e2d3c41010daadaa4
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Module.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Module.h"
24
25 #include "HYDROGUI.h"
26 #include "HYDROGUI_DataModel.h"
27 #include "HYDROGUI_Displayer.h"
28 #include "HYDROGUI_GVSelector.h"
29 #include "HYDROGUI_InputPanel.h"
30 #include "HYDROGUI_ObjSelector.h"
31 #include "HYDROGUI_OCCDisplayer.h"
32 #include "HYDROGUI_OCCSelector.h"
33 #include "HYDROGUI_Operations.h"
34 #include "HYDROGUI_PrsImage.h"
35 #include "HYDROGUI_Tool.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
43 #include <HYDROData_Image.h>
44 #include <HYDROData_Lambert93.h>
45 #include <HYDROData_Tool.h>
46
47 #include <HYDROData_OperationsFactory.h>
48
49 #include <GraphicsView_ViewFrame.h>
50 #include <GraphicsView_ViewManager.h>
51 #include <GraphicsView_ViewPort.h>
52 #include <GraphicsView_Viewer.h>
53
54 #include <ImageComposer_CutOperator.h>
55 #include <ImageComposer_CropOperator.h>
56 #include <ImageComposer_FuseOperator.h>
57
58 #include <LightApp_Application.h>
59 #include <LightApp_DataOwner.h>
60 #include <LightApp_GVSelector.h>
61 #include <LightApp_SelectionMgr.h>
62 #include <LightApp_UpdateFlags.h>
63
64 #include <OCCViewer_ViewFrame.h>
65 #include <OCCViewer_ViewManager.h>
66 #include <OCCViewer_ViewModel.h>
67
68 #include <SALOME_Event.h>
69
70 #include <SUIT_DataBrowser.h>
71 #include <SUIT_Desktop.h>
72 #include <SUIT_Study.h>
73 #include <SUIT_ViewManager.h>
74
75 #include <SVTK_ViewManager.h>
76 #include <SVTK_ViewModel.h>
77 #include <SVTK_Selector.h>
78
79 #include <QAction>
80 #include <QApplication>
81 #include <QGraphicsSceneMouseEvent>
82 #include <QMenu>
83
84 static int ViewManagerId = 0;
85
86 extern "C" HYDRO_EXPORT CAM_Module* createModule()
87 {
88   return new HYDROGUI_Module();
89 }
90
91 extern "C" HYDRO_EXPORT char* getModuleVersion()
92 {
93   return (char*)HYDRO_VERSION;
94 }
95
96 HYDROGUI_Module::HYDROGUI_Module()
97 : LightApp_Module( "HYDRO" ),
98   myDisplayer( 0 ),
99   myOCCDisplayer( 0 ),
100   myIsUpdateEnabled( true )
101 {
102 }
103
104 HYDROGUI_Module::~HYDROGUI_Module()
105 {
106 }
107
108 int HYDROGUI_Module::getStudyId() const
109 {
110   LightApp_Application* anApp = getApp();
111   return anApp ? anApp->activeStudy()->id() : 0;
112 }
113
114 void HYDROGUI_Module::initialize( CAM_Application* theApp )
115 {
116   LightApp_Module::initialize( theApp );
117
118   createActions();
119   createUndoRedoActions();
120   createMenus();
121   createPopups();
122   createToolbars();
123
124   setMenuShown( false );
125   setToolShown( false );
126
127   myDisplayer = new HYDROGUI_Displayer( this );
128   myOCCDisplayer = new HYDROGUI_OCCDisplayer( this );
129   myVTKDisplayer = new HYDROGUI_VTKPrsDisplayer( this );
130 }
131
132 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
133 {
134   bool aRes = LightApp_Module::activateModule( theStudy );
135
136   LightApp_Application* anApp = getApp();
137   SUIT_Desktop* aDesktop = anApp->desktop();
138
139   getApp()->setEditEnabled( false ); // hide SalomeApp copy/paste actions
140
141   setMenuShown( true );
142   setToolShown( true );
143
144 #ifndef DISABLE_PYCONSOLE
145   aDesktop->tabifyDockWidget( HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_PyConsole ) ), 
146                               HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_LogWindow ) ) );
147 #endif
148
149   update( UF_All );
150
151   updateCommandsStatus();
152
153   connect( anApp, SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ), 
154            this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
155
156   return aRes;
157 }
158
159 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
160 {
161   ViewManagerMapIterator anIter( myViewManagerMap );
162   while( anIter.hasNext() )
163     if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
164       getApp()->removeViewManager( aViewManager );
165   myViewManagerMap.clear();
166
167   myObjectStateMap.clear();
168   myShapesMap.clear();
169   myVTKPrsMap.clear();
170
171   // clear the data model's list of copying objects
172   HYDROGUI_DataModel::changeCopyingObjects( HYDROData_SequenceOfObjects() );
173
174   setMenuShown( false );
175   setToolShown( false );
176
177   getApp()->setEditEnabled( true ); // show SalomeApp copy/paste actions
178
179   disconnect( getApp(), SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ), 
180               this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
181
182   return LightApp_Module::deactivateModule( theStudy );
183 }
184
185 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
186 {
187   theMap.clear();
188   theMap.insert( LightApp_Application::WT_LogWindow,     Qt::BottomDockWidgetArea );
189 #ifndef DISABLE_PYCONSOLE
190   theMap.insert( LightApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
191 #endif
192   theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea   );
193 }
194
195 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
196 {
197   theTypesList << OCCViewer_Viewer::Type() << GraphicsView_Viewer::Type();
198 }
199
200 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
201                                         QMenu* theMenu,
202                                         QString& theTitle )
203 {
204   HYDROGUI_DataModel* aModel = getDataModel();
205
206   bool anIsObjectBrowser = theClient == getApp()->objectBrowser()->popupClientType();
207   bool anIsGraphicsView = theClient == GraphicsView_Viewer::Type();
208   bool anIsOCCView = theClient == OCCViewer_Viewer::Type();
209   bool anIsVTKView = theClient == SVTK_Viewer::Type();
210   if( !anIsObjectBrowser && !anIsGraphicsView && !anIsOCCView && !anIsVTKView )
211     return;
212
213   size_t anActiveViewId = HYDROGUI_Tool::GetActiveViewId( this );
214
215   bool anIsSelectedDataObjects = false;
216   bool anIsVisibleInSelection = false;
217   bool anIsHiddenInSelection = false;
218
219   bool anIsImage = false;
220   bool anIsImportedImage = false;
221   bool anIsImageHasRefs = false;
222   bool anIsFusedImage = false;
223   bool anIsCutImage = false;
224   bool anIsSplittedImage = false;
225   bool anIsMustBeUpdatedImage = false;
226   bool anIsPolyline = false;
227   bool anIsProfile = false;
228   bool anAllAreProfiles = false;
229   bool anIsBathymetry = false;
230   bool anIsCalculation = false;
231   bool anIsImmersibleZone = false;
232   bool anIsVisualState = false;
233   bool anIsRegion = false;
234   bool anIsZone = false;
235   bool anIsObstacle = false;
236   bool anIsGeomObject = false;
237
238   // check the selected GEOM objects
239   if ( !HYDROGUI_Tool::GetSelectedGeomObjects( this ).isEmpty() ) {
240     theMenu->addAction( action( ImportGeomObjectId ) );
241     theMenu->addSeparator();
242   }
243
244   // check the selected data model objects
245   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
246   int aNbOfSelectedProfiles = 0;
247   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
248   {
249     Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
250     if( !anObject.IsNull() )
251     {
252       anIsSelectedDataObjects = true;
253
254       bool aVisibility = isObjectVisible( anActiveViewId, anObject );
255       anIsVisibleInSelection |= aVisibility;
256       anIsHiddenInSelection |= !aVisibility;
257
258       if( anObject->GetKind() == KIND_IMAGE )
259       {
260         anIsImage = true;
261         Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
262         if( !anImage.IsNull() )
263         {
264           anIsImportedImage = anImage->HasLocalPoints();
265           anIsImageHasRefs = anImage->HasReferences();
266           if( HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory() )
267           {
268             if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
269             {
270               QString anOperatorName = anOperator->name();
271               if( anOperatorName == ImageComposer_FuseOperator::Type() )
272                 anIsFusedImage = true;
273               else if( anOperatorName == ImageComposer_CutOperator::Type() )
274                 anIsCutImage = true;
275               else if( anOperatorName == ImageComposer_CropOperator::Type() )
276                 anIsSplittedImage = true;
277             }
278           }
279           if ( anImage->IsMustBeUpdated() )
280           {
281             anIsMustBeUpdatedImage = true;
282           }
283         }
284       }
285       else if( anObject->GetKind() == KIND_POLYLINEXY )
286         anIsPolyline = true;
287       else if( anObject->GetKind() == KIND_PROFILE ) {
288         anIsProfile = true;
289         aNbOfSelectedProfiles++;
290       }
291       else if( anObject->GetKind() == KIND_CALCULATION )
292         anIsCalculation = true;
293       else if( anObject->GetKind() == KIND_IMMERSIBLE_ZONE )
294         anIsImmersibleZone = true;
295       else if( anObject->GetKind() == KIND_VISUAL_STATE )
296         anIsVisualState = true;
297       else if( anObject->GetKind() == KIND_REGION )
298         anIsRegion = true;
299       else if( anObject->GetKind() == KIND_ZONE )
300         anIsZone = true;
301       else if( anObject->GetKind() == KIND_BATHYMETRY )
302         anIsBathymetry = true;
303       else if( anObject->GetKind() == KIND_OBSTACLE )
304         anIsObstacle = true;
305     }
306
307     anIsGeomObject = HYDROData_Tool::IsGeometryObject( anObject );
308   }
309
310   // Check if all selected objects are profiles
311   anAllAreProfiles = ( aNbOfSelectedProfiles > 0 ) &&
312                      ( aNbOfSelectedProfiles == aSeq.Length() );
313
314   // check the selected partitions
315   if( !anIsSelectedDataObjects && anIsObjectBrowser )
316   {
317     ObjectKind aSelectedPartition = HYDROGUI_Tool::GetSelectedPartition( this );
318     if( aSelectedPartition != KIND_UNKNOWN )
319     {
320       switch( aSelectedPartition )
321       {
322         case KIND_IMAGE:
323           theMenu->addAction( action( ImportImageId ) );
324           break;
325         case KIND_POLYLINEXY:
326           theMenu->addAction( action( CreatePolylineId ) );
327           break;
328         case KIND_PROFILE:
329           theMenu->addAction( action( CreateProfileId ) );
330           theMenu->addAction( action( ImportProfilesId ) );
331           theMenu->addAction( action( AllGeoreferencementId ) );
332           break;
333         case KIND_VISUAL_STATE:
334           theMenu->addAction( action( SaveVisualStateId ) );
335           break;
336         case KIND_BATHYMETRY:
337           theMenu->addAction( action( ImportBathymetryId ) );
338           break;
339         case KIND_CALCULATION:
340           theMenu->addAction( action( CreateCalculationId ) );
341           break;
342         case KIND_IMMERSIBLE_ZONE:
343           theMenu->addAction( action( CreateImmersibleZoneId ) );
344           break;
345         case KIND_OBSTACLE:
346           theMenu->addAction( action( ImportObstacleFromFileId ) );
347           theMenu->addAction( action( CreateBoxId ) );
348           theMenu->addAction( action( CreateCylinderId ) );
349           break;
350       }
351       theMenu->addSeparator();
352     }
353   }
354
355   if( anIsSelectedDataObjects )
356   {
357     if ( anIsMustBeUpdatedImage )
358     {
359       theMenu->addAction( action( UpdateImageId ) );
360       theMenu->addSeparator();
361     }
362
363     if( aSeq.Length() == 1 )
364     {
365       if( anIsImage )
366       {
367         if( anIsImportedImage )
368           theMenu->addAction( action( EditImportedImageId ) );
369         else if( anIsImageHasRefs )
370         {
371           if( anIsFusedImage )
372             theMenu->addAction( action( EditFusedImageId ) );
373           else if( anIsCutImage )
374             theMenu->addAction( action( EditCutImageId ) );
375           else if( anIsSplittedImage )
376             theMenu->addAction( action( EditSplittedImageId ) );
377         }
378
379         theMenu->addAction( action( ObserveImageId ) );
380         theMenu->addAction( action( ExportImageId ) );
381         theMenu->addSeparator();
382
383         if( anIsImageHasRefs )
384         {
385           theMenu->addAction( action( RemoveImageRefsId ) );
386           theMenu->addSeparator();
387         }
388
389         theMenu->addAction( action( FuseImagesId ) );
390         theMenu->addAction( action( CutImagesId ) );
391         theMenu->addAction( action( SplitImageId ) );
392         theMenu->addSeparator();
393       }
394       else if( anIsPolyline )
395       {
396         theMenu->addAction( action( EditPolylineId ) );
397         theMenu->addSeparator();
398       }
399       else if( anIsProfile )
400       {
401         theMenu->addAction( action( EditProfileId ) );
402         theMenu->addAction( action( SelectedGeoreferencementId ) );
403         theMenu->addSeparator();
404       }
405       else if( anIsCalculation )
406       {
407         theMenu->addAction( action( EditCalculationId ) );
408         theMenu->addAction( action( ExportCalculationId ) );
409         theMenu->addSeparator();
410       }
411       else if( anIsImmersibleZone )
412       {
413         theMenu->addAction( action( EditImmersibleZoneId ) );
414         theMenu->addSeparator();
415       }
416       else if( anIsVisualState && anIsObjectBrowser )
417       {
418         theMenu->addAction( action( SaveVisualStateId ) );
419         theMenu->addAction( action( LoadVisualStateId ) );
420         theMenu->addSeparator();
421       }
422
423       // Add set color action for geometrical objects
424       if ( anIsGeomObject )
425       {
426         theMenu->addAction( action( SetColorId ) );
427         theMenu->addSeparator();
428       }
429     }
430
431     if ( anAllAreProfiles ) {
432       theMenu->addAction( action( SelectedGeoreferencementId ) );
433       theMenu->addSeparator();
434     }
435
436     theMenu->addAction( action( DeleteId ) );
437     theMenu->addSeparator();
438
439     if( anIsImage || anIsPolyline || anIsImmersibleZone || anIsZone || anIsRegion || anIsBathymetry || anIsObstacle )
440     {
441       if( anIsHiddenInSelection )
442         theMenu->addAction( action( ShowId ) );
443       theMenu->addAction( action( ShowOnlyId ) );
444       if( anIsVisibleInSelection )
445         theMenu->addAction( action( HideId ) );
446       theMenu->addSeparator();
447     }
448   }
449
450   if ( anIsOCCView )
451   {
452     SUIT_Operation* anOp = application()->activeStudy()->activeOperation();
453     HYDROGUI_PolylineOp* aPolylineOp = dynamic_cast<HYDROGUI_PolylineOp*>( anOp );
454     if ( aPolylineOp && aPolylineOp->deleteEnabled() )
455       theMenu->addAction( action( DeleteId ) );
456   }
457
458   if( anIsObjectBrowser || anIsGraphicsView || anIsOCCView || anIsVTKView )
459   {
460     theMenu->addAction( action( ShowAllId ) );
461     theMenu->addAction( action( HideAllId ) );
462     theMenu->addSeparator();
463   }
464 }
465
466 void HYDROGUI_Module::update( const int flags )
467 {
468   if( !isUpdateEnabled() )
469     return;
470
471   QApplication::setOverrideCursor( Qt::WaitCursor );
472
473   // To prevent calling this method recursively
474   // from one of the methods called below
475   setUpdateEnabled( false );
476
477   // store selected objects
478   QStringList aSelectedEntries = storeSelection();
479
480   bool aDoFitAll = flags & UF_FitAll;
481   if( ( flags & UF_Viewer ) )
482     updateViewer( getDisplayer(), flags & UF_GV_Init, flags & UF_GV_Forced, aDoFitAll ); 
483
484   if( ( flags & UF_OCCViewer ) )
485     updateViewer( getOCCDisplayer(), flags & UF_OCC_Init, flags & UF_OCC_Forced, aDoFitAll ); 
486
487   if( ( flags & UF_VTKViewer ) )
488     updateViewer( getVTKDisplayer(), flags & UF_VTK_Init, flags & UF_VTK_Forced, aDoFitAll ); 
489
490   if( ( flags & UF_Model ) && getDataModel() && getApp() )
491   {
492     getDataModel()->update( getStudyId() );
493
494     // Temporary workaround to prevent breaking
495     // the selection in the object browser.
496     // Note: processEvents() should be called after updateGV(),
497     // otherwise the application crashes from time to time.
498     //RKV: qApp->processEvents(); 
499     getApp()->updateObjectBrowser( true );
500   }
501
502   // Object browser is currently updated by using UF_Model flag
503   if( ( flags & UF_ObjBrowser ) && ((flags & UF_Model) == 0) && getApp() )
504     getApp()->updateObjectBrowser( true );
505
506   if( ( flags & UF_Controls ) && getApp() )
507     getApp()->updateActions();
508
509   // restore selected objects
510   restoreSelection( aSelectedEntries );
511
512   setUpdateEnabled( true );
513
514   QApplication::restoreOverrideCursor();
515 }
516
517 void HYDROGUI_Module::updateCommandsStatus()
518 {
519   LightApp_Module::updateCommandsStatus();
520
521   updateUndoRedoControls();
522
523   action( CopyId )->setEnabled( getDataModel()->canCopy() );
524   action( PasteId )->setEnabled( getDataModel()->canPaste() );
525 }
526
527 void HYDROGUI_Module::selectionChanged()
528 {
529   LightApp_Module::selectionChanged();
530   updateCommandsStatus();
531 }
532
533 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
534 {
535   return (HYDROGUI_DataModel*)dataModel();
536 }
537
538 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
539 {
540   return myDisplayer;
541 }
542
543 HYDROGUI_OCCDisplayer* HYDROGUI_Module::getOCCDisplayer() const
544 {
545   return myOCCDisplayer;
546 }
547
548 HYDROGUI_VTKPrsDisplayer* HYDROGUI_Module::getVTKDisplayer() const
549 {
550   return myVTKDisplayer;
551 }
552
553 SUIT_ViewManager* HYDROGUI_Module::getViewManager( const int theId ) const
554 {
555   if( myViewManagerMap.contains( theId ) )
556   {
557     return myViewManagerMap[ theId ].first;
558   }
559   return NULL;
560 }
561
562 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
563 {
564   if( myViewManagerMap.contains( theId ) )
565   {
566     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
567     GraphicsView_ViewManager* aViewManager =
568       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
569     if( aViewManager )
570       return aViewManager->getViewer();
571   }
572   return NULL;
573 }
574
575 OCCViewer_Viewer* HYDROGUI_Module::getOCCViewer( const int theId ) const
576 {
577   if( myViewManagerMap.contains( theId ) )
578   {
579     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
580     OCCViewer_ViewManager* aViewManager =
581       ::qobject_cast<OCCViewer_ViewManager*>( anInfo.first );
582     if( aViewManager )
583       return aViewManager->getOCCViewer();
584   }
585   return NULL;
586 }
587
588 SVTK_Viewer* HYDROGUI_Module::getVTKViewer( const int theId ) const
589 {
590   if( myViewManagerMap.contains( theId ) )
591   {
592     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
593     SVTK_ViewManager* aViewManager =
594       ::qobject_cast<SVTK_ViewManager*>( anInfo.first );
595     if( aViewManager )
596       return dynamic_cast<SVTK_Viewer*>( aViewManager->getViewModel() );
597   }
598   return NULL;
599 }
600
601 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
602 {
603   ViewManagerMapIterator anIter( myViewManagerMap );
604   while( anIter.hasNext() )
605   {
606     int anId = anIter.next().key();
607     const ViewManagerInfo& anInfo = anIter.value();
608     if( anInfo.first == theViewManager )
609       return anId;
610   }
611   return -1;
612 }
613
614 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
615 {
616   int anId = getViewManagerId( theViewManager );
617   if( anId != -1 )
618   {
619     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
620     return anInfo.second;
621   }
622   return VMR_Unknown;
623 }
624
625 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
626                                           const ViewManagerRole theRole )
627 {
628   int anId = getViewManagerId( theViewManager );
629   if( anId != -1 )
630   {
631     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
632     anInfo.second = theRole;
633   }
634 }
635
636 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
637                                        const Handle(HYDROData_Entity)& theObject ) const
638 {
639   if( theObject.IsNull() )
640     return false;
641
642   ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
643   if( anIter1 != myObjectStateMap.end() )
644   {
645     const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
646     Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
647     if( anIter2 != aName2ObjectStateMap.end() )
648     {
649       const ObjectState& anObjectState = anIter2.value();
650       return anObjectState.Visibility;
651     }
652   }
653   return false;
654 }
655
656 void HYDROGUI_Module::setObjectVisible( const int theViewId,
657                                         const Handle(HYDROData_Entity)& theObject,
658                                         const bool theState )
659 {
660   if( !theObject.IsNull() )
661   {
662     Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
663     ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
664     anObjectState.Visibility = theState;
665   }
666 }
667
668 /////////////////// OCC SHAPES PROCESSING
669 HYDROGUI_Shape* HYDROGUI_Module::getObjectShape( const int                       theViewId,
670                                                  const Handle(HYDROData_Entity)& theObject ) const
671 {
672   HYDROGUI_Shape* aResShape = NULL;
673   if( theObject.IsNull() )
674     return aResShape;
675
676   if ( myShapesMap.contains( theViewId ) )
677   {
678     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
679     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
680     {
681       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
682         continue;
683
684       aResShape = aShape;
685       break;
686     }
687   }
688
689   return aResShape;
690 }
691
692 void HYDROGUI_Module::setObjectShape( const int                       theViewId,
693                                       const Handle(HYDROData_Entity)& theObject,
694                                       HYDROGUI_Shape*                 theShape )
695 {
696   if( theObject.IsNull() )
697     return;
698
699   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
700   aViewShapes.append( theShape );
701 }
702
703 void HYDROGUI_Module::removeObjectShape( const int                       theViewId,
704                                          const Handle(HYDROData_Entity)& theObject )
705 {
706   if ( !myShapesMap.contains( theViewId ) )
707     return;
708
709   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
710   Handle(HYDROData_Entity) anObject;
711   for ( int i = 0; i < aViewShapes.length(); )
712   {
713     HYDROGUI_Shape* aShape = aViewShapes.at( i );
714     anObject = aShape->getObject();
715     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
716     {
717       delete aShape;
718       aViewShapes.removeAt( i );
719       continue;
720     }
721
722     ++i;
723   }
724 }
725
726 void HYDROGUI_Module::removeViewShapes( const int theViewId )
727 {
728   if ( !myShapesMap.contains( theViewId ) )
729     return;
730
731   const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
732   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
733   {
734     HYDROGUI_Shape* aShape = aViewShapes.at( i );
735     if ( aShape )
736       delete aShape;
737   }
738
739   myShapesMap.remove( theViewId );
740 }
741 /////////////////// END OF OCC SHAPES PROCESSING
742
743 /////////////////// VTKPrs PROCESSING
744 HYDROGUI_VTKPrs* HYDROGUI_Module::getObjectVTKPrs( const int                       theViewId,
745                                                  const Handle(HYDROData_Entity)& theObject ) const
746 {
747   HYDROGUI_VTKPrs* aResShape = NULL;
748   if( theObject.IsNull() )
749     return aResShape;
750
751   if ( myVTKPrsMap.contains( theViewId ) )
752   {
753     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
754     foreach ( HYDROGUI_VTKPrs* aShape, aViewShapes )
755     {
756       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
757         continue;
758
759       aResShape = aShape;
760       break;
761     }
762   }
763
764   return aResShape;
765 }
766
767 void HYDROGUI_Module::setObjectVTKPrs( const int                       theViewId,
768                                        const Handle(HYDROData_Entity)& theObject,
769                                        HYDROGUI_VTKPrs*                 theShape )
770 {
771   if( theObject.IsNull() )
772     return;
773
774   // Compute the new global Z range from the added presentation and the old global Z range.
775   double* aGlobalRange = getVTKDisplayer()->GetZRange( theViewId );
776   double* aRange = theShape->getInternalZRange();
777   bool anIsUpdate = false;
778   if ( aRange[0] < aGlobalRange[0] )
779   {
780     aGlobalRange[0] = aRange[0];
781     anIsUpdate = true;
782   }
783   if ( aRange[1] > aGlobalRange[1] )
784   {
785     aGlobalRange[1] = aRange[1];
786     anIsUpdate = true;
787   }
788
789   //if ( anIsUpdate )
790   //{
791     updateVTKZRange( theViewId, aGlobalRange );
792   //}
793
794   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
795   aViewShapes.append( theShape );
796 }
797
798 void HYDROGUI_Module::removeObjectVTKPrs( const int                       theViewId,
799                                           const Handle(HYDROData_Entity)& theObject )
800 {
801   if ( !myVTKPrsMap.contains( theViewId ) )
802     return;
803
804   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
805   Handle(HYDROData_Entity) anObject;
806   for ( int i = 0; i < aViewShapes.length(); )
807   {
808     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
809     anObject = aShape->getObject();
810     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
811     {
812       delete aShape;
813       aViewShapes.removeAt( i );
814       continue;
815     }
816
817     ++i;
818   }
819
820   // Invalidate global Z range
821   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
822   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
823 }
824
825 void HYDROGUI_Module::removeViewVTKPrs( const int theViewId )
826 {
827   if ( !myVTKPrsMap.contains( theViewId ) )
828     return;
829
830   const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
831   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
832   {
833     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
834     if ( aShape )
835       delete aShape;
836   }
837
838   myVTKPrsMap.remove( theViewId );
839 }
840
841 void HYDROGUI_Module::updateVTKZRange( const int theViewId, double theRange[] )
842 {
843   if ( myVTKPrsMap.contains( theViewId ) )
844   {
845     // For the given viewer id update all VTK presentations ...
846     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
847     HYDROGUI_VTKPrs* aShape;
848     for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
849     {
850       aShape = aViewShapes.at( i );
851       if ( aShape )
852       {
853         aShape->setZRange( theRange );
854       }
855     }
856   }
857   // ... and update the global color legend scalar bar.
858   getVTKDisplayer()->SetZRange( theViewId, theRange );
859 }
860 /////////////////// END OF VTKPrs PROCESSING
861
862 CAM_DataModel* HYDROGUI_Module::createDataModel()
863 {
864   return new HYDROGUI_DataModel( this );
865 }
866
867 void HYDROGUI_Module::customEvent( QEvent* e )
868 {
869   int aType = e->type();
870   if ( aType == NewViewEvent )
871   {
872     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
873     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
874     {
875       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
876       {
877         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
878         ViewManagerRole aRole = getViewManagerRole( aViewManager );
879
880         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
881         {
882           if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
883             aViewPort->scale( 1, -1 ); // invert the Y axis direction from down to up
884
885           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
886           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
887           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
888
889           //ouv: temporarily commented
890           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
891         }
892
893         if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
894           update( UF_Viewer );
895
896         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
897       }
898     }
899   }
900 }
901
902 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
903 {
904   QEvent::Type aType = theEvent->type();
905   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
906   {
907     if( aType == QEvent::Show )
908     {
909       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
910       e->setData( theObj );
911       QApplication::postEvent( this, e );
912       theObj->removeEventFilter( this );
913     }
914   }
915   return LightApp_Module::eventFilter( theObj, theEvent );
916 }
917
918 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
919 {
920   LightApp_Module::onViewManagerAdded( theViewManager );
921
922   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
923   { 
924     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
925              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
926   }
927   else if( theViewManager->getType() == OCCViewer_Viewer::Type() )
928   {
929     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
930              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
931   }
932
933   createSelector( theViewManager ); // replace the default selector
934
935   ViewManagerInfo anInfo( theViewManager, VMR_General );
936   myViewManagerMap.insert( ViewManagerId++, anInfo );
937 }
938
939 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
940 {
941   LightApp_Module::onViewManagerRemoved( theViewManager );
942
943   createSelector( theViewManager ); // replace the default selector
944
945   int anId = getViewManagerId( theViewManager );
946   if( anId != -1 )
947   {
948     OCCViewer_ViewManager* anOCCViewManager =
949       ::qobject_cast<OCCViewer_ViewManager*>( myViewManagerMap[ anId ].first );
950     if ( anOCCViewManager )
951     {
952       OCCViewer_Viewer* anOCCViewer = anOCCViewManager->getOCCViewer();
953       if ( anOCCViewer )
954         removeViewShapes( (size_t)anOCCViewer );
955     }
956
957     if ( getVTKDisplayer()->IsApplicable( theViewManager ) )
958     {
959       SVTK_Viewer* aVTKViewer = getVTKViewer( anId );
960       if ( aVTKViewer )
961       {
962         getVTKDisplayer()->DeleteScalarBar( anId );
963         removeViewShapes( (size_t)aVTKViewer );
964       }
965     }
966
967     myViewManagerMap.remove( anId );
968   }
969 }
970
971 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
972 {
973   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
974   {
975     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
976     {
977       aViewFrame->installEventFilter( this );
978
979       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
980
981       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
982                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
983     }
984   }
985   else if( theViewWindow && theViewWindow->inherits( "OCCViewer_ViewFrame" ) )
986   {
987     if( OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theViewWindow ) )
988     {
989       aViewFrame->onTopView();
990     }
991   }
992 }
993
994 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
995 {
996   /* ouv: currently unused
997   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
998   {
999     SUIT_ViewManager* aViewManager = 0;
1000
1001     QObject* aParent = aViewPort;
1002     while( aParent = aParent->parent() )
1003     {
1004       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
1005       {
1006         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
1007         {
1008           aViewManager = aViewer->getViewManager();
1009           break;
1010         }
1011       }
1012     }
1013
1014     if( !aViewManager )
1015       return;
1016
1017     double aMouseX = theEvent->scenePos().x();
1018     double aMouseY = theEvent->scenePos().y();
1019
1020     ViewManagerRole aRole = getViewManagerRole( aViewManager );
1021     if( aRole == VMR_General )
1022     {
1023       int aXDeg = 0, aYDeg = 0;
1024       int aXMin = 0, aYMin = 0;
1025       double aXSec = 0, aYSec = 0;
1026       HYDROData_Lambert93::secToDMS( aMouseX, aXDeg, aXMin, aXSec );
1027       HYDROData_Lambert93::secToDMS( aMouseY, aYDeg, aYMin, aYSec );
1028
1029       QString aDegSymbol( QChar( 0x00B0 ) );
1030       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
1031       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
1032
1033       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
1034     }
1035     else if( aRole == VMR_TransformImage )
1036       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
1037   }
1038   */
1039 }
1040
1041 void HYDROGUI_Module::updateViewer( HYDROGUI_AbstractDisplayer* theDisplayer, 
1042                                     const bool theIsInit, 
1043                                     const bool theIsForced, 
1044                                     const bool theDoFitAll )
1045 {
1046   QList<int> aViewManagerIdList;
1047
1048   // currently, all views are updated
1049   ViewManagerMapIterator anIter( myViewManagerMap );
1050   while( anIter.hasNext() )
1051   { 
1052     SUIT_ViewManager* aViewManager = anIter.next().value().first;
1053
1054     if ( theDisplayer->IsApplicable( aViewManager ) )
1055     {
1056       int anId = anIter.key();
1057       aViewManagerIdList.append( anId );
1058     }
1059   }
1060
1061   QListIterator<int> anIdIter( aViewManagerIdList );
1062   while( anIdIter.hasNext() )
1063   {
1064     theDisplayer->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
1065   }
1066 }
1067
1068 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
1069 {
1070   if( !theViewManager )
1071     return;
1072
1073   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1074   if( !aSelectionMgr )
1075     return;
1076
1077   QString aViewType = theViewManager->getType();
1078   if( aViewType != GraphicsView_Viewer::Type() &&
1079       aViewType != OCCViewer_Viewer::Type())
1080     return;
1081
1082   QList<SUIT_Selector*> aSelectorList;
1083   aSelectionMgr->selectors( aViewType, aSelectorList );
1084
1085   // disable all alien selectors
1086   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
1087   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
1088   {
1089     SUIT_Selector* aSelector = *anIter;
1090     if( aSelector && ( !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) &&
1091                        !dynamic_cast<SVTK_Selector*>( aSelector ) &&
1092                        !dynamic_cast<HYDROGUI_OCCSelector*>( aSelector ) ) )
1093       aSelector->setEnabled( false );
1094   }
1095
1096   if ( aViewType == GraphicsView_Viewer::Type() )
1097   {
1098     GraphicsView_ViewManager* aViewManager =
1099       ::qobject_cast<GraphicsView_ViewManager*>( theViewManager );
1100     if( aViewManager )
1101       new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
1102   }
1103   else if ( aViewType == OCCViewer_Viewer::Type() )
1104   {
1105     OCCViewer_ViewManager* aViewManager =
1106       ::qobject_cast<OCCViewer_ViewManager*>( theViewManager );
1107     if( aViewManager )
1108       new HYDROGUI_OCCSelector( this, aViewManager->getOCCViewer(), aSelectionMgr );
1109   }
1110 }
1111
1112 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
1113 {
1114   bool aPrevState = myIsUpdateEnabled;
1115   myIsUpdateEnabled = theState;
1116   return aPrevState;
1117 }
1118
1119 bool HYDROGUI_Module::isUpdateEnabled() const
1120 {
1121   return myIsUpdateEnabled;
1122 }
1123
1124 QStringList HYDROGUI_Module::storeSelection() const
1125 {
1126   QStringList anEntryList;
1127   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1128   {
1129     SUIT_DataOwnerPtrList aList( true );
1130     aSelectionMgr->selected( aList );
1131
1132     SUIT_DataOwnerPtrList::iterator anIter;
1133     for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
1134     {
1135       const LightApp_DataOwner* anOwner = 
1136         dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
1137       if( anOwner )
1138         anEntryList.append( anOwner->entry() );
1139     }
1140   }
1141   return anEntryList;
1142 }
1143
1144 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
1145 {
1146   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1147   {
1148     SUIT_DataOwnerPtrList aList( true );
1149     for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
1150       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
1151     aSelectionMgr->setSelected( aList );
1152   }
1153 }