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>
45 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
47 HYDROGUI_Displayer::HYDROGUI_Displayer( HYDROGUI_Module* theModule )
48 : HYDROGUI_AbstractDisplayer( theModule ),
49 myXPosition( -1 ), myYPosition( -1 ), myIsPositionSaved( false )
53 HYDROGUI_Displayer::~HYDROGUI_Displayer()
57 void HYDROGUI_Displayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
58 const int theViewerId )
60 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
64 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
68 GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
69 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
71 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
75 if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
76 aPrs->setIsToUpdate( true );
80 void HYDROGUI_Displayer::EraseAll( const int theViewerId )
82 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
86 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
90 GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
91 while( anIter.hasNext() )
93 if( GraphicsView_Object* anObject = anIter.next() )
95 aViewPort->removeItem( anObject );
101 void HYDROGUI_Displayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
102 const int theViewerId )
104 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
108 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
112 HYDROGUI_DataModel* aModel = (HYDROGUI_DataModel*)module()->dataModel();
115 GraphicsView_ObjectList anObjectList = HYDROGUI_Tool::GetPrsList( aViewPort );
116 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
118 // the object may be null or dead
119 const Handle(HYDROData_Entity)& anObj = theObjs.Value( i );
120 if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
122 aViewPort->removeItem( aPrs );
123 anObjectList.removeAll( aPrs );
130 void HYDROGUI_Displayer::Display( const HYDROData_SequenceOfObjects& theObjs,
131 const int theViewerId,
132 const bool theIsForced,
133 const bool theDoFitAll)
135 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
139 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
143 bool anIsDisplayed = false;
144 GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
145 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
147 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
151 HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList );
153 bool anIsInserted = ( aPrs != 0 );
154 if( !aPrs || aPrs->getIsToUpdate() || theIsForced )
156 if( HYDROGUI_PrsDriver* aDriver = getDriver( anObj ) )
158 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
159 aViewPort->addItem( aPrs );
165 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
166 aPrs->setVisible( anIsVisible );
170 aViewPort->onBoundingRectChanged(); // specific of HYDRO module
177 void HYDROGUI_Displayer::purgeObjects( const int theViewerId )
179 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
183 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
187 GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
188 while( anIter.hasNext() )
190 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
192 Handle(HYDROData_Entity) anObject = aPrs->getObject();
193 if( !anObject.IsNull() && anObject->IsRemoved() )
195 aViewPort->removeItem( aPrs );
202 HYDROGUI_PrsDriver* HYDROGUI_Displayer::getDriver( const Handle(HYDROData_Entity)& theObj )
204 HYDROGUI_PrsDriver* aDriver = NULL;
205 ObjectKind aKind = theObj->GetKind();
206 PrsDriversMap::iterator anIter = myPrsDriversMap.find( aKind );
207 if( anIter != myPrsDriversMap.end() )
208 aDriver = anIter.value();
214 aDriver = new HYDROGUI_PrsImageDriver();
216 case KIND_POLYLINEXY:
217 aDriver = new HYDROGUI_PrsPolylineDriver();
220 aDriver = new HYDROGUI_PrsZoneDriver();
227 myPrsDriversMap[ aKind ] = aDriver;
233 QString HYDROGUI_Displayer::GetType() const
235 return GraphicsView_Viewer::Type();
238 void HYDROGUI_Displayer::SaveCursorViewPosition( SUIT_ViewWindow* theViewWindow )
240 myIsPositionSaved = false;
244 SUIT_ViewWindow* aViewWindow = theViewWindow;
245 if ( !theViewWindow ) {
246 SUIT_ViewManager* aViewMgr = module()->getApp()->activeViewManager();
247 aViewWindow = aViewMgr ? aViewMgr->getActiveView() : 0;
252 OCCViewer_ViewWindow* anOCCViewWindow =
253 dynamic_cast<OCCViewer_ViewWindow*>( aViewWindow );
254 if ( anOCCViewWindow ) {
255 // Get the selected point coordinates
256 OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
258 QPoint aViewPos = aViewPort->mapFromGlobal( QCursor::pos() );
259 myXPosition = aViewPos.x();
260 myYPosition = aViewPos.y();
261 myIsPositionSaved = true;
265 SVTK_ViewWindow* aVTKViewWindow =
266 dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
267 if ( aVTKViewWindow ) {
268 vtkRenderer* aRen = aVTKViewWindow->getRenderer();
271 vtkCamera* aCamera = aRen->GetActiveCamera();
272 double* aNormal = aCamera->GetViewPlaneNormal();
273 vtkRenderWindowInteractor* anInteractor = aVTKViewWindow->getInteractor();
276 anInteractor->GetLastEventPosition( myXPosition, myYPosition );
277 myIsPositionSaved = true;
282 if (!myIsPositionSaved)
286 bool HYDROGUI_Displayer::GetCursorViewCoordinates( SUIT_ViewWindow* theViewWindow,
287 double& theXCoordinate,
288 double& theYCoordinate,
289 double& theZCoordinate )
295 if ( !theViewWindow || !myIsPositionSaved )
298 OCCViewer_ViewWindow* anOCCViewWindow =
299 dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
300 if ( anOCCViewWindow ) {
301 // Get the selected point coordinates
302 OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
306 gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( myXPosition, myYPosition,
307 aViewPort->getView() );
308 theXCoordinate = aPnt.X();
309 theYCoordinate = aPnt.Y();
314 SVTK_ViewWindow* aVTKViewWindow =
315 dynamic_cast<SVTK_ViewWindow*>(theViewWindow);
316 if ( aVTKViewWindow ) {
317 vtkRenderer* aRen = aVTKViewWindow->getRenderer();
320 vtkCamera* aCamera = aRen->GetActiveCamera();
321 double* aNormal = aCamera->GetViewPlaneNormal();
322 myPicker->Pick( myXPosition, myYPosition, 0, aRen );
323 double* aCoords = myPicker->GetPickPosition();
324 /////////////////////// Use the same algorithm as for OCC
326 double aXp, aYp, aZp;
334 Standard_Real aPrec = LOCAL_SELECTION_TOLERANCE;
335 if ( fabs( Vz ) > aPrec ) {
341 else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
343 if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
345 else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
348 /////////////////////////
349 theXCoordinate = aXp;
350 theYCoordinate = aYp;