From 98781da6b070a652acf87e8b946c26da98d42d85 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Fri, 16 Nov 2018 12:39:25 +0100 Subject: [PATCH 1/3] Make QgsCoordinateUtils functions Q_INVOKABLE --- src/core/CMakeLists.txt | 2 +- src/core/qgscoordinateutils.h | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index e00ef9f97e4..25b4cd5928a 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -593,6 +593,7 @@ SET(QGIS_CORE_MOC_HDRS qgsbrowsermodel.h qgsbrowserproxymodel.h qgscoordinatereferencesystem.h + qgscoordinateutils.h qgscredentials.h qgsdataitem.h qgsdataprovider.h @@ -840,7 +841,6 @@ SET(QGIS_CORE_HDRS qgscoordinateformatter.h qgscoordinatetransform.h qgscoordinatetransformcontext.h - qgscoordinateutils.h qgsdartmeasurement.h qgsdatadefinedsizelegend.h qgsdataitemprovider.h diff --git a/src/core/qgscoordinateutils.h b/src/core/qgscoordinateutils.h index 95c8ffa6f6e..3b4a9702e72 100644 --- a/src/core/qgscoordinateutils.h +++ b/src/core/qgscoordinateutils.h @@ -21,6 +21,7 @@ #define SIP_NO_FILE #include +#include #include "qgis_core.h" @@ -39,6 +40,8 @@ class QgsProject; */ class CORE_EXPORT QgsCoordinateUtils { + Q_GADGET + public: /** @@ -52,14 +55,14 @@ class CORE_EXPORT QgsCoordinateUtils * \param mapCrs CRS of map * \returns optimal number of decimal places for coordinates */ - static int calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs ); + Q_INVOKABLE static int calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs ); /** * Formats a \a point coordinate for use with the specified \a project, respecting the project's * coordinate display settings. * \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 ); }; From 06b5d7b318cb666532b1036eb36ed996122b15ae Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Fri, 16 Nov 2018 12:41:44 +0100 Subject: [PATCH 2/3] Less usage of QgsProject::instance --- src/core/qgscoordinateutils.cpp | 15 ++++++++++----- src/core/qgscoordinateutils.h | 23 +++++++++++++---------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/core/qgscoordinateutils.cpp b/src/core/qgscoordinateutils.cpp index 87e4199416b..46de3a830e5 100644 --- a/src/core/qgscoordinateutils.cpp +++ b/src/core/qgscoordinateutils.cpp @@ -24,15 +24,17 @@ #include "qgscoordinateformatter.h" ///@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 - bool automatic = QgsProject::instance()->readBoolEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/Automatic" ) ); + bool automatic = project->readBoolEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/Automatic" ) ); int dp = 0; 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" ) ); // we can only calculate an automatic precision if one of these is true: @@ -49,11 +51,14 @@ int QgsCoordinateUtils::calculateCoordinatePrecision( double mapUnitsPerPixel, c } else { - dp = format == QLatin1String( "D" ) ? 4 : 2; //guess sensible fallback + if ( format == QLatin1String( "D" ) ) + dp = 4; + else + dp = 2; } } else - dp = QgsProject::instance()->readNumEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DecimalPlaces" ) ); + dp = project->readNumEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DecimalPlaces" ) ); // Keep dp sensible if ( dp < 0 ) diff --git a/src/core/qgscoordinateutils.h b/src/core/qgscoordinateutils.h index 3b4a9702e72..03b1d8b6a93 100644 --- a/src/core/qgscoordinateutils.h +++ b/src/core/qgscoordinateutils.h @@ -45,17 +45,20 @@ class CORE_EXPORT QgsCoordinateUtils public: /** - * Returns the precision to use for displaying coordinates to the user, respecting - * the user's project settings. If the user has set the project to use "automatic" - * precision, this function tries to calculate an optimal coordinate precision for a given - * map units per pixel by calculating the number of decimal places for the coordinates - * with the aim of always having enough decimal places to show the difference in position - * between adjacent pixels. - * \param mapUnitsPerPixel number of map units per pixel - * \param mapCrs CRS of map - * \returns optimal number of decimal places for coordinates + * Returns the precision to use for displaying coordinates in \a mapCrs to the user. + * It respects the user's \a project settings. + * If the user has set the project to use "automatic" precision, this function tries + * to calculate an optimal coordinate precision for a given \a mapUnitsPerPixel by + * calculating the number of decimal places for the coordinates with the aim of always + * having enough decimal places to show the difference in position between adjacent + * pixels. + * + * \since QGIS 3.6 a new \a project parameter is available. Using the method without this + * a \a project paramter 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. */ - Q_INVOKABLE 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 From 236328fbd4f6c03d74169b62c026d530a4cd4e7d Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Fri, 16 Nov 2018 14:03:37 +0100 Subject: [PATCH 3/3] Make code_layout test happy --- src/core/qgscoordinateutils.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/qgscoordinateutils.h b/src/core/qgscoordinateutils.h index 03b1d8b6a93..2cf81f7a6d3 100644 --- a/src/core/qgscoordinateutils.h +++ b/src/core/qgscoordinateutils.h @@ -53,8 +53,8 @@ class CORE_EXPORT QgsCoordinateUtils * having enough decimal places to show the difference in position between adjacent * pixels. * - * \since QGIS 3.6 a new \a project parameter is available. Using the method without this - * a \a project paramter is deprecated and will be removed with QGIS 4. + * \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. */