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 <LightApp_Application.h>
36 #include <SVTK_ViewWindow.h>
37 #include <OCCViewer_ViewWindow.h>
38 #include <OCCViewer_ViewPort3d.h>
39 #include <SUIT_ViewManager.h>
41 #include <vtkRenderWindowInteractor.h>
42 #include <vtkRenderer.h>
43 #include <vtkWorldPointPicker.h>
44 #include <vtkCamera.h>
46 #include <GraphicsView_Viewer.h>
47 #include <GraphicsView_ViewPort.h>
49 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
51 HYDROGUI_Displayer::HYDROGUI_Displayer( HYDROGUI_Module* theModule )
52 : HYDROGUI_AbstractDisplayer( theModule ),
53 myXPosition( -1 ), myYPosition( -1 ), myIsPositionSaved( false )
57 HYDROGUI_Displayer::~HYDROGUI_Displayer()
61 void HYDROGUI_Displayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
62 const int theViewerId )
64 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
68 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
72 GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
73 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
75 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
79 if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
80 aPrs->setIsToUpdate( true );
84 void HYDROGUI_Displayer::EraseAll( const int theViewerId )
86 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
90 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
94 GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
95 while( anIter.hasNext() )
97 if( GraphicsView_Object* anObject = anIter.next() )
99 aViewPort->removeItem( anObject );
105 void HYDROGUI_Displayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
106 const int theViewerId )
108 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
112 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
116 HYDROGUI_DataModel* aModel = (HYDROGUI_DataModel*)module()->dataModel();
119 GraphicsView_ObjectList anObjectList = HYDROGUI_Tool::GetPrsList( aViewPort );
120 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
122 // the object may be null or dead
123 const Handle(HYDROData_Entity)& anObj = theObjs.Value( i );
124 if( HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList ) )
126 aViewPort->removeItem( aPrs );
127 anObjectList.removeAll( aPrs );
134 void HYDROGUI_Displayer::Display( const HYDROData_SequenceOfObjects& theObjs,
135 const int theViewerId,
136 const bool theIsForced,
137 const bool theDoFitAll)
139 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
143 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
147 bool anIsDisplayed = false;
148 GraphicsView_ObjectList anObjectList = aViewPort->getObjects();
149 for( int i = 1, n = theObjs.Length(); i <= n; i++ )
151 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
155 HYDROGUI_Prs* aPrs = HYDROGUI_Tool::GetPresentation( anObj, anObjectList );
157 bool anIsInserted = ( aPrs != 0 );
158 if( !aPrs || aPrs->getIsToUpdate() || theIsForced )
160 if( HYDROGUI_PrsDriver* aDriver = getDriver( anObj ) )
162 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
163 aViewPort->addItem( aPrs );
169 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
170 aPrs->setVisible( anIsVisible );
174 aViewPort->onBoundingRectChanged(); // specific of HYDRO module
181 void HYDROGUI_Displayer::purgeObjects( const int theViewerId )
183 GraphicsView_Viewer* aViewer = module()->getViewer( theViewerId );
187 GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
191 GraphicsView_ObjectListIterator anIter( HYDROGUI_Tool::GetPrsList( aViewPort ) );
192 while( anIter.hasNext() )
194 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
196 Handle(HYDROData_Entity) anObject = aPrs->getObject();
197 if( !anObject.IsNull() && anObject->IsRemoved() )
199 aViewPort->removeItem( aPrs );
206 HYDROGUI_PrsDriver* HYDROGUI_Displayer::getDriver( const Handle(HYDROData_Entity)& theObj )
208 HYDROGUI_PrsDriver* aDriver = NULL;
209 ObjectKind aKind = theObj->GetKind();
210 PrsDriversMap::iterator anIter = myPrsDriversMap.find( aKind );
211 if( anIter != myPrsDriversMap.end() )
212 aDriver = anIter.value();
218 aDriver = new HYDROGUI_PrsImageDriver();
220 case KIND_POLYLINEXY:
221 aDriver = new HYDROGUI_PrsPolylineDriver();
224 aDriver = new HYDROGUI_PrsZoneDriver();
231 myPrsDriversMap[ aKind ] = aDriver;
237 QString HYDROGUI_Displayer::GetType() const
239 return GraphicsView_Viewer::Type();
242 void HYDROGUI_Displayer::SaveCursorViewPosition( SUIT_ViewWindow* theViewWindow )
244 myIsPositionSaved = false;
248 SUIT_ViewWindow* aViewWindow = theViewWindow;
249 if ( !theViewWindow ) {
250 SUIT_ViewManager* aViewMgr = module()->getApp()->activeViewManager();
251 aViewWindow = aViewMgr ? aViewMgr->getActiveView() : 0;
256 OCCViewer_ViewWindow* anOCCViewWindow =
257 dynamic_cast<OCCViewer_ViewWindow*>( aViewWindow );
258 if ( anOCCViewWindow ) {
259 // Get the selected point coordinates
260 OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
262 QPoint aViewPos = aViewPort->mapFromGlobal( QCursor::pos() );
263 myXPosition = aViewPos.x();
264 myYPosition = aViewPos.y();
265 myIsPositionSaved = true;
269 SVTK_ViewWindow* aVTKViewWindow =
270 dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
271 if ( aVTKViewWindow ) {
272 vtkRenderer* aRen = aVTKViewWindow->getRenderer();
275 vtkCamera* aCamera = aRen->GetActiveCamera();
276 double* aNormal = aCamera->GetViewPlaneNormal();
277 vtkRenderWindowInteractor* anInteractor = aVTKViewWindow->getInteractor();
280 anInteractor->GetLastEventPosition( myXPosition, myYPosition );
281 myIsPositionSaved = true;
286 if (!myIsPositionSaved)
290 bool HYDROGUI_Displayer::GetCursorViewCoordinates( SUIT_ViewWindow* theViewWindow,
291 double& theXCoordinate,
292 double& theYCoordinate,
293 double& theZCoordinate )
299 if ( !theViewWindow || !myIsPositionSaved )
302 OCCViewer_ViewWindow* anOCCViewWindow =
303 dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
304 if ( anOCCViewWindow ) {
305 // Get the selected point coordinates
306 OCCViewer_ViewPort3d* aViewPort = anOCCViewWindow->getViewPort();
310 gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( myXPosition, myYPosition,
311 aViewPort->getView() );
312 theXCoordinate = aPnt.X();
313 theYCoordinate = aPnt.Y();
318 SVTK_ViewWindow* aVTKViewWindow =
319 dynamic_cast<SVTK_ViewWindow*>(theViewWindow);
320 if ( aVTKViewWindow ) {
321 vtkRenderer* aRen = aVTKViewWindow->getRenderer();
324 vtkCamera* aCamera = aRen->GetActiveCamera();
325 double* aNormal = aCamera->GetViewPlaneNormal();
326 myPicker->Pick( myXPosition, myYPosition, 0, aRen );
327 double* aCoords = myPicker->GetPickPosition();
328 /////////////////////// Use the same algorithm as for OCC
330 double aXp, aYp, aZp;
338 Standard_Real aPrec = LOCAL_SELECTION_TOLERANCE;
339 if ( fabs( Vz ) > aPrec ) {
345 else { // Vz = 0 - the eyed plane is orthogonal to Z plane - XOZ, or YOZ
347 if ( fabs( Vy ) < aPrec ) // Vy = 0 - the YOZ plane
349 else if ( fabs( Vx ) < aPrec ) // Vx = 0 - the XOZ plane
352 /////////////////////////
353 theXCoordinate = aXp;
354 theYCoordinate = aYp;