QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 <QPointer>
22 #include <QRect>
23 #include <Qt3DCore/QEntity>
24 
25 namespace Qt3DInput
26 {
27  class QKeyEvent;
28  class QKeyboardDevice;
29  class QKeyboardHandler;
30  class QMouseEvent;
31  class QMouseDevice;
32  class QMouseHandler;
33  class QWheelEvent;
34 }
35 
36 namespace Qt3DRender
37 {
38  class QCamera;
39  class QPickEvent;
40 }
41 
42 #include "qgscamerapose.h"
43 
44 class QDomDocument;
45 class QDomElement;
46 
47 class QgsCameraPose;
48 class QgsTerrainEntity;
49 class QgsVector3D;
50 
51 #define SIP_NO_FILE
52 
59 class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
60 {
61  Q_OBJECT
62  Q_PROPERTY( Qt3DRender::QCamera *camera READ camera WRITE setCamera NOTIFY cameraChanged )
63  Q_PROPERTY( QRect viewport READ viewport WRITE setViewport NOTIFY viewportChanged )
64  public:
66  QgsCameraController( Qt3DCore::QNode *parent = nullptr );
67 
69  Qt3DRender::QCamera *camera() const { return mCamera; }
71  QRect viewport() const { return mViewport; }
72 
78  void setTerrainEntity( QgsTerrainEntity *te );
79 
81  void setCamera( Qt3DRender::QCamera *camera );
83  void setViewport( QRect viewport );
85  void frameTriggered( float dt );
86 
88  void resetView( float distance );
89 
91  void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
92 
94  QgsVector3D lookingAtPoint() const;
95 
102  void setLookingAtPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
103 
108  void setCameraPose( const QgsCameraPose &camPose );
109 
114  QgsCameraPose cameraPose() const { return mCameraPose; }
115 
120  float distance() const { return mCameraPose.distanceFromCenterPoint(); }
121 
127  float pitch() const { return mCameraPose.pitchAngle(); }
128 
134  float yaw() const { return mCameraPose.headingAngle(); }
135 
137  QDomElement writeXml( QDomDocument &doc ) const;
139  void readXml( const QDomElement &elem );
140 
142  void zoom( float factor );
144  void tiltUpAroundViewCenter( float deltaPitch );
146  void rotateAroundViewCenter( float deltaYaw );
148  void setCameraHeadingAngle( float angle );
150  void moveView( float tx, float ty );
151 
152  private:
153  void rotateCamera( float diffPitch, float diffYaw );
154  void updateCameraFromPose( bool centerPointChanged = false );
155 
156  signals:
161 
162  private slots:
163  void onPositionChanged( Qt3DInput::QMouseEvent *mouse );
164  void onWheel( Qt3DInput::QWheelEvent *wheel );
165  void onMousePressed( Qt3DInput::QMouseEvent *mouse );
166  void onMouseReleased( Qt3DInput::QMouseEvent *mouse );
167  void onKeyPressed( Qt3DInput::QKeyEvent *event );
168  void onKeyReleased( Qt3DInput::QKeyEvent *event );
169  void onPickerMousePressed( Qt3DRender::QPickEvent *pick );
170 
171  private:
173  Qt3DRender::QCamera *mCamera = nullptr;
175  QRect mViewport;
177  float mLastPressedHeight = 0;
178 
179  QPointer<QgsTerrainEntity> mTerrainEntity;
180 
182  QgsCameraPose mCameraPose;
183 
185  QPoint mMousePos;
186 
188  Qt3DInput::QMouseDevice *mMouseDevice = nullptr;
189  Qt3DInput::QKeyboardDevice *mKeyboardDevice = nullptr;
190 
191  Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
192  Qt3DInput::QKeyboardHandler *mKeyboardHandler = nullptr;
193 
194 };
195 
196 #endif // QGSCAMERACONTROLLER_H
QgsCameraController::yaw
float yaw() const
Returns yaw angle in degrees.
Definition: qgscameracontroller.h:134
QgsVector3D
3 Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double preci...
Definition: qgsvector3d.h:32
QgsCameraController::cameraPose
QgsCameraPose cameraPose() const
Returns camera pose.
Definition: qgscameracontroller.h:114
QgsCameraController::pitch
float pitch() const
Returns pitch angle in degrees (0 = looking from the top, 90 = looking from the side).
Definition: qgscameracontroller.h:127
QgsCameraController::viewport
QRect viewport() const
Returns viewport rectangle.
Definition: qgscameracontroller.h:71
QgsCameraController::camera
Qt3DRender::QCamera * camera() const
Returns camera that is being controlled.
Definition: qgscameracontroller.h:69
Qt3DInput
Definition: qgscameracontroller.h:26
Qt3DRender
Definition: qgs3dmapscene.h:27
QgsCameraController::viewportChanged
void viewportChanged()
Emitted when viewport rectangle has been updated.
QgsCameraPose
3 Class that encapsulates camera pose in a 3D scene.
Definition: qgscamerapose.h:45
qgscamerapose.h
QgsCameraController::distance
float distance() const
Returns distance of the camera from the point it is looking at.
Definition: qgscameracontroller.h:120
QgsCameraController
3 Object that controls camera movement based on user input
Definition: qgscameracontroller.h:60
QgsCameraController::cameraChanged
void cameraChanged()
Emitted when camera has been updated.
MathUtils::angle
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