Salome HOME
refs #417: showing new objects
[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_VTKPrsShapeDriver.h"
30 #include "HYDROGUI_Tool.h"
31
32 #include "HYDROData_Tool.h"
33 #include <SVTK_ViewModel.h>
34 #include <SVTK_ViewWindow.h>
35 #include <SALOME_ListIO.hxx>
36 #include <SALOME_ListIteratorOfListIO.hxx>
37 #include <SALOME_InteractiveObject.hxx>
38 #include <SUIT_ViewManager.h>
39 #include <SUIT_Accel.h>
40
41 #include <vtkLookupTable.h>
42 #include <vtkRenderer.h>
43 #include <vtkTextProperty.h>
44 #include <vtkWindow.h>
45 #include <vtkActor2DCollection.h>
46
47 #include <QVector>
48
49 #define NB_COLORS 32
50
51 // Saturation of blue
52 //#define HUE_START 0.69 
53 //#define HUE_END   0.41
54 //#define SATURATION_START 1.0 
55 //#define SATURATION_END   0.4
56
57 #define HUE_START 0.7
58 #define HUE_END   0.0 
59 #define SATURATION_START 1.0 
60 #define SATURATION_END   1.0
61
62 HYDROGUI_VTKPrsDisplayer::HYDROGUI_VTKPrsDisplayer( HYDROGUI_Module* theModule )
63 : HYDROGUI_AbstractDisplayer( theModule ), myDriver( NULL ), myShapeDriver( NULL )
64 {
65 }
66
67 HYDROGUI_VTKPrsDisplayer::~HYDROGUI_VTKPrsDisplayer()
68 {
69 }
70
71 void HYDROGUI_VTKPrsDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
72                                       const int theViewerId )
73 {
74   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
75   if( !aViewer )
76   {
77     HYDROGUI_VTKPrs* anObjShape;
78     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
79     {
80       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
81       if( !anObj.IsNull() )
82       {
83         anObjShape = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
84         if ( anObjShape )
85         {
86           anObjShape->setIsToUpdate( true );
87         }
88       }
89     }
90   }
91 }
92
93 void HYDROGUI_VTKPrsDisplayer::EraseAll( const int theViewerId )
94 {
95   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
96   if( aViewer )
97   {
98     aViewer->EraseAll( true );
99     module()->removeViewVTKPrs( (size_t)aViewer );
100     EraseScalarBar( theViewerId );
101   }
102 }
103
104 void HYDROGUI_VTKPrsDisplayer::EraseScalarBar( const int theViewerId, const bool theIsDelete )
105 {
106   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
107   if( aViewer )
108   {
109     if ( myScalarBars.contains( (size_t)aViewer ) )
110     {
111       SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
112       if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
113       {
114         SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>( aViewMgr->getActiveView() );
115         if ( aView )
116         {
117           vtkScalarBarActor* aScalarBar = myScalarBars[ (size_t)aViewer ];
118           if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
119           {
120             aView->getRenderer()->RemoveActor2D( aScalarBar );
121           }
122         }
123       }
124       if ( theIsDelete )
125       {
126         myScalarBars.remove( (size_t)aViewer );
127       }
128     }
129   }
130 }
131
132 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
133                                 const int theViewerId )
134 {
135   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
136   if( aViewer )
137   {
138     HYDROGUI_VTKPrs* aPrs;
139     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
140     {
141       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
142       if( anObj.IsNull() )
143         continue;
144
145       aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
146       if ( aPrs )
147       {
148         aViewer->Erase( aPrs, true );
149       }
150       if ( anObj->IsRemoved() )
151       {
152         module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
153       }
154     }
155   }
156 }
157
158 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
159                                         const int theViewerId,
160                                         const bool theIsForced,
161                                         const bool theDoFitAll)
162 {
163   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
164   if( aViewer )
165   {
166     // Hide colors legend bar
167     SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
168       aViewer->getViewManager()->getActiveView() );
169     vtkScalarBarActor* aScalarBar = 0;
170     if ( aView )
171     {
172       if ( !myScalarBars.contains( (size_t)aViewer ) )
173       {
174         createScalarBar( (size_t)aViewer );
175       }
176       aScalarBar = myScalarBars[ (size_t)aViewer ];
177
178       if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
179       {
180         aView->getRenderer()->RemoveActor2D( aScalarBar );
181       }
182     }
183
184     // Invalidate global Z range
185     double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
186     SetZRange( (size_t)aViewer, anInvalidRange );
187
188     int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
189     bool isChanged = false;
190     bool isScalarBarNeeded = false;
191     HYDROGUI_VTKPrs* aPrs;
192     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
193     {
194       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
195       if( !anObj.IsNull() )
196       {
197         bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
198         aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
199
200         bool anIsInserted = ( aPrs != 0 );
201         if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
202         {
203           // Erase the presentation in the view because of the problem with GEOM_Actor upadting on second SetShape.
204           if ( aPrs )
205           {
206             aViewer->Erase( aPrs, true );
207           }
208           // Update the presentation
209           if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( (size_t)aViewer, anObj ) )
210           {
211             if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
212             {
213               module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
214             }
215           }
216         }
217
218         if( aPrs )
219         {
220           if ( anIsVisible )
221           {
222             if ( aPrs->needScalarBar() )
223             {
224               // Extend the global Z range if necessary
225               double* aGlobalRange = GetZRange( (size_t)aViewer );
226               double* aRange = aPrs->getInternalZRange();
227               bool anIsUpdate = false;
228               if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
229               {
230                 aGlobalRange[0] = aRange[0];
231                 anIsUpdate = true;
232               }
233               if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
234               {
235                 aGlobalRange[1] = aRange[1];
236                 anIsUpdate = true;
237               }
238
239               if ( anIsUpdate )
240               {
241                 module()->updateVTKZRange( (size_t)aViewer, aGlobalRange );
242               }
243             }
244
245             aViewer->Display( aPrs );
246             isScalarBarNeeded = isScalarBarNeeded || aPrs->needScalarBar();
247
248           }
249           else
250           {
251             aViewer->Erase( aPrs );
252           }
253           isChanged = true;
254         }
255       }
256     }
257
258     if ( aView ) 
259     {
260       if ( isChanged && isScalarBarNeeded && aScalarBar )
261       {
262         // Show colors legend bar
263           aView->getRenderer()->AddActor2D( aScalarBar );
264       }
265
266       // Refresh the view
267       if ( theDoFitAll )
268       {
269         // Repaint is done inside OnFitAll()
270         aView->onAccelAction( SUIT_Accel::ZoomFit );
271       } 
272       else if ( isChanged )
273       {
274         aView->Repaint( true );
275       }
276     }
277   }
278 }
279
280 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
281 {
282   bool doEraseScalarBar = false;
283
284   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
285   if( aViewer )
286   {
287     SALOME_ListIO aListIO;
288     aViewer->GetVisible( aListIO );
289     
290     HYDROGUI_VTKPrs* aPrs;
291     SALOME_ListIteratorOfListIO anIter( aListIO );
292     for( ; anIter.More(); anIter.Next() )
293     {
294       Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
295       if ( !aPrsObj.IsNull() )
296       {
297         Handle(HYDROData_Entity) anOwnerObj = 
298           module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
299         if ( anOwnerObj.IsNull() )
300         {
301           // We found an unknown IO presented in the viewer. 
302           // Remove such an orphan presentation from all the views of the viewer.
303           SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
304           if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
305           {
306             QVector<SUIT_ViewWindow*> aViews = aViewMgr->getViews();
307             foreach ( SUIT_ViewWindow* aView, aViews )
308             {
309               SVTK_ViewWindow* aVTKView = dynamic_cast<SVTK_ViewWindow*>( aView );
310               if ( aVTKView )
311               {
312                 aVTKView->Erase( aPrsObj, false );
313               }
314             }
315           }
316           // Remove the object presentation from the module's cache
317           module()->removeObjectVTKPrs( (size_t)aViewer, aPrsObj->getEntry() );
318           doEraseScalarBar = true;
319         }
320         else if ( anOwnerObj->IsRemoved() )
321         {
322           aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
323           if ( aPrs )
324           {
325             aViewer->Erase( aPrs );
326           }
327           else
328           {
329           }
330           module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
331           doEraseScalarBar = true;
332         }
333       }
334     }
335
336     if ( doEraseScalarBar )
337     {
338       EraseScalarBar( theViewerId );
339     }
340   }
341 }
342
343 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const int theViewId, const Handle(HYDROData_Entity)& theObj )
344 {
345   HYDROGUI_VTKPrsDriver* aDriver = NULL;
346   ObjectKind aKind = theObj->GetKind();
347   if( theObj->GetKind() == KIND_BATHYMETRY )
348   {
349     if ( !myDriver )
350     {
351       myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBars[ theViewId ] );
352     }
353     aDriver = myDriver;
354   }
355   else
356   {
357     if ( !myShapeDriver )
358     {
359       myShapeDriver = new HYDROGUI_VTKPrsShapeDriver( myScalarBars[ theViewId ] );
360     }
361     aDriver = myShapeDriver;
362   }
363
364   return aDriver;
365 }
366
367 QString HYDROGUI_VTKPrsDisplayer::GetType() const
368 {
369   return SVTK_Viewer::Type();
370 }
371
372 void HYDROGUI_VTKPrsDisplayer::SetZRange( const int theViewId, double theRange[] )
373 {
374   myScalarBars[ theViewId ]->GetLookupTable()->SetRange( theRange );
375 }
376
377 double* HYDROGUI_VTKPrsDisplayer::GetZRange( const int theViewId ) const
378 {
379   return myScalarBars[ theViewId ]->GetLookupTable()->GetRange();
380 }
381
382 void HYDROGUI_VTKPrsDisplayer::createScalarBar( const int theViewId )
383 {
384   if ( !myScalarBars.contains( theViewId ) )
385   {
386     // The invalid value is used to identify the case when the table range is not initialized yet.
387     double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
388     vtkLookupTable* aTable = vtkLookupTable::New();
389     aTable->SetHueRange( HUE_START, HUE_END );
390     aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
391     aTable->SetTableRange( anInvalidValue, anInvalidValue );
392     aTable->SetValueRange( 1.0, 1.0 );
393     aTable->SetAlphaRange( 1.0, 1.0 );
394     aTable->SetNumberOfColors( NB_COLORS );
395     aTable->Build();
396     vtkSmartPointer<vtkScalarBarActor> aScalarBar = vtkScalarBarActor::New();
397     aScalarBar->SetLookupTable( aTable );
398     aScalarBar->SetNumberOfLabels( NB_COLORS * 0.75 );
399     aScalarBar->SetWidth( aScalarBar->GetWidth() / 1.5 );
400     aScalarBar->SetTextureGridWidth( aScalarBar->GetTextureGridWidth() * 4. );
401     aTable->Delete();
402     myScalarBars.insert( theViewId, aScalarBar );
403   }
404 }