1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_Displayer.h"
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Prs.h"
24 #include "HYDROGUI_PrsImageDriver.h"
25 #include "HYDROGUI_PrsPolylineDriver.h"
26 #include "HYDROGUI_PrsZoneDriver.h"
27 #include "HYDROGUI_Tool2.h"
29 #include <CurveCreator_Utils.hxx>
31 #include <LightApp_Application.h>
32 #include <SVTK_ViewWindow.h>
33 #include <OCCViewer_ViewWindow.h>
34 #include <OCCViewer_ViewPort3d.h>
35 #include <SUIT_ViewManager.h>
37 #include <vtkRenderWindowInteractor.h>
38 #include <vtkRenderer.h>
39 #include <vtkWorldPointPicker.h>
40 #include <vtkCamera.h>
42 #include <GraphicsView_Viewer.h>
43 #include <GraphicsView_ViewPort.h>
46 #include "HYDRO_trace.hxx"
48 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
50 HYDROGUI_Displayer::HYDROGUI_Displayer( HYDROGUI_Module* theModule )
51 : HYDROGUI_AbstractDisplayer( theModule ),
52 myXPosition( -1 ), myYPosition( -1 ), myIsPositionSaved( false )
56 HYDROGUI_Displayer::~HYDROGUI_Displayer()
60 void HYDROGUI_Displayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
61 const size_t theViewerId )
63 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
67 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
71 GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
72 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
74 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
78 if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
79 aPrs->setIsToUpdate( true );
83 void HYDROGUI_Displayer::EraseAll( const size_t theViewerId )
85 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
89 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
93 GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
94 while( anIter.hasNext() )
96 if( GraphicsView_Object* anObject = anIter.next() )
98 aViewPort->removeItem( anObject );
104 void HYDROGUI_Displayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
105 const size_t theViewerId )
107 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
111 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
115 HYDROGUI_DataModel* aModel = (HYDROGUI_DataModel*)module()->dataModel();
118 GraphicsView_ObjectList anObjectList = HYDROGUI_Tool::GetPrsList( aViewPort );
119 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
121 // the object may be null or dead
122 const Handle(HYDROData_Entity)& anObj = theObjs.Value( i );
123 if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
125 aViewPort->removeItem( aPrs );
126 anObjectList.removeAll( aPrs );
133 void HYDROGUI_Displayer::Display( const HYDROData_SequenceOfObjects& theObjs,
134 const size_t theViewerId,
135 const bool theIsForced,
136 const bool theDoFitAll)
138 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
142 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
146 bool anIsDisplayed = false;
147 GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
148 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
150 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
154 HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList );
156 bool anIsInserted = ( aPrs != 0 );
157 if( !aPrs || aPrs->getIsToUpdate() || theIsForced )
159 if( HYDROGUI_PrsDriver* aDriver = getDriver( anObj ) )
161 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
163 DEBTRACE("Display, addItem");
164 aViewPort->addItem( aPrs );
171 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
172 aPrs->setVisible( anIsVisible );
176 aViewPort->onBoundingRectChanged(); // specific of HYDRO module
183 void HYDROGUI_Displayer::purgeObjects( const size_t theViewerId )
185 DEBTRACE("purgeObjects");
186 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
190 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
194 GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
195 while( anIter.hasNext() )
197 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
199 Handle(HYDROData_Entity) anObject = aPrs->getObject();
200 if( !anObject.IsNull() && anObject->IsRemoved() )
202 DEBTRACE("removeItem");
203 aViewPort->removeItem( aPrs );
210 HYDROGUI_PrsDriver* HYDROGUI_Displayer::getDriver( const Handle(HYDROData_Entity)& theObj )
212 HYDROGUI_PrsDriver* aDriver = NULL;
213 ObjectKind aKind = theObj->GetKind();
214 PrsDriversMap::iterator anIter = myPrsDriversMap.find( aKind );
215 if( anIter != myPrsDriversMap.end() )
216 aDriver = anIter.value();
222 aDriver = new HYDROGUI_PrsImageDriver();
224 case KIND_POLYLINEXY:
225 aDriver = new HYDROGUI_PrsPolylineDriver();
228 aDriver = new HYDROGUI_PrsZoneDriver();
235 myPrsDriversMap[ aKind ] = aDriver;
241 QString HYDROGUI_Displayer::GetType() const
243 return GraphicsView_Viewer::Type();
246 void HYDROGUI_Displayer::SaveCursorViewPosition( SUIT_ViewWindow* theViewWindow )
248 myIsPositionSaved = false;
252 SUIT_ViewWindow* aViewWindow = theViewWindow;
253 if ( !theViewWindow ) {
254 SUIT_ViewManager* aViewMgr = module()->getApp()->activeViewManager();
255 aViewWindow = aViewMgr ? aViewMgr->getActiveView() : 0;
260 OCCViewer_ViewWindow* anOCCViewWindow =
261 dynamic_cast<OCCViewer_ViewWindow*>( aViewWindow );
262 if ( anOCCViewWindow ) {
263 // Get the selected point coordinates
264 OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
266 QPoint aViewPos = aViewPort->mapFromGlobal( QCursor::pos() );
267 myXPosition = aViewPos.x();
268 myYPosition = aViewPos.y();
269 myIsPositionSaved = true;
273 SVTK_ViewWindow* aVTKViewWindow =
274 dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
275 if ( aVTKViewWindow ) {
276 vtkRenderer* aRen = aVTKViewWindow->getRenderer();
279 vtkCamera* aCamera = aRen->GetActiveCamera();
280 double* aNormal = aCamera->GetViewPlaneNormal();
281 vtkRenderWindowInteractor* anInteractor = aVTKViewWindow->getInteractor();
284 anInteractor->GetLastEventPosition( myXPosition, myYPosition );
285 myIsPositionSaved = true;
290 if (!myIsPositionSaved)
294 bool HYDROGUI_Displayer::GetCursorViewCoordinates( SUIT_ViewWindow* theViewWindow,
295 double& theXCoordinate,
296 double& theYCoordinate,
297 double& theZCoordinate )
303 if ( !theViewWindow || !myIsPositionSaved )
306 OCCViewer_ViewWindow* anOCCViewWindow =
307 dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
308 if ( anOCCViewWindow ) {
309 // Get the selected point coordinates
310 OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
314 gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( myXPosition, myYPosition,
315 aViewPort->getView() );
316 theXCoordinate = aPnt.X();
317 theYCoordinate = aPnt.Y();
322 SVTK_ViewWindow* aVTKViewWindow =
323 dynamic_cast<SVTK_ViewWindow*>(theViewWindow);
324 if ( aVTKViewWindow ) {
325 vtkRenderer* aRen = aVTKViewWindow->getRenderer();
328 vtkCamera* aCamera = aRen->GetActiveCamera();
329 double* aNormal = aCamera->GetViewPlaneNormal();
330 myPicker->Pick( myXPosition, myYPosition, 0, aRen );
331 double* aCoords = myPicker->GetPickPosition();
332 /////////////////////// Use the same algorithm as for OCC
334 double aXp, aYp, aZp;
342 Standard_Real aPrec = LOCAL_SELECTION_TOLERANCE;
343 if ( fabs( Vz ) > aPrec ) {
349 else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
351 if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
353 else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
356 /////////////////////////
357 theXCoordinate = aXp;
358 theYCoordinate = aYp;