QGIS API Documentation 3.43.0-Master (9e873c7bc91)
Loading...
Searching...
No Matches
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#include "qgscamerapose.h"
21
22#include <Qt3DCore/QEntity>
23#include <Qt3DInput/QMouseEvent>
24#include <QImage>
25
26#ifndef SIP_RUN
27namespace Qt3DInput
28{
29 class QKeyEvent;
30 class QKeyboardHandler;
31 class QMouseEvent;
32 class QMouseHandler;
33 class QWheelEvent;
34} // namespace Qt3DInput
35
36namespace Qt3DRender
37{
38 class QCamera;
39}
40
41#endif
42
43class QDomDocument;
44class QDomElement;
45
46class QgsCameraPose;
47class QgsVector3D;
49class Qgs3DMapScene;
50
55#ifndef SIP_RUN
56class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
57{
58#else
59class _3D_EXPORT QgsCameraController : public QObject
60{
61#endif
62
63 Q_OBJECT
64 public:
68
69#ifndef SIP_RUN
70
75 Qt3DRender::QCamera *camera() const { return mCamera; }
76#endif
77
82 Qgis::NavigationMode cameraNavigationMode() const { return mCameraNavigationMode; }
83
88 double cameraMovementSpeed() const { return mCameraMovementSpeed; }
89
94 void setCameraMovementSpeed( double movementSpeed );
95
100 Qgis::VerticalAxisInversion verticalAxisInversion() const { return mVerticalAxisInversion; }
101
106 void setVerticalAxisInversion( Qgis::VerticalAxisInversion inversion );
107
109 void frameTriggered( float dt );
110
112 void resetView( float distance );
113
115 void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
116
118 QgsVector3D lookingAtPoint() const;
119
126 void setLookingAtPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
127
132 QgsVector3D lookingAtMapPoint() const;
133
138 void setLookingAtMapPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
139
144 void setCameraPose( const QgsCameraPose &camPose, bool force = false );
145
150 QgsCameraPose cameraPose() const { return mCameraPose; }
151
157 float distance() const { return mCameraPose.distanceFromCenterPoint(); }
158
164 float pitch() const { return mCameraPose.pitchAngle(); }
165
171 float yaw() const { return mCameraPose.headingAngle(); }
172
174 QDomElement writeXml( QDomDocument &doc ) const;
176 void readXml( const QDomElement &elem );
177
179 void zoom( float factor );
181 void tiltUpAroundViewCenter( float deltaPitch );
183 void rotateAroundViewCenter( float deltaYaw );
185 void setCameraHeadingAngle( float angle );
187 void moveView( float tx, float ty );
188
193 void walkView( double tx, double ty, double tz );
194
201 void rotateCamera( float diffPitch, float diffYaw );
202
208 void rotateCameraAroundPivot( float newPitch, float newHeading, const QVector3D &pivotPoint );
209
216 void zoomCameraAroundPivot( const QVector3D &oldCameraPosition, double zoomFactor, const QVector3D &pivotPoint );
217
223 bool willHandleKeyEvent( QKeyEvent *event );
224
231 void setOrigin( const QgsVector3D &origin );
232
237 void setInputHandlersEnabled( bool enable ) { mInputHandlersEnabled = enable; }
238
243 bool hasInputHandlersEnabled() const { return mInputHandlersEnabled; }
244
245 public slots:
246
251 void setCameraNavigationMode( Qgis::NavigationMode navigationMode );
252
257 void depthBufferCaptured( const QImage &depthImage );
258
259 private:
260#ifdef SIP_RUN
263#endif
264
265 void updateCameraFromPose();
266 void moveCameraPositionBy( const QVector3D &posDiff );
268 QWindow *window() const;
269
271 enum class MouseOperation
272 {
273 None = 0, // no operation
274 Translation, // left button pressed, no modifier
275 RotationCamera, // left button pressed + ctrl modifier
276 RotationCenter, // left button pressed + shift modifier
277 Zoom, // right button pressed
278 ZoomWheel // mouse wheel scroll
279 };
280
281 // This list gathers all the rotation and translation operations.
282 // It is used to update the appropriate parameters when successive
283 // translation and rotation happen.
284 const QList<MouseOperation> mTranslateOrRotate = {
285 MouseOperation::Translation,
286 MouseOperation::RotationCamera,
287 MouseOperation::RotationCenter
288 };
289
290 // check that current sequence (current operation and new operation) is a rotation or translation
291 bool isATranslationRotationSequence( MouseOperation newOperation ) const;
292
293 void setMouseParameters( const MouseOperation &newOperation, const QPoint &clickPoint = QPoint() );
294
295 signals:
298
301
305 void cameraMovementSpeedChanged( double speed );
306
311 void setCursorPosition( QPoint point );
312
318
323 void cameraRotationCenterChanged( QVector3D position );
324
325 private slots:
326 void onPositionChanged( Qt3DInput::QMouseEvent *mouse );
327 void onWheel( Qt3DInput::QWheelEvent *wheel );
328 void onMousePressed( Qt3DInput::QMouseEvent *mouse );
329 void onMouseReleased( Qt3DInput::QMouseEvent *mouse );
330 void onKeyPressed( Qt3DInput::QKeyEvent *event );
331 void onKeyReleased( Qt3DInput::QKeyEvent *event );
332 void applyFlyModeKeyMovements();
333
334 private:
335 void onKeyPressedFlyNavigation( Qt3DInput::QKeyEvent *event );
336 void onKeyPressedTerrainNavigation( Qt3DInput::QKeyEvent *event );
337 void onPositionChangedFlyNavigation( Qt3DInput::QMouseEvent *mouse );
338 void onPositionChangedTerrainNavigation( Qt3DInput::QMouseEvent *mouse );
339
340 void handleTerrainNavigationWheelZoom();
341
346 double sampleDepthBuffer( int px, int py );
347
348#ifndef SIP_RUN
350 bool screenPointToWorldPos( QPoint position, Qt3DRender::QCamera *cameraBefore, double &depth, QVector3D &worldPosition );
351#endif
352
354 Qgs3DMapScene *mScene = nullptr;
355
357 Qt3DRender::QCamera *mCamera = nullptr;
358
360 QgsCameraPose mCameraPose;
361
363 QPoint mMousePos;
364
366 QPoint mClickPoint;
367
368 bool mDepthBufferIsReady = false;
369 QImage mDepthBufferImage;
370 // -1 when unset
371 // TODO: Change to std::optional<double>
372 double mDepthBufferNonVoidAverage = -1;
373
374 std::unique_ptr<Qt3DRender::QCamera> mCameraBefore;
375
376 bool mRotationCenterCalculated = false;
377 QVector3D mRotationCenter;
378 double mRotationDistanceFromCenter = 0;
379 double mRotationPitch = 0;
380 double mRotationYaw = 0;
381
382 bool mDragPointCalculated = false;
383 QVector3D mDragPoint;
384 double mDragDepth;
385
386 bool mZoomPointCalculated = false;
387 QVector3D mZoomPoint;
388
389 Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
390 Qt3DInput::QKeyboardHandler *mKeyboardHandler = nullptr;
391 bool mInputHandlersEnabled = true;
394 double mCameraMovementSpeed = 5.0;
395
396 QSet<int> mDepressedKeys;
397 bool mCaptureFpsMouseMovements = false;
398 bool mIgnoreNextMouseMove = false;
399 QTimer *mFpsNavTimer = nullptr;
400
401 double mCumulatedWheelY = 0;
402
403 MouseOperation mCurrentOperation = MouseOperation::None;
404
405 // 3D world's origin in map coordinates
406 QgsVector3D mOrigin;
407
409 bool mCameraChanged = false;
410
411 // To test the cameracontroller
412 friend class TestQgs3DRendering;
413 friend class TestQgs3DCameraController;
414};
415
416#endif // QGSCAMERACONTROLLER_H
VerticalAxisInversion
Vertical axis inversion options for 3D views.
Definition qgis.h:4009
@ WhenDragging
Invert vertical axis movements when dragging in first person modes.
NavigationMode
The navigation mode used by 3D cameras.
Definition qgis.h:3997
@ TerrainBased
The default navigation based on the terrain.
void navigationModeChanged(Qgis::NavigationMode mode)
Emitted when the navigation mode is changed using the hotkey ctrl + ~.
bool hasInputHandlersEnabled() const
Returns whether the camera controller responds to mouse and keyboard events.
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 setInputHandlersEnabled(bool enable)
Sets whether the camera controller responds to mouse and keyboard events.
void setCursorPosition(QPoint point)
Emitted when the mouse cursor position should be moved to the specified point on the map viewport.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
#define SIP_SKIP
Definition qgis_sip.h:126