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