From 4b5afc788f8f476a7bb2f57872ad8a017bf0c870 Mon Sep 17 00:00:00 2001 From: Tuomas Heimonen Date: Thu, 28 Jun 2018 10:49:43 +0300 Subject: [PATCH] QIntValidator: Intermediate for number if digits equal or less than max Input value which is over the highest acceptable value, but consisting of a number of digits equal to or less than the max value should be considered intermediate. [ChangeLog][QtGui][QIntValidator] Input value with over the highest acceptable value, but with equal or less amount of digits than the maximum value is now considered intermediate. Task-number: QTBUG-59650 Change-Id: I71a77c9c266f0f3b62c71ac6cb995019385c1cf5 Reviewed-by: Andy Shaw --- src/gui/util/qvalidator.cpp | 18 ++++++++++++++---- .../util/qintvalidator/tst_qintvalidator.cpp | 8 ++++---- .../widgets/qlineedit/tst_qlineedit.cpp | 8 ++++---- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/gui/util/qvalidator.cpp b/src/gui/util/qvalidator.cpp index 2d228bf871..ae4f56a12e 100644 --- a/src/gui/util/qvalidator.cpp +++ b/src/gui/util/qvalidator.cpp @@ -411,13 +411,15 @@ QValidator::State QIntValidator::validate(QString & input, int&) const if (buff.isEmpty()) return Intermediate; - if (b >= 0 && buff.startsWith('-')) + const bool startsWithMinus(buff[0] == '-'); + if (b >= 0 && startsWithMinus) return Invalid; - if (t < 0 && buff.startsWith('+')) + const bool startsWithPlus(buff[0] == '+'); + if (t < 0 && startsWithPlus) return Invalid; - if (buff.size() == 1 && (buff.at(0) == '+' || buff.at(0) == '-')) + if (buff.size() == 1 && (startsWithPlus || startsWithMinus)) return Intermediate; bool ok; @@ -433,7 +435,15 @@ QValidator::State QIntValidator::validate(QString & input, int&) const if (entered >= 0) { // the -entered < b condition is necessary to allow people to type // the minus last (e.g. for right-to-left languages) - return (entered > t && -entered < b) ? Invalid : Intermediate; + // The buffLength > tLength condition validates values consisting + // of a number of digits equal to or less than the max value as intermediate. + + int buffLength = buff.size(); + if (startsWithPlus) + buffLength--; + const int tLength = t != 0 ? static_cast(std::log10(qAbs(t))) + 1 : 1; + + return (entered > t && -entered < b && buffLength > tLength) ? Invalid : Intermediate; } else { return (entered < b) ? Invalid : Intermediate; } diff --git a/tests/auto/gui/util/qintvalidator/tst_qintvalidator.cpp b/tests/auto/gui/util/qintvalidator/tst_qintvalidator.cpp index 57f55c3121..ec0d63f67c 100644 --- a/tests/auto/gui/util/qintvalidator/tst_qintvalidator.cpp +++ b/tests/auto/gui/util/qintvalidator/tst_qintvalidator.cpp @@ -54,7 +54,7 @@ void tst_QIntValidator::validate_data() QTest::addColumn("state"); QTest::newRow("data0") << 0 << 100 << QString("50") << ACC; - QTest::newRow("data1") << 0 << 100 << QString("500") << INV; + QTest::newRow("data1") << 0 << 100 << QString("500") << INT; QTest::newRow("data1a") << 0 << 100 << QString("5000") << INV; QTest::newRow("data1b") << -100 << 0 << QString("50") << INT; QTest::newRow("data1c") << -100 << 0 << QString("500") << INV; @@ -121,7 +121,7 @@ void tst_QIntValidator::validate_data() QTest::newRow("5.1") << 6 << 8 << QString("5") << INT; QTest::newRow("5.2") << 6 << 8 << QString("7") << ACC; - QTest::newRow("5.3") << 6 << 8 << QString("9") << INV; + QTest::newRow("5.3") << 6 << 8 << QString("9") << INT; QTest::newRow("5.3a") << 6 << 8 << QString("19") << INV; QTest::newRow("5.4") << -8 << -6 << QString("-5") << INT; QTest::newRow("5.5") << -8 << -6 << QString("-7") << ACC; @@ -129,13 +129,13 @@ void tst_QIntValidator::validate_data() QTest::newRow("5.6a") << -8 << -6 << QString("-19") << INV; QTest::newRow("5.7") << -8 << -6 << QString("5") << INT; QTest::newRow("5.8") << -8 << -6 << QString("7") << INT; - QTest::newRow("5.9") << -8 << -6 << QString("9") << INV; + QTest::newRow("5.9") << -8 << -6 << QString("9") << INT; QTest::newRow("5.10") << -6 << 8 << QString("-5") << ACC; QTest::newRow("5.11") << -6 << 8 << QString("5") << ACC; QTest::newRow("5.12") << -6 << 8 << QString("-7") << INV; QTest::newRow("5.13") << -6 << 8 << QString("7") << ACC; QTest::newRow("5.14") << -6 << 8 << QString("-9") << INV; - QTest::newRow("5.15") << -6 << 8 << QString("9") << INV; + QTest::newRow("5.15") << -6 << 8 << QString("9") << INT; QTest::newRow("6.1") << 100 << 102 << QString("11") << INT; QTest::newRow("6.2") << 100 << 102 << QString("-11") << INV; diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index a10f128860..448e2030bc 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -2664,9 +2664,9 @@ void tst_QLineEdit::setValidator_QIntValidator_data() << 0 << 100 << QString("153") - << QString(useKeys ? "15" : "") + << QString("153") << bool(useKeys) - << bool(useKeys ? true : false) + << bool(false) << uint(QLineEdit::Normal); QTest::newRow(QString(inputMode + "range [-100,100] int '-153'").toLatin1()) << -100 @@ -2687,7 +2687,7 @@ void tst_QLineEdit::setValidator_QIntValidator_data() QTest::newRow(QString(inputMode + "range [3,7] int '8'").toLatin1()) << 3 << 7 - << QString("8") + << QString("") << QString("") << bool(useKeys) << bool(false) @@ -3292,7 +3292,7 @@ void tst_QLineEdit::editInvalidText() { QLineEdit *testWidget = ensureTestWidget(); testWidget->clear(); - testWidget->setValidator(new QIntValidator(0, 120, 0)); + testWidget->setValidator(new QIntValidator(0, 12, 0)); testWidget->setText("1234"); QVERIFY(!testWidget->hasAcceptableInput());