Skip to content

Commit

Permalink
Coordinate formatter localization
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jun 16, 2021
1 parent 870945c commit 69ea7e6
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 200 deletions.
9 changes: 9 additions & 0 deletions python/core/auto_generated/qgscoordinateformatter.sip.in
Expand Up @@ -87,6 +87,15 @@ Optional ``flags`` can be specified to control the output format.
%Docstring
Formats coordinates as an "``x``,``y``" pair, with optional decimal ``precision`` (number
of decimal places to include).
%End

static QChar separator( );
%Docstring
Returns the charachter used as X/Y separator, this is a `,` on locales that do not use
`,` as decimal separator, it is a space otherwise.

.. seealso:: :py:func:`splitPair`
\from QGIS 3.22
%End

};
Expand Down
59 changes: 33 additions & 26 deletions src/core/qgscoordinateformatter.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgis.h"

#include <QObject> // for tr()
#include <QLocale>

QString QgsCoordinateFormatter::formatX( double x, QgsCoordinateFormatter::Format format, int precision, FormatFlags flags )
{
Expand Down Expand Up @@ -60,21 +61,27 @@ QString QgsCoordinateFormatter::formatY( double y, QgsCoordinateFormatter::Forma

QString QgsCoordinateFormatter::format( const QgsPointXY &point, QgsCoordinateFormatter::Format format, int precision, FormatFlags flags )
{
return QStringLiteral( "%1,%2" ).arg( formatX( point.x(), format, precision, flags ),
formatY( point.y(), format, precision, flags ) );
return QStringLiteral( "%1%2%3" ).arg( formatX( point.x(), format, precision, flags ),
QgsCoordinateFormatter::separator(),
formatY( point.y(), format, precision, flags ) );
}

QString QgsCoordinateFormatter::asPair( double x, double y, int precision )
{
QString s = formatAsPair( x, precision );
s += ',';
s += QgsCoordinateFormatter::separator();
s += formatAsPair( y, precision );
return s;
}

QChar QgsCoordinateFormatter::separator()
{
return QLocale().decimalPoint() == QLatin1Char( ',' ) ? QLatin1Char( ' ' ) : QLatin1Char( ',' );
}

QString QgsCoordinateFormatter::formatAsPair( double val, int precision )
{
return std::isfinite( val ) ? QString::number( val, 'f', precision ) : QObject::tr( "infinite" );
return std::isfinite( val ) ? QLocale().toString( val, 'f', precision ) : QObject::tr( "infinite" );
}

QString QgsCoordinateFormatter::formatXAsDegreesMinutesSeconds( double val, int precision, FormatFlags flags )
Expand Down Expand Up @@ -120,7 +127,7 @@ QString QgsCoordinateFormatter::formatXAsDegreesMinutesSeconds( double val, int
{
if ( wrappedX < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
Expand All @@ -143,17 +150,17 @@ QString QgsCoordinateFormatter::formatXAsDegreesMinutesSeconds( double val, int
//pad with leading digits if required
if ( flags.testFlag( FlagDegreesPadMinutesSeconds ) )
{
minutesX = QString( "%1" ).arg( intMinutesX, 2, 10, QChar( '0' ) );
minutesX = QString( "%L1" ).arg( intMinutesX, 2, 10, QChar( '0' ) );
int digits = 2 + ( precision == 0 ? 0 : 1 + precision ); //1 for decimal place if required
strSecondsX = QString( "%1" ).arg( secondsX, digits, 'f', precision, QChar( '0' ) );
strSecondsX = QString( "%L1" ).arg( secondsX, digits, 'f', precision, QChar( '0' ) );
}
else
{
minutesX = QString::number( intMinutesX );
strSecondsX = QString::number( secondsX, 'f', precision );
minutesX = QLocale().toString( intMinutesX );
strSecondsX = QLocale().toString( secondsX, 'f', precision );
}

return sign + QString::number( degreesX ) + QChar( 176 ) +
return sign + QLocale().toString( degreesX ) + QChar( 176 ) +
minutesX + QChar( 0x2032 ) +
strSecondsX + QChar( 0x2033 ) +
hemisphere;
Expand Down Expand Up @@ -202,7 +209,7 @@ QString QgsCoordinateFormatter::formatYAsDegreesMinutesSeconds( double val, int
{
if ( wrappedY < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
Expand All @@ -219,17 +226,17 @@ QString QgsCoordinateFormatter::formatYAsDegreesMinutesSeconds( double val, int
//pad with leading digits if required
if ( flags.testFlag( FlagDegreesPadMinutesSeconds ) )
{
strMinutesY = QString( "%1" ).arg( intMinutesY, 2, 10, QChar( '0' ) );
strMinutesY = QString( "%L1" ).arg( intMinutesY, 2, 10, QChar( '0' ) );
int digits = 2 + ( precision == 0 ? 0 : 1 + precision ); //1 for decimal place if required
strSecondsY = QString( "%1" ).arg( secondsY, digits, 'f', precision, QChar( '0' ) );
strSecondsY = QString( "%L1" ).arg( secondsY, digits, 'f', precision, QChar( '0' ) );
}
else
{
strMinutesY = QString::number( intMinutesY );
strSecondsY = QString::number( secondsY, 'f', precision );
strMinutesY = QLocale().toString( intMinutesY );
strSecondsY = QLocale().toString( secondsY, 'f', precision );
}

return sign + QString::number( degreesY ) + QChar( 176 ) +
return sign + QLocale().toString( degreesY ) + QChar( 176 ) +
strMinutesY + QChar( 0x2032 ) +
strSecondsY + QChar( 0x2033 ) +
hemisphere;
Expand Down Expand Up @@ -271,7 +278,7 @@ QString QgsCoordinateFormatter::formatXAsDegreesMinutes( double val, int precisi
{
if ( wrappedX < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
Expand All @@ -291,9 +298,9 @@ QString QgsCoordinateFormatter::formatXAsDegreesMinutes( double val, int precisi
//pad minutes with leading digits if required
int digits = 2 + ( precision == 0 ? 0 : 1 + precision ); //1 for decimal place if required
QString strMinutesX = flags.testFlag( FlagDegreesPadMinutesSeconds ) ? QString( "%1" ).arg( floatMinutesX, digits, 'f', precision, QChar( '0' ) )
: QString::number( floatMinutesX, 'f', precision );
: QLocale().toString( floatMinutesX, 'f', precision );

return sign + QString::number( degreesX ) + QChar( 176 ) +
return sign + QLocale().toString( degreesX ) + QChar( 176 ) +
strMinutesX + QChar( 0x2032 ) +
hemisphere;
}
Expand Down Expand Up @@ -334,7 +341,7 @@ QString QgsCoordinateFormatter::formatYAsDegreesMinutes( double val, int precisi
{
if ( wrappedY < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
Expand All @@ -349,9 +356,9 @@ QString QgsCoordinateFormatter::formatYAsDegreesMinutes( double val, int precisi
//pad minutes with leading digits if required
int digits = 2 + ( precision == 0 ? 0 : 1 + precision ); //1 for decimal place if required
QString strMinutesY = flags.testFlag( FlagDegreesPadMinutesSeconds ) ? QString( "%1" ).arg( floatMinutesY, digits, 'f', precision, QChar( '0' ) )
: QString::number( floatMinutesY, 'f', precision );
: QLocale().toString( floatMinutesY, 'f', precision );

return sign + QString::number( degreesY ) + QChar( 176 ) +
return sign + QLocale().toString( degreesY ) + QChar( 176 ) +
strMinutesY + QChar( 0x2032 ) +
hemisphere;
}
Expand Down Expand Up @@ -384,7 +391,7 @@ QString QgsCoordinateFormatter::formatXAsDegrees( double val, int precision, For
{
if ( wrappedX < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
Expand All @@ -402,7 +409,7 @@ QString QgsCoordinateFormatter::formatXAsDegrees( double val, int precision, For
hemisphere.clear();
}

return sign + QString::number( absX, 'f', precision ) + QChar( 176 ) + hemisphere;
return sign + QLocale().toString( absX, 'f', precision ) + QChar( 176 ) + hemisphere;
}

QString QgsCoordinateFormatter::formatYAsDegrees( double val, int precision, FormatFlags flags )
Expand Down Expand Up @@ -433,7 +440,7 @@ QString QgsCoordinateFormatter::formatYAsDegrees( double val, int precision, For
{
if ( wrappedY < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
Expand All @@ -444,5 +451,5 @@ QString QgsCoordinateFormatter::formatYAsDegrees( double val, int precision, For
hemisphere.clear();
}

return sign + QString::number( absY, 'f', precision ) + QChar( 176 ) + hemisphere;
return sign + QLocale().toString( absY, 'f', precision ) + QChar( 176 ) + hemisphere;
}
8 changes: 8 additions & 0 deletions src/core/qgscoordinateformatter.h
Expand Up @@ -105,6 +105,14 @@ class CORE_EXPORT QgsCoordinateFormatter
*/
static QString asPair( double x, double y, int precision = 12 );

/**
* Returns the charachter used as X/Y separator, this is a `,` on locales that do not use
* `,` as decimal separator, it is a space otherwise.
* \see splitPair( )
* \from QGIS 3.22
*/
static QChar separator( );

private:

static QString formatAsPair( double val, int precision );
Expand Down
5 changes: 3 additions & 2 deletions src/gui/qgsmaptoolidentify.cpp
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include "qgsapplication.h"
#include "qgscoordinateformatter.h"
#include "qgsdistancearea.h"
#include "qgsfeature.h"
#include "qgsfeatureiterator.h"
Expand Down Expand Up @@ -748,13 +749,13 @@ QString QgsMapToolIdentify::formatCoordinate( const QgsPointXY &canvasPoint ) co
QString QgsMapToolIdentify::formatXCoordinate( const QgsPointXY &canvasPoint ) const
{
QString coordinate = formatCoordinate( canvasPoint );
return coordinate.split( ',' ).at( 0 );
return coordinate.split( QgsCoordinateFormatter::separator() ).at( 0 );
}

QString QgsMapToolIdentify::formatYCoordinate( const QgsPointXY &canvasPoint ) const
{
QString coordinate = formatCoordinate( canvasPoint );
return coordinate.split( ',' ).at( 1 );
return coordinate.split( QgsCoordinateFormatter::separator() ).at( 1 );
}

QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( const QgsFeature &feature, QgsMapLayer *layer, const QgsPointXY &layerPoint )
Expand Down

0 comments on commit 69ea7e6

Please sign in to comment.