Navigation Menu

Skip to content

Commit

Permalink
Less usage of QgsProject::instance
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Nov 16, 2018
1 parent 98781da commit 06b5d7b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
15 changes: 10 additions & 5 deletions src/core/qgscoordinateutils.cpp
Expand Up @@ -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:
Expand All @@ -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 )
Expand Down
23 changes: 13 additions & 10 deletions src/core/qgscoordinateutils.h
Expand Up @@ -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
Expand Down

0 comments on commit 06b5d7b

Please sign in to comment.