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