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
This commit is contained in:
Lukáš Tinkl 2024-08-14 14:50:43 +02:00 committed by Lukáš Tinkl
parent 7ddbcf926b
commit aae265123b
4 changed files with 40 additions and 6 deletions

View File

@ -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)
}
}
}

View File

@ -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;

View File

@ -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

View File

@ -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();
}