Skip to content
Permalink
Browse files

[3d] Read/write camera position of the 3D map view in project

  • Loading branch information
wonder-sk committed Dec 5, 2017
1 parent ad54073 commit 011e254e8a84de3db11bd2eb1e5e3abab70862a7
Showing with 39 additions and 0 deletions.
  1. +22 −0 src/3d/qgscameracontroller.cpp
  2. +8 −0 src/3d/qgscameracontroller.h
  3. +9 −0 src/app/qgisapp.cpp
@@ -18,6 +18,7 @@

#include "qgis.h"

#include <QDomDocument>
#include <Qt3DRender/QObjectPicker>
#include <Qt3DRender/QPickEvent>

@@ -307,6 +308,27 @@ void QgsCameraController::setLookingAtPoint( const QgsVector3D &point )
emit cameraChanged();
}

QDomElement QgsCameraController::writeXml( QDomDocument &doc ) const
{
QDomElement elemCamera = doc.createElement( "camera" );
elemCamera.setAttribute( QStringLiteral( "x" ), mCameraData.x );
elemCamera.setAttribute( QStringLiteral( "y" ), mCameraData.y );
elemCamera.setAttribute( QStringLiteral( "dist" ), mCameraData.dist );
elemCamera.setAttribute( QStringLiteral( "pitch" ), mCameraData.pitch );
elemCamera.setAttribute( QStringLiteral( "yaw" ), mCameraData.yaw );
return elemCamera;
}

void QgsCameraController::readXml( const QDomElement &elem )
{
float x = elem.attribute( QStringLiteral( "x" ) ).toFloat();
float y = elem.attribute( QStringLiteral( "y" ) ).toFloat();
float dist = elem.attribute( QStringLiteral( "dist" ) ).toFloat();
float pitch = elem.attribute( QStringLiteral( "pitch" ) ).toFloat();
float yaw = elem.attribute( QStringLiteral( "yaw" ) ).toFloat();
setCameraData( x, y, dist, pitch, yaw );
}

void QgsCameraController::onPositionChanged( Qt3DInput::QMouseEvent *mouse )
{
mMousePos = QPoint( mouse->x(), mouse->y() );
@@ -22,6 +22,9 @@
#include <Qt3DInput>
#include <Qt3DRender>

class QDomDocument;
class QDomElement;

class QgsVector3D;

/**
@@ -66,6 +69,11 @@ class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
//! Sets the point toward which the camera is looking - this is used when world origin changes (e.g. after terrain generator changes)
void setLookingAtPoint( const QgsVector3D &point );

//! Writes camera configuration to the given DOM element
QDomElement writeXml( QDomDocument &doc ) const;
//! Reads camera configuration from the given DOM element
void readXml( const QDomElement &elem );

private:
void setCameraData( float x, float y, float dist, float pitch = 0, float yaw = 0 );

@@ -87,6 +87,7 @@
#include "qgs3drendererregistry.h"
#include "qgs3dmapcanvas.h"
#include "qgs3dmapsettings.h"
#include "qgscameracontroller.h"
#include "qgsflatterraingenerator.h"
#include "qgsvectorlayer3drenderer.h"
#include "processing/qgs3dalgorithms.h"
@@ -12392,6 +12393,8 @@ void QgisApp::writeProject( QDomDocument &doc )
elem3DMap.setAttribute( QStringLiteral( "name" ), w->mapCanvas3D()->objectName() );
QDomElement elem3DMapSettings = w->mapCanvas3D()->map()->writeXml( doc, readWriteContext );
elem3DMap.appendChild( elem3DMapSettings );
QDomElement elemCamera = w->mapCanvas3D()->cameraController()->writeXml( doc );
elem3DMap.appendChild( elemCamera );
writeDockWidgetSettings( w, elem3DMap );
elem3DMaps.appendChild( elem3DMap );
}
@@ -12498,6 +12501,12 @@ void QgisApp::readProject( const QDomDocument &doc )

mapCanvasDock3D->setMapSettings( map );

QDomElement elemCamera = elem3DMap.firstChildElement( QStringLiteral( "camera" ) );
if ( !elemCamera.isNull() )
{
mapCanvasDock3D->mapCanvas3D()->cameraController()->readXml( elemCamera );
}

elem3DMap = elem3DMap.nextSiblingElement( QStringLiteral( "view" ) );
}
}

0 comments on commit 011e254

Please sign in to comment.
You can’t perform that action at this time.