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