Skip to content

Commit

Permalink
[georeferencer] Use QLocale for number format
Browse files Browse the repository at this point in the history
Fix #58765
  • Loading branch information
elpaso committed Oct 9, 2024
1 parent d7b43ba commit fb4d3c7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 15 deletions.
12 changes: 7 additions & 5 deletions src/app/georeferencer/qgsgcplistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
#include "qgsgeorefdatapoint.h"
#include "qgsgeoreftransform.h"
#include "qgssettings.h"
#include "qgsdoublevalidator.h"

#include <cmath>
#include <QLocale>

QgsGCPListModel::QgsGCPListModel( QObject *parent )
: QAbstractTableModel( parent )
Expand Down Expand Up @@ -253,9 +255,9 @@ bool QgsGCPListModel::setData( const QModelIndex &index, const QVariant &value,
{
QgsPointXY sourcePoint = point->sourcePoint();
if ( column == QgsGCPListModel::Column::SourceX )
sourcePoint.setX( value.toDouble() );
sourcePoint.setX( QgsDoubleValidator::toDouble( value.toString() ) );
else
sourcePoint.setY( value.toDouble() );
sourcePoint.setY( QgsDoubleValidator::toDouble( value.toString() ) );
point->setSourcePoint( sourcePoint );
emit dataChanged( index, index );
updateResiduals();
Expand All @@ -269,9 +271,9 @@ bool QgsGCPListModel::setData( const QModelIndex &index, const QVariant &value,
// as this is what we were showing to users
QgsPointXY destinationPoint = point->transformedDestinationPoint( mTargetCrs, mTransformContext );
if ( column == QgsGCPListModel::Column::DestinationX )
destinationPoint.setX( value.toDouble() );
destinationPoint.setX( QgsDoubleValidator::toDouble( value.toString() ) );
else
destinationPoint.setY( value.toDouble() );
destinationPoint.setY( QgsDoubleValidator::toDouble( value.toString() ) );
point->setDestinationPoint( destinationPoint );
// we also have to update the destination point crs to the target crs, as the point is now in a different CRS
point->setDestinationPointCrs( mTargetCrs );
Expand Down Expand Up @@ -439,7 +441,7 @@ QString QgsGCPListModel::formatNumber( double number )
else if ( std::fabs( number ) < 1000 )
decimalPlaces = 6;

return QString::number( number, 'f', decimalPlaces );
return QLocale().toString( number, 'f', decimalPlaces );
}


93 changes: 83 additions & 10 deletions tests/src/app/testqgsgeoreferencer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class TestQgsGeoreferencer : public QObject
void testUpdateResiduals();
void testListModel();
void testListModelCrs();
void testListModelLocalization();
void testGdalCommands();
void testWorldFile();

Expand All @@ -67,6 +68,8 @@ void TestQgsGeoreferencer::initTestCase()
QgsApplication::init();
QgsApplication::initQgis();
mQgisApp = new QgisApp();
// Set locale to ensure tests are consistent
QLocale::setDefault( QLocale( QStringLiteral( "en_US.UTF-8" ) ) );
}

//runs after all tests
Expand Down Expand Up @@ -690,8 +693,8 @@ void TestQgsGeoreferencer::testListModel()

QCOMPARE( model.rowCount(), 3 );
QCOMPARE( model.data( model.index( 0, 1 ) ).toString(), QStringLiteral( "0" ) );
QCOMPARE( model.data( model.index( 0, 2 ) ).toString(), QStringLiteral( "781662.38" ) );
QCOMPARE( model.data( model.index( 0, 3 ) ).toString(), QStringLiteral( "3350923.13" ) );
QCOMPARE( model.data( model.index( 0, 2 ) ).toString(), QStringLiteral( "781,662.38" ) );
QCOMPARE( model.data( model.index( 0, 3 ) ).toString(), QStringLiteral( "3,350,923.13" ) );
QCOMPARE( model.data( model.index( 0, 4 ) ).toString(), QStringLiteral( "-30.000000" ) );
QCOMPARE( model.data( model.index( 0, 5 ) ).toString(), QStringLiteral( "40.000000" ) );
QCOMPARE( model.data( model.index( 0, 4 ), Qt::ToolTipRole ).toString(), QStringLiteral( "<b>-30.000000</b><br>EPSG:4326 - WGS 84" ) );
Expand All @@ -702,8 +705,8 @@ void TestQgsGeoreferencer::testListModel()
QCOMPARE( model.data( model.index( 0, 8 ) ).toString(), QStringLiteral( "n/a" ) );

QCOMPARE( model.data( model.index( 1, 1 ) ).toString(), QStringLiteral( "1" ) );
QCOMPARE( model.data( model.index( 1, 2 ) ).toString(), QStringLiteral( "787362.38" ) );
QCOMPARE( model.data( model.index( 1, 3 ) ).toString(), QStringLiteral( "3350923.13" ) );
QCOMPARE( model.data( model.index( 1, 2 ) ).toString(), QStringLiteral( "787,362.38" ) );
QCOMPARE( model.data( model.index( 1, 3 ) ).toString(), QStringLiteral( "3,350,923.13" ) );
QCOMPARE( model.data( model.index( 1, 4 ) ).toString(), QStringLiteral( "149.999994" ) );
QCOMPARE( model.data( model.index( 1, 5 ) ).toString(), QStringLiteral( "-29.999993" ) );
// tooltip should show target CRS details for user clarification
Expand All @@ -714,8 +717,8 @@ void TestQgsGeoreferencer::testListModel()
QCOMPARE( model.data( model.index( 1, 8 ) ).toString(), QStringLiteral( "n/a" ) );

QCOMPARE( model.data( model.index( 2, 1 ) ).toString(), QStringLiteral( "2" ) );
QCOMPARE( model.data( model.index( 2, 2 ) ).toString(), QStringLiteral( "787362.38" ) );
QCOMPARE( model.data( model.index( 2, 3 ) ).toString(), QStringLiteral( "3362323.13" ) );
QCOMPARE( model.data( model.index( 2, 2 ) ).toString(), QStringLiteral( "787,362.38" ) );
QCOMPARE( model.data( model.index( 2, 3 ) ).toString(), QStringLiteral( "3,362,323.13" ) );
QCOMPARE( model.data( model.index( 2, 4 ) ).toString(), QStringLiteral( "-35.000000" ) );
QCOMPARE( model.data( model.index( 2, 5 ) ).toString(), QStringLiteral( "42.000000" ) );
QCOMPARE( model.data( model.index( 2, 6 ) ).toString(), QStringLiteral( "n/a" ) );
Expand Down Expand Up @@ -745,19 +748,19 @@ void TestQgsGeoreferencer::testListModel()
QVERIFY( !model.setData( model.index( 0, 8 ), 11 ) );

QVERIFY( model.setData( model.index( 0, 2 ), 777777.77 ) );
QCOMPARE( model.data( model.index( 0, 2 ) ).toString(), QStringLiteral( "777777.77" ) );
QCOMPARE( model.data( model.index( 0, 2 ) ).toString(), QStringLiteral( "777,777.77" ) );
QCOMPARE( list.at( 0 )->sourcePoint().x(), 777777.77 );

QVERIFY( model.setData( model.index( 0, 3 ), 3333333.33 ) );
QCOMPARE( model.data( model.index( 0, 3 ) ).toString(), QStringLiteral( "3333333.33" ) );
QCOMPARE( model.data( model.index( 0, 3 ) ).toString(), QStringLiteral( "3,333,333.33" ) );
QCOMPARE( list.at( 0 )->sourcePoint().y(), 3333333.33 );

QVERIFY( model.setData( model.index( 0, 4 ), 1660000.77 ) );
QCOMPARE( model.data( model.index( 0, 4 ) ).toString(), QStringLiteral( "1660000.77" ) );
QCOMPARE( model.data( model.index( 0, 4 ) ).toString(), QStringLiteral( "1,660,000.77" ) );
QCOMPARE( list.at( 0 )->destinationPoint().x(), 1660000.77 );

QVERIFY( model.setData( model.index( 0, 5 ), 4433333.33 ) );
QCOMPARE( model.data( model.index( 0, 5 ) ).toString(), QStringLiteral( "4433333.33" ) );
QCOMPARE( model.data( model.index( 0, 5 ) ).toString(), QStringLiteral( "4,433,333.33" ) );
QCOMPARE( list.at( 0 )->destinationPoint().y(), 4433333.33 );

// disable point
Expand Down Expand Up @@ -844,6 +847,54 @@ void TestQgsGeoreferencer::testListModelCrs()
QCOMPARE( list.at( 2 )->destinationPointCrs().authid(), QStringLiteral( "EPSG:4326" ) );
}

