Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #6866 from m-kuhn/syslocale
Consistent locale usage for number representation
  • Loading branch information
m-kuhn committed May 3, 2018
2 parents dea03b2 + 6de0e4b commit 3217442
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
16 changes: 3 additions & 13 deletions src/core/fieldformatter/qgsrangefieldformatter.cpp
Expand Up @@ -40,16 +40,6 @@ QString QgsRangeFieldFormatter::representValue( QgsVectorLayer *layer, int field

QString result;

// Prepare locale
std::function<QLocale()> f_locale = [ ]
{
QLocale locale( QgsApplication::instance()->locale() );
QLocale::NumberOptions options( locale.numberOptions() );
options |= QLocale::NumberOption::OmitGroupSeparator;
locale.setNumberOptions( options );
return locale;
};

const QgsField field = layer->fields().at( fieldIndex );

if ( field.type() == QVariant::Double &&
Expand All @@ -64,7 +54,7 @@ QString QgsRangeFieldFormatter::representValue( QgsVectorLayer *layer, int field
if ( ok )
{
// TODO: make the format configurable!
result = f_locale().toString( val, 'f', precision );
result = QLocale().toString( val, 'f', precision );
}
}
}
Expand All @@ -75,7 +65,7 @@ QString QgsRangeFieldFormatter::representValue( QgsVectorLayer *layer, int field
double val( value.toInt( &ok ) );
if ( ok )
{
result = f_locale().toString( val, 'f', 0 );
result = QLocale().toString( val, 'f', 0 );
}
}
else if ( ( field.type() == QVariant::LongLong ) &&
Expand All @@ -85,7 +75,7 @@ QString QgsRangeFieldFormatter::representValue( QgsVectorLayer *layer, int field
double val( value.toLongLong( &ok ) );
if ( ok )
{
result = f_locale().toString( val, 'f', 0 );
result = QLocale().toString( val, 'f', 0 );
}
}
else
Expand Down
1 change: 0 additions & 1 deletion src/gui/editorwidgets/qgsdoublespinbox.cpp
Expand Up @@ -32,7 +32,6 @@ QgsDoubleSpinBox::QgsDoubleSpinBox( QWidget *parent )
mLineEdit = new QgsSpinBoxLineEdit();

// By default, group separator is off
setLocale( QLocale( QgsApplication::locale( ) ) );
setLineEdit( mLineEdit );

QSize msz = minimumSizeHint();
Expand Down
5 changes: 4 additions & 1 deletion src/ui/qgsoptionsbase.ui
Expand Up @@ -371,7 +371,7 @@
<item row="0" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Locale to use instead</string>
<string>User Interface Translation</string>
</property>
</widget>
</item>
Expand All @@ -392,6 +392,9 @@
</item>
<item>
<widget class="QLabel" name="lblSystemLocale">
<property name="toolTip">
<string>This locale is used for number representation.</string>
</property>
<property name="text">
<string>Detected active locale on your system</string>
</property>
Expand Down
15 changes: 8 additions & 7 deletions tests/src/python/test_qgsfieldformatters.py
Expand Up @@ -18,7 +18,7 @@
QgsValueMapFieldFormatter, QgsValueRelationFieldFormatter,
QgsRelationReferenceFieldFormatter, QgsRangeFieldFormatter, QgsSettings)

from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtCore import QCoreApplication, QLocale
from qgis.testing import start_app, unittest

start_app()
Expand Down Expand Up @@ -243,6 +243,8 @@ def test_representValue(self):

fieldFormatter = QgsRangeFieldFormatter()

QLocale.setDefault(QLocale.c())

# Precision is ignored for integers and longlongs
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '123'), '123')
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '123000'), '123000')
Expand Down Expand Up @@ -272,18 +274,17 @@ def test_representValue(self):
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 3}, None, '-0.127'), '-0.127')
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 3}, None, '-1.27e-1'), '-0.127')

QgsSettings().setValue("locale/overrideFlag", True)
QgsSettings().setValue("locale/userLocale", 'it')
QLocale.setDefault(QLocale('it'))

self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '9999999'),
'9999999') # no scientific notation for integers!
'9.999.999') # scientific notation for integers!
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123'), '123')
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123000'), '123000')
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '9999999'), '9999999') # no scientific notation for long longs!
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123000'), '123.000')
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '9999999'), '9.999.999') # scientific notation for long longs!
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, None), 'NULL')

self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, None), 'NULL')
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, '123000'), '123000,00')
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, '123000'), '123.000,00')
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, '0'), '0,00')
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, '123'), '123,00')
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, '0.123'), '0,12')
Expand Down

0 comments on commit 3217442

Please sign in to comment.