QDoubleValidator: Allow intermediate values as close as possible to one considered invalid

The documentation states that if you have a range of
0.00-2.00 with the number of decimals set to 2 then
any number up to 9.99 would be considered intermediate.
This is because the number of digits still matches both before
and after the decimal point. If it is 10.0 or 9.999 then
it is still considered invalid.

In the case of 9.999 being invalid in this case, the documentation
is corrected as this was incorrectly indicated as Intermediate,
as the code indicates it as Invalid.

Change-Id: I07b433e856f355916a1240deafdf4ef58e680639
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
bb10
Andy Shaw 2016-03-11 21:27:32 +01:00
parent 49b4433adf
commit 0b70f5ada9
2 changed files with 21 additions and 5 deletions

View File

@ -624,10 +624,10 @@ QDoubleValidator::~QDoubleValidator()
that is within the valid range and is in the correct format.
Returns \l Intermediate if \a input contains a double that is
outside the range or is in the wrong format; e.g. with too many
digits after the decimal point or is empty.
outside the range or is in the wrong format; e.g. is empty.
Returns \l Invalid if the \a input is not a double.
Returns \l Invalid if the \a input is not a double or with too many
digits after the decimal point.
Note: If the valid range consists of just positive doubles (e.g. 0.0 to 100.0)
and \a input is a negative double then \l Invalid is returned. If notation()
@ -690,8 +690,16 @@ QValidator::State QDoubleValidatorPrivate::validateWithLocale(QString &input, QL
if (notation == QDoubleValidator::StandardNotation) {
double max = qMax(qAbs(q->b), qAbs(q->t));
if (max < LLONG_MAX) {
qlonglong n = pow10(numDigits(qlonglong(max))) - 1;
if (qAbs(i) > n)
qlonglong n = pow10(numDigits(qlonglong(max)));
// In order to get the highest possible number in the intermediate
// range we need to get 10 to the power of the number of digits
// after the decimal's and subtract that from the top number.
//
// For example, where q->dec == 2 and with a range of 0.0 - 9.0
// then the minimum possible number is 0.00 and the maximum
// possible is 9.99. Therefore 9.999 and 10.0 should be seen as
// invalid.
if (qAbs(i) > (n - std::pow(10, -q->dec)))
return QValidator::Invalid;
}
}

View File

@ -155,6 +155,10 @@ void tst_QDoubleValidator::validate_data()
QTest::newRow("data48") << "C" << 0.0 << 100.0 << 1 << QString("0.0") << ACC << ACC;
QTest::newRow("data49") << "C" << 0.0 << 100.0 << 0 << QString(".") << ITM << ITM;
QTest::newRow("data50") << "C" << 0.0 << 100.0 << 1 << QString(".") << ITM << ITM;
QTest::newRow("data51") << "C" << 0.0 << 2.0 << 2 << QString("9.99") << ITM << ITM;
QTest::newRow("data52") << "C" << 100.0 << 200.0 << 4 << QString("999.9999") << ITM << ITM;
QTest::newRow("data53") << "C" << 0.0 << 2.0 << 2 << QString("9.9999") << INV << INV;
QTest::newRow("data54") << "C" << 100.0 << 200.0 << 4 << QString("9999.9999") << ITM << INV;
QTest::newRow("data_de0") << "de" << 0.0 << 100.0 << 1 << QString("50,0") << ACC << ACC;
QTest::newRow("data_de1") << "de" << 00.0 << 100.0 << 1 << QString("500,0") << ITM << ITM;
@ -206,6 +210,10 @@ void tst_QDoubleValidator::validate_data()
QTest::newRow("data_de43") << "de" << 0.01 << 0.09 << 2 << QString("0") << ITM << ITM;
QTest::newRow("data_de44") << "de" << 0.0 << 10.0 << 1 << QString("11") << ITM << ITM;
QTest::newRow("data_de45") << "de" << 0.0 << 10.0 << 2 << QString("11") << ITM << ITM;
QTest::newRow("data_de46") << "de" << 0.0 << 2.0 << 2 << QString("9,99") << ITM << ITM;
QTest::newRow("data_de47") << "de" << 100.0 << 200.0 << 4 << QString("999,9999") << ITM << ITM;
QTest::newRow("data_de48") << "de" << 0.0 << 2.0 << 2 << QString("9,9999") << INV << INV;
QTest::newRow("data_de49") << "de" << 100.0 << 200.0 << 4 << QString("9999,9999") << ITM << INV;
QString arabicNum;
arabicNum += QChar(1633); // "18.4" in arabic