void TestQgsGeoreferencer::testListModelLocalization()
{
QgsGCPList list;
QgsMapCanvas c1;
QgsMapCanvas c2;
list.append( new QgsGeorefDataPoint( &c1, &c2,
QgsPointXY( 781662.375, 3350923.125 ), QgsPointXY( -30, 40 ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ),
true ) );
list.append( new QgsGeorefDataPoint( &c1, &c2,
QgsPointXY( 787362.375, 3350923.125 ), QgsPointXY( 16697923, -3503549 ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ),
true ) );
list.append( new QgsGeorefDataPoint( &c1, &c2,
QgsPointXY( 787362.375, 3362323.125 ), QgsPointXY( 17697923, -3403549 ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ),
true ) );

QgsGCPListModel model;
model.setGCPList( &list );
model.setTargetCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), QgsProject::instance()->transformContext() );

const QLocale defaultLocale = QLocale();

// Set locale to German
QLocale::setDefault( QLocale( QStringLiteral( "de_DE" ) ) );

// Check that the numbers are formatted correctly
QCOMPARE( model.data( model.index( 0, 4 ) ).toString(), QStringLiteral( "-30,000000" ) );
QCOMPARE( model.data( model.index( 0, 5 ) ).toString(), QStringLiteral( "40,000000" ) );
QCOMPARE( model.data( model.index( 1, 4 ) ).toString(), QStringLiteral( "149,999994" ) );
QCOMPARE( model.data( model.index( 1, 5 ) ).toString(), QStringLiteral( "-29,999993" ) );
QCOMPARE( model.data( model.index( 2, 4 ) ).toString(), QStringLiteral( "158,983147" ) );
QCOMPARE( model.data( model.index( 2, 5 ) ).toString(), QStringLiteral( "-29,218996" ) );

