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