QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 #include <Qt3DInput/QMouseEvent>
25 
26 namespace Qt3DInput
27 {
28  class QKeyEvent;
29  class QKeyboardDevice;
30  class QKeyboardHandler;
31  class QMouseEvent;
32  class QMouseDevice;
33  class QMouseHandler;
34  class QWheelEvent;
35 }
36 
37 namespace Qt3DRender
38 {
39  class QCamera;
40  class QPickEvent;
41 }
42 
43 #include "qgscamerapose.h"
44 
45 class QDomDocument;
46 class QDomElement;
47 
48 class QgsCameraPose;
49 class QgsTerrainEntity;
50 class QgsVector3D;
51 
52 #define SIP_NO_FILE
53 
60 class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
61 {
62  Q_OBJECT
63  Q_PROPERTY( Qt3DRender::QCamera *camera READ camera WRITE setCamera NOTIFY cameraChanged )
64  Q_PROPERTY( QRect viewport READ viewport WRITE setViewport NOTIFY viewportChanged )
65  public:
66 
69  {
71  WalkNavigation
72  };
73  Q_ENUM( NavigationMode )
74 
75 
77  {
81  };
82  Q_ENUM( VerticalAxisInversion )
83 
84  public:
86  QgsCameraController( Qt3DCore::QNode *parent = nullptr );
87 
89  Qt3DRender::QCamera *camera() const { return mCamera; }
91  QRect viewport() const { return mViewport; }
92 
97  QgsCameraController::NavigationMode cameraNavigationMode() const { return mCameraNavigationMode; }
98 
103  double cameraMovementSpeed() const { return mCameraMovementSpeed; }
104 
109  void setCameraMovementSpeed( double movementSpeed );
110 
115  QgsCameraController::VerticalAxisInversion verticalAxisInversion() const { return mVerticalAxisInversion; }
116 
121  void setVerticalAxisInversion( QgsCameraController::VerticalAxisInversion inversion );
122 
128  void setTerrainEntity( QgsTerrainEntity *te );
129 
131  void setCamera( Qt3DRender::QCamera *camera );
133  void setViewport( QRect viewport );
135  void frameTriggered( float dt );
136 
138  void resetView( float distance );
139 
141  void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
142 
144  QgsVector3D lookingAtPoint() const;
145 
152  void setLookingAtPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
153 
158  void setCameraPose( const QgsCameraPose &camPose );
159 
164  QgsCameraPose cameraPose() const { return mCameraPose; }
165 
170  float distance() const { return mCameraPose.distanceFromCenterPoint(); }
171 
177  float pitch() const { return mCameraPose.pitchAngle(); }
178 
184  float yaw() const { return mCameraPose.headingAngle(); }
185 
187  QDomElement writeXml( QDomDocument &doc ) const;
189  void readXml( const QDomElement &elem );
190 
192  void zoom( float factor );
194  void tiltUpAroundViewCenter( float deltaPitch );
196  void rotateAroundViewCenter( float deltaYaw );
198  void setCameraHeadingAngle( float angle );
200  void moveView( float tx, float ty );
201 
207  bool willHandleKeyEvent( QKeyEvent *event );
208 
209  public slots:
210 
215  void setCameraNavigationMode( QgsCameraController::NavigationMode navigationMode );
216 
217  private:
218  void rotateCamera( float diffPitch, float diffYaw );
219  void updateCameraFromPose( bool centerPointChanged = false );
220  void moveCameraPositionBy( const QVector3D &posDiff );
221 
222  signals:
229 
233  void cameraMovementSpeedChanged( double speed );
234 
239  void setCursorPosition( QPoint point );
240 
241  private slots:
242  void onPositionChanged( Qt3DInput::QMouseEvent *mouse );
243  void onWheel( Qt3DInput::QWheelEvent *wheel );
244  void onMousePressed( Qt3DInput::QMouseEvent *mouse );
245  void onMouseReleased( Qt3DInput::QMouseEvent *mouse );
246  void onKeyPressed( Qt3DInput::QKeyEvent *event );
247  void onKeyReleased( Qt3DInput::QKeyEvent *event );
248  void onPickerMousePressed( Qt3DRender::QPickEvent *pick );
249  void applyFlyModeKeyMovements();
250 
251  private:
252  void onKeyPressedFlyNavigation( Qt3DInput::QKeyEvent *event );
253  void onKeyPressedTerrainNavigation( Qt3DInput::QKeyEvent *event );
254  void onPositionChangedFlyNavigation( Qt3DInput::QMouseEvent *mouse );
255  void onPositionChangedTerrainNavigation( Qt3DInput::QMouseEvent *mouse );
256 
257  private:
259  Qt3DRender::QCamera *mCamera = nullptr;
261  QRect mViewport;
263  float mLastPressedHeight = 0;
264 
265  QPointer<QgsTerrainEntity> mTerrainEntity;
266 
268  QgsCameraPose mCameraPose;
269 
271  QPoint mMousePos;
272  bool mMousePressed = false;
273  Qt3DInput::QMouseEvent::Buttons mPressedButton = Qt3DInput::QMouseEvent::Buttons::NoButton;
274 
276  Qt3DInput::QMouseDevice *mMouseDevice = nullptr;
277  Qt3DInput::QKeyboardDevice *mKeyboardDevice = nullptr;
278 
279  Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
280  Qt3DInput::QKeyboardHandler *mKeyboardHandler = nullptr;
281  NavigationMode mCameraNavigationMode = NavigationMode::TerrainBasedNavigation;
282  VerticalAxisInversion mVerticalAxisInversion = WhenDragging;
283  double mCameraMovementSpeed = 5.0;
284 
285  QSet< int > mDepressedKeys;
286  bool mCaptureFpsMouseMovements = false;
287  bool mIgnoreNextMouseMove = false;
288  QTimer *mFpsNavTimer = nullptr;
289 };
290 
291 #endif // QGSCAMERACONTROLLER_H
QgsCameraController::NavigationMode cameraNavigationMode() const
Returns the navigation mode used by the camera controller.
QgsCameraPose cameraPose() const
Returns camera pose.
QRect viewport() const
Returns viewport rectangle.
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.
float yaw() const
Returns yaw angle in degrees.
void navigationModeHotKeyPressed(QgsCameraController::NavigationMode mode)
Emitted when the navigation mode is changed using the hotkey ctrl + ~.
float distance() const
Returns distance of the camera from the point it is looking at.
VerticalAxisInversion
Vertical axis inversion options.
@ WhenDragging
Invert vertical axis movements when dragging in first person modes.
@ Always
Always invert vertical axis movements.
@ Never
Never invert vertical axis movements.
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.
QgsCameraController::VerticalAxisInversion verticalAxisInversion() const
Returns the vertical axis inversion behavior.
NavigationMode
The navigation mode used by the camera.
@ TerrainBasedNavigation
The default navigation based on the terrain.
void viewportChanged()
Emitted when viewport rectangle has been updated.
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