Salome HOME
0733dcdc5917ab3dd56e088c349e1bb65578c9fa
[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
39 #include <HYDROData_Image.h>
40 #include <HYDROData_Lambert93.h>
41
42 #include <HYDROData_OperationsFactory.h>
43
44 #include <GraphicsView_ViewFrame.h>
45 #include <GraphicsView_ViewManager.h>
46 #include <GraphicsView_ViewPort.h>
47 #include <GraphicsView_Viewer.h>
48
49 #include <ImageComposer_CutOperator.h>
50 #include <ImageComposer_CropOperator.h>
51 #include <ImageComposer_FuseOperator.h>
52
53 #include <LightApp_Application.h>
54 #include <LightApp_DataOwner.h>
55 #include <LightApp_GVSelector.h>
56 #include <LightApp_SelectionMgr.h>
57 #include <LightApp_UpdateFlags.h>
58
59 #include <OCCViewer_ViewFrame.h>
60 #include <OCCViewer_ViewManager.h>
61 #include <OCCViewer_ViewModel.h>
62
63 #include <SALOME_Event.h>
64
65 #include <SUIT_DataBrowser.h>
66 #include <SUIT_Desktop.h>
67 #include <SUIT_Study.h>
68 #include <SUIT_ViewManager.h>
69
70 #include <QAction>
71 #include <QApplication>
72 #include <QGraphicsSceneMouseEvent>
73 #include <QMenu>
74
75 static int ViewManagerId = 0;
76
77 extern "C" HYDRO_EXPORT CAM_Module* createModule()
78 {
79   return new HYDROGUI_Module();
80 }
81
82 extern "C" HYDRO_EXPORT char* getModuleVersion()
83 {
84   return (char*)HYDRO_VERSION;
85 }
86
87 HYDROGUI_Module::HYDROGUI_Module()
88 : LightApp_Module( "HYDRO" ),
89   myDisplayer( 0 ),
90   myOCCDisplayer( 0 ),
91   myIsUpdateEnabled( true )
92 {
93 }
94
95 HYDROGUI_Module::~HYDROGUI_Module()
96 {
97 }
98
99 int HYDROGUI_Module::getStudyId() const
100 {
101   LightApp_Application* anApp = getApp();
102   return anApp ? anApp->activeStudy()->id() : 0;
103 }
104
105 void HYDROGUI_Module::initialize( CAM_Application* theApp )
106 {
107   LightApp_Module::initialize( theApp );
108
109   createActions();
110   createUndoRedoActions();
111   createMenus();
112   createPopups();
113   createToolbars();
114
115   setMenuShown( false );
116   setToolShown( false );
117
118   myDisplayer = new HYDROGUI_Displayer( this );
119   myOCCDisplayer = new HYDROGUI_OCCDisplayer( this );
120 }
121
122 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
123 {
124   bool aRes = LightApp_Module::activateModule( theStudy );
125
126   LightApp_Application* anApp = getApp();
127   SUIT_Desktop* aDesktop = anApp->desktop();
128
129   getApp()->setEditEnabled( false ); // hide SalomeApp copy/paste actions
130
131   setMenuShown( true );
132   setToolShown( true );
133
134 #ifndef DISABLE_PYCONSOLE
135   aDesktop->tabifyDockWidget( HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_PyConsole ) ), 
136                               HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_LogWindow ) ) );
137 #endif
138
139   update( UF_All );
140
141   updateCommandsStatus();
142
143   connect( anApp, SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ), 
144            this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
145
146   return aRes;
147 }
148
149 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
150 {
151   ViewManagerMapIterator anIter( myViewManagerMap );
152   while( anIter.hasNext() )
153     if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
154       getApp()->removeViewManager( aViewManager );
155   myViewManagerMap.clear();
156
157   myObjectStateMap.clear();
158   myShapesMap.clear();
159
160   // clear the data model's list of copying objects
161   HYDROGUI_DataModel::changeCopyingObjects( HYDROData_SequenceOfObjects() );
162
163   setMenuShown( false );
164   setToolShown( false );
165
166   getApp()->setEditEnabled( true ); // show SalomeApp copy/paste actions
167
168   disconnect( getApp(), SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ), 
169               this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
170
171   return LightApp_Module::deactivateModule( theStudy );
172 }
173
174 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
175 {
176   theMap.clear();
177   theMap.insert( LightApp_Application::WT_LogWindow,     Qt::BottomDockWidgetArea );
178 #ifndef DISABLE_PYCONSOLE
179   theMap.insert( LightApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
180 #endif
181   theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea   );
182 }
183
184 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
185 {
186   theTypesList << OCCViewer_Viewer::Type() << GraphicsView_Viewer::Type();
187 }
188
189 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
190                                         QMenu* theMenu,
191                                         QString& theTitle )
192 {
193   HYDROGUI_DataModel* aModel = getDataModel();
194
195   bool anIsObjectBrowser = theClient == getApp()->objectBrowser()->popupClientType();
196   bool anIsGraphicsView = theClient == GraphicsView_Viewer::Type();
197   bool anIsOCCView = theClient == OCCViewer_Viewer::Type();
198   if( !anIsObjectBrowser && !anIsGraphicsView && !anIsOCCView )
199     return;
200
201   size_t anActiveViewId = HYDROGUI_Tool::GetActiveViewId( this );
202
203   bool anIsSelectedDataObjects = false;
204   bool anIsVisibleInSelection = false;
205   bool anIsHiddenInSelection = false;
206
207   bool anIsImage = false;
208   bool anIsImportedImage = false;
209   bool anIsImageHasRefs = false;
210   bool anIsFusedImage = false;
211   bool anIsCutImage = false;
212   bool anIsSplittedImage = false;
213   bool anIsMustBeUpdatedImage = false;
214   bool anIsPolyline = false;
215   bool anIsCalculation = false;
216   bool anIsImmersibleZone = false;
217   bool anIsVisualState = false;
218   bool anIsRegion = false;
219   bool anIsZone = false;
220   bool anIsObstacle = false;
221
222   // check the selected GEOM objects
223   if ( !HYDROGUI_Tool::GetSelectedGeomObjects( this ).isEmpty() ) {
224     theMenu->addAction( action( ImportGeomObjectId ) );
225     theMenu->addSeparator();
226   }
227
228   // check the selected data model objects
229   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
230   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
231   {
232     Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
233     if( !anObject.IsNull() )
234     {
235       anIsSelectedDataObjects = true;
236
237       bool aVisibility = isObjectVisible( anActiveViewId, anObject );
238       anIsVisibleInSelection |= aVisibility;
239       anIsHiddenInSelection |= !aVisibility;
240
241       if( anObject->GetKind() == KIND_IMAGE )
242       {
243         anIsImage = true;
244         Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
245         if( !anImage.IsNull() )
246         {
247           anIsImportedImage = anImage->HasLocalPoints() && !anImage->IsSelfSplitted();
248           anIsImageHasRefs = anImage->HasReferences();
249           if( HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory() )
250           {
251             if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
252             {
253               QString anOperatorName = anOperator->name();
254               if( anOperatorName == ImageComposer_FuseOperator::Type() )
255                 anIsFusedImage = true;
256               else if( anOperatorName == ImageComposer_CutOperator::Type() )
257                 anIsCutImage = true;
258               else if( anOperatorName == ImageComposer_CropOperator::Type() )
259                 anIsSplittedImage = true;
260             }
261           }
262           if ( anImage->IsMustBeUpdated() )
263           {
264             anIsMustBeUpdatedImage = true;
265           }
266         }
267       }
268       else if( anObject->GetKind() == KIND_POLYLINE )
269         anIsPolyline = true;
270       else if( anObject->GetKind() == KIND_CALCULATION )
271         anIsCalculation = true;
272       else if( anObject->GetKind() == KIND_IMMERSIBLE_ZONE )
273         anIsImmersibleZone = true;
274       else if( anObject->GetKind() == KIND_VISUAL_STATE )
275         anIsVisualState = true;
276       else if( anObject->GetKind() == KIND_REGION )
277         anIsRegion = true;
278       else if( anObject->GetKind() == KIND_ZONE )
279         anIsZone = true;
280       else if( anObject->GetKind() == KIND_OBSTACLE )
281         anIsObstacle = true;
282     }
283   }
284
285   // check the selected partitions
286   if( !anIsSelectedDataObjects && anIsObjectBrowser )
287   {
288     ObjectKind aSelectedPartition = HYDROGUI_Tool::GetSelectedPartition( this );
289     if( aSelectedPartition != KIND_UNKNOWN )
290     {
291       switch( aSelectedPartition )
292       {
293         case KIND_IMAGE:
294           theMenu->addAction( action( ImportImageId ) );
295           break;
296         case KIND_POLYLINE:
297           theMenu->addAction( action( CreatePolylineId ) );
298           break;
299         case KIND_VISUAL_STATE:
300           theMenu->addAction( action( SaveVisualStateId ) );
301           break;
302         case KIND_BATHYMETRY:
303           theMenu->addAction( action( ImportBathymetryId ) );
304           break;
305         case KIND_CALCULATION:
306           theMenu->addAction( action( CreateCalculationId ) );
307           break;
308         case KIND_IMMERSIBLE_ZONE:
309           theMenu->addAction( action( CreateImmersibleZoneId ) );
310           break;
311         case KIND_OBSTACLE:
312           theMenu->addAction( action( ImportObstacleFromFileId ) );
313           theMenu->addAction( action( CreateBoxId ) );
314           theMenu->addAction( action( CreateCylinderId ) );
315           break;
316       }
317       theMenu->addSeparator();
318     }
319   }
320
321   if( anIsSelectedDataObjects )
322   {
323     if ( anIsMustBeUpdatedImage )
324     {
325       theMenu->addAction( action( UpdateImageId ) );
326       theMenu->addSeparator();
327     }
328
329     if( aSeq.Length() == 1 )
330     {
331       if( anIsImage )
332       {
333         if( anIsImportedImage )
334           theMenu->addAction( action( EditImportedImageId ) );
335         else if( anIsImageHasRefs )
336         {
337           if( anIsFusedImage )
338             theMenu->addAction( action( EditFusedImageId ) );
339           else if( anIsCutImage )
340             theMenu->addAction( action( EditCutImageId ) );
341           else if( anIsSplittedImage )
342             theMenu->addAction( action( EditSplittedImageId ) );
343         }
344
345         theMenu->addAction( action( ObserveImageId ) );
346         theMenu->addAction( action( ExportImageId ) );
347         theMenu->addSeparator();
348
349         if( anIsImageHasRefs )
350         {
351           theMenu->addAction( action( RemoveImageRefsId ) );
352           theMenu->addSeparator();
353         }
354
355         theMenu->addAction( action( FuseImagesId ) );
356         theMenu->addAction( action( CutImagesId ) );
357         theMenu->addAction( action( SplitImageId ) );
358         theMenu->addSeparator();
359       }
360       else if( anIsPolyline )
361       {
362         theMenu->addAction( action( EditPolylineId ) );
363         theMenu->addSeparator();
364       }
365       else if( anIsCalculation )
366       {
367         theMenu->addAction( action( EditCalculationId ) );
368         theMenu->addAction( action( ExportCalculationId ) );
369         theMenu->addSeparator();
370       }
371       else if( anIsImmersibleZone )
372       {
373         theMenu->addAction( action( EditImmersibleZoneId ) );
374         theMenu->addSeparator();
375       }
376       else if( anIsVisualState && anIsObjectBrowser )
377       {
378         theMenu->addAction( action( SaveVisualStateId ) );
379         theMenu->addAction( action( LoadVisualStateId ) );
380         theMenu->addSeparator();
381       }
382     }
383
384     theMenu->addAction( action( DeleteId ) );
385     theMenu->addSeparator();
386
387     if( anIsImage || anIsPolyline || anIsImmersibleZone || anIsZone || anIsRegion || anIsObstacle )
388     {
389       if( anIsHiddenInSelection )
390         theMenu->addAction( action( ShowId ) );
391       theMenu->addAction( action( ShowOnlyId ) );
392       if( anIsVisibleInSelection )
393         theMenu->addAction( action( HideId ) );
394       theMenu->addSeparator();
395     }
396   }
397
398   if( anIsObjectBrowser || anIsGraphicsView || anIsOCCView )
399   {
400     theMenu->addAction( action( ShowAllId ) );
401     theMenu->addAction( action( HideAllId ) );
402     theMenu->addSeparator();
403   }
404 }
405
406 void HYDROGUI_Module::update( const int flags )
407 {
408   if( !isUpdateEnabled() )
409     return;
410
411   QApplication::setOverrideCursor( Qt::WaitCursor );
412
413   // To prevent calling this method recursively
414   // from one of the methods called below
415   setUpdateEnabled( false );
416
417   // store selected objects
418   QStringList aSelectedEntries = storeSelection();
419
420   if( ( flags & UF_Viewer ) )
421     updateGV( flags & UF_GV_Init,
422               flags & UF_GV_Forced,
423               flags & UF_FitAll );
424
425   if( ( flags & UF_OCCViewer ) )
426     updateOCCViewer( flags & UF_OCC_Init,
427                      flags & UF_OCC_Forced,
428                      flags & UF_FitAll );
429
430   if( ( flags & UF_Model ) && getDataModel() && getApp() )
431   {
432     getDataModel()->update( getStudyId() );
433
434     // Temporary workaround to prevent breaking
435     // the selection in the object browser.
436     // Note: processEvents() should be called after updateGV(),
437     // otherwise the application crashes from time to time.
438     //RKV: qApp->processEvents(); 
439     getApp()->updateObjectBrowser( true );
440   }
441
442   // Object browser is currently updated by using UF_Model flag
443   if( ( flags & UF_ObjBrowser ) && ((flags & UF_Model) == 0) && getApp() )
444     getApp()->updateObjectBrowser( true );
445
446   if( ( flags & UF_Controls ) && getApp() )
447     getApp()->updateActions();
448
449   // restore selected objects
450   restoreSelection( aSelectedEntries );
451
452   setUpdateEnabled( true );
453
454   QApplication::restoreOverrideCursor();
455 }
456
457 void HYDROGUI_Module::updateCommandsStatus()
458 {
459   LightApp_Module::updateCommandsStatus();
460
461   updateUndoRedoControls();
462
463   action( CopyId )->setEnabled( getDataModel()->canCopy() );
464   action( PasteId )->setEnabled( getDataModel()->canPaste() );
465 }
466
467 void HYDROGUI_Module::selectionChanged()
468 {
469   LightApp_Module::selectionChanged();
470   updateCommandsStatus();
471 }
472
473 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
474 {
475   return (HYDROGUI_DataModel*)dataModel();
476 }
477
478 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
479 {
480   return myDisplayer;
481 }
482
483 HYDROGUI_OCCDisplayer* HYDROGUI_Module::getOCCDisplayer() const
484 {
485   return myOCCDisplayer;
486 }
487
488 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
489 {
490   if( myViewManagerMap.contains( theId ) )
491   {
492     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
493     GraphicsView_ViewManager* aViewManager =
494       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
495     if( aViewManager )
496       return aViewManager->getViewer();
497   }
498   return NULL;
499 }
500
501 OCCViewer_Viewer* HYDROGUI_Module::getOCCViewer( const int theId ) const
502 {
503   if( myViewManagerMap.contains( theId ) )
504   {
505     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
506     OCCViewer_ViewManager* aViewManager =
507       ::qobject_cast<OCCViewer_ViewManager*>( anInfo.first );
508     if( aViewManager )
509       return aViewManager->getOCCViewer();
510   }
511   return NULL;
512 }
513
514 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
515 {
516   ViewManagerMapIterator anIter( myViewManagerMap );
517   while( anIter.hasNext() )
518   {
519     int anId = anIter.next().key();
520     const ViewManagerInfo& anInfo = anIter.value();
521     if( anInfo.first == theViewManager )
522       return anId;
523   }
524   return -1;
525 }
526
527 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
528 {
529   int anId = getViewManagerId( theViewManager );
530   if( anId != -1 )
531   {
532     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
533     return anInfo.second;
534   }
535   return VMR_Unknown;
536 }
537
538 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
539                                           const ViewManagerRole theRole )
540 {
541   int anId = getViewManagerId( theViewManager );
542   if( anId != -1 )
543   {
544     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
545     anInfo.second = theRole;
546   }
547 }
548
549 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
550                                        const Handle(HYDROData_Entity)& theObject ) const
551 {
552   if( theObject.IsNull() )
553     return false;
554
555   ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
556   if( anIter1 != myObjectStateMap.end() )
557   {
558     const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
559     Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
560     if( anIter2 != aName2ObjectStateMap.end() )
561     {
562       const ObjectState& anObjectState = anIter2.value();
563       return anObjectState.Visibility;
564     }
565   }
566   return false;
567 }
568
569 void HYDROGUI_Module::setObjectVisible( const int theViewId,
570                                         const Handle(HYDROData_Entity)& theObject,
571                                         const bool theState )
572 {
573   if( !theObject.IsNull() )
574   {
575     Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
576     ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
577     anObjectState.Visibility = theState;
578   }
579 }
580
581 HYDROGUI_Shape* HYDROGUI_Module::getObjectShape( const int                       theViewId,
582                                                  const Handle(HYDROData_Entity)& theObject ) const
583 {
584   HYDROGUI_Shape* aResShape = NULL;
585   if( theObject.IsNull() )
586     return aResShape;
587
588   if ( myShapesMap.contains( theViewId ) )
589   {
590     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
591     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
592     {
593       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
594         continue;
595
596       aResShape = aShape;
597       break;
598     }
599   }
600
601   return aResShape;
602 }
603
604 void HYDROGUI_Module::setObjectShape( const int                       theViewId,
605                                       const Handle(HYDROData_Entity)& theObject,
606                                       HYDROGUI_Shape*                 theShape )
607 {
608   if( theObject.IsNull() )
609     return;
610
611   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
612   aViewShapes.append( theShape );
613 }
614
615 void HYDROGUI_Module::removeObjectShape( const int                       theViewId,
616                                          const Handle(HYDROData_Entity)& theObject )
617 {
618   if ( !myShapesMap.contains( theViewId ) )
619     return;
620
621   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
622   Handle(HYDROData_Entity) anObject;
623   for ( int i = 0; i < aViewShapes.length(); )
624   {
625     HYDROGUI_Shape* aShape = aViewShapes.at( i );
626     anObject = aShape->getObject();
627     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
628     {
629       delete aShape;
630       aViewShapes.removeAt( i );
631       continue;
632     }
633
634     ++i;
635   }
636 }
637
638 void HYDROGUI_Module::removeViewShapes( const int theViewId )
639 {
640   if ( !myShapesMap.contains( theViewId ) )
641     return;
642
643   const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
644   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
645   {
646     HYDROGUI_Shape* aShape = aViewShapes.at( i );
647     if ( aShape )
648       delete aShape;
649   }
650
651   myShapesMap.remove( theViewId );
652 }
653
654 CAM_DataModel* HYDROGUI_Module::createDataModel()
655 {
656   return new HYDROGUI_DataModel( this );
657 }
658
659 void HYDROGUI_Module::customEvent( QEvent* e )
660 {
661   int aType = e->type();
662   if ( aType == NewViewEvent )
663   {
664     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
665     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
666     {
667       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
668       {
669         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
670         ViewManagerRole aRole = getViewManagerRole( aViewManager );
671
672         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
673         {
674           if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
675             aViewPort->scale( 1, -1 ); // invert the Y axis direction from down to up
676
677           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
678           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
679           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
680
681           //ouv: temporarily commented
682           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
683         }
684
685         if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
686           update( UF_Viewer );
687
688         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
689       }
690     }
691   }
692 }
693
694 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
695 {
696   QEvent::Type aType = theEvent->type();
697   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
698   {
699     if( aType == QEvent::Show )
700     {
701       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
702       e->setData( theObj );
703       QApplication::postEvent( this, e );
704       theObj->removeEventFilter( this );
705     }
706   }
707   return LightApp_Module::eventFilter( theObj, theEvent );
708 }
709
710 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
711 {
712   LightApp_Module::onViewManagerAdded( theViewManager );
713
714   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
715   { 
716     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
717              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
718   }
719   else if( theViewManager->getType() == OCCViewer_Viewer::Type() )
720   {
721     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
722              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
723   }
724
725   createSelector( theViewManager ); // replace the default selector
726
727   ViewManagerInfo anInfo( theViewManager, VMR_General );
728   myViewManagerMap.insert( ViewManagerId++, anInfo );
729 }
730
731 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
732 {
733   LightApp_Module::onViewManagerRemoved( theViewManager );
734
735   createSelector( theViewManager ); // replace the default selector
736
737   int anId = getViewManagerId( theViewManager );
738   if( anId != -1 )
739   {
740     OCCViewer_ViewManager* anOCCViewManager =
741       ::qobject_cast<OCCViewer_ViewManager*>( myViewManagerMap[ anId ].first );
742     if ( anOCCViewManager )
743     {
744       OCCViewer_Viewer* anOCCViewer = anOCCViewManager->getOCCViewer();
745       if ( anOCCViewer )
746         removeViewShapes( (size_t)anOCCViewer );
747     }
748
749     myViewManagerMap.remove( anId );
750   }
751 }
752
753 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
754 {
755   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
756   {
757     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
758     {
759       aViewFrame->installEventFilter( this );
760
761       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
762
763       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
764                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
765     }
766   }
767   else if( theViewWindow && theViewWindow->inherits( "OCCViewer_ViewFrame" ) )
768   {
769     if( OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theViewWindow ) )
770     {
771       aViewFrame->onTopView();
772     }
773   }
774 }
775
776 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
777 {
778   /* ouv: currently unused
779   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
780   {
781     SUIT_ViewManager* aViewManager = 0;
782
783     QObject* aParent = aViewPort;
784     while( aParent = aParent->parent() )
785     {
786       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
787       {
788         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
789         {
790           aViewManager = aViewer->getViewManager();
791           break;
792         }
793       }
794     }
795
796     if( !aViewManager )
797       return;
798
799     double aMouseX = theEvent->scenePos().x();
800     double aMouseY = theEvent->scenePos().y();
801
802     ViewManagerRole aRole = getViewManagerRole( aViewManager );
803     if( aRole == VMR_General )
804     {
805       int aXDeg = 0, aYDeg = 0;
806       int aXMin = 0, aYMin = 0;
807       double aXSec = 0, aYSec = 0;
808       HYDROData_Lambert93::secToDMS( aMouseX, aXDeg, aXMin, aXSec );
809       HYDROData_Lambert93::secToDMS( aMouseY, aYDeg, aYMin, aYSec );
810
811       QString aDegSymbol( QChar( 0x00B0 ) );
812       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
813       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
814
815       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
816     }
817     else if( aRole == VMR_TransformImage )
818       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
819   }
820   */
821 }
822
823 void HYDROGUI_Module::updateGV( const bool theIsInit,
824                                 const bool theIsForced,
825                                 const bool theDoFitAll )
826 {
827   if( !getDisplayer() )
828     return;
829
830   QList<int> aViewManagerIdList;
831
832   // currently, all views are updated
833   ViewManagerMapIterator anIter( myViewManagerMap );
834   while( anIter.hasNext() )
835   { 
836     GraphicsView_ViewManager* aViewManager =
837       dynamic_cast<GraphicsView_ViewManager*>( anIter.next().value().first );
838     if ( !aViewManager )
839       continue;
840
841     int anId = anIter.key();
842     aViewManagerIdList.append( anId );
843   }
844
845   QListIterator<int> anIdIter( aViewManagerIdList );
846   while( anIdIter.hasNext() )
847     getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
848 }
849
850 void HYDROGUI_Module::updateOCCViewer( const bool theIsInit,
851                                        const bool theIsForced,
852                                        const bool theDoFitAll )
853 {
854   HYDROGUI_OCCDisplayer* anOCCDisplayer = getOCCDisplayer();
855   if( !anOCCDisplayer )
856     return;
857
858   QList<int> aViewManagerIdList;
859
860   // currently, all views are updated
861   ViewManagerMapIterator anIter( myViewManagerMap );
862   while( anIter.hasNext() )
863   { 
864     OCCViewer_ViewManager* aViewManager =
865       ::qobject_cast<OCCViewer_ViewManager*>( anIter.next().value().first );
866     if ( !aViewManager )
867       continue;
868
869     int anId = anIter.key();
870     aViewManagerIdList.append( anId );
871   }
872
873   QListIterator<int> anIdIter( aViewManagerIdList );
874   while( anIdIter.hasNext() )
875     anOCCDisplayer->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
876 }
877
878 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
879 {
880   if( !theViewManager )
881     return;
882
883   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
884   if( !aSelectionMgr )
885     return;
886
887   QString aViewType = theViewManager->getType();
888   if( aViewType != GraphicsView_Viewer::Type() &&
889       aViewType != OCCViewer_Viewer::Type())
890     return;
891
892   QList<SUIT_Selector*> aSelectorList;
893   aSelectionMgr->selectors( aViewType, aSelectorList );
894
895   // disable all alien selectors
896   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
897   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
898   {
899     SUIT_Selector* aSelector = *anIter;
900     if( aSelector && ( !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) &&
901                        !dynamic_cast<HYDROGUI_OCCSelector*>( aSelector ) ) )
902       aSelector->setEnabled( false );
903   }
904
905   if ( aViewType == GraphicsView_Viewer::Type() )
906   {
907     GraphicsView_ViewManager* aViewManager =
908       ::qobject_cast<GraphicsView_ViewManager*>( theViewManager );
909     if( aViewManager )
910       new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
911   }
912   else if ( aViewType == OCCViewer_Viewer::Type() )
913   {
914     OCCViewer_ViewManager* aViewManager =
915       ::qobject_cast<OCCViewer_ViewManager*>( theViewManager );
916     if( aViewManager )
917       new HYDROGUI_OCCSelector( this, aViewManager->getOCCViewer(), aSelectionMgr );
918   }
919 }
920
921 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
922 {
923   bool aPrevState = myIsUpdateEnabled;
924   myIsUpdateEnabled = theState;
925   return aPrevState;
926 }
927
928 bool HYDROGUI_Module::isUpdateEnabled() const
929 {
930   return myIsUpdateEnabled;
931 }
932
933 QStringList HYDROGUI_Module::storeSelection() const
934 {
935   QStringList anEntryList;
936   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
937   {
938     SUIT_DataOwnerPtrList aList( true );
939     aSelectionMgr->selected( aList );
940
941     SUIT_DataOwnerPtrList::iterator anIter;
942     for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
943     {
944       const LightApp_DataOwner* anOwner = 
945         dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
946       if( anOwner )
947         anEntryList.append( anOwner->entry() );
948     }
949   }
950   return anEntryList;
951 }
952
953 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
954 {
955   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
956   {
957     SUIT_DataOwnerPtrList aList( true );
958     for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
959       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
960     aSelectionMgr->setSelected( aList );
961   }
962 }