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_PrsZoneDriver.h"
31 #include "HYDROGUI_Tool.h"
33 #include <CurveCreator_Utils.h>
35 #include <SVTK_ViewWindow.h>
36 #include <OCCViewer_ViewWindow.h>
37 #include <OCCViewer_ViewPort3d.h>
39 #include <vtkRenderWindowInteractor.h>
40 #include <vtkRenderer.h>
41 #include <vtkWorldPointPicker.h>
42 #include <vtkCamera.h>
44 #include <GraphicsView_Viewer.h>
45 #include <GraphicsView_ViewPort.h>
47 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
49 HYDROGUI_Displayer::HYDROGUI_Displayer( HYDROGUI_Module* theModule )
50 : HYDROGUI_AbstractDisplayer( theModule )
54 HYDROGUI_Displayer::~HYDROGUI_Displayer()
58 void HYDROGUI_Displayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
59 const int theViewerId )
61 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
65 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
69 GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
70 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
72 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
76 if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
77 aPrs->setIsToUpdate( true );
81 void HYDROGUI_Displayer::EraseAll( const int theViewerId )
83 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
87 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
91 GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
92 while( anIter.hasNext() )
94 if( GraphicsView_Object* anObject = anIter.next() )
96 aViewPort->removeItem( anObject );
102 void HYDROGUI_Displayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
103 const int theViewerId )
105 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
109 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
113 HYDROGUI_DataModel* aModel = (HYDROGUI_DataModel*)module()->dataModel();
116 GraphicsView_ObjectList anObjectList = HYDROGUI_Tool::GetPrsList( aViewPort );
117 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
119 // the object may be null or dead
120 const Handle(HYDROData_Entity)& anObj = theObjs.Value( i );
121 if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
123 aViewPort->removeItem( aPrs );
124 anObjectList.removeAll( aPrs );
131 void HYDROGUI_Displayer::Display( const HYDROData_SequenceOfObjects& theObjs,
132 const int theViewerId,
133 const bool theIsForced,
134 const bool theDoFitAll)
136 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
140 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
144 bool anIsDisplayed = false;
145 GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
146 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
148 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
152 HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList );
154 bool anIsInserted = ( aPrs != 0 );
155 if( !aPrs || aPrs->getIsToUpdate() || theIsForced )
157 if( HYDROGUI_PrsDriver* aDriver = getDriver( anObj ) )
159 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
160 aViewPort->addItem( aPrs );
166 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
167 aPrs->setVisible( anIsVisible );
171 aViewPort->onBoundingRectChanged(); // specific of HYDRO module
178 void HYDROGUI_Displayer::purgeObjects( const int theViewerId )
180 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
184 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
188 GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
189 while( anIter.hasNext() )
191 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
193 Handle(HYDROData_Entity) anObject = aPrs->getObject();
194 if( !anObject.IsNull() && anObject->IsRemoved() )
196 aViewPort->removeItem( aPrs );
203 HYDROGUI_PrsDriver* HYDROGUI_Displayer::getDriver( const Handle(HYDROData_Entity)& theObj )
205 HYDROGUI_PrsDriver* aDriver = NULL;
206 ObjectKind aKind = theObj->GetKind();
207 PrsDriversMap::iterator anIter = myPrsDriversMap.find( aKind );
208 if( anIter != myPrsDriversMap.end() )
209 aDriver = anIter.value();
215 aDriver = new HYDROGUI_PrsImageDriver();
217 case KIND_POLYLINEXY:
218 aDriver = new HYDROGUI_PrsPolylineDriver();
221 aDriver = new HYDROGUI_PrsZoneDriver();
228 myPrsDriversMap[ aKind ] = aDriver;
234 QString HYDROGUI_Displayer::GetType() const
236 return GraphicsView_Viewer::Type();
240 bool HYDROGUI_Displayer::GetCursorViewCoordinates( SUIT_ViewWindow* theViewWindow,
241 const QPoint& theCursorPos,
242 double& theXCoordinate,
243 double& theYCoordinate,
244 double& theZCoordinate )
251 if ( !theViewWindow )
254 OCCViewer_ViewWindow* anOCCViewWindow =
255 dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
256 if ( anOCCViewWindow ) {
257 // Get the selected point coordinates
258 OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
262 QPoint aViewPos = aViewPort->mapFromGlobal( theCursorPos );
263 gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( aViewPos.x(), aViewPos.y(),
264 aViewPort->getView() );
265 theXCoordinate = aPnt.X();
266 theYCoordinate = aPnt.Y();
267 //std::cout << "Coordinates: " << theCursorPos.x() << ", " << theCursorPos.y() << std::endl;
272 SVTK_ViewWindow* aViewWindow =
273 dynamic_cast<SVTK_ViewWindow*>(theViewWindow);
275 vtkRenderer* aRen = aViewWindow->getRenderer();
278 vtkCamera* aCamera = aRen->GetActiveCamera();
279 double* aNormal = aCamera->GetViewPlaneNormal();
280 int event_x, event_y;
281 vtkRenderWindowInteractor* anInteractor = aViewWindow->getInteractor();
284 anInteractor->GetLastEventPosition(event_x, event_y);
285 // Use a WorldPicker to get current coords
286 //std::cout << "Coordinates: " << theCursorPos.x() << ", " << theCursorPos.y() << std::endl;
287 //SVTK_RenderWindowInteractor* anInteractor =
288 //std::cout << "VTK Coordinates: " << event_x << ", " << event_y << std::endl;
289 myPicker->Pick( event_x, event_y, 0, aRen );
290 double* aCoords = myPicker->GetPickPosition();
291 /////////////////////// Use the same algorithm as for OCC
293 double aXp, aYp, aZp;
301 Standard_Real aPrec = LOCAL_SELECTION_TOLERANCE;
302 if ( fabs( Vz ) > aPrec ) {
308 else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
310 if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
312 else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
315 /////////////////////////
316 theXCoordinate = aXp;
317 theYCoordinate = aYp;