Skip to content

Commit 06b5d7b

Browse files
committed
Less usage of QgsProject::instance
1 parent 98781da commit 06b5d7b

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/core/qgscoordinateutils.cpp

+10-5
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

+13-10
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,20 @@ class CORE_EXPORT QgsCoordinateUtils
4545
public:
4646

4747
/**
48-
* Returns the precision to use for displaying coordinates to the user, respecting
49-
* the user's project settings. If the user has set the project to use "automatic"
50-
* precision, this function tries to calculate an optimal coordinate precision for a given
51-
* map units per pixel by calculating the number of decimal places for the coordinates
52-
* with the aim of always having enough decimal places to show the difference in position
53-
* between adjacent pixels.
54-
* \param mapUnitsPerPixel number of map units per pixel
55-
* \param mapCrs CRS of map
56-
* \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+
* \since QGIS 3.6 a new \a project parameter is available. Using the method without this
57+
* a \a project paramter 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.
5760
*/
58-
Q_INVOKABLE static int calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs );
61+
Q_INVOKABLE static int calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs, QgsProject *project = nullptr );
5962

6063
/**
6164
* Formats a \a point coordinate for use with the specified \a project, respecting the project's

0 commit comments

Comments
 (0)