16#include <Qt3DCore/QAspectEngine>
17#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
18#include <Qt3DCore/QCoreAspect>
20#include <Qt3DRender/QRenderSettings>
21#include <Qt3DRender/QRenderAspect>
22#include <Qt3DInput/QInputAspect>
23#include <Qt3DInput/QInputSettings>
24#include <Qt3DLogic/QLogicAspect>
25#include <Qt3DLogic/QFrameAction>
38#include "moc_qgs3dmapcanvas.cpp"
42 : m_aspectEngine( new
Qt3DCore::QAspectEngine )
43 , m_renderAspect( new
Qt3DRender::QRenderAspect )
44 , m_inputAspect( new
Qt3DInput::QInputAspect )
45 , m_logicAspect( new
Qt3DLogic::QLogicAspect )
46 , m_renderSettings( new
Qt3DRender::QRenderSettings )
48 , m_inputSettings( new
Qt3DInput::QInputSettings )
50 , m_userRoot( nullptr )
51 , m_initialized( false )
53 setSurfaceType( QSurface::OpenGLSurface );
56#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
57 m_aspectEngine->registerAspect(
new Qt3DCore::QCoreAspect );
59 m_aspectEngine->registerAspect( m_renderAspect );
60 m_aspectEngine->registerAspect( m_inputAspect );
61 m_aspectEngine->registerAspect( m_logicAspect );
63 m_defaultCamera->setParent( m_root );
64 m_inputSettings->setEventSource(
this );
70 image.save( mCaptureFileName, mCaptureFileFormat.toLocal8Bit().data() );
75 setCursor( Qt::OpenHandCursor );
76 installEventFilter(
this );
84 mScene->deleteLater();
86 mMapSettings->deleteLater();
87 mMapSettings =
nullptr;
88 qDeleteAll( mHighlights );
91 delete m_aspectEngine;
96 if ( m_userRoot != root )
99 m_userRoot->setParent(
static_cast<Qt3DCore::QNode *
>(
nullptr ) );
101 root->setParent( m_root );
113 return m_renderSettings->activeFrameGraph();
118 return m_defaultCamera;
123 return m_renderSettings;
128 if ( !m_initialized )
130 m_root->addComponent( m_renderSettings );
131 m_root->addComponent( m_inputSettings );
132 m_aspectEngine->setRootEntity( Qt3DCore::QEntityPtr( m_root ) );
134 m_initialized =
true;
136 QWindow::showEvent( e );
141 m_defaultCamera->setAspectRatio(
float( width() ) / std::max( 1.f,
static_cast<float>( height() ) ) );
149 Q_ASSERT( !mMapSettings );
159 mScene->deleteLater();
172 QCursor::setPos( mapToGlobal( point ) );
202 const float worldX = center.
x() - mMapSettings->
origin().
x();
203 const float worldY = center.
y() - mMapSettings->
origin().
y();
209 if ( !mScene || fileName.isEmpty() )
212 mCaptureFileName = fileName;
213 mCaptureFileFormat = fileFormat;
216 Qt3DLogic::QFrameAction *screenCaptureFrameAction =
new Qt3DLogic::QFrameAction;
217 mScene->addComponent( screenCaptureFrameAction );
219 connect( screenCaptureFrameAction, &Qt3DLogic::QFrameAction::triggered,
this, [=](
float ) {
221 mScene->removeComponent( screenCaptureFrameAction );
222 screenCaptureFrameAction->deleteLater();
226void Qgs3DMapCanvas::captureDepthBuffer()
232 Qt3DLogic::QFrameAction *screenCaptureFrameAction =
new Qt3DLogic::QFrameAction;
233 mScene->addComponent( screenCaptureFrameAction );
235 connect( screenCaptureFrameAction, &Qt3DLogic::QFrameAction::triggered,
this, [=](
float ) {
237 mScene->removeComponent( screenCaptureFrameAction );
238 screenCaptureFrameAction->deleteLater();
247 if ( tool == mMapTool )
251 if ( mMapTool && !tool )
254 setCursor( Qt::OpenHandCursor );
265 setCursor( mMapTool->
cursor() );
271 if ( watched !=
this )
280 if ( event->type() == QEvent::ShortcutOverride )
296 switch ( event->type() )
298 case QEvent::MouseButtonPress:
301 case QEvent::MouseButtonRelease:
304 case QEvent::MouseMove:
307 case QEvent::KeyPress:
308 mMapTool->
keyPressEvent(
static_cast<QKeyEvent *
>( event ) );
310 case QEvent::KeyRelease:
324 if ( mTemporalController )
327 mTemporalController = temporalController;
331void Qgs3DMapCanvas::updateTemporalRange(
const QgsDateTimeRange &temporalrange )
367 if ( !mHighlights.contains( layer ) )
373 band->setColor( color );
374 band->setMarkerType( QgsRubberBand3D::MarkerType::Square );
377 band->setWidth( pcRenderer->symbol()->pointSize() + 1 );
379 mHighlights.insert( layer, band );
383 mHighlights[layer]->addPoint( pt );
386void Qgs3DMapCanvas::updateHighlightSizes()
388 if (
QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( sender() ) )
392 if ( mHighlights.contains( layer ) )
394 mHighlights[layer]->setWidth( rnd->symbol()->pointSize() + 1 );
402 for (
auto it = mHighlights.keyBegin(); it != mHighlights.keyEnd(); it++ )
407 qDeleteAll( mHighlights );
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
NavigationMode
The navigation mode used by 3D cameras.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
bool handleEvent(QEvent *event)
Returns if the 3D axis controller will handle the specified event.
void saveAsImage(const QString &fileName, const QString &fileFormat)
Saves the current scene as an image.
QVector< QgsPointXY > viewFrustum2DExtent()
Calculates the 2D extent viewed by the 3D camera as the vertices of the viewed trapezoid.
Qgs3DMapSettings * mapSettings()
Returns access to the 3D scene configuration.
void setTemporalController(QgsTemporalController *temporalController)
Sets the temporal controller.
void mapSettingsChanged()
Emitted when the the map setting is changed.
Qt3DRender::QCamera * camera() const
Returns the default camera of the 3D Window.
void viewed2DExtentFrom3DChanged(QVector< QgsPointXY > extent)
Emitted when the viewed 2D extent seen by the 3D camera has changed.
void fpsCountChanged(float fpsCount)
Emitted when the FPS count changes (at most every frame)
void setRootEntity(Qt3DCore::QEntity *root)
Sets the specified root entity of the scene.
void setViewFromTop(const QgsPointXY ¢er, float distance, float rotation=0)
Sets camera position to look down at the given point (in map coordinates) in given distance from plan...
void setActiveFrameGraph(Qt3DRender::QFrameGraphNode *activeFrameGraph)
Activates the specified activeFrameGraph.
void setMapSettings(Qgs3DMapSettings *mapSettings)
Configure map scene being displayed. Takes ownership.
void showEvent(QShowEvent *e) override
Manages the display events specified in e.
void cameraNavigationSpeedChanged(double speed)
Emitted when the camera navigation speed is changed.
Qt3DRender::QRenderSettings * renderSettings() const
Returns the render settings of the 3D Window.
Qt3DRender::QFrameGraphNode * activeFrameGraph() const
Returns the node of the active frame graph.
void setMapTool(Qgs3DMapTool *tool)
Sets the active map tool that will receive events from the 3D canvas.
void setViewFrom2DExtent(const QgsRectangle &extent)
Resets camera view to show the extent (top view)
void resizeEvent(QResizeEvent *) override
Resets the aspect ratio of the 3D window.
void highlightFeature(const QgsFeature &feature, QgsMapLayer *layer)
Highlights a feature from layer using a QgsRubberBand3D.
void resetView()
Resets camera position to the default: looking down at the origin of world coordinates.
void savedAsImage(const QString &fileName)
Emitted when the 3D map canvas was successfully saved as image.
void clearHighlights()
Clears all QgsRubberBand3D highlights.
void fpsCounterEnabledChanged(bool enabled)
Emitted when the FPS counter is enabled or disabeld.
QgsCameraController * cameraController()
Returns access to the view's camera controller. Returns nullptr if the scene has not been initialized...
bool eventFilter(QObject *watched, QEvent *event) override
Entity that encapsulates our 3D scene - contains all other entities (such as terrain) as children.
Qgs3DAxis * get3DAxis() const
Returns the 3D axis object.
void viewed2DExtentFrom3DChanged(QVector< QgsPointXY > extent)
Emitted when the viewed 2D extent seen by the 3D camera has changed.
void fpsCountChanged(float fpsCount)
Emitted when the FPS count changes.
void setViewFrom2DExtent(const QgsRectangle &extent)
Resets camera view to show the extent extent (top view)
QgsCameraController * cameraController() const
Returns camera controller.
void updateTemporal()
Updates the temporale entities.
void fpsCounterEnabledChanged(bool fpsCounterEnabled)
Emitted when the FPS counter is activated or deactivated.
QVector< QgsPointXY > viewFrustum2DExtent() const
Calculates the 2D extent viewed by the 3D camera as the vertices of the viewed trapezoid.
void viewZoomFull()
Resets camera view to show the whole scene (top view)
void setCameraNavigationMode(Qgis::NavigationMode navigationMode)
Sets the navigation mode for the camera.
void setCameraMovementSpeed(double movementSpeed)
Sets the camera movement speed.
QgsVector3D origin() const
Returns coordinates in map CRS at which 3D scene has origin (0,0,0).
void requestCaptureImage()
Starts a request for an image rendered by the engine.
void requestDepthBufferCapture()
Starts a request for an image containing the depth buffer data of the engine.
void imageCaptured(const QImage &image)
Emitted after a call to requestCaptureImage() to return the captured image.
void depthBufferCaptured(const QImage &image)
Emitted after a call to requestDepthBufferCapture() to return the captured depth buffer.
void setRenderCaptureEnabled(bool enabled)
Sets whether it will be possible to render to an image.
QgsFrameGraph * frameGraph()
Returns the shadow rendering frame graph object used to render the scene.
Object that controls camera movement based on user input.
void navigationModeChanged(Qgis::NavigationMode mode)
Emitted when the navigation mode is changed using the hotkey ctrl + ~.
void requestDepthBufferCapture()
Emitted to ask for the depth buffer image.
void cameraMovementSpeedChanged(double speed)
Emitted whenever the camera movement speed is changed by the controller.
void setViewFromTop(float worldX, float worldY, float distance, float yaw=0)
Sets camera to look down towards given point in world coordinate, in given distance from plane with z...
void depthBufferCaptured(const QImage &depthImage)
Sets the depth buffer image used by the camera controller to calculate world position from a pixel's ...
void setCursorPosition(QPoint point)
Emitted when the mouse cursor position should be moved to the specified point on the map viewport.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Qt3DCore::QEntity * rubberBandsRootEntity()
Returns entity for all rubber bands (to show them always on top)
A geometry is the spatial representation of a feature.
QgsPoint vertexAt(int atVertex) const
Returns coordinates of a vertex.
Base class for all map layer types.
QgsAbstract3DRenderer * renderer3D() const
Returns 3D renderer associated with the layer.
void renderer3DChanged()
Signal emitted when 3D renderer associated with the layer has changed.
3D renderer that renders all points from a point cloud layer.
Point geometry type, with support for z-dimension and m-values.
A rectangle specified with double values.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
A controller base class for temporal objects, contains a signal for notifying updates of the objects ...
void updateTemporalRange(const QgsDateTimeRange &range)
Signals that a temporal range has changed and needs to be updated in all connected objects.
void setTemporalRange(const QgsDateTimeRange &range)
Sets the temporal range for the object.
double y() const
Returns Y coordinate.
double x() const
Returns X coordinate.
On-screen 3D engine: it creates an OpenGL window (QWindow) and displays rendered 3D scenes there.
void setRootEntity(Qt3DCore::QEntity *root) override
Sets root entity of the 3D scene.
void setSize(QSize s) override
Sets the size of the rendering area (in pixels)