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