Salome HOME
Disable Join and JoinAll operations till it will be described in
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_VTKPrsDisplayer.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_VTKPrsDisplayer.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_VTKPrs.h"
28 #include "HYDROGUI_VTKPrsBathymetryDriver.h"
29 #include "HYDROGUI_Tool.h"
30
31 #include "HYDROData_Tool.h"
32 #include <SVTK_ViewModel.h>
33 #include <SVTK_ViewWindow.h>
34 #include <SALOME_ListIO.hxx>
35 #include <SALOME_ListIteratorOfListIO.hxx>
36 #include <SALOME_InteractiveObject.hxx>
37 #include <SUIT_ViewManager.h>
38 #include <SUIT_Accel.h>
39
40 #include <vtkLookupTable.h>
41 #include <vtkRenderer.h>
42 #include <vtkWindow.h>
43 #include <vtkActor2DCollection.h>
44
45 #include <QVector>
46
47 #define NB_COLORS 32
48
49 // Saturation of blue
50 //#define HUE_START 0.69 
51 //#define HUE_END   0.41
52 //#define SATURATION_START 1.0 
53 //#define SATURATION_END   0.4
54
55 #define HUE_START 0.7 
56 #define HUE_END   0.0
57 #define SATURATION_START 1.0 
58 #define SATURATION_END   1.0
59
60 HYDROGUI_VTKPrsDisplayer::HYDROGUI_VTKPrsDisplayer( HYDROGUI_Module* theModule )
61 : HYDROGUI_AbstractDisplayer( theModule ), myDriver( NULL )
62 {
63   // The invalid value is used to identify the case when the table range is not initialized yet.
64   double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
65   vtkLookupTable* aTable = vtkLookupTable::New();
66   aTable->SetHueRange( HUE_START, HUE_END );
67   aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
68   aTable->SetTableRange( anInvalidValue, anInvalidValue );
69   aTable->SetValueRange( 1.0, 1.0 );
70   aTable->SetAlphaRange( 1.0, 1.0 );
71   aTable->SetNumberOfColors( NB_COLORS );
72   myScalarBar->SetLookupTable( aTable );
73 }
74
75 HYDROGUI_VTKPrsDisplayer::~HYDROGUI_VTKPrsDisplayer()
76 {
77 }
78
79 void HYDROGUI_VTKPrsDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
80                                       const int theViewerId )
81 {
82   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
83   if( !aViewer )
84   {
85     HYDROGUI_VTKPrs* anObjShape;
86     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
87     {
88       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
89       if( !anObj.IsNull() )
90       {
91         anObjShape = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
92         if ( anObjShape )
93         {
94           anObjShape->setIsToUpdate( true );
95         }
96       }
97     }
98   }
99 }
100
101 void HYDROGUI_VTKPrsDisplayer::EraseAll( const int theViewerId )
102 {
103   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
104   if( aViewer )
105   {
106     aViewer->EraseAll( true );
107     module()->removeViewVTKPrs( (size_t)aViewer );
108   }
109 }
110
111 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
112                                 const int theViewerId )
113 {
114   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
115   if( aViewer )
116   {
117     HYDROGUI_VTKPrs* aPrs;
118     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
119     {
120       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
121       if( anObj.IsNull() )
122         continue;
123
124       aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
125       if ( aPrs )
126       {
127         aViewer->Erase( aPrs, true );
128       }
129       if ( anObj->IsRemoved() )
130       {
131         module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
132       }
133     }
134   }
135 }
136
137 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
138                                         const int theViewerId,
139                                         const bool theIsForced,
140                                         const bool theDoFitAll)
141 {
142   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
143   if( aViewer )
144   {
145     // Invalidate global Z range
146     double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
147     SetZRange( anInvalidRange );
148     // Hide colors legend bar
149     SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
150       aViewer->getViewManager()->getActiveView() );
151     if ( aView )
152     {
153       if ( aView->getRenderer()->HasViewProp( myScalarBar.GetPointer() ) )
154       {
155         aView->getRenderer()->RemoveActor2D( myScalarBar.GetPointer() );
156       }
157     }
158
159
160     int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
161     bool isChanged = false;
162     HYDROGUI_VTKPrs* aPrs;
163     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
164     {
165       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
166       if( !anObj.IsNull() )
167       {
168         bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
169         aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
170
171         bool anIsInserted = ( aPrs != 0 );
172         if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
173         {
174           if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( anObj ) )
175           {
176             if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
177             {
178               module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
179             }
180           }
181         }
182
183         if( aPrs )
184         {
185           if ( anIsVisible )
186           {
187
188             // Extend the global Z range if necessary
189             double* aGlobalRange = GetZRange();
190             double* aRange = aPrs->getInternalZRange();
191             bool anIsUpdate = false;
192             if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
193             {
194               aGlobalRange[0] = aRange[0];
195               anIsUpdate = true;
196             }
197             if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
198             {
199               aGlobalRange[1] = aRange[1];
200               anIsUpdate = true;
201             }
202
203             if ( anIsUpdate )
204             {
205               module()->updateVTKZRange( aGlobalRange );
206             }
207
208             aViewer->Display( aPrs );
209
210           }
211           else
212           {
213             aViewer->Erase( aPrs );
214           }
215           isChanged = true;
216         }
217       }
218     }
219
220     if ( isChanged )
221     {
222       // Show colors legend bar
223       if ( aView )
224       {
225         aView->getRenderer()->AddActor2D( myScalarBar.GetPointer() );
226       }
227     }
228
229     if ( theDoFitAll )
230     {
231       // Repaint is done inside OnFitAll()
232       aViewer->getViewManager()->getActiveView()->onAccelAction( SUIT_Accel::ZoomFit );
233     } 
234     else if ( isChanged )
235     {
236       aViewer->Repaint();
237     }
238   }
239 }
240
241 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
242 {
243   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
244   if( aViewer )
245   {
246     SALOME_ListIO aListIO;
247     aViewer->GetVisible( aListIO );
248
249     HYDROGUI_VTKPrs* aPrs;
250     SALOME_ListIteratorOfListIO anIter( aListIO );
251     for( ; anIter.More(); anIter.Next() )
252     {
253       Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
254       if ( !aPrsObj.IsNull() )
255       {
256         Handle(HYDROData_Entity) anOwnerObj = 
257           module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
258         if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
259         {
260           aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
261           if ( aPrs )
262           {
263             aViewer->Erase( aPrs );
264           }
265           module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
266         }
267       }
268     }
269   }
270 }
271
272 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const Handle(HYDROData_Entity)& theObj )
273 {
274   HYDROGUI_VTKPrsDriver* aDriver = NULL;
275   ObjectKind aKind = theObj->GetKind();
276   if( theObj->GetKind() == KIND_BATHYMETRY )
277   {
278     if ( !myDriver )
279     {
280       myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBar.GetPointer() );
281     }
282     aDriver = myDriver;
283   }
284
285   return aDriver;
286 }
287
288 QString HYDROGUI_VTKPrsDisplayer::GetType() const
289 {
290   return SVTK_Viewer::Type();
291 }
292
293 void HYDROGUI_VTKPrsDisplayer::SetZRange( double theRange[] )
294 {
295   myScalarBar->GetLookupTable()->SetRange( theRange );
296 }
297
298 double* HYDROGUI_VTKPrsDisplayer::GetZRange() const
299 {
300   return myScalarBar->GetLookupTable()->GetRange();
301 }