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 { TextField {
id: amountFiat id: amountFiat
visible: Config.fiatCurrency != ''
font.family: FixedFont font.family: FixedFont
Layout.fillWidth: true Layout.fillWidth: true
inputMethodHints: Qt.ImhDigitsOnly inputMethodHints: Qt.ImhDigitsOnly
} }
Label { Label {
text: qsTr('EUR') visible: Config.fiatCurrency != ''
text: Config.fiatCurrency
color: Material.accentColor color: Material.accentColor
} }
Item { visible: Config.fiatCurrency == ''; width: 1; height: 1; Layout.columnSpan: 2 }
RowLayout { RowLayout {
Layout.columnSpan: 4 Layout.columnSpan: 4
Layout.alignment: Qt.AlignHCenter 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 { GridLayout {
width: parent.width width: parent.width
columns: 4 columns: 6
BalanceSummary { BalanceSummary {
Layout.columnSpan: 4 Layout.columnSpan: 6
Layout.alignment: Qt.AlignHCenter Layout.alignment: Qt.AlignHCenter
} }
@@ -21,7 +21,7 @@ Pane {
TextField { TextField {
id: address id: address
Layout.columnSpan: 2 Layout.columnSpan: 4
Layout.fillWidth: true Layout.fillWidth: true
font.family: FixedFont font.family: FixedFont
placeholderText: qsTr('Paste address or invoice') placeholderText: qsTr('Paste address or invoice')
@@ -46,11 +46,26 @@ Pane {
} }
Label { Label {
text: Config.baseUnit text: Config.baseUnit + ' ' // add spaces for easy right margin
color: Material.accentColor 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 Item { width: 1; height: 1 } // workaround colspan on baseunit messing up row above
Label { Label {
@@ -61,11 +76,11 @@ Pane {
id: fee id: fee
font.family: FixedFont font.family: FixedFont
placeholderText: qsTr('sat/vB') placeholderText: qsTr('sat/vB')
Layout.columnSpan: 3 Layout.columnSpan: 5
} }
RowLayout { RowLayout {
Layout.columnSpan: 4 Layout.columnSpan: 6
Layout.alignment: Qt.AlignHCenter Layout.alignment: Qt.AlignHCenter
spacing: 10 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 // make clicking the dialog background move the scope away from textedit fields
// so the keyboard goes away // so the keyboard goes away
MouseArea { MouseArea {

View File

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

View File

@@ -1,4 +1,5 @@
import os import os
from decimal import Decimal
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QUrl from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QUrl
from PyQt5.QtCore import Qt, QAbstractListModel, QModelIndex 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.logging import get_logger
from electrum.wallet import Wallet, Abstract_Wallet from electrum.wallet import Wallet, Abstract_Wallet
from electrum.storage import WalletStorage, StorageReadWriteError from electrum.storage import WalletStorage, StorageReadWriteError
from electrum.bitcoin import COIN
from .qewallet import QEWallet from .qewallet import QEWallet
@@ -133,6 +135,32 @@ class QEDaemon(QObject):
self._logger.error(str(e)) self._logger.error(str(e))
self.walletOpenError.emit(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') @pyqtProperty('QString')
def path(self): def path(self):
return self._path return self._path