Salome HOME
Merge branch 'BR_LAND_COVER_MAP' into BR_quadtree
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ShowHideOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ShowHideOp.h"
20
21 #include <GraphicsView_Viewer.h>
22
23 #include "HYDROGUI_Module.h"
24 #include "HYDROGUI_OCCDisplayer.h"
25 #include "HYDROGUI_Operations.h"
26 #include "HYDROGUI_Tool2.h"
27 #include "HYDROGUI_UpdateFlags.h"
28
29 #include <HYDROData_Iterator.h>
30 #include <HYDROData_Entity.h>
31 #include <HYDROData_Region.h>
32
33 #include <LightApp_Application.h>
34 #include <LightApp_Displayer.h>
35
36 #include <OCCViewer_ViewModel.h>
37 #include <OCCViewer_ViewManager.h>
38
39 #include <SVTK_ViewModel.h>
40
41 #include <SUIT_ViewManager.h>
42
43 HYDROGUI_ShowHideOp::HYDROGUI_ShowHideOp( HYDROGUI_Module* theModule, int theId )
44 : HYDROGUI_Operation( theModule ),
45   myId( theId )
46 {
47   QString aName;
48   switch( myId )
49   {
50     case ShowId:     aName = tr( "SHOW" ); break;
51     case ShowOnlyId: aName = tr( "SHOW_ONLY" ); break;
52     case ShowAllId:  aName = tr( "SHOW_ALL" ); break;
53     case HideId:     aName = tr( "HIDE" ); break;
54     case HideAllId:  aName = tr( "HIDE_ALL" ); break;
55     default: break;
56   }
57   setName( aName );
58 }
59
60 HYDROGUI_ShowHideOp::~HYDROGUI_ShowHideOp()
61 {
62 }
63
64 void HYDROGUI_ShowHideOp::startOperation()
65 {
66   HYDROGUI_Operation::startOperation();
67
68   HYDROGUI_Module* aModule = module();
69
70   size_t aViewId = HYDROGUI_Tool::GetActiveViewId( aModule );
71
72   int anUpdateFlags = 0;
73
74   SUIT_ViewManager* aVTKMgr           = NULL;
75   OCCViewer_ViewManager* anOCCManager = NULL;
76
77   SUIT_ViewManager* aViewMgr = aModule->getApp()->activeViewManager();
78   if ( aViewMgr )
79   {
80     if ( aViewMgr->getType() == GraphicsView_Viewer::Type() )
81     {
82       anUpdateFlags |= UF_Viewer;
83     }
84     else if ( aViewMgr->getType() == OCCViewer_Viewer::Type() )
85     {
86       anUpdateFlags |= UF_OCCViewer;
87       anOCCManager = ::qobject_cast<OCCViewer_ViewManager*>( aViewMgr );
88     }
89     else if ( aViewMgr->getType() == SVTK_Viewer::Type() )
90     {
91       anUpdateFlags |= UF_VTKViewer;
92     }
93   }
94
95   // for all objects
96   if( myId == ShowOnlyId || myId == ShowAllId || myId == HideAllId )
97   {
98     bool aVisibility = myId == ShowAllId;
99     HYDROData_Iterator anIterator( doc() );
100     for( ; anIterator.More(); anIterator.Next() )
101     {
102       Handle(HYDROData_Entity) anObject = anIterator.Current();
103       if( !anObject.IsNull() )
104         aModule->setObjectVisible( aViewId, anObject, aVisibility );
105     }
106
107     // For occ viewer we do the additional step to hide objects from other modules
108     if ( anOCCManager != NULL && !aVisibility )
109       LightApp_Displayer().EraseAll( true );
110   }
111
112   bool isFoundImage = false;
113   // for selected objects
114   if( myId == ShowId || myId == ShowOnlyId || myId == HideId )
115   {
116     HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( aModule );
117   
118     bool aVisibility = myId == ShowId || myId == ShowOnlyId || myId == ShowAllId;
119     Handle( HYDROData_Entity ) anObject;
120     for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
121     {
122       anObject = aSeq.Value( anIndex );
123       if( !anObject.IsNull() )
124       {
125         if ( anObject->GetKind() == KIND_IMAGE )
126           isFoundImage = true;
127
128         aModule->setObjectVisible( aViewId, anObject, aVisibility );
129         if ( anObject->GetKind() == KIND_REGION )
130         {
131           Handle( HYDROData_Region ) aRegion = Handle( HYDROData_Region )::DownCast( anObject );
132           if ( !aRegion.IsNull() )
133           {
134             HYDROData_SequenceOfObjects aZonesSeq = aRegion->GetZones();
135             for( Standard_Integer aZoneIdx = 1, aNbZones = aZonesSeq.Length(); aZoneIdx <= aNbZones; aZoneIdx++ )
136             {
137               anObject = aZonesSeq.Value( aZoneIdx );
138               if( !anObject.IsNull() )
139               {
140                 aModule->setObjectVisible( aViewId, anObject, aVisibility );
141               }
142             }
143           }
144         }
145         else if ( anObject->GetKind() == KIND_BATHYMETRY && aVisibility )
146         {
147           if ( anUpdateFlags & UF_OCCViewer )
148           {
149             aModule->setObjectVisible( aViewId, anObject, aVisibility );
150           }
151           else if ( !(anUpdateFlags & UF_VTKViewer) )
152           {
153             // Activate VTK viewer if show a bathymetry
154             aVTKMgr = aModule->getApp()->viewManager( SVTK_Viewer::Type() );
155             if ( !aVTKMgr )
156             {
157               aVTKMgr = aModule->getApp()->createViewManager( SVTK_Viewer::Type() );
158             }
159             if ( aVTKMgr )
160             {
161               aModule->setObjectVisible( (size_t)aVTKMgr->getViewModel(), anObject, aVisibility );
162             }
163           }
164         }
165       }
166     }
167   }
168
169   if ( myId == ShowOnlyId || myId == ShowId || myId == ShowAllId )
170   {
171     if( isFoundImage || myId == ShowAllId )
172       anUpdateFlags |= UF_FitAll;
173   }
174
175   // Set VTK viewer active if show a bathymetry
176   if ( aVTKMgr )
177   {
178     anUpdateFlags |= UF_VTKViewer;
179     aVTKMgr->setShown( true );
180   }
181
182   aModule->update( anUpdateFlags );
183   commit();
184 }