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