Skip to content
Permalink
Browse files
Fix qgsdoublevalidator group separator
be tolerant and live long and prosper
  • Loading branch information
elpaso authored and nyalldawson committed Nov 2, 2021
1 parent 1edecf2 commit 024adab0e754d56490f7f6bfdbca2e599fee869d
Showing with 29 additions and 6 deletions.
  1. +6 −6 src/gui/qgsdoublevalidator.cpp
  2. +23 −0 tests/src/gui/testqgsdoublevalidator.cpp
@@ -128,12 +128,7 @@ QValidator::State QgsDoubleValidator::validate( QString &input ) const
double QgsDoubleValidator::toDouble( const QString &input )
{
bool ok = false;
double value = QLocale().toDouble( input, &ok );
if ( ! ok )
{
value = QLocale( QLocale::C ).toDouble( input, &ok );
}
return value;
return toDouble( input, &ok );
}

double QgsDoubleValidator::toDouble( const QString &input, bool *ok )
@@ -144,5 +139,10 @@ double QgsDoubleValidator::toDouble( const QString &input, bool *ok )
{
value = QLocale( QLocale::C ).toDouble( input, ok );
}
// Still non ok? Try without locale's group separator
if ( ! *ok && ! QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator )
{
value = QLocale( ).toDouble( QString( input ).replace( QLocale().groupSeparator(), QString() ), ok );
}
return value ;
}
@@ -66,6 +66,7 @@ void TestQgsDoubleValidator::validate_data()
// QgsDoubleValidator doesn't expect group separator but it tolerates it,
// so the result will be QValidator::Intermediate and not QValidator::Acceptable
QTest::newRow( "locale group separator + locale decimal" ) << QString( "4lg444ld6" ) << int( QValidator::Intermediate );
QTest::newRow( "locale group separator misplaced + locale decimal" ) << QString( "44lg44ld6" ) << int( QValidator::Intermediate );
QTest::newRow( "locale group separator + c decimal" ) << QString( "4lg444cd6" ) << int( QValidator::Invalid );
QTest::newRow( "c group separator + locale decimal" ) << QString( "4cg444ld6" ) << int( QValidator::Invalid );
QTest::newRow( "c group separator + c decimal" ) << QString( "4cg444cd6" ) << int( QValidator::Intermediate );
@@ -88,6 +89,7 @@ void TestQgsDoubleValidator::toDouble_data()
// QgsDoubleValidator doesn't expect group separator but it tolerates it,
// so the result will be QValidator::Intermediate and not QValidator::Acceptable
QTest::newRow( "locale group separator + locale decimal" ) << QString( "4lg444ld6" ) << 4444.6;
QTest::newRow( "locale group separator misplaced + locale decimal" ) << QString( "4lg444ld6" ) << 4444.6;
QTest::newRow( "locale group separator + c decimal" ) << QString( "4lg444cd6" ) << 0.0;
QTest::newRow( "c group separator + locale decimal" ) << QString( "4cg444ld6" ) << 0.0;
QTest::newRow( "c group separator + c decimal" ) << QString( "4cg444cd6" ) << 4444.6;
@@ -137,6 +139,17 @@ void TestQgsDoubleValidator::validate()
QLocale( QLocale::C ).decimalPoint() == QLocale().decimalPoint() )
&& value != "string" && expectedValue == 0 )
expectedValue = 1;
// There is another corner case in the test where the group separator is equal
// to the C decimal point and there is no decimal point,
// in that case the value is valid, because the fall
// back check is to test after removing all group separators
if ( QLocale( ).groupSeparator() == QLocale( QLocale::C ).decimalPoint()
&& ! value.contains( QLocale( ).decimalPoint() )
&& value != "string" && expectedValue == 0 )
{
expectedValue = 1;
}
// qDebug() << value << loc << int( validator->validate( value ) ) << expectedValue;
QCOMPARE( int( validator->validate( value ) ), expectedValue );
}
}
@@ -175,6 +188,16 @@ void TestQgsDoubleValidator::toDouble()
QLocale( QLocale::C ).decimalPoint() == QLocale().decimalPoint() )
&& value != "string" && expectedValue == 0.0 )
expectedValue = 4444.6;
// There is another corner case in the test where the group separator is equal
// to the C decimal point and there is no decimal point,
// in that case the value is valid, because the fall
// back check is to test after removing all group separators
if ( QLocale( ).groupSeparator() == QLocale( QLocale::C ).decimalPoint()
&& ! value.contains( QLocale( ).decimalPoint() )
&& value != "string" && expectedValue == 0 )
{
expectedValue = 44446;
}

QCOMPARE( QgsDoubleValidator::toDouble( value ), expectedValue );
}

0 comments on commit 024adab

Please sign in to comment.