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