Salome HOME
Removing of presentations from view corrected.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Displayer.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_Displayer.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Prs.h"
28 #include "HYDROGUI_PrsImageDriver.h"
29 #include "HYDROGUI_PrsPolylineDriver.h"
30 #include "HYDROGUI_PrsZoneDriver.h"
31 #include "HYDROGUI_Tool.h"
32
33 #include <GraphicsView_Viewer.h>
34 #include <GraphicsView_ViewPort.h>
35
36 HYDROGUI_Displayer::HYDROGUI_Displayer( HYDROGUI_Module* theModule )
37 : HYDROGUI_AbstractDisplayer( theModule )
38 {
39 }
40
41 HYDROGUI_Displayer::~HYDROGUI_Displayer()
42 {
43 }
44
45 void HYDROGUI_Displayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
46                                       const int theViewerId )
47 {
48   GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
49   if( !aViewer )
50     return;
51
52   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
53   if( !aViewPort )
54     return;
55
56   GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
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       continue;
62
63     if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
64       aPrs->setIsToUpdate( true );
65   }
66 }
67
68 void HYDROGUI_Displayer::EraseAll( const int theViewerId )
69 {
70   GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
71   if( !aViewer )
72     return;
73
74   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
75   if( !aViewPort )
76     return;
77
78   GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
79   while( anIter.hasNext() )
80   {
81     if( GraphicsView_Object* anObject = anIter.next() )
82     {
83       aViewPort->removeItem( anObject );
84       delete anObject;
85     }
86   }
87 }
88
89 void HYDROGUI_Displayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
90                                 const int theViewerId )
91 {
92   GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
93   if( !aViewer )
94     return;
95
96   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
97   if( !aViewPort )
98     return;
99
100   HYDROGUI_DataModel* aModel = (HYDROGUI_DataModel*)module()->dataModel();
101   if( aModel ) 
102   {
103     GraphicsView_ObjectList anObjectList = HYDROGUI_Tool::GetPrsList( aViewPort );
104     for( int i = 1, n = theObjs.Length(); i <= n; i++ )
105     {
106       // the object may be null or dead
107       const Handle(HYDROData_Entity)& anObj = theObjs.Value( i );
108       if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
109       {
110         aViewPort->removeItem( aPrs );
111         anObjectList.removeAll( aPrs );
112         delete aPrs;
113       }
114     }
115   }
116 }
117
118 void HYDROGUI_Displayer::Display( const HYDROData_SequenceOfObjects& theObjs,
119                                   const int theViewerId,
120                                   const bool theIsForced,
121                                   const bool theDoFitAll)
122 {
123   GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
124   if( !aViewer )
125     return;
126
127   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
128   if( !aViewPort )
129     return;
130
131   bool anIsDisplayed = false;
132   GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
133   for( int i = 1, n = theObjs.Length(); i <= n; i++ )
134   {
135     Handle(HYDROData_Entity) anObj = theObjs.Value( i );
136     if( anObj.IsNull() )
137       continue;
138
139     HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList );
140
141     bool anIsInserted = ( aPrs != 0 );
142     if( !aPrs || aPrs->getIsToUpdate() || theIsForced )
143     {
144       if( HYDROGUI_PrsDriver* aDriver = getDriver( anObj ) )
145       {
146         if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
147           aViewPort->addItem( aPrs );
148       }
149     }
150
151     if( aPrs )
152     {
153       bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
154       aPrs->setVisible( anIsVisible );
155     }
156   }
157
158   aViewPort->onBoundingRectChanged(); // specific of HYDRO module
159   if ( theDoFitAll )
160   {
161     aViewPort->fitAll();
162   }
163 }
164
165 void HYDROGUI_Displayer::purgeObjects( const int theViewerId )
166 {
167   GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
168   if( !aViewer )
169     return;
170
171   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
172   if( !aViewPort )
173     return;
174
175   GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
176   while( anIter.hasNext() )
177   {
178     if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
179     {
180       Handle(HYDROData_Entity) anObject = aPrs->getObject();
181       if( !anObject.IsNull() && anObject->IsRemoved() )
182       {
183         aViewPort->removeItem( aPrs );
184         delete aPrs;
185       }
186     }
187   }
188 }
189
190 HYDROGUI_PrsDriver* HYDROGUI_Displayer::getDriver( const Handle(HYDROData_Entity)& theObj )
191 {
192   HYDROGUI_PrsDriver* aDriver = NULL;
193   ObjectKind aKind = theObj->GetKind();
194   PrsDriversMap::iterator anIter = myPrsDriversMap.find( aKind );
195   if( anIter != myPrsDriversMap.end() )
196     aDriver = anIter.value();
197   else 
198   {
199     switch( aKind )
200     {
201       case KIND_IMAGE:
202         aDriver = new HYDROGUI_PrsImageDriver();
203         break;
204       case KIND_POLYLINE:
205         aDriver = new HYDROGUI_PrsPolylineDriver();
206         break;
207       case KIND_ZONE:
208         aDriver = new HYDROGUI_PrsZoneDriver();
209         break;
210       default:
211         break;
212     }
213
214     if ( aDriver )
215       myPrsDriversMap[ aKind ] = aDriver;
216   }
217
218   return aDriver;
219 }
220
221 QString HYDROGUI_Displayer::GetType() const
222 {
223   return GraphicsView_Viewer::Type();
224 }