Skip to content

Commit

Permalink
[3d] Read/write camera position of the 3D map view in project
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Dec 5, 2017
1 parent ad54073 commit 011e254
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/3d/qgscameracontroller.cpp
Expand Up @@ -18,6 +18,7 @@

#include "qgis.h"

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

Expand Down Expand Up @@ -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() );
Expand Down
8 changes: 8 additions & 0 deletions src/3d/qgscameracontroller.h
Expand Up @@ -22,6 +22,9 @@
#include <Qt3DInput>
#include <Qt3DRender>

class QDomDocument;
class QDomElement;

class QgsVector3D;

/**
Expand Down Expand Up @@ -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 );

Expand Down
9 changes: 9 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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" ) );
}
}
Expand Down

0 comments on commit 011e254

Please sign in to comment.