Salome HOME
Fix for the #trefs 121: undo leads error
[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       if ( anObj->IsRemoved() )
101       {
102         module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
103       }
104     }
105   }
106 }
107
108 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
109                                         const int theViewerId,
110                                         const bool theIsForced,
111                                         const bool theDoFitAll)
112 {
113   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
114   if( aViewer )
115   {
116     bool isChanged = false;
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       {
123         bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
124         aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
125
126         bool anIsInserted = ( aPrs != 0 );
127         if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
128         {
129           if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( anObj ) )
130           {
131             if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
132             {
133               module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
134             }
135           }
136         }
137
138         if( aPrs )
139         {
140           if ( anIsVisible )
141           {
142             aViewer->Display( aPrs );
143           }
144           else
145           {
146             aViewer->Erase( aPrs );
147           }
148           isChanged = true;
149         }
150       }
151     }
152
153     if ( theDoFitAll )
154     {
155       // Repaint is done inside OnFitAll()
156       aViewer->getViewManager()->getActiveView()->onAccelAction( SUIT_Accel::ZoomFit );
157     } 
158     else if ( isChanged )
159     {
160       aViewer->Repaint();
161     }
162   }
163 }
164
165 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
166 {
167   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
168   if( aViewer )
169   {
170     SALOME_ListIO aListIO;
171     aViewer->GetVisible( aListIO );
172
173     HYDROGUI_VTKPrs* aPrs;
174     SALOME_ListIteratorOfListIO anIter( aListIO );
175     for( ; anIter.More(); anIter.Next() )
176     {
177       Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
178       if ( !aPrsObj.IsNull() )
179       {
180         Handle(HYDROData_Entity) anOwnerObj = 
181           module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
182         if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
183         {
184           aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
185           if ( aPrs )
186           {
187             aViewer->Erase( aPrs );
188           }
189           module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
190         }
191       }
192     }
193   }
194 }
195
196 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const Handle(HYDROData_Entity)& theObj )
197 {
198   HYDROGUI_VTKPrsDriver* aDriver = NULL;
199   ObjectKind aKind = theObj->GetKind();
200   if( theObj->GetKind() == KIND_BATHYMETRY )
201   {
202     if ( !myDriver )
203     {
204       myDriver = new HYDROGUI_VTKPrsBathymetryDriver();
205     }
206     aDriver = myDriver;
207   }
208
209   return aDriver;
210 }
211
212 QString HYDROGUI_VTKPrsDisplayer::GetType() const
213 {
214   return SVTK_Viewer::Type();
215 }