From aae265123b34e00ae3480395a98ab4d43ea4200d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Tinkl?= Date: Wed, 14 Aug 2024 14:50:43 +0200 Subject: [PATCH] feat(GenericValidator): expose locale name - make it possible to configure the locale of the validator - use the correct default decimal point in AmountValidator - update/add the relevant tests for AmountToSendNew --- .../qmlTests/tests/tst_AmountToSendNew.qml | 24 +++++++++++++++---- ui/StatusQ/include/StatusQ/genericvalidator.h | 7 ++++++ .../StatusQ/Validators/AmountValidator.qml | 2 +- ui/StatusQ/src/genericvalidator.cpp | 13 ++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/storybook/qmlTests/tests/tst_AmountToSendNew.qml b/storybook/qmlTests/tests/tst_AmountToSendNew.qml index d8ea4be81b..cfbd5f2a9b 100644 --- a/storybook/qmlTests/tests/tst_AmountToSendNew.qml +++ b/storybook/qmlTests/tests/tst_AmountToSendNew.qml @@ -11,9 +11,7 @@ Item { Component { id: componentUnderTest - AmountToSendNew { - decimalPoint: "." - } + AmountToSendNew {} } property AmountToSendNew amountToSend @@ -24,8 +22,7 @@ Item { function type(key, times = 1) { for (let i = 0; i < times; i++) { - keyPress(key) - keyRelease(key) + keyClick(key) } } @@ -169,5 +166,22 @@ Item { compare(amountToSend.amount, "0") compare(textField.text, "") } + + function test_localeAndDecimalPoint() { + verify(!!amountToSend) + + // set a different locale, thus a different decimal separator + amountToSend.locale = Qt.locale("cs_CZ") + tryCompare(amountToSend.locale, "name", "cs_CZ") + tryCompare(amountToSend, "decimalPoint", ",") // "," is the default decimal separator for cs_CZ locale + + const textField = findChild(amountToSend, "amountToSend_textField") + verify(!!textField) + + amountToSend.setValue("2.5") + tryCompare(amountToSend, "text", "2,5") + tryCompare(textField, "text", "2,5") + verify(amountToSend.valid) + } } } diff --git a/ui/StatusQ/include/StatusQ/genericvalidator.h b/ui/StatusQ/include/StatusQ/genericvalidator.h index 6cd023f35e..5dd615bf6f 100644 --- a/ui/StatusQ/include/StatusQ/genericvalidator.h +++ b/ui/StatusQ/include/StatusQ/genericvalidator.h @@ -20,6 +20,8 @@ class GenericValidator : public QValidator READ validateScriptString WRITE setValidateScriptString NOTIFY validateScriptStringChanged) + Q_PROPERTY(QString locale READ localeName WRITE setLocaleName NOTIFY localeChanged FINAL) ///< locale name, defaults to user's own + public: enum State { Invalid = QValidator::Invalid, @@ -43,9 +45,14 @@ signals: void fixupScriptStringChanged(); void validateScriptStringChanged(); + void localeChanged(); + private: bool isValidState(int state) const; + QString localeName() const; + void setLocaleName(const QString& newLocaleName); + QQmlScriptString m_fixupScriptString; QQmlScriptString m_validateScriptString; diff --git a/ui/StatusQ/src/StatusQ/Validators/AmountValidator.qml b/ui/StatusQ/src/StatusQ/Validators/AmountValidator.qml index 026c8cd858..1d746110f5 100644 --- a/ui/StatusQ/src/StatusQ/Validators/AmountValidator.qml +++ b/ui/StatusQ/src/StatusQ/Validators/AmountValidator.qml @@ -21,7 +21,7 @@ import StatusQ 0.1 GenericValidator { id: root - property string decimalPoint: "." + property string decimalPoint: Qt.locale(locale).decimalPoint property int maxIntegralDigits: 10 property int maxDecimalDigits: 10 diff --git a/ui/StatusQ/src/genericvalidator.cpp b/ui/StatusQ/src/genericvalidator.cpp index 13e4a09f30..ba6cfcfe7d 100644 --- a/ui/StatusQ/src/genericvalidator.cpp +++ b/ui/StatusQ/src/genericvalidator.cpp @@ -187,3 +187,16 @@ bool GenericValidator::isValidState(int state) const || stateCasted == QValidator::Intermediate || stateCasted == QValidator::Acceptable; } + +QString GenericValidator::localeName() const { + return locale().name(); +} + +void GenericValidator::setLocaleName(const QString &newLocaleName) { + if (newLocaleName == localeName()) + return; + + setLocale(newLocaleName); + emit localeChanged(); + emit changed(); +}