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