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