Salome HOME
db43680470e477aaf2a6aea29d3ba5ae2f71195c
[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               flags & UF_FitAll );
403
404   if( ( flags & UF_OCCViewer ) )
405     updateOCCViewer( flags & UF_OCC_Init,
406                      flags & UF_OCC_Forced,
407                      flags & UF_FitAll );
408
409   if( ( flags & UF_Model ) && getDataModel() && getApp() )
410   {
411     getDataModel()->update( getStudyId() );
412
413     // Temporary workaround to prevent breaking
414     // the selection in the object browser.
415     // Note: processEvents() should be called after updateGV(),
416     // otherwise the application crashes from time to time.
417     //RKV: qApp->processEvents(); 
418     getApp()->updateObjectBrowser( true );
419   }
420
421   // Object browser is currently updated by using UF_Model flag
422   //if( ( flags & UF_ObjBrowser ) && getApp() )
423   //  getApp()->updateObjectBrowser( true );
424
425   if( ( flags & UF_Controls ) && getApp() )
426     getApp()->updateActions();
427
428   // restore selected objects
429   restoreSelection( aSelectedEntries );
430
431   setUpdateEnabled( true );
432
433   QApplication::restoreOverrideCursor();
434 }
435
436 void HYDROGUI_Module::updateCommandsStatus()
437 {
438   LightApp_Module::updateCommandsStatus();
439
440   updateUndoRedoControls();
441
442   action( CopyId )->setEnabled( getDataModel()->canCopy() );
443   action( PasteId )->setEnabled( getDataModel()->canPaste() );
444 }
445
446 void HYDROGUI_Module::selectionChanged()
447 {
448   LightApp_Module::selectionChanged();
449   updateCommandsStatus();
450 }
451
452 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
453 {
454   return (HYDROGUI_DataModel*)dataModel();
455 }
456
457 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
458 {
459   return myDisplayer;
460 }
461
462 HYDROGUI_OCCDisplayer* HYDROGUI_Module::getOCCDisplayer() const
463 {
464   return myOCCDisplayer;
465 }
466
467 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
468 {
469   if( myViewManagerMap.contains( theId ) )
470   {
471     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
472     GraphicsView_ViewManager* aViewManager =
473       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
474     if( aViewManager )
475       return aViewManager->getViewer();
476   }
477   return NULL;
478 }
479
480 OCCViewer_Viewer* HYDROGUI_Module::getOCCViewer( const int theId ) const
481 {
482   if( myViewManagerMap.contains( theId ) )
483   {
484     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
485     OCCViewer_ViewManager* aViewManager =
486       ::qobject_cast<OCCViewer_ViewManager*>( anInfo.first );
487     if( aViewManager )
488       return aViewManager->getOCCViewer();
489   }
490   return NULL;
491 }
492
493 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
494 {
495   ViewManagerMapIterator anIter( myViewManagerMap );
496   while( anIter.hasNext() )
497   {
498     int anId = anIter.next().key();
499     const ViewManagerInfo& anInfo = anIter.value();
500     if( anInfo.first == theViewManager )
501       return anId;
502   }
503   return -1;
504 }
505
506 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
507 {
508   int anId = getViewManagerId( theViewManager );
509   if( anId != -1 )
510   {
511     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
512     return anInfo.second;
513   }
514   return VMR_Unknown;
515 }
516
517 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
518                                           const ViewManagerRole theRole )
519 {
520   int anId = getViewManagerId( theViewManager );
521   if( anId != -1 )
522   {
523     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
524     anInfo.second = theRole;
525   }
526 }
527
528 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
529                                        const Handle(HYDROData_Entity)& theObject ) const
530 {
531   if( theObject.IsNull() )
532     return false;
533
534   ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
535   if( anIter1 != myObjectStateMap.end() )
536   {
537     const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
538     Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
539     if( anIter2 != aName2ObjectStateMap.end() )
540     {
541       const ObjectState& anObjectState = anIter2.value();
542       return anObjectState.Visibility;
543     }
544   }
545   return false;
546 }
547
548 void HYDROGUI_Module::setObjectVisible( const int theViewId,
549                                         const Handle(HYDROData_Entity)& theObject,
550                                         const bool theState )
551 {
552   if( !theObject.IsNull() )
553   {
554     Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
555     ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
556     anObjectState.Visibility = theState;
557   }
558 }
559
560 HYDROGUI_Shape* HYDROGUI_Module::getObjectShape( const int                       theViewId,
561                                                  const Handle(HYDROData_Entity)& theObject ) const
562 {
563   HYDROGUI_Shape* aResShape = NULL;
564   if( theObject.IsNull() )
565     return aResShape;
566
567   if ( myShapesMap.contains( theViewId ) )
568   {
569     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
570     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
571     {
572       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
573         continue;
574
575       aResShape = aShape;
576       break;
577     }
578   }
579
580   return aResShape;
581 }
582
583 void HYDROGUI_Module::setObjectShape( const int                       theViewId,
584                                       const Handle(HYDROData_Entity)& theObject,
585                                       HYDROGUI_Shape*                 theShape )
586 {
587   if( theObject.IsNull() )
588     return;
589
590   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
591   aViewShapes.append( theShape );
592 }
593
594 void HYDROGUI_Module::removeObjectShape( const int                       theViewId,
595                                          const Handle(HYDROData_Entity)& theObject )
596 {
597   if ( !myShapesMap.contains( theViewId ) )
598     return;
599
600   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
601   Handle(HYDROData_Entity) anObject;
602   for ( int i = 0; i < aViewShapes.length(); )
603   {
604     HYDROGUI_Shape* aShape = aViewShapes.at( i );
605     anObject = aShape->getObject();
606     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
607     {
608       delete aShape;
609       aViewShapes.removeAt( i );
610       continue;
611     }
612
613     ++i;
614   }
615 }
616
617 void HYDROGUI_Module::removeViewShapes( const int theViewId )
618 {
619   if ( !myShapesMap.contains( theViewId ) )
620     return;
621
622   const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
623   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
624   {
625     HYDROGUI_Shape* aShape = aViewShapes.at( i );
626     if ( aShape )
627       delete aShape;
628   }
629
630   myShapesMap.remove( theViewId );
631 }
632
633 CAM_DataModel* HYDROGUI_Module::createDataModel()
634 {
635   return new HYDROGUI_DataModel( this );
636 }
637
638 void HYDROGUI_Module::customEvent( QEvent* e )
639 {
640   int aType = e->type();
641   if ( aType == NewViewEvent )
642   {
643     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
644     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
645     {
646       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
647       {
648         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
649         ViewManagerRole aRole = getViewManagerRole( aViewManager );
650
651         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
652         {
653           if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
654             aViewPort->scale( 1, -1 ); // invert the Y axis direction from down to up
655
656           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
657           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
658           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
659
660           //ouv: temporarily commented
661           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
662         }
663
664         if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
665           update( UF_Viewer );
666
667         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
668       }
669     }
670   }
671 }
672
673 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
674 {
675   QEvent::Type aType = theEvent->type();
676   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
677   {
678     if( aType == QEvent::Show )
679     {
680       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
681       e->setData( theObj );
682       QApplication::postEvent( this, e );
683       theObj->removeEventFilter( this );
684     }
685   }
686   return LightApp_Module::eventFilter( theObj, theEvent );
687 }
688
689 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
690 {
691   LightApp_Module::onViewManagerAdded( theViewManager );
692
693   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
694   { 
695     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
696              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
697   }
698   else if( theViewManager->getType() == OCCViewer_Viewer::Type() )
699   {
700     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
701              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
702   }
703
704   createSelector( theViewManager ); // replace the default selector
705
706   ViewManagerInfo anInfo( theViewManager, VMR_General );
707   myViewManagerMap.insert( ViewManagerId++, anInfo );
708 }
709
710 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
711 {
712   LightApp_Module::onViewManagerRemoved( theViewManager );
713
714   createSelector( theViewManager ); // replace the default selector
715
716   int anId = getViewManagerId( theViewManager );
717   if( anId != -1 )
718   {
719     OCCViewer_ViewManager* anOCCViewManager =
720       ::qobject_cast<OCCViewer_ViewManager*>( myViewManagerMap[ anId ].first );
721     if ( anOCCViewManager )
722     {
723       OCCViewer_Viewer* anOCCViewer = anOCCViewManager->getOCCViewer();
724       if ( anOCCViewer )
725         removeViewShapes( (size_t)anOCCViewer );
726     }
727
728     myViewManagerMap.remove( anId );
729   }
730 }
731
732 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
733 {
734   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
735   {
736     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
737     {
738       aViewFrame->installEventFilter( this );
739
740       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
741
742       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
743                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
744     }
745   }
746   else if( theViewWindow && theViewWindow->inherits( "OCCViewer_ViewFrame" ) )
747   {
748     if( OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theViewWindow ) )
749     {
750       aViewFrame->onTopView();
751     }
752   }
753 }
754
755 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
756 {
757   /* ouv: currently unused
758   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
759   {
760     SUIT_ViewManager* aViewManager = 0;
761
762     QObject* aParent = aViewPort;
763     while( aParent = aParent->parent() )
764     {
765       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
766       {
767         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
768         {
769           aViewManager = aViewer->getViewManager();
770           break;
771         }
772       }
773     }
774
775     if( !aViewManager )
776       return;
777
778     double aMouseX = theEvent->scenePos().x();
779     double aMouseY = theEvent->scenePos().y();
780
781     ViewManagerRole aRole = getViewManagerRole( aViewManager );
782     if( aRole == VMR_General )
783     {
784       int aXDeg = 0, aYDeg = 0;
785       int aXMin = 0, aYMin = 0;
786       double aXSec = 0, aYSec = 0;
787       HYDROData_Lambert93::secToDMS( aMouseX, aXDeg, aXMin, aXSec );
788       HYDROData_Lambert93::secToDMS( aMouseY, aYDeg, aYMin, aYSec );
789
790       QString aDegSymbol( QChar( 0x00B0 ) );
791       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
792       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
793
794       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
795     }
796     else if( aRole == VMR_TransformImage )
797       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
798   }
799   */
800 }
801
802 void HYDROGUI_Module::updateGV( const bool theIsInit,
803                                 const bool theIsForced,
804                                 const bool theDoFitAll )
805 {
806   if( !getDisplayer() )
807     return;
808
809   QList<int> aViewManagerIdList;
810
811   // currently, all views are updated
812   ViewManagerMapIterator anIter( myViewManagerMap );
813   while( anIter.hasNext() )
814   { 
815     GraphicsView_ViewManager* aViewManager =
816       dynamic_cast<GraphicsView_ViewManager*>( anIter.next().value().first );
817     if ( !aViewManager )
818       continue;
819
820     int anId = anIter.key();
821     aViewManagerIdList.append( anId );
822   }
823
824   QListIterator<int> anIdIter( aViewManagerIdList );
825   while( anIdIter.hasNext() )
826     getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
827 }
828
829 void HYDROGUI_Module::updateOCCViewer( const bool theIsInit,
830                                        const bool theIsForced,
831                                        const bool theDoFitAll )
832 {
833   HYDROGUI_OCCDisplayer* anOCCDisplayer = getOCCDisplayer();
834   if( !anOCCDisplayer )
835     return;
836
837   QList<int> aViewManagerIdList;
838
839   // currently, all views are updated
840   ViewManagerMapIterator anIter( myViewManagerMap );
841   while( anIter.hasNext() )
842   { 
843     OCCViewer_ViewManager* aViewManager =
844       ::qobject_cast<OCCViewer_ViewManager*>( anIter.next().value().first );
845     if ( !aViewManager )
846       continue;
847
848     int anId = anIter.key();
849     aViewManagerIdList.append( anId );
850   }
851
852   QListIterator<int> anIdIter( aViewManagerIdList );
853   while( anIdIter.hasNext() )
854     anOCCDisplayer->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
855 }
856
857 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
858 {
859   if( !theViewManager )
860     return;
861
862   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
863   if( !aSelectionMgr )
864     return;
865
866   QString aViewType = theViewManager->getType();
867   if( aViewType != GraphicsView_Viewer::Type() &&
868       aViewType != OCCViewer_Viewer::Type())
869     return;
870
871   QList<SUIT_Selector*> aSelectorList;
872   aSelectionMgr->selectors( aViewType, aSelectorList );
873
874   // disable all alien selectors
875   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
876   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
877   {
878     SUIT_Selector* aSelector = *anIter;
879     if( aSelector && ( !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) &&
880                        !dynamic_cast<HYDROGUI_OCCSelector*>( aSelector ) ) )
881       aSelector->setEnabled( false );
882   }
883
884   if ( aViewType == GraphicsView_Viewer::Type() )
885   {
886     GraphicsView_ViewManager* aViewManager =
887       ::qobject_cast<GraphicsView_ViewManager*>( theViewManager );
888     if( aViewManager )
889       new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
890   }
891   else if ( aViewType == OCCViewer_Viewer::Type() )
892   {
893     OCCViewer_ViewManager* aViewManager =
894       ::qobject_cast<OCCViewer_ViewManager*>( theViewManager );
895     if( aViewManager )
896       new HYDROGUI_OCCSelector( this, aViewManager->getOCCViewer(), aSelectionMgr );
897   }
898 }
899
900 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
901 {
902   bool aPrevState = myIsUpdateEnabled;
903   myIsUpdateEnabled = theState;
904   return aPrevState;
905 }
906
907 bool HYDROGUI_Module::isUpdateEnabled() const
908 {
909   return myIsUpdateEnabled;
910 }
911
912 QStringList HYDROGUI_Module::storeSelection() const
913 {
914   QStringList anEntryList;
915   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
916   {
917     SUIT_DataOwnerPtrList aList( true );
918     aSelectionMgr->selected( aList );
919
920     SUIT_DataOwnerPtrList::iterator anIter;
921     for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
922     {
923       const LightApp_DataOwner* anOwner = 
924         dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
925       if( anOwner )
926         anEntryList.append( anOwner->entry() );
927     }
928   }
929   return anEntryList;
930 }
931
932 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
933 {
934   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
935   {
936     SUIT_DataOwnerPtrList aList( true );
937     for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
938       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
939     aSelectionMgr->setSelected( aList );
940   }
941 }