2023-09-15 08:51:06 +00:00
|
|
|
import QtQuick 2.15
|
2024-08-14 21:56:48 +00:00
|
|
|
import QtQuick.Controls 2.15
|
2023-09-15 08:51:06 +00:00
|
|
|
import QtQuick.Layouts 1.15
|
2022-12-14 21:06:14 +00:00
|
|
|
|
|
|
|
import StatusQ.Core 0.1
|
|
|
|
import StatusQ.Core.Theme 0.1
|
2023-09-15 08:51:06 +00:00
|
|
|
import StatusQ.Core.Utils 0.1 as SQUtils
|
2024-05-28 17:39:41 +00:00
|
|
|
import StatusQ.Components 0.1
|
2024-08-14 21:56:48 +00:00
|
|
|
import StatusQ.Validators 0.1
|
2022-12-14 21:06:14 +00:00
|
|
|
|
|
|
|
import utils 1.0
|
2024-08-14 21:56:48 +00:00
|
|
|
import shared.controls 1.0
|
2022-12-14 21:06:14 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
Control {
|
2022-12-14 21:06:14 +00:00
|
|
|
id: root
|
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
/* Crypto value in a base unit as a string integer, e.g. 1000000000000000000
|
|
|
|
* for 1 ETH */
|
|
|
|
readonly property alias amount: d.amountBaseUnit
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
/* In fiat mode the input value is meant to be a fiat value, conversely,
|
|
|
|
* crypto value otherwise. */
|
|
|
|
readonly property alias fiatMode: d.fiatMode
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
/* Indicates whether toggling the fiatMode is enabled for the user */
|
|
|
|
property bool fiatInputInteractive: interactive
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
/* Indicates if input represent valid number. E.g. empty input or containing
|
|
|
|
* only decimal point is not valid. */
|
|
|
|
readonly property alias valid: textField.acceptableInput
|
|
|
|
readonly property bool empty: textField.length === 0
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
// TODO: remove, temporarily for backward compatibility. External components
|
|
|
|
// should not rely on formatted amount because formatting rules are internal
|
|
|
|
// detail of that component.
|
|
|
|
readonly property alias text: textField.text
|
2022-12-14 21:06:14 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
/* Decimal point character to be displayed. Both "." and "," will be
|
|
|
|
* replaced by the provided decimal point on the fly */
|
|
|
|
property alias decimalPoint: validator.decimalPoint
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
/* Number of fiat decimal places used to limit allowed decimal places in
|
|
|
|
* fiatMode */
|
|
|
|
property int fiatDecimalPlaces: 2
|
2024-05-28 17:39:41 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
/* Specifies how divisible given cryptocurrency is, e.g. 18 for ETH. Used
|
|
|
|
* for limiting allowed decimal places and computing final amount as an
|
|
|
|
* integer value */
|
|
|
|
property int multiplierIndex: 18
|
2024-05-28 17:39:41 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
/* Price of one unit of given cryptocurrency (e.g. price for 1 ETH) */
|
|
|
|
property real price: 1.0
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
property alias caption: captionText.text
|
|
|
|
property bool interactive: true
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
readonly property bool cursorVisible: textField.cursorVisible
|
|
|
|
readonly property alias placeholderText: textField.placeholderText
|
2022-12-14 21:06:14 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
/* Loading states for the input and text below */
|
2024-06-14 11:34:20 +00:00
|
|
|
property bool mainInputLoading
|
|
|
|
property bool bottomTextLoading
|
2024-05-28 17:39:41 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
/* Allows mark input as invalid when it's valid number but doesn't satisfy
|
|
|
|
* arbitrary external criteria, e.g. is higher than maximum expected value. */
|
|
|
|
property bool markAsInvalid: false
|
|
|
|
|
|
|
|
/* Methods for formatting crypto and fiat value expecting double values,
|
|
|
|
e.g. 1.0 for 1 ETH or 1.0 for 1 USD. */
|
|
|
|
property var formatFiat: balance =>
|
|
|
|
`${balance.toLocaleString(Qt.locale())} FIAT`
|
|
|
|
property var formatBalance: balance =>
|
|
|
|
`${balance.toLocaleString(Qt.locale())} CRYPTO`
|
|
|
|
|
|
|
|
/* Allows to set value to be displayed. The value is expected to be a not
|
|
|
|
localized string like "1", "1.1" or "0.000000023400234222". Provided
|
|
|
|
value will be formatted and displayed. Depending on the fiatMode flag
|
|
|
|
it will affect output amount appropriately. */
|
|
|
|
function setValue(valueString) {
|
|
|
|
if (!valueString)
|
|
|
|
valueString = "0"
|
|
|
|
|
|
|
|
const decimalPlaces = d.fiatMode ? root.fiatDecimalPlaces
|
|
|
|
: root.multiplierIndex
|
|
|
|
|
|
|
|
const stringNumber = SQUtils.AmountsArithmetic.fromString(
|
|
|
|
valueString).toFixed(decimalPlaces)
|
|
|
|
|
|
|
|
const trimmed = d.fiatMode
|
|
|
|
? stringNumber
|
|
|
|
: d.removeDecimalTrailingZeros(stringNumber)
|
|
|
|
|
|
|
|
textField.text = d.localize(trimmed)
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear() {
|
|
|
|
textField.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
function forceActiveFocus() {
|
|
|
|
textField.forceActiveFocus()
|
|
|
|
}
|
2022-12-14 21:06:14 +00:00
|
|
|
|
|
|
|
QtObject {
|
|
|
|
id: d
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
property bool fiatMode: false
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
readonly property string inputDelocalized:
|
|
|
|
root.valid && textField.length !== 0
|
|
|
|
? textField.text.replace(root.decimalPoint, ".") : "0"
|
|
|
|
|
|
|
|
function removeDecimalTrailingZeros(num) {
|
|
|
|
if (!num.includes("."))
|
|
|
|
return num
|
|
|
|
|
|
|
|
return num.replace(/\.?0*$/g, "")
|
2023-09-15 08:51:06 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
function localize(num) {
|
|
|
|
return num.replace(".", root.decimalPoint)
|
2023-09-15 08:51:06 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
readonly property string amountBaseUnit: {
|
|
|
|
if (d.fiatMode)
|
|
|
|
return secondaryValue
|
2024-02-05 16:44:49 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
const multiplier = SQUtils.AmountsArithmetic.fromExponent(
|
|
|
|
root.multiplierIndex)
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
return SQUtils.AmountsArithmetic.times(
|
|
|
|
SQUtils.AmountsArithmetic.fromString(inputDelocalized),
|
|
|
|
multiplier).toFixed()
|
2024-06-13 00:45:33 +00:00
|
|
|
}
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
readonly property string secondaryValue: {
|
|
|
|
const price = isNaN(root.price) ? 0 : root.price
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
if (!d.fiatMode)
|
|
|
|
return SQUtils.AmountsArithmetic.times(
|
|
|
|
SQUtils.AmountsArithmetic.fromString(inputDelocalized),
|
|
|
|
SQUtils.AmountsArithmetic.fromNumber(
|
|
|
|
price * (10 ** root.fiatDecimalPlaces))).toFixed()
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
if (!price) // prevent div by zero below
|
|
|
|
return 0
|
|
|
|
|
|
|
|
const multiplier = SQUtils.AmountsArithmetic.fromExponent(
|
|
|
|
root.multiplierIndex)
|
|
|
|
|
|
|
|
return SQUtils.AmountsArithmetic.div(
|
|
|
|
SQUtils.AmountsArithmetic.times(
|
|
|
|
SQUtils.AmountsArithmetic.fromString(inputDelocalized),
|
|
|
|
multiplier),
|
|
|
|
SQUtils.AmountsArithmetic.fromNumber(price)).toFixed()
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
contentItem: ColumnLayout {
|
|
|
|
StatusBaseText {
|
|
|
|
id: captionText
|
2022-12-19 13:02:56 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
Layout.fillWidth: true
|
2024-07-17 16:48:16 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
visible: text.length > 0
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
font.pixelSize: 13
|
|
|
|
lineHeight: 18
|
|
|
|
lineHeightMode: Text.FixedHeight
|
|
|
|
color: Theme.palette.directColor1
|
|
|
|
elide: Text.ElideRight
|
|
|
|
}
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
RowLayout {
|
|
|
|
StyledTextField {
|
|
|
|
id: textField
|
|
|
|
|
|
|
|
objectName: "amountToSend_textField"
|
|
|
|
|
|
|
|
Layout.fillWidth: true
|
|
|
|
|
|
|
|
implicitHeight: 44
|
|
|
|
padding: 0
|
|
|
|
background: null
|
|
|
|
|
|
|
|
readOnly: !root.interactive
|
|
|
|
|
|
|
|
color: text.length === 0 || (root.valid && !root.markAsInvalid)
|
|
|
|
? Theme.palette.directColor1
|
|
|
|
: Theme.palette.dangerColor1
|
|
|
|
|
|
|
|
placeholderText: {
|
|
|
|
if (!d.fiatMode || root.fiatDecimalPlaces === 0)
|
|
|
|
return "0"
|
|
|
|
|
|
|
|
return "0" + root.decimalPoint
|
|
|
|
+ "0".repeat(root.fiatDecimalPlaces)
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
font.pixelSize: Utils.getFontSizeBasedOnLetterCount(text)
|
|
|
|
|
|
|
|
validator: AmountValidator {
|
|
|
|
id: validator
|
|
|
|
|
|
|
|
maxDecimalDigits: d.fiatMode ? root.fiatDecimalPlaces
|
|
|
|
: root.multiplierIndex
|
|
|
|
locale: root.locale.name
|
|
|
|
}
|
|
|
|
visible: !root.mainInputLoading
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
2024-08-14 21:56:48 +00:00
|
|
|
LoadingComponent {
|
|
|
|
objectName: "topAmountToSendInputLoadingComponent"
|
|
|
|
Layout.preferredWidth: textField.width
|
|
|
|
Layout.preferredHeight: textField.height
|
|
|
|
visible: root.mainInputLoading
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusBaseText {
|
|
|
|
id: bottomItem
|
|
|
|
|
|
|
|
objectName: "bottomItemText"
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
Layout.fillWidth: true
|
|
|
|
|
|
|
|
text: {
|
|
|
|
const divisor = SQUtils.AmountsArithmetic.fromExponent(
|
|
|
|
d.fiatMode ? root.multiplierIndex
|
|
|
|
: root.fiatDecimalPlaces)
|
|
|
|
const divided = SQUtils.AmountsArithmetic.div(
|
|
|
|
SQUtils.AmountsArithmetic.fromString(
|
|
|
|
d.secondaryValue), divisor)
|
|
|
|
const asNumber = SQUtils.AmountsArithmetic.toNumber(divided)
|
|
|
|
|
|
|
|
return d.fiatMode ? root.formatBalance(asNumber)
|
|
|
|
: root.formatFiat(asNumber)
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
2024-05-28 17:39:41 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
elide: Text.ElideMiddle
|
|
|
|
font.pixelSize: 13
|
|
|
|
color: Theme.palette.directColor5
|
|
|
|
|
|
|
|
MouseArea {
|
|
|
|
objectName: "amountToSend_mouseArea"
|
|
|
|
|
|
|
|
anchors.fill: parent
|
|
|
|
cursorShape: enabled ? Qt.PointingHandCursor : undefined
|
|
|
|
enabled: root.fiatInputInteractive
|
|
|
|
|
|
|
|
onClicked: {
|
|
|
|
const secondaryValue = d.secondaryValue
|
|
|
|
|
|
|
|
d.fiatMode = !d.fiatMode
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
if (textField.length === 0)
|
|
|
|
return
|
|
|
|
|
|
|
|
const decimalPlaces = d.fiatMode ? root.fiatDecimalPlaces
|
|
|
|
: root.multiplierIndex
|
|
|
|
const divisor = SQUtils.AmountsArithmetic.fromExponent(
|
|
|
|
decimalPlaces)
|
|
|
|
|
|
|
|
const stringNumber = SQUtils.AmountsArithmetic.div(
|
|
|
|
SQUtils.AmountsArithmetic.fromString(secondaryValue),
|
|
|
|
divisor).toFixed(decimalPlaces)
|
|
|
|
|
|
|
|
const trimmed = d.fiatMode
|
|
|
|
? stringNumber
|
|
|
|
: d.removeDecimalTrailingZeros(stringNumber)
|
|
|
|
|
|
|
|
textField.text = d.localize(trimmed)
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-14 21:56:48 +00:00
|
|
|
visible: !root.bottomTextLoading
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 21:56:48 +00:00
|
|
|
LoadingComponent {
|
|
|
|
objectName: "bottomItemTextLoadingComponent"
|
|
|
|
Layout.preferredWidth: bottomItem.width
|
|
|
|
Layout.preferredHeight: bottomItem.height
|
|
|
|
visible: root.bottomTextLoading
|
|
|
|
}
|
2024-05-28 17:39:41 +00:00
|
|
|
}
|
|
|
|
}
|