]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_Displayer.cxx
Salome HOME
a99292ae0a714423475286734f15a47418d14626
[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_Tool.h"
31
32 #include <GraphicsView_Viewer.h>
33 #include <GraphicsView_ViewPort.h>
34
35 HYDROGUI_Displayer::HYDROGUI_Displayer( HYDROGUI_Module* theModule )
36 : myModule( theModule )
37 {
38 }
39
40 HYDROGUI_Displayer::~HYDROGUI_Displayer()
41 {
42 }
43
44 void HYDROGUI_Displayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
45                                       const int theViewerId )
46 {
47   GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
48   if( !aViewer )
49     return;
50
51   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
52   if( !aViewPort )
53     return;
54
55   GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
56   for( int i = 1, n = theObjs.Length(); i <= n; i++ )
57   {
58     Handle(HYDROData_Object) anObj = theObjs.Value( i );
59     if( anObj.IsNull() )
60       continue;
61
62     if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
63       aPrs->setIsToUpdate( true );
64   }
65 }
66
67 void HYDROGUI_Displayer::UpdateAll( const int theViewerId,
68                                     const bool theIsInit,
69                                     const bool theIsForced )
70 {
71   if( theIsInit )
72     EraseAll( theViewerId );
73   DisplayAll( theViewerId, theIsForced );
74 }
75
76 void HYDROGUI_Displayer::EraseAll( const int theViewerId )
77 {
78   GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
79   if( !aViewer )
80     return;
81
82   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
83   if( !aViewPort )
84     return;
85
86   GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
87   while( anIter.hasNext() )
88   {
89     if( GraphicsView_Object* anObject = anIter.next() )
90     {
91       aViewPort->removeItem( anObject );
92       delete anObject;
93     }
94   }
95 }
96
97 void HYDROGUI_Displayer::DisplayAll( const int theViewerId,
98                                      const bool theIsForced )
99 {
100   HYDROGUI_DataModel* aModel = (HYDROGUI_DataModel*)myModule->dataModel();
101   if( aModel ) 
102   {
103     HYDROData_SequenceOfObjects aSeq;
104     HYDROGUI_Tool::GetPrsSubObjects( aModel, theViewerId, aSeq );
105     Update( aSeq, theViewerId, theIsForced );
106   }
107 }
108
109 void HYDROGUI_Displayer::Update( const HYDROData_SequenceOfObjects& theObjs,
110                                  const int theViewerId,
111                                  const bool theIsForced )
112 {
113   // First of all, kill all bad presentations
114   purgeObjects( theViewerId );
115
116   // Now dig in the data model
117   HYDROData_SequenceOfObjects anObjectsToErase, anObjectsToDisplay;
118
119   for( int i = 1, n = theObjs.Length(); i <= n; i++ )
120   {
121     const Handle(HYDROData_Object)& anObj = theObjs.Value( i );
122     if( anObj.IsNull() )
123       anObjectsToErase.Append( anObj );
124     else
125       anObjectsToDisplay.Append( anObj );
126   }
127
128   if( anObjectsToErase.Length() )
129     Erase( anObjectsToErase, theViewerId );
130   if( anObjectsToDisplay.Length() )
131     Display( anObjectsToDisplay, theViewerId, theIsForced );
132 }
133
134 void HYDROGUI_Displayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
135                                 const int theViewerId )
136 {
137   GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
138   if( !aViewer )
139     return;
140
141   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
142   if( !aViewPort )
143     return;
144
145   HYDROGUI_DataModel* aModel = (HYDROGUI_DataModel*)myModule->dataModel();
146   if( aModel ) 
147   {
148     GraphicsView_ObjectList anObjectList = HYDROGUI_Tool::GetPrsList( aViewPort );
149     for( int i = 1, n = theObjs.Length(); i <= n; i++ )
150     {
151       // the object may be null or dead
152       const Handle(HYDROData_Object)& anObj = theObjs.Value( i );
153       if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
154       {
155         aViewPort->removeItem( aPrs );
156         delete aPrs;
157       }
158     }
159   }
160 }
161
162 void HYDROGUI_Displayer::Display( const HYDROData_SequenceOfObjects& theObjs,
163                                   const int theViewerId,
164                                   const bool theIsForced )
165 {
166   GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
167   if( !aViewer )
168     return;
169
170   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
171   if( !aViewPort )
172     return;
173
174   bool anIsDisplayed = false;
175   GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
176   for( int i = 1, n = theObjs.Length(); i <= n; i++ )
177   {
178     Handle(HYDROData_Object) anObj = theObjs.Value( i );
179     if( anObj.IsNull() )
180       continue;
181
182     HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList );
183
184     bool anIsInserted = ( aPrs != 0 );
185     if( !aPrs || aPrs->getIsToUpdate() || theIsForced )
186     {
187       if( HYDROGUI_PrsDriver* aDriver = getDriver( anObj ) )
188       {
189         if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
190           aViewPort->addItem( aPrs );
191       }
192     }
193
194     if( aPrs )
195     {
196       bool anIsVisible = anObj->GetVisibility();
197       aPrs->setVisible( anIsVisible );
198     }
199   }
200
201   aViewPort->fitAll();
202 }
203
204 void HYDROGUI_Displayer::purgeObjects( const int theViewerId )
205 {
206   GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
207   if( !aViewer )
208     return;
209
210   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
211   if( !aViewPort )
212     return;
213
214   GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
215   while( anIter.hasNext() )
216   {
217     if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
218     {
219       Handle(HYDROData_Object) anObject = aPrs->getObject();
220       if( !anObject.IsNull() && anObject->IsRemoved() )
221       {
222         aViewPort->removeItem( aPrs );
223         delete aPrs;
224       }
225     }
226   }
227 }
228
229 HYDROGUI_PrsDriver* HYDROGUI_Displayer::getDriver( const Handle(HYDROData_Object)& theObj )
230 {
231   HYDROGUI_PrsDriver* aDriver = NULL;
232   ObjectKind aKind = theObj->GetKind();
233   PrsDriversMap::iterator anIter = myPrsDriversMap.find( aKind );
234   if( anIter != myPrsDriversMap.end() )
235     aDriver = anIter.value();
236   else 
237   {
238     switch( aKind )
239     {
240       case KIND_IMAGE:
241         aDriver = new HYDROGUI_PrsImageDriver();
242         myPrsDriversMap[ aKind ] = aDriver;
243         break;
244       case KIND_POLYLINE:
245         aDriver = new HYDROGUI_PrsPolylineDriver();
246         myPrsDriversMap[ aKind ] = aDriver;
247         break;
248       default:
249         break;
250     }
251   }
252   return aDriver;
253 }