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