// Check that setData works with localized numbers
model.setData( model.index( 0, 4 ), QStringLiteral( "-31,123" ) );
QCOMPARE( QLocale().toDouble( model.data( model.index( 0, 4 ) ).toString() ), -31.123 );

model.setData( model.index( 0, 4 ), QStringLiteral( "1.123,1234" ) );
QCOMPARE( QLocale().toDouble( model.data( model.index( 0, 4 ) ).toString() ), 1123.1234 );

// Permissive check using C locale
model.setData( model.index( 0, 4 ), QStringLiteral( "123.4" ) );
QCOMPARE( QLocale().toDouble( model.data( model.index( 0, 4 ) ).toString() ), 123.4 );

// Revert locale to english
QLocale::setDefault( defaultLocale );

}

void TestQgsGeoreferencer::testGdalCommands()
{
QgsGeoreferencerMainWindow window;
Expand Down Expand Up @@ -875,6 +926,28 @@ void TestQgsGeoreferencer::testGdalCommands()
QgsDebugMsgLevel( command, 1 );
QCOMPARE( command, QStringLiteral( "ogr2ogr -gcp 783414 3350122 783414.00123457 3350122.00234568 -gcp 791344 3349795 791344 33497952 -gcp 783077 334093 783077 334093 -gcp 791134 3341401 791134 3341401 -tps -t_srs EPSG:4326 \"\" \"%1\"" ).arg(
QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/landsat.tif" ) ) );

const QLocale defaultLocale = QLocale();

// Set locale to German
QLocale::setDefault( QLocale( QStringLiteral( "de_DE" ) ) );

command = window.generateGDALtranslateCommand();
QgsDebugMsgLevel( command, 1 );
QCOMPARE( command, QStringLiteral( "gdal_translate -of GTiff -co TFW=YES -gcp 30.73 14.055 783414.00123457 3350122.00234568 -gcp 169.853 19.792 791344 33497952 -gcp 24.818 52926.844 783077 334093 -gcp 166.169 167.055 791134 3341401 \"%1\" \"%2\"" ).arg(
QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/landsat.tif" ),
QDir::tempPath() + QStringLiteral( "/landsat.tif" ) ) );

command = window.generateGDALogr2ogrCommand();
QgsDebugMsgLevel( command, 1 );
QCOMPARE( command, QStringLiteral( "ogr2ogr -gcp 783414 3350122 783414.00123457 3350122.00234568 -gcp 791344 3349795 791344 33497952 -gcp 783077 334093 783077 334093 -gcp 791134 3341401 791134 3341401 -tps -t_srs EPSG:4326 \"\" \"%1\"" ).arg(
QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/landsat.tif" ) ) );



// Revert locale to english
QLocale::setDefault( defaultLocale );

}

void TestQgsGeoreferencer::testWorldFile()
Expand Down

0 comments on commit fb4d3c7

Please sign in to comment.