Salome HOME
bug #136 - problems with performance
[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 #include "HYDROGUI_VTKPrs.h"
39 #include "HYDROGUI_VTKPrsDisplayer.h"
40 #include "HYDROGUI_AbstractDisplayer.h"
41 #include "HYDROGUI_PolylineOp.h"
42
43 #include <HYDROData_Image.h>
44 #include <HYDROData_Profile.h>
45 #include <HYDROData_Lambert93.h>
46 #include <HYDROData_Tool.h>
47
48 #include <HYDROData_OperationsFactory.h>
49
50 #include <GraphicsView_ViewFrame.h>
51 #include <GraphicsView_ViewManager.h>
52 #include <GraphicsView_ViewPort.h>
53 #include <GraphicsView_Viewer.h>
54
55 #include <ImageComposer_CutOperator.h>
56 #include <ImageComposer_CropOperator.h>
57 #include <ImageComposer_FuseOperator.h>
58
59 #include <LightApp_Application.h>
60 #include <LightApp_DataOwner.h>
61 #include <LightApp_GVSelector.h>
62 #include <LightApp_SelectionMgr.h>
63 #include <LightApp_UpdateFlags.h>
64
65 #include <OCCViewer_ViewFrame.h>
66 #include <OCCViewer_ViewManager.h>
67 #include <OCCViewer_ViewModel.h>
68
69 #include <SALOME_Event.h>
70
71 #include <SUIT_DataBrowser.h>
72 #include <SUIT_Desktop.h>
73 #include <SUIT_Study.h>
74 #include <SUIT_ViewManager.h>
75
76 #include <SVTK_ViewManager.h>
77 #include <SVTK_ViewModel.h>
78 #include <SVTK_Selector.h>
79
80 #include <QAction>
81 #include <QApplication>
82 #include <QGraphicsSceneMouseEvent>
83 #include <QMenu>
84
85 static int ViewManagerId = 0;
86
87 extern "C" HYDRO_EXPORT CAM_Module* createModule()
88 {
89   return new HYDROGUI_Module();
90 }
91
92 extern "C" HYDRO_EXPORT char* getModuleVersion()
93 {
94   return (char*)HYDRO_VERSION;
95 }
96
97 HYDROGUI_Module::HYDROGUI_Module()
98 : LightApp_Module( "HYDRO" ),
99   myDisplayer( 0 ),
100   myOCCDisplayer( 0 ),
101   myIsUpdateEnabled( true )
102 {
103 }
104
105 HYDROGUI_Module::~HYDROGUI_Module()
106 {
107 }
108
109 int HYDROGUI_Module::getStudyId() const
110 {
111   LightApp_Application* anApp = getApp();
112   return anApp ? anApp->activeStudy()->id() : 0;
113 }
114
115 void HYDROGUI_Module::initialize( CAM_Application* theApp )
116 {
117   LightApp_Module::initialize( theApp );
118
119   createActions();
120   createUndoRedoActions();
121   createMenus();
122   createPopups();
123   createToolbars();
124
125   setMenuShown( false );
126   setToolShown( false );
127
128   myDisplayer = new HYDROGUI_Displayer( this );
129   myOCCDisplayer = new HYDROGUI_OCCDisplayer( this );
130   myVTKDisplayer = new HYDROGUI_VTKPrsDisplayer( this );
131 }
132
133 bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
134 {
135   bool aRes = LightApp_Module::activateModule( theStudy );
136
137   LightApp_Application* anApp = getApp();
138   SUIT_Desktop* aDesktop = anApp->desktop();
139
140   getApp()->setEditEnabled( false ); // hide SalomeApp copy/paste actions
141
142   setMenuShown( true );
143   setToolShown( true );
144
145 #ifndef DISABLE_PYCONSOLE
146   aDesktop->tabifyDockWidget( HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_PyConsole ) ), 
147                               HYDROGUI_Tool::WindowDock( anApp->getWindow( LightApp_Application::WT_LogWindow ) ) );
148 #endif
149
150   update( UF_All );
151
152   updateCommandsStatus();
153
154   connect( anApp, SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ), 
155            this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
156
157   HYDROGUI_Tool::setOCCActionShown( this, OCCViewer_ViewWindow::MaximizedId, false );
158
159   return aRes;
160 }
161
162 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
163 {
164   ViewManagerMapIterator anIter( myViewManagerMap );
165   while( anIter.hasNext() )
166     if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
167       getApp()->removeViewManager( aViewManager );
168   myViewManagerMap.clear();
169
170   myObjectStateMap.clear();
171   myShapesMap.clear();
172   myVTKPrsMap.clear();
173
174   // clear the data model's list of copying objects
175   HYDROGUI_DataModel::changeCopyingObjects( HYDROData_SequenceOfObjects() );
176
177   setMenuShown( false );
178   setToolShown( false );
179
180   getApp()->setEditEnabled( true ); // show SalomeApp copy/paste actions
181
182   disconnect( getApp(), SIGNAL( operationFinished( const QString&, const QString&, const QStringList& ) ), 
183               this, SLOT( onExternalOperationFinished( const QString&, const QString&, const QStringList& ) ) );
184
185   HYDROGUI_Tool::setOCCActionShown( this, OCCViewer_ViewWindow::MaximizedId, true );
186
187   return LightApp_Module::deactivateModule( theStudy );
188 }
189
190 void HYDROGUI_Module::windows( QMap<int, int>& theMap ) const
191 {
192   theMap.clear();
193   theMap.insert( LightApp_Application::WT_LogWindow,     Qt::BottomDockWidgetArea );
194 #ifndef DISABLE_PYCONSOLE
195   theMap.insert( LightApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
196 #endif
197   theMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea   );
198 }
199
200 void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
201 {
202   theTypesList << OCCViewer_Viewer::Type() << GraphicsView_Viewer::Type();
203 }
204
205 void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
206                                         QMenu* theMenu,
207                                         QString& theTitle )
208 {
209   HYDROGUI_DataModel* aModel = getDataModel();
210
211   bool anIsObjectBrowser = theClient == getApp()->objectBrowser()->popupClientType();
212   bool anIsGraphicsView = theClient == GraphicsView_Viewer::Type();
213   bool anIsOCCView = theClient == OCCViewer_Viewer::Type();
214   bool anIsVTKView = theClient == SVTK_Viewer::Type();
215   if( !anIsObjectBrowser && !anIsGraphicsView && !anIsOCCView && !anIsVTKView )
216     return;
217
218   size_t anActiveViewId = HYDROGUI_Tool::GetActiveViewId( this );
219
220   bool anIsSelectedDataObjects = false;
221   bool anIsVisibleInSelection = false;
222   bool anIsHiddenInSelection = false;
223
224   bool anIsImage = false;
225   bool anIsImportedImage = false;
226   bool anIsImageHasRefs = false;
227   bool anIsFusedImage = false;
228   bool anIsCutImage = false;
229   bool anIsSplittedImage = false;
230   bool anIsMustBeUpdatedImage = false;
231   bool anIsPolyline = false;
232   bool anIsProfile = false;
233   bool anIsValidProfile = false;
234   bool anAllAreProfiles = false;
235   bool anIsBathymetry = false;
236   bool anIsCalculation = false;
237   bool anIsImmersibleZone = false;
238   bool anIsVisualState = false;
239   bool anIsRegion = false;
240   bool anIsZone = false;
241   bool anIsObstacle = false;
242   bool anIsGeomObject = false;
243
244   // check the selected GEOM objects
245   if ( !HYDROGUI_Tool::GetSelectedGeomObjects( this ).isEmpty() ) {
246     theMenu->addAction( action( ImportGeomObjectId ) );
247     theMenu->addSeparator();
248   }
249
250   // check the selected data model objects
251   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
252   int aNbOfSelectedProfiles = 0;
253   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
254   {
255     Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
256     if( !anObject.IsNull() )
257     {
258       anIsSelectedDataObjects = true;
259
260       bool aVisibility = isObjectVisible( anActiveViewId, anObject );
261       anIsVisibleInSelection |= aVisibility;
262       anIsHiddenInSelection |= !aVisibility;
263
264       if( anObject->GetKind() == KIND_IMAGE )
265       {
266         anIsImage = true;
267         Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
268         if( !anImage.IsNull() )
269         {
270           anIsImportedImage = anImage->HasLocalPoints();
271           anIsImageHasRefs = anImage->HasReferences();
272           if( HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory() )
273           {
274             if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
275             {
276               QString anOperatorName = anOperator->name();
277               if( anOperatorName == ImageComposer_FuseOperator::Type() )
278                 anIsFusedImage = true;
279               else if( anOperatorName == ImageComposer_CutOperator::Type() )
280                 anIsCutImage = true;
281               else if( anOperatorName == ImageComposer_CropOperator::Type() )
282                 anIsSplittedImage = true;
283             }
284           }
285           if ( anImage->IsMustBeUpdated() )
286           {
287             anIsMustBeUpdatedImage = true;
288           }
289         }
290       }
291       else if( anObject->GetKind() == KIND_POLYLINEXY )
292         anIsPolyline = true;
293       else if( anObject->GetKind() == KIND_PROFILE ) {
294         anIsProfile = true;
295         aNbOfSelectedProfiles++;
296
297         Handle(HYDROData_Profile) aProfile = 
298           Handle(HYDROData_Profile)::DownCast( anObject );
299         if( !aProfile.IsNull() && aProfile->IsValid() ) {
300           anIsValidProfile = true;
301         }
302       }
303       else if( anObject->GetKind() == KIND_CALCULATION )
304         anIsCalculation = true;
305       else if( anObject->GetKind() == KIND_IMMERSIBLE_ZONE )
306         anIsImmersibleZone = true;
307       else if( anObject->GetKind() == KIND_VISUAL_STATE )
308         anIsVisualState = true;
309       else if( anObject->GetKind() == KIND_REGION )
310         anIsRegion = true;
311       else if( anObject->GetKind() == KIND_ZONE )
312         anIsZone = true;
313       else if( anObject->GetKind() == KIND_BATHYMETRY )
314         anIsBathymetry = true;
315       else if( anObject->GetKind() == KIND_OBSTACLE )
316         anIsObstacle = true;
317     }
318
319     anIsGeomObject = HYDROData_Tool::IsGeometryObject( anObject );
320   }
321
322   // Check if all selected objects are profiles
323   anAllAreProfiles = ( aNbOfSelectedProfiles > 0 ) &&
324                      ( aNbOfSelectedProfiles == aSeq.Length() );
325
326   // check the selected partitions
327   if( !anIsSelectedDataObjects && anIsObjectBrowser )
328   {
329     ObjectKind aSelectedPartition = HYDROGUI_Tool::GetSelectedPartition( this );
330     if( aSelectedPartition != KIND_UNKNOWN )
331     {
332       switch( aSelectedPartition )
333       {
334         case KIND_IMAGE:
335           theMenu->addAction( action( ImportImageId ) );
336           break;
337         case KIND_POLYLINEXY:
338           theMenu->addAction( action( CreatePolylineId ) );
339           break;
340         case KIND_PROFILE:
341           theMenu->addAction( action( CreateProfileId ) );
342           theMenu->addAction( action( ImportProfilesId ) );
343           theMenu->addAction( action( AllGeoreferencementId ) );
344           break;
345         case KIND_VISUAL_STATE:
346           theMenu->addAction( action( SaveVisualStateId ) );
347           break;
348         case KIND_BATHYMETRY:
349           theMenu->addAction( action( ImportBathymetryId ) );
350           break;
351         case KIND_CALCULATION:
352           theMenu->addAction( action( CreateCalculationId ) );
353           break;
354         case KIND_IMMERSIBLE_ZONE:
355           theMenu->addAction( action( CreateImmersibleZoneId ) );
356           break;
357         case KIND_OBSTACLE:
358           theMenu->addAction( action( ImportObstacleFromFileId ) );
359           theMenu->addAction( action( CreateBoxId ) );
360           theMenu->addAction( action( CreateCylinderId ) );
361           break;
362       }
363       theMenu->addSeparator();
364     }
365   }
366
367   if( anIsSelectedDataObjects )
368   {
369     if ( anIsMustBeUpdatedImage )
370     {
371       theMenu->addAction( action( UpdateImageId ) );
372       theMenu->addSeparator();
373     }
374
375     if( aSeq.Length() == 1 )
376     {
377       if( anIsImage )
378       {
379         if( anIsImportedImage )
380           theMenu->addAction( action( EditImportedImageId ) );
381         else if( anIsImageHasRefs )
382         {
383           if( anIsFusedImage )
384             theMenu->addAction( action( EditFusedImageId ) );
385           else if( anIsCutImage )
386             theMenu->addAction( action( EditCutImageId ) );
387           else if( anIsSplittedImage )
388             theMenu->addAction( action( EditSplittedImageId ) );
389         }
390
391         theMenu->addAction( action( ObserveImageId ) );
392         theMenu->addAction( action( ExportImageId ) );
393         theMenu->addSeparator();
394
395         if( anIsImageHasRefs )
396         {
397           theMenu->addAction( action( RemoveImageRefsId ) );
398           theMenu->addSeparator();
399         }
400
401         theMenu->addAction( action( FuseImagesId ) );
402         theMenu->addAction( action( CutImagesId ) );
403         theMenu->addAction( action( SplitImageId ) );
404         theMenu->addSeparator();
405       }
406       else if( anIsPolyline )
407       {
408         theMenu->addAction( action( EditPolylineId ) );
409         theMenu->addSeparator();
410       }
411       else if( anIsProfile )
412       {
413         theMenu->addAction( action( EditProfileId ) );
414         theMenu->addAction( action( SelectedGeoreferencementId ) );
415         theMenu->addSeparator();
416       }
417       else if( anIsCalculation )
418       {
419         theMenu->addAction( action( EditCalculationId ) );
420         theMenu->addAction( action( ExportCalculationId ) );
421         theMenu->addSeparator();
422       }
423       else if( anIsImmersibleZone )
424       {
425         theMenu->addAction( action( EditImmersibleZoneId ) );
426         theMenu->addSeparator();
427       }
428       else if( anIsVisualState && anIsObjectBrowser )
429       {
430         theMenu->addAction( action( SaveVisualStateId ) );
431         theMenu->addAction( action( LoadVisualStateId ) );
432         theMenu->addSeparator();
433       }
434
435       // Add set color action for geometrical objects
436       if ( anIsGeomObject )
437       {
438         theMenu->addAction( action( SetColorId ) );
439         theMenu->addSeparator();
440       }
441     } else if ( anAllAreProfiles ) {
442       theMenu->addAction( action( SelectedGeoreferencementId ) );
443       theMenu->addSeparator();
444     }
445
446     theMenu->addAction( action( DeleteId ) );
447     theMenu->addSeparator();
448
449     if( anIsImage || anIsPolyline || anIsImmersibleZone || anIsZone || 
450         anIsRegion || anIsBathymetry || anIsObstacle || anIsValidProfile )
451     {
452       if( anIsHiddenInSelection )
453         theMenu->addAction( action( ShowId ) );
454       theMenu->addAction( action( ShowOnlyId ) );
455       if( anIsVisibleInSelection )
456         theMenu->addAction( action( HideId ) );
457       theMenu->addSeparator();
458     }
459   }
460
461   if ( anIsOCCView )
462   {
463     SUIT_Operation* anOp = application()->activeStudy()->activeOperation();
464     HYDROGUI_PolylineOp* aPolylineOp = dynamic_cast<HYDROGUI_PolylineOp*>( anOp );
465     if ( aPolylineOp && aPolylineOp->deleteEnabled() )
466       theMenu->addAction( action( DeleteId ) );
467   }
468
469   if( anIsObjectBrowser || anIsGraphicsView || anIsOCCView || anIsVTKView )
470   {
471     theMenu->addAction( action( ShowAllId ) );
472     theMenu->addAction( action( HideAllId ) );
473     theMenu->addSeparator();
474   }
475 }
476
477 void HYDROGUI_Module::update( const int flags )
478 {
479   if( !isUpdateEnabled() )
480     return;
481
482   QApplication::setOverrideCursor( Qt::WaitCursor );
483
484   // To prevent calling this method recursively
485   // from one of the methods called below
486   setUpdateEnabled( false );
487
488   // store selected objects
489   QStringList aSelectedEntries = storeSelection();
490
491   bool aDoFitAll = flags & UF_FitAll;
492   if( ( flags & UF_Viewer ) )
493     updateViewer( getDisplayer(), flags & UF_GV_Init, flags & UF_GV_Forced, aDoFitAll ); 
494
495   if( ( flags & UF_OCCViewer ) )
496     updateViewer( getOCCDisplayer(), flags & UF_OCC_Init, flags & UF_OCC_Forced, aDoFitAll ); 
497
498   if( ( flags & UF_VTKViewer ) )
499     updateViewer( getVTKDisplayer(), flags & UF_VTK_Init, flags & UF_VTK_Forced, aDoFitAll ); 
500
501   if( ( flags & UF_Model ) && getDataModel() && getApp() )
502   {
503     getDataModel()->update( getStudyId() );
504
505     // Temporary workaround to prevent breaking
506     // the selection in the object browser.
507     // Note: processEvents() should be called after updateGV(),
508     // otherwise the application crashes from time to time.
509     //RKV: qApp->processEvents(); 
510     getApp()->updateObjectBrowser( true );
511   }
512
513   // Object browser is currently updated by using UF_Model flag
514   if( ( flags & UF_ObjBrowser ) && ((flags & UF_Model) == 0) && getApp() )
515     getApp()->updateObjectBrowser( true );
516
517   if( ( flags & UF_Controls ) && getApp() )
518     getApp()->updateActions();
519
520   // restore selected objects
521   restoreSelection( aSelectedEntries );
522
523   setUpdateEnabled( true );
524
525   QApplication::restoreOverrideCursor();
526 }
527
528 void HYDROGUI_Module::updateCommandsStatus()
529 {
530   LightApp_Module::updateCommandsStatus();
531
532   updateUndoRedoControls();
533
534   action( CopyId )->setEnabled( getDataModel()->canCopy() );
535   action( PasteId )->setEnabled( getDataModel()->canPaste() );
536 }
537
538 void HYDROGUI_Module::selectionChanged()
539 {
540   LightApp_Module::selectionChanged();
541   updateCommandsStatus();
542 }
543
544 HYDROGUI_DataModel* HYDROGUI_Module::getDataModel() const
545 {
546   return (HYDROGUI_DataModel*)dataModel();
547 }
548
549 HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
550 {
551   return myDisplayer;
552 }
553
554 HYDROGUI_OCCDisplayer* HYDROGUI_Module::getOCCDisplayer() const
555 {
556   return myOCCDisplayer;
557 }
558
559 HYDROGUI_VTKPrsDisplayer* HYDROGUI_Module::getVTKDisplayer() const
560 {
561   return myVTKDisplayer;
562 }
563
564 SUIT_ViewManager* HYDROGUI_Module::getViewManager( const int theId ) const
565 {
566   if( myViewManagerMap.contains( theId ) )
567   {
568     return myViewManagerMap[ theId ].first;
569   }
570   return NULL;
571 }
572
573 GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
574 {
575   if( myViewManagerMap.contains( theId ) )
576   {
577     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
578     GraphicsView_ViewManager* aViewManager =
579       dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
580     if( aViewManager )
581       return aViewManager->getViewer();
582   }
583   return NULL;
584 }
585
586 OCCViewer_Viewer* HYDROGUI_Module::getOCCViewer( const int theId ) const
587 {
588   if( myViewManagerMap.contains( theId ) )
589   {
590     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
591     OCCViewer_ViewManager* aViewManager =
592       ::qobject_cast<OCCViewer_ViewManager*>( anInfo.first );
593     if( aViewManager )
594       return aViewManager->getOCCViewer();
595   }
596   return NULL;
597 }
598
599 SVTK_Viewer* HYDROGUI_Module::getVTKViewer( const int theId ) const
600 {
601   if( myViewManagerMap.contains( theId ) )
602   {
603     ViewManagerInfo anInfo = myViewManagerMap[ theId ];
604     SVTK_ViewManager* aViewManager =
605       ::qobject_cast<SVTK_ViewManager*>( anInfo.first );
606     if( aViewManager )
607       return dynamic_cast<SVTK_Viewer*>( aViewManager->getViewModel() );
608   }
609   return NULL;
610 }
611
612 int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
613 {
614   ViewManagerMapIterator anIter( myViewManagerMap );
615   while( anIter.hasNext() )
616   {
617     int anId = anIter.next().key();
618     const ViewManagerInfo& anInfo = anIter.value();
619     if( anInfo.first == theViewManager )
620       return anId;
621   }
622   return -1;
623 }
624
625 HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
626 {
627   int anId = getViewManagerId( theViewManager );
628   if( anId != -1 )
629   {
630     const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
631     return anInfo.second;
632   }
633   return VMR_Unknown;
634 }
635
636 void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
637                                           const ViewManagerRole theRole )
638 {
639   int anId = getViewManagerId( theViewManager );
640   if( anId != -1 )
641   {
642     ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
643     anInfo.second = theRole;
644   }
645 }
646
647 bool HYDROGUI_Module::isObjectVisible( const int theViewId,
648                                        const Handle(HYDROData_Entity)& theObject ) const
649 {
650   if( theObject.IsNull() )
651     return false;
652
653   ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
654   if( anIter1 != myObjectStateMap.end() )
655   {
656     const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
657     Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
658     if( anIter2 != aName2ObjectStateMap.end() )
659     {
660       const ObjectState& anObjectState = anIter2.value();
661       return anObjectState.Visibility;
662     }
663   }
664   return false;
665 }
666
667 void HYDROGUI_Module::setObjectVisible( const int theViewId,
668                                         const Handle(HYDROData_Entity)& theObject,
669                                         const bool theState )
670 {
671   if( !theObject.IsNull() )
672   {
673     Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
674     ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
675     anObjectState.Visibility = theState;
676   }
677 }
678
679 /////////////////// OCC SHAPES PROCESSING
680 HYDROGUI_Shape* HYDROGUI_Module::getObjectShape( const int                       theViewId,
681                                                  const Handle(HYDROData_Entity)& theObject ) const
682 {
683   HYDROGUI_Shape* aResShape = NULL;
684   if( theObject.IsNull() )
685     return aResShape;
686
687   if ( myShapesMap.contains( theViewId ) )
688   {
689     const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
690     foreach ( HYDROGUI_Shape* aShape, aViewShapes )
691     {
692       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
693         continue;
694
695       aResShape = aShape;
696       break;
697     }
698   }
699
700   return aResShape;
701 }
702
703 void HYDROGUI_Module::setObjectShape( const int                       theViewId,
704                                       const Handle(HYDROData_Entity)& theObject,
705                                       HYDROGUI_Shape*                 theShape )
706 {
707   if( theObject.IsNull() )
708     return;
709
710   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
711   aViewShapes.append( theShape );
712 }
713
714 void HYDROGUI_Module::removeObjectShape( const int                       theViewId,
715                                          const Handle(HYDROData_Entity)& theObject )
716 {
717   if ( !myShapesMap.contains( theViewId ) )
718     return;
719
720   ListOfShapes& aViewShapes = myShapesMap[ theViewId ];
721   Handle(HYDROData_Entity) anObject;
722   for ( int i = 0; i < aViewShapes.length(); )
723   {
724     HYDROGUI_Shape* aShape = aViewShapes.at( i );
725     anObject = aShape->getObject();
726     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
727     {
728       delete aShape;
729       aViewShapes.removeAt( i );
730       continue;
731     }
732
733     ++i;
734   }
735 }
736
737 void HYDROGUI_Module::removeViewShapes( const int theViewId )
738 {
739   if ( !myShapesMap.contains( theViewId ) )
740     return;
741
742   const ListOfShapes& aViewShapes = myShapesMap.value( theViewId );
743   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
744   {
745     HYDROGUI_Shape* aShape = aViewShapes.at( i );
746     if ( aShape )
747       delete aShape;
748   }
749
750   myShapesMap.remove( theViewId );
751 }
752 /////////////////// END OF OCC SHAPES PROCESSING
753
754 /////////////////// VTKPrs PROCESSING
755 HYDROGUI_VTKPrs* HYDROGUI_Module::getObjectVTKPrs( const int                       theViewId,
756                                                  const Handle(HYDROData_Entity)& theObject ) const
757 {
758   HYDROGUI_VTKPrs* aResShape = NULL;
759   if( theObject.IsNull() )
760     return aResShape;
761
762   if ( myVTKPrsMap.contains( theViewId ) )
763   {
764     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
765     foreach ( HYDROGUI_VTKPrs* aShape, aViewShapes )
766     {
767       if ( !aShape || !IsEqual( aShape->getObject(), theObject ) )
768         continue;
769
770       aResShape = aShape;
771       break;
772     }
773   }
774
775   return aResShape;
776 }
777
778 void HYDROGUI_Module::setObjectVTKPrs( const int                       theViewId,
779                                        const Handle(HYDROData_Entity)& theObject,
780                                        HYDROGUI_VTKPrs*                 theShape )
781 {
782   if( theObject.IsNull() )
783     return;
784
785   // Compute the new global Z range from the added presentation and the old global Z range.
786   double* aGlobalRange = getVTKDisplayer()->GetZRange( theViewId );
787   double* aRange = theShape->getInternalZRange();
788   bool anIsUpdate = false;
789   if ( aRange[0] < aGlobalRange[0] )
790   {
791     aGlobalRange[0] = aRange[0];
792     anIsUpdate = true;
793   }
794   if ( aRange[1] > aGlobalRange[1] )
795   {
796     aGlobalRange[1] = aRange[1];
797     anIsUpdate = true;
798   }
799
800   //if ( anIsUpdate )
801   //{
802     updateVTKZRange( theViewId, aGlobalRange );
803   //}
804
805   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
806   aViewShapes.append( theShape );
807 }
808
809 void HYDROGUI_Module::removeObjectVTKPrs( const int                       theViewId,
810                                           const Handle(HYDROData_Entity)& theObject )
811 {
812   if ( !myVTKPrsMap.contains( theViewId ) )
813     return;
814
815   ListOfVTKPrs& aViewShapes = myVTKPrsMap[ theViewId ];
816   Handle(HYDROData_Entity) anObject;
817   for ( int i = 0; i < aViewShapes.length(); )
818   {
819     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
820     anObject = aShape->getObject();
821     if ( aShape && (!anObject.IsNull()) && IsEqual( anObject, theObject ) )
822     {
823       delete aShape;
824       aViewShapes.removeAt( i );
825       continue;
826     }
827
828     ++i;
829   }
830
831   // Invalidate global Z range
832   double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
833   getVTKDisplayer()->SetZRange( theViewId, anInvalidRange );
834 }
835
836 void HYDROGUI_Module::removeViewVTKPrs( const int theViewId )
837 {
838   if ( !myVTKPrsMap.contains( theViewId ) )
839     return;
840
841   const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
842   for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
843   {
844     HYDROGUI_VTKPrs* aShape = aViewShapes.at( i );
845     if ( aShape )
846       delete aShape;
847   }
848
849   myVTKPrsMap.remove( theViewId );
850 }
851
852 void HYDROGUI_Module::updateVTKZRange( const int theViewId, double theRange[] )
853 {
854   if ( myVTKPrsMap.contains( theViewId ) )
855   {
856     // For the given viewer id update all VTK presentations ...
857     const ListOfVTKPrs& aViewShapes = myVTKPrsMap.value( theViewId );
858     HYDROGUI_VTKPrs* aShape;
859     for ( int i = 0, n = aViewShapes.length(); i < n; ++i )
860     {
861       aShape = aViewShapes.at( i );
862       if ( aShape )
863       {
864         aShape->setZRange( theRange );
865       }
866     }
867   }
868   // ... and update the global color legend scalar bar.
869   getVTKDisplayer()->SetZRange( theViewId, theRange );
870 }
871 /////////////////// END OF VTKPrs PROCESSING
872
873 CAM_DataModel* HYDROGUI_Module::createDataModel()
874 {
875   return new HYDROGUI_DataModel( this );
876 }
877
878 void HYDROGUI_Module::customEvent( QEvent* e )
879 {
880   int aType = e->type();
881   if ( aType == NewViewEvent )
882   {
883     SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
884     if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
885     {
886       if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
887       {
888         SUIT_ViewManager* aViewManager = aViewer->getViewManager();
889         ViewManagerRole aRole = getViewManagerRole( aViewManager );
890
891         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
892         {
893           if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
894             aViewPort->scale( 1, -1 ); // invert the Y axis direction from down to up
895
896           aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
897           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
898           aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
899
900           //ouv: temporarily commented
901           //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
902         }
903
904         if( aRole != VMR_TransformImage && aRole != VMR_ReferenceImage )
905           update( UF_Viewer );
906
907         aViewer->activateTransform( GraphicsView_Viewer::FitAll );
908       }
909     }
910   }
911 }
912
913 bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
914 {
915   QEvent::Type aType = theEvent->type();
916   if( theObj->inherits( "GraphicsView_ViewFrame" ) )
917   {
918     if( aType == QEvent::Show )
919     {
920       SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
921       e->setData( theObj );
922       QApplication::postEvent( this, e );
923       theObj->removeEventFilter( this );
924     }
925   }
926   return LightApp_Module::eventFilter( theObj, theEvent );
927 }
928
929 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
930 {
931   LightApp_Module::onViewManagerAdded( theViewManager );
932
933   if( theViewManager->getType() == GraphicsView_Viewer::Type() )
934   { 
935     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
936              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
937   }
938   else if( theViewManager->getType() == OCCViewer_Viewer::Type() )
939   {
940     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
941              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
942   }
943
944   createSelector( theViewManager ); // replace the default selector
945
946   ViewManagerInfo anInfo( theViewManager, VMR_General );
947   myViewManagerMap.insert( ViewManagerId++, anInfo );
948 }
949
950 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
951 {
952   LightApp_Module::onViewManagerRemoved( theViewManager );
953
954   createSelector( theViewManager ); // replace the default selector
955
956   int anId = getViewManagerId( theViewManager );
957   if( anId != -1 )
958   {
959     OCCViewer_ViewManager* anOCCViewManager =
960       ::qobject_cast<OCCViewer_ViewManager*>( myViewManagerMap[ anId ].first );
961     if ( anOCCViewManager )
962     {
963       OCCViewer_Viewer* anOCCViewer = anOCCViewManager->getOCCViewer();
964       if ( anOCCViewer )
965         removeViewShapes( (size_t)anOCCViewer );
966     }
967
968     if ( getVTKDisplayer()->IsApplicable( theViewManager ) )
969     {
970       SVTK_Viewer* aVTKViewer = getVTKViewer( anId );
971       if ( aVTKViewer )
972       {
973         getVTKDisplayer()->DeleteScalarBar( anId );
974         removeViewShapes( (size_t)aVTKViewer );
975       }
976     }
977
978     myViewManagerMap.remove( anId );
979   }
980 }
981
982 void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
983 {
984   if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
985   {
986     if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
987     {
988       aViewFrame->installEventFilter( this );
989
990       GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
991
992       connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
993                this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
994     }
995   }
996   else if( theViewWindow && theViewWindow->inherits( "OCCViewer_ViewFrame" ) )
997   {
998     if( OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theViewWindow ) )
999     {
1000       aViewFrame->onTopView();
1001
1002       HYDROGUI_Tool::setOCCActionShown( aViewFrame, OCCViewer_ViewWindow::MaximizedId, false );
1003     }
1004   }
1005 }
1006
1007 void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
1008 {
1009   /* ouv: currently unused
1010   if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
1011   {
1012     SUIT_ViewManager* aViewManager = 0;
1013
1014     QObject* aParent = aViewPort;
1015     while( aParent = aParent->parent() )
1016     {
1017       if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
1018       {
1019         if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
1020         {
1021           aViewManager = aViewer->getViewManager();
1022           break;
1023         }
1024       }
1025     }
1026
1027     if( !aViewManager )
1028       return;
1029
1030     double aMouseX = theEvent->scenePos().x();
1031     double aMouseY = theEvent->scenePos().y();
1032
1033     ViewManagerRole aRole = getViewManagerRole( aViewManager );
1034     if( aRole == VMR_General )
1035     {
1036       int aXDeg = 0, aYDeg = 0;
1037       int aXMin = 0, aYMin = 0;
1038       double aXSec = 0, aYSec = 0;
1039       HYDROData_Lambert93::secToDMS( aMouseX, aXDeg, aXMin, aXSec );
1040       HYDROData_Lambert93::secToDMS( aMouseY, aYDeg, aYMin, aYSec );
1041
1042       QString aDegSymbol( QChar( 0x00B0 ) );
1043       QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
1044       QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
1045
1046       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
1047     }
1048     else if( aRole == VMR_TransformImage )
1049       aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
1050   }
1051   */
1052 }
1053
1054 void HYDROGUI_Module::updateViewer( HYDROGUI_AbstractDisplayer* theDisplayer, 
1055                                     const bool theIsInit, 
1056                                     const bool theIsForced, 
1057                                     const bool theDoFitAll )
1058 {
1059   QList<int> aViewManagerIdList;
1060
1061   // currently, all views are updated
1062   ViewManagerMapIterator anIter( myViewManagerMap );
1063   while( anIter.hasNext() )
1064   { 
1065     SUIT_ViewManager* aViewManager = anIter.next().value().first;
1066
1067     if ( theDisplayer->IsApplicable( aViewManager ) )
1068     {
1069       int anId = anIter.key();
1070       aViewManagerIdList.append( anId );
1071     }
1072   }
1073
1074   QListIterator<int> anIdIter( aViewManagerIdList );
1075   while( anIdIter.hasNext() )
1076   {
1077     theDisplayer->UpdateAll( anIdIter.next(), theIsInit, theIsForced, theDoFitAll );
1078   }
1079 }
1080
1081 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )
1082 {
1083   if( !theViewManager )
1084     return;
1085
1086   LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
1087   if( !aSelectionMgr )
1088     return;
1089
1090   QString aViewType = theViewManager->getType();
1091   if( aViewType != GraphicsView_Viewer::Type() &&
1092       aViewType != OCCViewer_Viewer::Type())
1093     return;
1094
1095   QList<SUIT_Selector*> aSelectorList;
1096   aSelectionMgr->selectors( aViewType, aSelectorList );
1097
1098   // disable all alien selectors
1099   QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
1100   for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
1101   {
1102     SUIT_Selector* aSelector = *anIter;
1103     if( aSelector && ( !dynamic_cast<HYDROGUI_GVSelector*>( aSelector ) &&
1104                        !dynamic_cast<SVTK_Selector*>( aSelector ) &&
1105                        !dynamic_cast<HYDROGUI_OCCSelector*>( aSelector ) ) )
1106       aSelector->setEnabled( false );
1107   }
1108
1109   if ( aViewType == GraphicsView_Viewer::Type() )
1110   {
1111     GraphicsView_ViewManager* aViewManager =
1112       ::qobject_cast<GraphicsView_ViewManager*>( theViewManager );
1113     if( aViewManager )
1114       new HYDROGUI_GVSelector( this, aViewManager->getViewer(), aSelectionMgr );
1115   }
1116   else if ( aViewType == OCCViewer_Viewer::Type() )
1117   {
1118     OCCViewer_ViewManager* aViewManager =
1119       ::qobject_cast<OCCViewer_ViewManager*>( theViewManager );
1120     if( aViewManager )
1121       new HYDROGUI_OCCSelector( this, aViewManager->getOCCViewer(), aSelectionMgr );
1122   }
1123 }
1124
1125 bool HYDROGUI_Module::setUpdateEnabled( const bool theState )
1126 {
1127   bool aPrevState = myIsUpdateEnabled;
1128   myIsUpdateEnabled = theState;
1129   return aPrevState;
1130 }
1131
1132 bool HYDROGUI_Module::isUpdateEnabled() const
1133 {
1134   return myIsUpdateEnabled;
1135 }
1136
1137 QStringList HYDROGUI_Module::storeSelection() const
1138 {
1139   QStringList anEntryList;
1140   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1141   {
1142     SUIT_DataOwnerPtrList aList( true );
1143     aSelectionMgr->selected( aList );
1144
1145     SUIT_DataOwnerPtrList::iterator anIter;
1146     for( anIter = aList.begin(); anIter != aList.end(); anIter++ )
1147     {
1148       const LightApp_DataOwner* anOwner = 
1149         dynamic_cast<const LightApp_DataOwner*>( (*anIter).operator->() );
1150       if( anOwner )
1151         anEntryList.append( anOwner->entry() );
1152     }
1153   }
1154   return anEntryList;
1155 }
1156
1157 void HYDROGUI_Module::restoreSelection( const QStringList& theEntryList )
1158 {
1159   if( LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr() )
1160   {
1161     SUIT_DataOwnerPtrList aList( true );
1162     for( int anIndex = 0, aSize = theEntryList.size(); anIndex < aSize; anIndex++ )
1163       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( theEntryList[ anIndex ] ) ) );
1164     aSelectionMgr->setSelected( aList );
1165   }
1166 }