QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgscameracontroller.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscameracontroller.h
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSCAMERACONTROLLER_H
17#define QGSCAMERACONTROLLER_H
18
19#include "qgis_3d.h"
20
21#include <Qt3DCore/QEntity>
22#include <Qt3DInput/QMouseEvent>
23#include <QImage>
24
25#ifndef SIP_RUN
26namespace Qt3DInput
27{
28 class QKeyEvent;
29 class QKeyboardHandler;
30 class QMouseEvent;
31 class QMouseHandler;
32 class QWheelEvent;
33}
34
35namespace Qt3DRender
36{
37 class QCamera;
38}
39
40#endif
41
42#include "qgscamerapose.h"
43
44class QDomDocument;
45class QDomElement;
46
47class QgsCameraPose;
48class QgsVector3D;
50class Qgs3DMapScene;
51class QgsCameraController4Test;
52
59#ifndef SIP_RUN
60class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
61{
62#else
63class _3D_EXPORT QgsCameraController : public QObject
64{
65#endif
66
67 Q_OBJECT
68 public:
69
73
74#ifndef SIP_RUN
75
80 Qt3DRender::QCamera *camera() const { return mCamera; }
81#endif
82
87 Qgis::NavigationMode cameraNavigationMode() const { return mCameraNavigationMode; }
88
93 double cameraMovementSpeed() const { return mCameraMovementSpeed; }
94
99 void setCameraMovementSpeed( double movementSpeed );
100
105 Qgis::VerticalAxisInversion verticalAxisInversion() const { return mVerticalAxisInversion; }
106
111 void setVerticalAxisInversion( Qgis::VerticalAxisInversion inversion );
112
114 void frameTriggered( float dt );
115
117 void resetView( float distance );
118
120 void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
121
123 QgsVector3D lookingAtPoint() const;
124
131 void setLookingAtPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
132
137 void setCameraPose( const QgsCameraPose &camPose );
138
143 QgsCameraPose cameraPose() const { return mCameraPose; }
144
150 float distance() const { return mCameraPose.distanceFromCenterPoint(); }
151
157 float pitch() const { return mCameraPose.pitchAngle(); }
158
164 float yaw() const { return mCameraPose.headingAngle(); }
165
167 QDomElement writeXml( QDomDocument &doc ) const;
169 void readXml( const QDomElement &elem );
170
172 void zoom( float factor );
174 void tiltUpAroundViewCenter( float deltaPitch );
176 void rotateAroundViewCenter( float deltaYaw );
178 void setCameraHeadingAngle( float angle );
180 void moveView( float tx, float ty );
181
186 void walkView( double tx, double ty, double tz );
187
194 void rotateCamera( float diffPitch, float diffYaw );
195
201 bool willHandleKeyEvent( QKeyEvent *event );
202
203 public slots:
204
209 void setCameraNavigationMode( Qgis::NavigationMode navigationMode );
210
215 void depthBufferCaptured( const QImage &depthImage );
216
217 private:
218#ifdef SIP_RUN
221#endif
222
223 void updateCameraFromPose();
224 void moveCameraPositionBy( const QVector3D &posDiff );
226 QWindow *window() const;
227
228 signals:
231
234
238 void cameraMovementSpeedChanged( double speed );
239
244 void setCursorPosition( QPoint point );
245
251
256 void cameraRotationCenterChanged( QVector3D position );
257
258 private slots:
259 void onPositionChanged( Qt3DInput::QMouseEvent *mouse );
260 void onWheel( Qt3DInput::QWheelEvent *wheel );
261 void onMousePressed( Qt3DInput::QMouseEvent *mouse );
262 void onMouseReleased( Qt3DInput::QMouseEvent *mouse );
263 void onKeyPressed( Qt3DInput::QKeyEvent *event );
264 void onKeyReleased( Qt3DInput::QKeyEvent *event );
265 void applyFlyModeKeyMovements();
266
267 private:
268 void onKeyPressedFlyNavigation( Qt3DInput::QKeyEvent *event );
269 void onKeyPressedTerrainNavigation( Qt3DInput::QKeyEvent *event );
270 void onPositionChangedFlyNavigation( Qt3DInput::QMouseEvent *mouse );
271 void onPositionChangedTerrainNavigation( Qt3DInput::QMouseEvent *mouse );
272
273 void handleTerrainNavigationWheelZoom();
274
279 double sampleDepthBuffer( const QImage &buffer, int px, int py );
280
281#ifndef SIP_RUN
283 bool screenPointToWorldPos( QPoint position, Qt3DRender::QCamera *cameraBefore, double &depth, QVector3D &worldPosition );
284#endif
285
287 Qgs3DMapScene *mScene = nullptr;
288
290 Qt3DRender::QCamera *mCamera = nullptr;
291
293 QgsCameraPose mCameraPose;
294
296 QPoint mMousePos;
297 bool mMousePressed = false;
298 Qt3DInput::QMouseEvent::Buttons mPressedButton = Qt3DInput::QMouseEvent::Buttons::NoButton;
299
300 bool mDepthBufferIsReady = false;
301 QImage mDepthBufferImage;
302
303 QPoint mMiddleButtonClickPos;
304 bool mRotationCenterCalculated = false;
305 QVector3D mRotationCenter;
306 double mRotationDistanceFromCenter;
307 double mRotationPitch = 0;
308 double mRotationYaw = 0;
309 std::unique_ptr< Qt3DRender::QCamera > mCameraBeforeRotation;
310
311 QPoint mDragButtonClickPos;
312 std::unique_ptr< Qt3DRender::QCamera > mCameraBeforeDrag;
313 bool mDragPointCalculated = false;
314 QVector3D mDragPoint;
315 double mDragDepth;
316
317 bool mIsInZoomInState = false;
318 std::unique_ptr< Qt3DRender::QCamera > mCameraBeforeZoom;
319 bool mZoomPointCalculated = false;
320 QVector3D mZoomPoint;
321
322 Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
323 Qt3DInput::QKeyboardHandler *mKeyboardHandler = nullptr;
326 double mCameraMovementSpeed = 5.0;
327
328 QSet< int > mDepressedKeys;
329 bool mCaptureFpsMouseMovements = false;
330 bool mIgnoreNextMouseMove = false;
331 QTimer *mFpsNavTimer = nullptr;
332
333 double mCumulatedWheelY = 0;
334
335 friend QgsCameraController4Test;
336};
337
338#endif // QGSCAMERACONTROLLER_H
VerticalAxisInversion
Vertical axis inversion options for 3D views.
Definition: qgis.h:2624
@ WhenDragging
Invert vertical axis movements when dragging in first person modes.
NavigationMode
The navigation mode used by 3D cameras.
Definition: qgis.h:2612
@ TerrainBased
The default navigation based on the terrain.
void navigationModeChanged(Qgis::NavigationMode mode)
Emitted when the navigation mode is changed using the hotkey ctrl + ~.
QgsCameraPose cameraPose() const
Returns camera pose.
float pitch() const
Returns pitch angle in degrees (0 = looking from the top, 90 = looking from the side).
Qt3DRender::QCamera * camera() const
Returns camera that is being controlled.
~QgsCameraController() override
float yaw() const
Returns yaw angle in degrees.
void requestDepthBufferCapture()
Emitted to ask for the depth buffer image.
Qgis::VerticalAxisInversion verticalAxisInversion() const
Returns the vertical axis inversion behavior.
float distance() const
Returns distance of the camera from the point it is looking at.
double cameraMovementSpeed() const
Returns the camera movement speed.
void cameraChanged()
Emitted when camera has been updated.
void cameraMovementSpeedChanged(double speed)
Emitted whenever the camera movement speed is changed by the controller.
Qgis::NavigationMode cameraNavigationMode() const
Returns the navigation mode used by the camera controller.
void cameraRotationCenterChanged(QVector3D position)
Emitted when the camera rotation center changes.
void setCursorPosition(QPoint point)
Emitted when the mouse cursor position should be moved to the specified point on the map viewport.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
#define SIP_SKIP
Definition: qgis_sip.h:126