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