Skip to content
Permalink
Browse files
Update quick map settings
  • Loading branch information
m-kuhn committed Jun 5, 2021
1 parent 0dca7c9 commit 5cd4617df67026823fdcc74b58020a54ce973537
@@ -16,15 +16,16 @@
#include <QQuickWindow>
#include <QSGSimpleTextureNode>
#include <QScreen>
#include <qgis.h>
#include <qgsexpressioncontextutils.h>
#include <qgsmaprenderercache.h>
#include <qgsmaprendererparalleljob.h>
#include <qgsmessagelog.h>
#include <qgspallabeling.h>
#include <qgsproject.h>
#include <qgsvectorlayer.h>
#include <qgslabelingresults.h>

#include "qgis.h"
#include "qgsexpressioncontextutils.h"
#include "qgsmaprenderercache.h"
#include "qgsmaprendererparalleljob.h"
#include "qgsmessagelog.h"
#include "qgspallabeling.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgslabelingresults.h"

#include "qgsquickmapcanvasmap.h"
#include "qgsquickmapsettings.h"
@@ -16,6 +16,7 @@
#ifndef QGSQUICKMAPCANVASMAP_H
#define QGSQUICKMAPCANVASMAP_H

#include "qgis_quick.h"
#include "qgsquickmapsettings.h"

#include <QFutureSynchronizer>
@@ -200,8 +201,6 @@ class QUICK_EXPORT QgsQuickMapCanvasMap : public QQuickItem
bool mIncrementalRendering = false;

QQuickWindow *mWindow = nullptr;

QSizeF mOutputSize;
};

#endif // QGSQUICKMAPCANVASMAP_H
@@ -14,13 +14,13 @@
***************************************************************************/


#include "qgis.h"
#include "qgsquickmapsettings.h"

#include "qgsmaplayer.h"
#include "qgsmaplayerstylemanager.h"
#include "qgsmessagelog.h"
#include "qgsproject.h"
#include "qgis.h"

#include "qgsquickmapsettings.h"

QgsQuickMapSettings::QgsQuickMapSettings( QObject *parent )
: QObject( parent )
@@ -51,8 +51,10 @@ void QgsQuickMapSettings::setProject( QgsProject *project )
if ( mProject )
{
connect( mProject, &QgsProject::readProject, this, &QgsQuickMapSettings::onReadProject );
connect( mProject, &QgsProject::crsChanged, this, &QgsQuickMapSettings::onCrsChanged );
setDestinationCrs( mProject->crs() );
mMapSettings.setTransformContext( mProject->transformContext() );
mMapSettings.setPathResolver( mProject->pathResolver() );
}
else
{
@@ -104,6 +106,26 @@ double QgsQuickMapSettings::mapUnitsPerPixel() const
return mMapSettings.mapUnitsPerPixel();
}

void QgsQuickMapSettings::setCenterToLayer( QgsMapLayer *layer, bool shouldZoom )
{
Q_ASSERT( layer );

const QgsRectangle extent = mapSettings().layerToMapCoordinates( layer, layer->extent() );

if ( !extent.isEmpty() )
{
if ( shouldZoom )
setExtent( extent );
else
setCenter( QgsPoint( extent.center() ) );
}
}

double QgsQuickMapSettings::mapUnitsPerPoint() const
{
return mMapSettings.mapUnitsPerPixel() * devicePixelRatio();
}

QgsRectangle QgsQuickMapSettings::visibleExtent() const
{
return mMapSettings.visibleExtent();
@@ -113,15 +135,14 @@ QPointF QgsQuickMapSettings::coordinateToScreen( const QgsPoint &point ) const
{
QgsPointXY pt( point.x(), point.y() );
QgsPointXY pp = mMapSettings.mapToPixel().transform( pt );
pp.setX( pp.x() / devicePixelRatio() );
pp.setY( pp.y() / devicePixelRatio() );
return pp.toQPointF();
}

QgsPoint QgsQuickMapSettings::screenToCoordinate( const QPointF &point ) const
{
// use floating point precision with mapToCoordinates (i.e. do not use QPointF::toPoint)
// this is to avoid rounding errors with an odd screen width or height
// and the point being set to the exact center of it
const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.x(), point.y() );
const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.x() * devicePixelRatio(), point.y() * devicePixelRatio() );
return QgsPoint( pp );
}

