1
0

add fiat<->sat conversion methods and hook up UI

This commit is contained in:
Sander van Grieken
2022-04-04 17:18:04 +02:00
parent 5d77daa5e3
commit d5cfb67ebe
4 changed files with 107 additions and 8 deletions

View File

@@ -81,16 +81,20 @@ Pane {
TextField {
id: amountFiat
visible: Config.fiatCurrency != ''
font.family: FixedFont
Layout.fillWidth: true
inputMethodHints: Qt.ImhDigitsOnly
}
Label {
text: qsTr('EUR')
visible: Config.fiatCurrency != ''
text: Config.fiatCurrency
color: Material.accentColor
}
Item { visible: Config.fiatCurrency == ''; width: 1; height: 1; Layout.columnSpan: 2 }
RowLayout {
Layout.columnSpan: 4
Layout.alignment: Qt.AlignHCenter
@@ -346,4 +350,29 @@ Pane {
}
}
Connections {
target: amount
function onTextChanged() {
if (amountFiat.activeFocus)
return
var a = Config.unitsToSats(amount.text)
amountFiat.text = Daemon.fiatValue(a)
}
}
Connections {
target: amountFiat
function onTextChanged() {
if (amountFiat.activeFocus) {
amount.text = Daemon.satoshiValue(amountFiat.text)
}
}
}
Connections {
target: Network
function onFiatUpdated() {
var a = Config.unitsToSats(amount.text)
amountFiat.text = Daemon.fiatValue(a)
}
}
}

View File

@@ -8,10 +8,10 @@ Pane {
GridLayout {
width: parent.width
columns: 4
columns: 6
BalanceSummary {
Layout.columnSpan: 4
Layout.columnSpan: 6
Layout.alignment: Qt.AlignHCenter
}
@@ -21,7 +21,7 @@ Pane {
TextField {
id: address
Layout.columnSpan: 2
Layout.columnSpan: 4
Layout.fillWidth: true
font.family: FixedFont
placeholderText: qsTr('Paste address or invoice')
@@ -46,11 +46,26 @@ Pane {
}
Label {
text: Config.baseUnit
text: Config.baseUnit + ' ' // add spaces for easy right margin
color: Material.accentColor
Layout.fillWidth: true
}
TextField {
id: amountFiat
visible: Config.fiatCurrency != ''
font.family: FixedFont
placeholderText: qsTr('Amount')
inputMethodHints: Qt.ImhPreferNumbers
}
Label {
visible: Config.fiatCurrency != ''
text: Config.fiatCurrency
color: Material.accentColor
}
Item { visible: Config.fiatCurrency == ''; height: 1; Layout.columnSpan: 2; Layout.fillWidth: true }
Item { width: 1; height: 1 } // workaround colspan on baseunit messing up row above
Label {
@@ -61,11 +76,11 @@ Pane {
id: fee
font.family: FixedFont
placeholderText: qsTr('sat/vB')
Layout.columnSpan: 3
Layout.columnSpan: 5
}
RowLayout {
Layout.columnSpan: 4
Layout.columnSpan: 6
Layout.alignment: Qt.AlignHCenter
spacing: 10
@@ -94,6 +109,31 @@ Pane {
}
}
Connections {
target: amount
function onTextChanged() {
if (amountFiat.activeFocus)
return
var a = Config.unitsToSats(amount.text)
amountFiat.text = Daemon.fiatValue(a)
}
}
Connections {
target: amountFiat
function onTextChanged() {
if (amountFiat.activeFocus) {
amount.text = Daemon.satoshiValue(amountFiat.text)
}
}
}
Connections {
target: Network
function onFiatUpdated() {
var a = Config.unitsToSats(amount.text)
amountFiat.text = Daemon.fiatValue(a)
}
}
// make clicking the dialog background move the scope away from textedit fields
// so the keyboard goes away
MouseArea {

View File

@@ -121,6 +121,8 @@ class ElectrumQmlApplication(QGuiApplication):
'protocol_version': version.PROTOCOL_VERSION
})
self._qeconfig.fiatCurrencyChanged.connect(self._qedaemon.setFiatCurrency)
qInstallMessageHandler(self.message_handler)
# get notified whether root QML document loads or not

View File

@@ -1,4 +1,5 @@
import os
from decimal import Decimal
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QUrl
from PyQt5.QtCore import Qt, QAbstractListModel, QModelIndex
@@ -7,6 +8,7 @@ from electrum.util import register_callback, get_new_wallet_name, WalletFileExce
from electrum.logging import get_logger
from electrum.wallet import Wallet, Abstract_Wallet
from electrum.storage import WalletStorage, StorageReadWriteError
from electrum.bitcoin import COIN
from .qewallet import QEWallet
@@ -133,6 +135,32 @@ class QEDaemon(QObject):
self._logger.error(str(e))
self.walletOpenError.emit(str(e))
@pyqtSlot(str, result=str)
def fiatValue(self, satoshis):
rate = self.daemon.fx.exchange_rate()
try:
sd = Decimal(satoshis)
if sd == 0:
return ''
except:
return ''
return self.daemon.fx.value_str(satoshis,rate)
# TODO: move conversion to FxThread
@pyqtSlot(str, result=str)
def satoshiValue(self, fiat):
rate = self.daemon.fx.exchange_rate()
try:
fd = Decimal(fiat)
except:
return ''
v = fd / Decimal(rate) * COIN
return '' if v.is_nan() else self.daemon.config.format_amount(v)
@pyqtSlot()
def setFiatCurrency(self):
self.daemon.fx.set_currency(self.daemon.config.get('currency'))
@pyqtProperty('QString')
def path(self):
return self._path