Skip to content
Permalink
Browse files

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 3217442071926b7d499c68f6c26620df311ed980
@@ -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 &&
@@ -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 );
}
}
}
@@ -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 ) &&
@@ -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
@@ -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();
@@ -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>
@@ -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>
@@ -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()
@@ -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')
@@ -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')

0 comments on commit 3217442

Please sign in to comment.
You can’t perform that action at this time.