1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_Displayer.h"
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"
32 #include <GraphicsView_Viewer.h>
33 #include <GraphicsView_ViewPort.h>
35 HYDROGUI_Displayer::HYDROGUI_Displayer( HYDROGUI_Module* theModule )
36 : myModule( theModule )
40 HYDROGUI_Displayer::~HYDROGUI_Displayer()
44 void HYDROGUI_Displayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
45 const int theViewerId )
47 GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
51 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
55 GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
56 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
58 Handle(HYDROData_Object) anObj = theObjs.Value( i );
62 if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
63 aPrs->setIsToUpdate( true );
67 void HYDROGUI_Displayer::UpdateAll( const int theViewerId,
69 const bool theIsForced )
72 EraseAll( theViewerId );
73 DisplayAll( theViewerId, theIsForced );
76 void HYDROGUI_Displayer::EraseAll( const int theViewerId )
78 GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
82 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
86 GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
87 while( anIter.hasNext() )
89 if( GraphicsView_Object* anObject = anIter.next() )
91 aViewPort->removeItem( anObject );
97 void HYDROGUI_Displayer::DisplayAll( const int theViewerId,
98 const bool theIsForced )
100 HYDROData_SequenceOfObjects aSeq;
101 HYDROGUI_Tool::GetPrsSubObjects( myModule, aSeq );
102 Update( aSeq, theViewerId, theIsForced );
105 void HYDROGUI_Displayer::Update( const HYDROData_SequenceOfObjects& theObjs,
106 const int theViewerId,
107 const bool theIsForced )
109 // First of all, kill all bad presentations
110 purgeObjects( theViewerId );
112 // Now dig in the data model
113 HYDROData_SequenceOfObjects anObjectsToErase, anObjectsToDisplay;
115 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
117 const Handle(HYDROData_Object)& anObj = theObjs.Value( i );
119 anObjectsToErase.Append( anObj );
121 anObjectsToDisplay.Append( anObj );
124 if( anObjectsToErase.Length() )
125 Erase( anObjectsToErase, theViewerId );
126 if( anObjectsToDisplay.Length() )
127 Display( anObjectsToDisplay, theViewerId, theIsForced );
130 void HYDROGUI_Displayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
131 const int theViewerId )
133 GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
137 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
141 HYDROGUI_DataModel* aModel = (HYDROGUI_DataModel*)myModule->dataModel();
144 GraphicsView_ObjectList anObjectList = HYDROGUI_Tool::GetPrsList( aViewPort );
145 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
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 ) )
151 aViewPort->removeItem( aPrs );
158 void HYDROGUI_Displayer::Display( const HYDROData_SequenceOfObjects& theObjs,
159 const int theViewerId,
160 const bool theIsForced )
162 GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
166 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
170 bool anIsDisplayed = false;
171 GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
172 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
174 Handle(HYDROData_Object) anObj = theObjs.Value( i );
178 HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList );
180 bool anIsInserted = ( aPrs != 0 );
181 if( !aPrs || aPrs->getIsToUpdate() || theIsForced )
183 if( HYDROGUI_PrsDriver* aDriver = getDriver( anObj ) )
185 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
186 aViewPort->addItem( aPrs );
192 bool anIsVisible = myModule->isObjectVisible( (size_t)aViewer, anObj );
193 aPrs->setVisible( anIsVisible );
197 aViewPort->onBoundingRectChanged(); // specific of HYDRO module
201 void HYDROGUI_Displayer::purgeObjects( const int theViewerId )
203 GraphicsView_Viewer* aViewer = myModule->getViewer( theViewerId );
207 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
211 GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
212 while( anIter.hasNext() )
214 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
216 Handle(HYDROData_Object) anObject = aPrs->getObject();
217 if( !anObject.IsNull() && anObject->IsRemoved() )
219 aViewPort->removeItem( aPrs );
226 HYDROGUI_PrsDriver* HYDROGUI_Displayer::getDriver( const Handle(HYDROData_Object)& theObj )
228 HYDROGUI_PrsDriver* aDriver = NULL;
229 ObjectKind aKind = theObj->GetKind();
230 PrsDriversMap::iterator anIter = myPrsDriversMap.find( aKind );
231 if( anIter != myPrsDriversMap.end() )
232 aDriver = anIter.value();
238 aDriver = new HYDROGUI_PrsImageDriver();
239 myPrsDriversMap[ aKind ] = aDriver;
242 aDriver = new HYDROGUI_PrsPolylineDriver();
243 myPrsDriversMap[ aKind ] = aDriver;