Salome HOME
Merge remote-tracking branch 'origin/BR_SHAPE_RECOGNITION' into BR_v14_rc
[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_Operations.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27
28 #include <HYDROData_Iterator.h>
29 #include <HYDROData_Entity.h>
30 #include <HYDROData_Region.h>
31
32 #include <LightApp_Application.h>
33 #include <LightApp_Displayer.h>
34
35 #include <OCCViewer_ViewModel.h>
36 #include <OCCViewer_ViewManager.h>
37
38 #include <SVTK_ViewModel.h>
39
40 #include <SUIT_ViewManager.h>
41
42 HYDROGUI_ShowHideOp::HYDROGUI_ShowHideOp( HYDROGUI_Module* theModule, int theId )
43 : HYDROGUI_Operation( theModule ),
44   myId( theId )
45 {
46   QString aName;
47   switch( myId )
48   {
49     case ShowId:     aName = tr( "SHOW" ); break;
50     case ShowOnlyId: aName = tr( "SHOW_ONLY" ); break;
51     case ShowAllId:  aName = tr( "SHOW_ALL" ); break;
52     case HideId:     aName = tr( "HIDE" ); break;
53     case HideAllId:  aName = tr( "HIDE_ALL" ); break;
54     default: break;
55   }
56   setName( aName );
57 }
58
59 HYDROGUI_ShowHideOp::~HYDROGUI_ShowHideOp()
60 {
61 }
62
63 void HYDROGUI_ShowHideOp::startOperation()
64 {
65   HYDROGUI_Operation::startOperation();
66
67   HYDROGUI_Module* aModule = module();
68
69   size_t aViewId = HYDROGUI_Tool::GetActiveViewId( aModule );
70
71   int anUpdateFlags = 0;
72
73   SUIT_ViewManager* aVTKMgr           = NULL;
74   OCCViewer_ViewManager* anOCCManager = NULL;
75
76   SUIT_ViewManager* aViewMgr = aModule->getApp()->activeViewManager();
77   if ( aViewMgr )
78   {
79     if ( aViewMgr->getType() == GraphicsView_Viewer::Type() )
80     {
81       anUpdateFlags |= UF_Viewer;
82     }
83     else if ( aViewMgr->getType() == OCCViewer_Viewer::Type() )
84     {
85       anUpdateFlags |= UF_OCCViewer;
86       anOCCManager = ::qobject_cast<OCCViewer_ViewManager*>( aViewMgr );
87     }
88     else if ( aViewMgr->getType() == SVTK_Viewer::Type() )
89     {
90       anUpdateFlags |= UF_VTKViewer;
91     }
92   }
93
94   // for all objects
95   if( myId == ShowOnlyId || myId == ShowAllId || myId == HideAllId )
96   {
97     bool aVisibility = myId == ShowAllId;
98     HYDROData_Iterator anIterator( doc() );
99     for( ; anIterator.More(); anIterator.Next() )
100     {
101       Handle(HYDROData_Entity) anObject = anIterator.Current();
102       if( !anObject.IsNull() )
103         aModule->setObjectVisible( aViewId, anObject, aVisibility );
104     }
105
106     // For occ viewer we do the additional step to hide objects from other modules
107     if ( anOCCManager != NULL && !aVisibility )
108       LightApp_Displayer().EraseAll( true );
109   }
110
111   bool isFoundImage = false;
112   // for selected objects
113   if( myId == ShowId || myId == ShowOnlyId || myId == HideId )
114   {
115     HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( aModule );
116   
117     bool aVisibility = myId == ShowId || myId == ShowOnlyId || myId == ShowAllId;
118     Handle( HYDROData_Entity ) anObject;
119     for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
120     {
121       anObject = aSeq.Value( anIndex );
122       if( !anObject.IsNull() )
123       {
124         if ( anObject->GetKind() == KIND_IMAGE )
125           isFoundImage = true;
126
127         aModule->setObjectVisible( aViewId, anObject, aVisibility );
128         if ( anObject->GetKind() == KIND_REGION )
129         {
130           Handle( HYDROData_Region ) aRegion = Handle( HYDROData_Region )::DownCast( anObject );
131           if ( !aRegion.IsNull() )
132           {
133             HYDROData_SequenceOfObjects aZonesSeq = aRegion->GetZones();
134             for( Standard_Integer aZoneIdx = 1, aNbZones = aZonesSeq.Length(); aZoneIdx <= aNbZones; aZoneIdx++ )
135             {
136               anObject = aZonesSeq.Value( aZoneIdx );
137               if( !anObject.IsNull() )
138               {
139                 aModule->setObjectVisible( aViewId, anObject, aVisibility );
140               }
141             }
142           }
143         }
144         else if ( anObject->GetKind() == KIND_BATHYMETRY && aVisibility )
145         {
146           if ( anUpdateFlags & UF_OCCViewer )
147           {
148             aModule->setObjectVisible( aViewId, anObject, aVisibility );
149           }
150           else if ( !(anUpdateFlags & UF_VTKViewer) )
151           {
152             // Activate VTK viewer if show a bathymetry
153             aVTKMgr = aModule->getApp()->viewManager( SVTK_Viewer::Type() );
154             if ( !aVTKMgr )
155             {
156               aVTKMgr = aModule->getApp()->createViewManager( SVTK_Viewer::Type() );
157             }
158             if ( aVTKMgr )
159             {
160               aModule->setObjectVisible( (size_t)aVTKMgr->getViewModel(), anObject, aVisibility );
161             }
162           }
163         }
164       }
165     }
166   }
167
168   if ( myId == ShowOnlyId || myId == ShowId || myId == ShowAllId )
169   {
170     if( isFoundImage || myId == ShowAllId )
171       anUpdateFlags |= UF_FitAll;
172   }
173
174   // Set VTK viewer active if show a bathymetry
175   if ( aVTKMgr )
176   {
177     anUpdateFlags |= UF_VTKViewer;
178     aVTKMgr->setShown( true );
179   }
180
181   aModule->update( anUpdateFlags );
182   commit();
183 }