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