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