@@ -130,18 +151,15 @@ QgsMapSettings QgsQuickMapSettings::mapSettings() const
return mMapSettings;
}

void QgsQuickMapSettings::setTransformContext( const QgsCoordinateTransformContext &ctx )
{
mMapSettings.setTransformContext( ctx );
}

QSize QgsQuickMapSettings::outputSize() const
{
return mMapSettings.outputSize();
}

void QgsQuickMapSettings::setOutputSize( const QSize &outputSize )
void QgsQuickMapSettings::setOutputSize( QSize outputSize )
{
outputSize.setWidth( outputSize.width() * devicePixelRatio() );
outputSize.setHeight( outputSize.height() * devicePixelRatio() );
if ( mMapSettings.outputSize() == outputSize )
return;

@@ -156,6 +174,7 @@ double QgsQuickMapSettings::outputDpi() const

void QgsQuickMapSettings::setOutputDpi( double outputDpi )
{
outputDpi *= devicePixelRatio();
if ( qgsDoubleNear( mMapSettings.outputDpi(), outputDpi ) )
return;

@@ -188,31 +207,53 @@ void QgsQuickMapSettings::setLayers( const QList<QgsMapLayer *> &layers )
emit layersChanged();
}

void QgsQuickMapSettings::onCrsChanged()
{
setDestinationCrs( mProject->crs() );
}

void QgsQuickMapSettings::onReadProject( const QDomDocument &doc )
{
if ( mProject )
{
mMapSettings.setBackgroundColor( mProject->backgroundColor() );
int red = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorRedPart" ), 255 );
int green = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorGreenPart" ), 255 );
int blue = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorBluePart" ), 255 );
mMapSettings.setBackgroundColor( QColor( red, green, blue ) );
}

QDomNodeList nodes = doc.elementsByTagName( "mapcanvas" );
if ( nodes.count() )
bool foundTheMapCanvas = false;
for ( int i = 0; i < nodes.size(); i++ )
{
QDomNode node = nodes.item( 0 );
QDomElement element = node.toElement();

mMapSettings.readXml( node );
if ( element.hasAttribute( QStringLiteral( "name" ) ) && element.attribute( QStringLiteral( "name" ) ) == QStringLiteral( "theMapCanvas" ) )
{
foundTheMapCanvas = true;
mMapSettings.readXml( node );

if ( !qgsDoubleNear( mMapSettings.rotation(), 0 ) )
QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( mMapSettings.rotation() ) );
}
}
if ( !foundTheMapCanvas )
{
mMapSettings.setDestinationCrs( mProject->crs() );
mMapSettings.setExtent( mMapSettings.fullExtent() );
}

if ( !qgsDoubleNear( mMapSettings.rotation(), 0 ) )
QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( mMapSettings.rotation() ) );
mMapSettings.setRotation( 0 );

mMapSettings.setRotation( 0 );
mMapSettings.setTransformContext( mProject->transformContext() );
mMapSettings.setPathResolver( mProject->pathResolver() );

emit extentChanged();
emit destinationCrsChanged();
emit outputSizeChanged();
emit outputDpiChanged();
emit layersChanged();
}
emit extentChanged();
emit destinationCrsChanged();
emit outputSizeChanged();
emit outputDpiChanged();
emit layersChanged();
}

double QgsQuickMapSettings::rotation() const
@@ -239,3 +280,13 @@ void QgsQuickMapSettings::setBackgroundColor( const QColor &color )
mMapSettings.setBackgroundColor( color );
emit backgroundColorChanged();
}

qreal QgsQuickMapSettings::devicePixelRatio() const
{
return mDevicePixelRatio;
}

void QgsQuickMapSettings::setDevicePixelRatio( const qreal &devicePixelRatio )
{
mDevicePixelRatio = devicePixelRatio;
}
@@ -16,16 +16,17 @@
#ifndef QGSQUICKMAPSETTINGS_H
#define QGSQUICKMAPSETTINGS_H

#include "qgis_quick.h"

#include <QObject>

#include "qgscoordinatetransformcontext.h"
#include "qgsmaplayer.h"
#include "qgsmapsettings.h"
#include "qgsmapthemecollection.h"
#include "qgspoint.h"
#include "qgsrectangle.h"

#include "qgis_quick.h"

class QgsProject;

