Salome HOME
- VTKPrsDisplayer is introduced for Bathymetry visualization.
[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 <SVTK_ViewModel.h>
32 #include <SVTK_ViewWindow.h>
33 #include <SALOME_ListIO.hxx>
34 #include <SALOME_ListIteratorOfListIO.hxx>
35 #include <SALOME_InteractiveObject.hxx>
36 #include <SUIT_ViewManager.h>
37 #include <SUIT_Accel.h>
38
39 #include <QVector>
40
41 HYDROGUI_VTKPrsDisplayer::HYDROGUI_VTKPrsDisplayer( HYDROGUI_Module* theModule )
42 : HYDROGUI_AbstractDisplayer( theModule ), myDriver( NULL )
43 {
44 }
45
46 HYDROGUI_VTKPrsDisplayer::~HYDROGUI_VTKPrsDisplayer()
47 {
48 }
49
50 void HYDROGUI_VTKPrsDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
51                                       const int theViewerId )
52 {
53   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
54   if( !aViewer )
55   {
56     HYDROGUI_VTKPrs* anObjShape;
57     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
58     {
59       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
60       if( !anObj.IsNull() )
61       {
62         anObjShape = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
63         if ( anObjShape )
64         {
65           anObjShape->setIsToUpdate( true );
66         }
67       }
68     }
69   }
70 }
71
72 void HYDROGUI_VTKPrsDisplayer::EraseAll( const int theViewerId )
73 {
74   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
75   if( aViewer )
76   {
77     aViewer->EraseAll( true );
78     module()->removeViewVTKPrs( (size_t)aViewer );
79   }
80 }
81
82 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
83                                 const int theViewerId )
84 {
85   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
86   if( aViewer )
87   {
88     HYDROGUI_VTKPrs* aPrs;
89     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
90     {
91       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
92       if( anObj.IsNull() )
93         continue;
94
95       aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
96       if ( aPrs )
97       {
98         aViewer->Erase( aPrs, true );
99       }
100       module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
101     }
102   }
103 }
104
105 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
106                                         const int theViewerId,
107                                         const bool theIsForced,
108                                         const bool theDoFitAll)
109 {
110   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
111   if( aViewer )
112   {
113     bool isChanged = false;
114     HYDROGUI_VTKPrs* aPrs;
115     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
116     {
117       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
118       if( !anObj.IsNull() )
119       {
120         bool anIsVisible = module()->isObjectVisible( (size_t)(aViewer->getViewManager()->getViewModel()), anObj );
121         aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
122
123         bool anIsInserted = ( aPrs != 0 );
124         if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
125         {
126           if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( anObj ) )
127           {
128             if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
129             {
130               module()->setObjectVTKPrs( theViewerId, anObj, aPrs );
131               aViewer->Display( aPrs );
132               isChanged = true;
133             }
134           }
135         }
136
137         if( aPrs && !anIsVisible )
138         {
139           aViewer->Erase( aPrs );
140           isChanged = true;
141         }
142       }
143     }
144
145     if ( theDoFitAll )
146     {
147       // Repaint is done inside OnFitAll()
148       aViewer->getViewManager()->getActiveView()->onAccelAction( SUIT_Accel::ZoomFit );
149     } 
150     else if ( isChanged )
151     {
152       aViewer->Repaint();
153     }
154   }
155 }
156
157 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
158 {
159   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
160   if( aViewer )
161   {
162     SALOME_ListIO aListIO;
163     aViewer->GetVisible( aListIO );
164
165     HYDROGUI_VTKPrs* aPrs;
166     SALOME_ListIteratorOfListIO anIter( aListIO );
167     for( ; anIter.More(); anIter.Next() )
168     {
169       Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
170       if ( !aPrsObj.IsNull() )
171       {
172         Handle(HYDROData_Entity) anOwnerObj = 
173           module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
174         if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
175         {
176           aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
177           if ( aPrs )
178           {
179             aViewer->Erase( aPrs );
180           }
181           module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
182         }
183       }
184     }
185   }
186 }
187
188 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const Handle(HYDROData_Entity)& theObj )
189 {
190   HYDROGUI_VTKPrsDriver* aDriver = NULL;
191   ObjectKind aKind = theObj->GetKind();
192   if( theObj->GetKind() == KIND_BATHYMETRY )
193   {
194     if ( !myDriver )
195     {
196       myDriver = new HYDROGUI_VTKPrsBathymetryDriver();
197     }
198     aDriver = myDriver;
199   }
200
201   return aDriver;
202 }
203
204 QString HYDROGUI_VTKPrsDisplayer::GetType() const
205 {
206   return SVTK_Viewer::Type();
207 }