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