Skip to content

Commit 1ef04d7

Browse files
authoredNov 16, 2018
Merge pull request #8496 from m-kuhn/coordinateUtilsFunctions
Expose QgsCoordinateUtils functions via Q_INVOKABLE
2 parents 0a42a10 + 236328f commit 1ef04d7

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed
 

‎src/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ SET(QGIS_CORE_MOC_HDRS
594594
qgsbrowsermodel.h
595595
qgsbrowserproxymodel.h
596596
qgscoordinatereferencesystem.h
597+
qgscoordinateutils.h
597598
qgscredentials.h
598599
qgsdataitem.h
599600
qgsdataprovider.h
@@ -842,7 +843,6 @@ SET(QGIS_CORE_HDRS
842843
qgscoordinateformatter.h
843844
qgscoordinatetransform.h
844845
qgscoordinatetransformcontext.h
845-
qgscoordinateutils.h
846846
qgsdartmeasurement.h
847847
qgsdatadefinedsizelegend.h
848848
qgsdataitemprovider.h

‎src/core/qgscoordinateutils.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@
2424
#include "qgscoordinateformatter.h"
2525
///@cond NOT_STABLE_API
2626

27-
int QgsCoordinateUtils::calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs )
27+
int QgsCoordinateUtils::calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs, QgsProject *project )
2828
{
29+
if ( !project )
30+
project = QgsProject::instance();
2931
// Get the display precision from the project settings
30-
bool automatic = QgsProject::instance()->readBoolEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/Automatic" ) );
32+
bool automatic = project->readBoolEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/Automatic" ) );
3133
int dp = 0;
3234

3335
if ( automatic )
3436
{
35-
QString format = QgsProject::instance()->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DegreeFormat" ), QStringLiteral( "MU" ) );
37+
QString format = project->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DegreeFormat" ), QStringLiteral( "MU" ) );
3638
bool formatGeographic = ( format == QLatin1String( "DM" ) || format == QLatin1String( "DMS" ) || format == QLatin1String( "D" ) );
3739

3840
// we can only calculate an automatic precision if one of these is true:
@@ -49,11 +51,14 @@ int QgsCoordinateUtils::calculateCoordinatePrecision( double mapUnitsPerPixel, c
4951
}
5052
else
5153
{
52-
dp = format == QLatin1String( "D" ) ? 4 : 2; //guess sensible fallback
54+
if ( format == QLatin1String( "D" ) )
55+
dp = 4;
56+
else
57+
dp = 2;
5358
}
5459
}
5560
else
56-
dp = QgsProject::instance()->readNumEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DecimalPlaces" ) );
61+
dp = project->readNumEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DecimalPlaces" ) );
5762

5863
// Keep dp sensible
5964
if ( dp < 0 )

‎src/core/qgscoordinateutils.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define SIP_NO_FILE
2222

2323
#include <QString>
24+
#include <QObject>
2425

2526
#include "qgis_core.h"
2627

@@ -39,27 +40,32 @@ class QgsProject;
3940
*/
4041
class CORE_EXPORT QgsCoordinateUtils
4142
{
43+
Q_GADGET
44+
4245
public:
4346

4447
/**
45-
* Returns the precision to use for displaying coordinates to the user, respecting
46-
* the user's project settings. If the user has set the project to use "automatic"
47-
* precision, this function tries to calculate an optimal coordinate precision for a given
48-
* map units per pixel by calculating the number of decimal places for the coordinates
49-
* with the aim of always having enough decimal places to show the difference in position
50-
* between adjacent pixels.
51-
* \param mapUnitsPerPixel number of map units per pixel
52-
* \param mapCrs CRS of map
53-
* \returns optimal number of decimal places for coordinates
48+
* Returns the precision to use for displaying coordinates in \a mapCrs to the user.
49+
* It respects the user's \a project settings.
50+
* If the user has set the project to use "automatic" precision, this function tries
51+
* to calculate an optimal coordinate precision for a given \a mapUnitsPerPixel by
52+
* calculating the number of decimal places for the coordinates with the aim of always
53+
* having enough decimal places to show the difference in position between adjacent
54+
* pixels.
55+
*
56+
* \note Since QGIS 3.6 a new \a project parameter is available. Using the method without this
57+
* a \a project parameter is deprecated and will be removed with QGIS 4.
58+
* For backward compatibility, QgsProject.instance() will be used if the \a project
59+
* parameter is not specified.
5460
*/
55-
static int calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs );
61+
Q_INVOKABLE static int calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs, QgsProject *project = nullptr );
5662

5763
/**
5864
* Formats a \a point coordinate for use with the specified \a project, respecting the project's
5965
* coordinate display settings.
6066
* \since QGIS 3.2
6167
*/
62-
static QString formatCoordinateForProject( QgsProject *project, const QgsPointXY &point, const QgsCoordinateReferenceSystem &destCrs, int precision );
68+
Q_INVOKABLE static QString formatCoordinateForProject( QgsProject *project, const QgsPointXY &point, const QgsCoordinateReferenceSystem &destCrs, int precision );
6369

6470
};
6571

0 commit comments

Comments
 (0)