Files
pay/desktop/widgets/BitcoinAmountLabel.qml
T

139 lines
4.5 KiB
QML
Raw Permalink Normal View History

2020-05-24 13:20:03 +02:00
/*
* This file is part of the Flowee project
2021-01-05 14:03:30 +01:00
* Copyright (C) 2020 Tom Zander <tom@flowee.org>
2020-05-24 13:20:03 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2021-05-23 00:27:34 +02:00
import QtQuick 2.11
import QtQuick.Controls 2.11
import QtQuick.Layouts 1.11
2020-05-24 13:20:03 +02:00
2020-10-16 18:17:56 +02:00
/**
* This class displays a Bitcoin value using the current settings
* and renders it smartly to avoid it just being a long list of digits.
*/
2021-11-30 12:01:31 +01:00
Control {
2020-05-24 13:20:03 +02:00
id: root
2020-10-15 20:04:10 +02:00
property double value: 5E8
2020-05-24 13:20:03 +02:00
property bool colorize: true
2020-10-15 19:18:54 +02:00
property bool includeUnit: true
2021-05-08 00:03:07 +02:00
property bool showFiat: true
2022-08-12 21:22:04 +02:00
/**
* If this is set to something other than zero we assume its a unix
* date and the fiat price is calculated based on the historical price
* This is only used if you fill it with a date object.
*/
property var fiatTimestamp: null
2021-11-30 12:01:31 +01:00
property color color: palette.text
2021-05-07 12:46:00 +02:00
property alias fontPtSize: main.font.pointSize
2020-05-24 13:20:03 +02:00
2021-10-29 18:20:42 +02:00
implicitHeight: row.implicitHeight
implicitWidth: Math.max(row.maxWidth, row.implicitWidth)
2021-10-29 18:20:42 +02:00
2020-10-15 19:18:54 +02:00
height: main.height
2021-11-23 17:16:20 +01:00
width: implicitWidth
2020-10-15 19:18:54 +02:00
baselineOffset: main.baselineOffset
2020-05-24 13:20:03 +02:00
2021-10-29 18:20:42 +02:00
onValueChanged: row.calcString(value)
2020-10-15 20:04:10 +02:00
2021-10-29 18:20:42 +02:00
RowLayout {
id: row
height: parent.height
2020-10-15 20:04:10 +02:00
property int maxWidth: 0
onImplicitWidthChanged: maxWidth = Math.max(maxWidth, implicitWidth)
2021-10-29 18:20:42 +02:00
// calculated
property string amountString: "";
Connections {
2021-11-02 19:29:14 +01:00
target: Pay
2021-10-29 18:20:42 +02:00
function onUnitChanged(unit) {
2021-11-03 12:23:09 +01:00
row.calcString(root.value);
2020-10-17 20:26:36 +02:00
}
}
2021-10-29 18:20:42 +02:00
function calcString(sats) {
2022-08-14 16:46:23 +02:00
amountString = Pay.amountToString(sats)
2020-05-24 13:20:03 +02:00
}
2021-10-29 18:20:42 +02:00
Label {
id: main
text: {
var s = row.amountString
2021-11-02 19:29:14 +01:00
var removeChars = Pay.unitAllowedDecimals
2021-10-29 18:20:42 +02:00
if (removeChars > 3)
removeChars -= 3; // the next text field eats those
return s.substring(0, s.length - removeChars)
}
color: {
if (root.colorize) {
var num = root.value
if (num > 0)
// positive value
2021-11-02 19:29:14 +01:00
return Pay.useDarkSkin ? "#86ffa8" : "green";
2021-10-29 18:20:42 +02:00
else if (num < 0) // negative
2021-11-02 19:29:14 +01:00
return Pay.useDarkSkin ? "#ffdede" : "#444446";
2021-10-29 18:20:42 +02:00
// zero is shown without color, like below.
}
2021-10-30 19:23:43 +02:00
return root.color
2021-10-29 18:20:42 +02:00
}
Layout.alignment: Qt.AlignBaseline
2020-05-24 13:20:03 +02:00
}
2021-10-29 18:20:42 +02:00
Label {
text: {
var s = row.amountString
var pos = s.length - 5
return s.substring(pos, pos + 3);
}
font.pointSize: satsLabel.font.pointSize
color: main.color
opacity: (satsLabel.opacity !== 1 && text == "000") ? 0.3 : 1
Layout.alignment: Qt.AlignBaseline
2021-11-02 19:29:14 +01:00
visible: Pay.unitAllowedDecimals === 8
2021-10-29 18:20:42 +02:00
}
Label {
id: satsLabel
text: {
var s = row.amountString
return s.substring(s.length - 2);
}
font.pointSize: main.font.pointSize / 10 * 8
color: main.color
opacity: text == "00" ? 0.3 : 1
Layout.alignment: Qt.AlignBaseline
2021-11-02 19:29:14 +01:00
visible: Pay.unitAllowedDecimals >= 2
2021-10-29 18:20:42 +02:00
}
2021-05-08 00:03:07 +02:00
2021-10-29 18:20:42 +02:00
Label {
2021-11-02 19:29:14 +01:00
text: Pay.unitName
2021-10-29 18:20:42 +02:00
color: main.color
visible: root.includeUnit
Layout.alignment: Qt.AlignBaseline
}
Label {
2022-08-12 21:22:04 +02:00
visible: root.showFiat
Layout.alignment: Qt.AlignBaseline
text: {
var fiatPrice;
2022-08-14 00:06:37 +02:00
if (root.fiatTimestamp == null || fiatHistory == null)
2022-08-12 21:22:04 +02:00
fiatPrice = Fiat.price; // todays price
else
fiatPrice = fiatHistory.historicalPrice(root.fiatTimestamp);
Fiat.formattedPrice(root.value, fiatPrice)
}
2021-10-29 18:20:42 +02:00
}
2021-05-08 00:03:07 +02:00
}
2020-05-24 13:20:03 +02:00
}