Skip to content
Permalink
Browse files
Localize numbers in expression preview
Fixes #43681
  • Loading branch information
elpaso authored and nyalldawson committed Jun 13, 2021
1 parent 02f21d4 commit 2fff67ab528e42f4073efb7976d80b53b8d21e25
Showing with 73 additions and 0 deletions.
  1. +38 −0 src/core/expression/qgsexpression.cpp
  2. +35 −0 tests/src/core/testqgsexpression.cpp
@@ -1085,6 +1085,44 @@ QString QgsExpression::formatPreviewString( const QVariant &value, const bool ht
listStr += QLatin1Char( ']' );
return listStr;
}
else if ( value.type() == QVariant::Int || value.type() == QVariant::UInt || value.type() == QVariant::LongLong || value.type() == QVariant::ULongLong )
{
bool ok;
QString res;

if ( value.type() == QVariant::ULongLong )
{
res = QLocale().toString( value.toULongLong( &ok ) );
}
else
{
res = QLocale().toString( value.toLongLong( &ok ) );
}

if ( ok )
{
return res;
}
else
{
return value.toString();
}
}
// Qt madness with QMetaType::Float :/
else if ( value.type() == QVariant::Double || value.type() == static_cast<QVariant::Type>( QMetaType::Float ) )
{
bool ok;
const QString res { QLocale().toString( value.toDouble( &ok ), 'f', 6 ) };

if ( ok )
{
return res;
}
else
{
return value.toString();
}
}
else
{
return value.toString();
@@ -3957,6 +3957,41 @@ class TestQgsExpression: public QObject
QStringLiteral( "[ 'One', 'Two', 'A very long string that is going to be tr… ]" ) );
}

void test_formatPreviewStringWithLocale()
{
const QVariant t_int { 12345 };
QVariant t_uint( QVariant::UInt );
t_uint = 12345;
QVariant t_long( QVariant::LongLong );
t_long = 12345;
QVariant t_ulong( QVariant::ULongLong );
t_ulong = 12345;
const QVariant t_float { 12345.001F };
const QVariant t_double { 12345.001 };

QLocale().setDefault( QLocale::English );

QCOMPARE( QgsExpression::formatPreviewString( t_int ), QStringLiteral( "12,345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_uint ), QStringLiteral( "12,345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_long ), QStringLiteral( "12,345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_ulong ), QStringLiteral( "12,345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_float ), QStringLiteral( "12,345.000977" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_double ), QStringLiteral( "12,345.001000" ) );

QLocale().setDefault( QLocale::Italian );

QCOMPARE( QgsExpression::formatPreviewString( t_int ), QStringLiteral( "12.345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_uint ), QStringLiteral( "12.345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_long ), QStringLiteral( "12.345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_ulong ), QStringLiteral( "12.345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_float ), QStringLiteral( "12.345,000977" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_double ), QStringLiteral( "12.345,001000" ) );

QLocale().setDefault( QLocale::English );

}


void test_nowStatic()
{
QgsExpression e( QStringLiteral( "now()" ) );

0 comments on commit 2fff67a

Please sign in to comment.