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