From 426198dd426c54b4e867d6e8bc34257379326804 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Wed, 23 Mar 2022 13:51:46 +0100 Subject: [PATCH] add base unit setting and conversion methods in qeconfig.py --- electrum/gui/qml/qeconfig.py | 60 ++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/electrum/gui/qml/qeconfig.py b/electrum/gui/qml/qeconfig.py index d65e22819..14e9f9ba2 100644 --- a/electrum/gui/qml/qeconfig.py +++ b/electrum/gui/qml/qeconfig.py @@ -1,5 +1,7 @@ from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject +from decimal import Decimal + from electrum.logging import get_logger class QEConfig(QObject): @@ -10,9 +12,6 @@ class QEConfig(QObject): _logger = get_logger(__name__) autoConnectChanged = pyqtSignal() - serverStringChanged = pyqtSignal() - manualServerChanged = pyqtSignal() - @pyqtProperty(bool, notify=autoConnectChanged) def autoConnect(self): return self.config.get('auto_connect') @@ -27,6 +26,7 @@ class QEConfig(QObject): def autoConnectDefined(self): return self.config.get('auto_connect') is not None + serverStringChanged = pyqtSignal() @pyqtProperty('QString', notify=serverStringChanged) def serverString(self): return self.config.get('server') @@ -36,6 +36,7 @@ class QEConfig(QObject): self.config.set_key('server', server, True) self.serverStringChanged.emit() + manualServerChanged = pyqtSignal() @pyqtProperty(bool, notify=manualServerChanged) def manualServer(self): return self.config.get('oneserver') @@ -45,3 +46,56 @@ class QEConfig(QObject): self.config.set_key('oneserver', oneserver, True) self.manualServerChanged.emit() + baseUnitChanged = pyqtSignal() + @pyqtProperty(str, notify=baseUnitChanged) + def baseUnit(self): + return self.config.get_base_unit() + + @baseUnit.setter + def baseUnit(self, unit): + self.config.set_base_unit(unit) + self.baseUnitChanged.emit() + + thousandsSeparatorChanged = pyqtSignal() + @pyqtProperty(bool, notify=thousandsSeparatorChanged) + def thousandsSeparator(self): + return self.config.get('amt_add_thousands_sep', False) + + @thousandsSeparator.setter + def thousandsSeparator(self, checked): + self.config.set_key('amt_add_thousands_sep', checked) + self.config.amt_add_thousands_sep = checked + self.thousandsSeparatorChanged.emit() + + + @pyqtSlot(int, result=str) + @pyqtSlot(int, bool, result=str) + def formatSats(self, satoshis, with_unit=False): + if with_unit: + return self.config.format_amount_and_units(satoshis) + else: + return self.config.format_amount(satoshis) + + # TODO delegate all this to config.py/util.py + def decimal_point(self): + return self.config.get('decimal_point') + + def max_precision(self): + return self.decimal_point() + 0 #self.extra_precision + + @pyqtSlot(str, result=int) + def unitsToSats(self, unitAmount): + # returns amt in satoshis + try: + x = Decimal(unitAmount) + except: + return None + # scale it to max allowed precision, make it an int + max_prec_amount = int(pow(10, self.max_precision()) * x) + # if the max precision is simply what unit conversion allows, just return + if self.max_precision() == self.decimal_point(): + return max_prec_amount + # otherwise, scale it back to the expected unit + #amount = Decimal(max_prec_amount) / Decimal(pow(10, self.max_precision()-self.decimal_point())) + #return int(amount) #Decimal(amount) if not self.is_int else int(amount) +