Merge pull request #8496 from m-kuhn/coordinateUtilsFunctions

Expose QgsCoordinateUtils functions via Q_INVOKABLE
This commit is contained in:
Matthias Kuhn 2018-11-16 14:41:29 +01:00 committed by GitHub
commit 1ef04d7ab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 17 deletions

View File

@ -594,6 +594,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsbrowsermodel.h qgsbrowsermodel.h
qgsbrowserproxymodel.h qgsbrowserproxymodel.h
qgscoordinatereferencesystem.h qgscoordinatereferencesystem.h
qgscoordinateutils.h
qgscredentials.h qgscredentials.h
qgsdataitem.h qgsdataitem.h
qgsdataprovider.h qgsdataprovider.h
@ -842,7 +843,6 @@ SET(QGIS_CORE_HDRS
qgscoordinateformatter.h qgscoordinateformatter.h
qgscoordinatetransform.h qgscoordinatetransform.h
qgscoordinatetransformcontext.h qgscoordinatetransformcontext.h
qgscoordinateutils.h
qgsdartmeasurement.h qgsdartmeasurement.h
qgsdatadefinedsizelegend.h qgsdatadefinedsizelegend.h
qgsdataitemprovider.h qgsdataitemprovider.h

View File

@ -24,15 +24,17 @@
#include "qgscoordinateformatter.h" #include "qgscoordinateformatter.h"
///@cond NOT_STABLE_API ///@cond NOT_STABLE_API
int QgsCoordinateUtils::calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs ) int QgsCoordinateUtils::calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs, QgsProject *project )
{ {
if ( !project )
project = QgsProject::instance();
// Get the display precision from the project settings // Get the display precision from the project settings
bool automatic = QgsProject::instance()->readBoolEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/Automatic" ) ); bool automatic = project->readBoolEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/Automatic" ) );
int dp = 0; int dp = 0;
if ( automatic ) if ( automatic )
{ {
QString format = QgsProject::instance()->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DegreeFormat" ), QStringLiteral( "MU" ) ); QString format = project->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DegreeFormat" ), QStringLiteral( "MU" ) );
bool formatGeographic = ( format == QLatin1String( "DM" ) || format == QLatin1String( "DMS" ) || format == QLatin1String( "D" ) ); bool formatGeographic = ( format == QLatin1String( "DM" ) || format == QLatin1String( "DMS" ) || format == QLatin1String( "D" ) );
// we can only calculate an automatic precision if one of these is true: // we can only calculate an automatic precision if one of these is true:
@ -49,11 +51,14 @@ int QgsCoordinateUtils::calculateCoordinatePrecision( double mapUnitsPerPixel, c
} }
else else
{ {
dp = format == QLatin1String( "D" ) ? 4 : 2; //guess sensible fallback if ( format == QLatin1String( "D" ) )
dp = 4;
else
dp = 2;
} }
} }
else else
dp = QgsProject::instance()->readNumEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DecimalPlaces" ) ); dp = project->readNumEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DecimalPlaces" ) );
// Keep dp sensible // Keep dp sensible
if ( dp < 0 ) if ( dp < 0 )

View File

@ -21,6 +21,7 @@
#define SIP_NO_FILE #define SIP_NO_FILE
#include <QString> #include <QString>
#include <QObject>
#include "qgis_core.h" #include "qgis_core.h"
@ -39,27 +40,32 @@ class QgsProject;
*/ */
class CORE_EXPORT QgsCoordinateUtils class CORE_EXPORT QgsCoordinateUtils
{ {
Q_GADGET
public: public:
/** /**
* Returns the precision to use for displaying coordinates to the user, respecting * Returns the precision to use for displaying coordinates in \a mapCrs to the user.
* the user's project settings. If the user has set the project to use "automatic" * It respects the user's \a project settings.
* precision, this function tries to calculate an optimal coordinate precision for a given * If the user has set the project to use "automatic" precision, this function tries
* map units per pixel by calculating the number of decimal places for the coordinates * to calculate an optimal coordinate precision for a given \a mapUnitsPerPixel by
* with the aim of always having enough decimal places to show the difference in position * calculating the number of decimal places for the coordinates with the aim of always
* between adjacent pixels. * having enough decimal places to show the difference in position between adjacent
* \param mapUnitsPerPixel number of map units per pixel * pixels.
* \param mapCrs CRS of map *
* \returns optimal number of decimal places for coordinates * \note Since QGIS 3.6 a new \a project parameter is available. Using the method without this
* a \a project parameter is deprecated and will be removed with QGIS 4.
* For backward compatibility, QgsProject.instance() will be used if the \a project
* parameter is not specified.
*/ */
static int calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs ); Q_INVOKABLE static int calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs, QgsProject *project = nullptr );
/** /**
* Formats a \a point coordinate for use with the specified \a project, respecting the project's * Formats a \a point coordinate for use with the specified \a project, respecting the project's
* coordinate display settings. * coordinate display settings.
* \since QGIS 3.2 * \since QGIS 3.2
*/ */
static QString formatCoordinateForProject( QgsProject *project, const QgsPointXY &point, const QgsCoordinateReferenceSystem &destCrs, int precision ); Q_INVOKABLE static QString formatCoordinateForProject( QgsProject *project, const QgsPointXY &point, const QgsCoordinateReferenceSystem &destCrs, int precision );
}; };