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