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