/**
@@ -69,6 +70,8 @@ class QUICK_EXPORT QgsQuickMapSettings : public QObject
Q_PROPERTY( QgsRectangle visibleExtent READ visibleExtent NOTIFY visibleExtentChanged )
//! \copydoc QgsMapSettings::mapUnitsPerPixel()
Q_PROPERTY( double mapUnitsPerPixel READ mapUnitsPerPixel NOTIFY mapUnitsPerPixelChanged )
//! Returns the distance in geographical coordinates that equals to one point unit in the map
Q_PROPERTY( double mapUnitsPerPoint READ mapUnitsPerPoint NOTIFY mapUnitsPerPointChanged )

/**
* The rotation of the resulting map image, in degrees clockwise.
@@ -79,7 +82,7 @@ class QUICK_EXPORT QgsQuickMapSettings : public QObject
/**
* The background color used to render the map
*
* The value is set to the project's background color setting on QgsProject::readProject
* The value is set to the project's bacckground color setting on QgsProject::readProject
*/
Q_PROPERTY( QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged )

@@ -116,7 +119,7 @@ class QUICK_EXPORT QgsQuickMapSettings : public QObject

public:
//! Create new map settings
QgsQuickMapSettings( QObject *parent = nullptr );
explicit QgsQuickMapSettings( QObject *parent = nullptr );
~QgsQuickMapSettings() = default;

//! Clone map settings
@@ -140,6 +143,12 @@ class QUICK_EXPORT QgsQuickMapSettings : public QObject
//! \copydoc QgsMapSettings::mapUnitsPerPixel()
double mapUnitsPerPixel() const;

//! Move current map extent to have center point defined by \a layer. Optionally only pan to the layer if \a shouldZoom is false.
Q_INVOKABLE void setCenterToLayer( QgsMapLayer *layer, bool shouldZoom = true );

//! \copydoc QgsQuickMapSettings::mapUnitsPerPoint
double mapUnitsPerPoint() const;

//! \copydoc QgsMapSettings::visibleExtent()
QgsRectangle visibleExtent() const;

@@ -155,7 +164,6 @@ class QUICK_EXPORT QgsQuickMapSettings : public QObject
*/
Q_INVOKABLE QPointF coordinateToScreen( const QgsPoint &point ) const;


/**
* Convert a screen coordinate to a map coordinate
*
@@ -192,7 +200,7 @@ class QUICK_EXPORT QgsQuickMapSettings : public QObject
*
* \see outputSize()
*/
void setOutputSize( const QSize &outputSize );
void setOutputSize( QSize outputSize );

//! \copydoc QgsMapSettings::outputDpi()
double outputDpi() const;
@@ -226,6 +234,21 @@ class QUICK_EXPORT QgsQuickMapSettings : public QObject
*/
void setLayers( const QList<QgsMapLayer *> &layers );

/**
* Returns the ratio between physical pixels and device-independent pixels.
* This value is dependent on the screen the window is on, and may change when the window is moved.
* Common values are 1.0 on normal displays and 2.0 on Apple "retina" displays.
*/
qreal devicePixelRatio() const;


/**
* Sets the ratio between physical pixels and device-independent pixels.
* This value is dependent on the screen the window is on, and may change when the window is moved.
* Common values are 1.0 on normal displays and 2.0 on Apple "retina" displays.
*/
void setDevicePixelRatio( const qreal &devicePixelRatio );

signals:
//! \copydoc QgsQuickMapSettings::project
void projectChanged();
@@ -238,6 +261,8 @@ class QUICK_EXPORT QgsQuickMapSettings : public QObject

//! \copydoc QgsQuickMapSettings::mapUnitsPerPixel
void mapUnitsPerPixelChanged();
//! \copydoc QgsQuickMapSettings::mapUnitsPerPoint
void mapUnitsPerPointChanged();

//! \copydoc QgsQuickMapSettings::rotation
void rotationChanged();
@@ -266,10 +291,15 @@ class QUICK_EXPORT QgsQuickMapSettings : public QObject
*/
void onReadProject( const QDomDocument &doc );

/**
* Sets the destination CRS to match the changed project CRS
*/
void onCrsChanged();

private:
QgsProject *mProject = nullptr;
QgsMapSettings mMapSettings;

qreal mDevicePixelRatio = 1.0;
};

#endif // QGSQUICKMAPSETTINGS_H

0 comments on commit 5cd4617

Please sign in to comment.