2022-09-15 10:12:38 +00:00
|
|
|
import QtQuick 2.14
|
|
|
|
import QtQuick.Layouts 1.14
|
|
|
|
|
|
|
|
import StatusQ.Core 0.1
|
|
|
|
import StatusQ.Core.Theme 0.1
|
|
|
|
|
|
|
|
import utils 1.0
|
|
|
|
|
|
|
|
Input {
|
|
|
|
id: root
|
|
|
|
|
|
|
|
property int maximumLength: 10
|
2023-01-13 11:52:03 +00:00
|
|
|
property var locale: Qt.locale()
|
2022-09-15 10:12:38 +00:00
|
|
|
|
|
|
|
readonly property alias amount: d.amount
|
|
|
|
readonly property bool valid: validationError.length === 0
|
|
|
|
property bool allowDecimals: true
|
|
|
|
|
2023-04-27 21:38:09 +00:00
|
|
|
property bool validateMaximumAmount: false
|
|
|
|
property real maximumAmount: 0
|
|
|
|
|
2022-09-15 10:12:38 +00:00
|
|
|
validationErrorTopMargin: 8
|
|
|
|
fontPixelSize: 13
|
|
|
|
customHeight: 36
|
|
|
|
placeholderText: locale.zeroDigit
|
|
|
|
|
|
|
|
textField.rightPadding: labelText.implicitWidth + labelText.anchors.rightMargin
|
|
|
|
+ textField.leftPadding
|
|
|
|
|
|
|
|
function setAmount(amount) {
|
2023-04-04 16:13:33 +00:00
|
|
|
root.text = LocaleUtils.numberToLocaleString(amount, -1, root.locale)
|
2022-09-15 10:12:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 21:38:09 +00:00
|
|
|
onTextChanged: d.validate()
|
|
|
|
onValidateMaximumAmountChanged: d.validate()
|
|
|
|
onMaximumAmountChanged: d.validate()
|
|
|
|
|
2022-09-15 10:12:38 +00:00
|
|
|
QtObject {
|
|
|
|
id: d
|
|
|
|
|
|
|
|
property real amount: 0
|
2023-03-02 11:33:14 +00:00
|
|
|
|
|
|
|
function getEffectiveDigitsCount(str) {
|
|
|
|
const digits = LocaleUtils.getLocalizedDigitsCount(text, root.locale)
|
|
|
|
return str.startsWith(locale.decimalPoint) ? digits + 1 : digits
|
|
|
|
}
|
2023-04-27 21:38:09 +00:00
|
|
|
|
|
|
|
function validate() {
|
|
|
|
if (!root.allowDecimals)
|
|
|
|
root.text = root.text.replace(root.locale.decimalPoint, "")
|
|
|
|
|
|
|
|
if(root.text.length === 0) {
|
|
|
|
d.amount = 0
|
|
|
|
root.validationError = ""
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d.getEffectiveDigitsCount(text) > root.maximumLength) {
|
|
|
|
root.validationError = qsTr("The maximum number of characters is %1").arg(root.maximumLength)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const amount = LocaleUtils.numberFromLocaleString(root.text, root.locale)
|
|
|
|
if (isNaN(amount)) {
|
|
|
|
d.amount = 0
|
|
|
|
root.validationError = qsTr("Invalid amount format")
|
|
|
|
} else if (root.validateMaximumAmount && amount > root.maximumAmount) {
|
|
|
|
root.validationError = qsTr("Amount exceeds balance")
|
|
|
|
} else {
|
|
|
|
d.amount = amount
|
|
|
|
root.validationError = ""
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 10:12:38 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 22:56:58 +00:00
|
|
|
validator: DoubleValidator {
|
2022-09-15 10:12:38 +00:00
|
|
|
id: doubleValidator
|
|
|
|
|
|
|
|
decimals: root.allowDecimals ? 100 : 0
|
|
|
|
bottom: 0
|
|
|
|
notation: DoubleValidator.StandardNotation
|
2023-05-08 18:18:24 +00:00
|
|
|
locale: root.locale.name.split("_")[0] // For whatever reason, this doesn't work properly when being
|
|
|
|
// passed "language_country". We pass only the language part.
|
2022-09-15 10:12:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StatusBaseText {
|
|
|
|
id: labelText
|
|
|
|
|
|
|
|
parent: root.textField
|
|
|
|
|
|
|
|
anchors.right: parent.right
|
|
|
|
anchors.rightMargin: 13
|
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
text: qsTr("Amount")
|
|
|
|
color: Theme.palette.baseColor1
|
|
|
|
font.pixelSize: 13
|
|
|
|
}
|
|
|
|
}
|