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