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