Browse Source

move units and amount formatting to simple_config

master
ThomasV 4 years ago
parent
commit
1c436bbc22
  1. 12
      electrum/gui/kivy/main_window.py
  2. 17
      electrum/gui/qt/main_window.py
  3. 15
      electrum/gui/qt/settings_dialog.py
  4. 33
      electrum/simple_config.py

12
electrum/gui/kivy/main_window.py

@ -258,16 +258,10 @@ class ElectrumWindow(App):
self.show_info(_('Payment failed') + '\n\n' + reason)
def _get_bu(self):
decimal_point = self.electrum_config.get('decimal_point', DECIMAL_POINT_DEFAULT)
try:
return decimal_point_to_base_unit_name(decimal_point)
except UnknownBaseUnit:
return decimal_point_to_base_unit_name(DECIMAL_POINT_DEFAULT)
return self.electrum_config.get_base_unit()
def _set_bu(self, value):
assert value in base_units.keys()
decimal_point = base_unit_name_to_decimal_point(value)
self.electrum_config.set_key('decimal_point', decimal_point, True)
self.electrum_config.set_base_unit(value)
self._trigger_update_status()
self._trigger_update_history()
@ -279,7 +273,7 @@ class ElectrumWindow(App):
self._trigger_update_history()
def decimal_point(self):
return base_units[self.base_unit]
return self.electrum_config.get_decimal_point()
def btc_to_fiat(self, amount_str):
if not amount_str:

17
electrum/gui/qt/main_window.py

@ -192,12 +192,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
self.create_status_bar()
self.need_update = threading.Event()
self.decimal_point = config.get('decimal_point', DECIMAL_POINT_DEFAULT)
try:
decimal_point_to_base_unit_name(self.decimal_point)
except UnknownBaseUnit:
self.decimal_point = DECIMAL_POINT_DEFAULT
self.num_zeros = int(config.get('num_zeros', 0))
self.completions = QStringListModel()
@ -859,24 +853,23 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
self.notify_transactions()
def format_amount(self, x, is_diff=False, whitespaces=False):
return format_satoshis(x, self.num_zeros, self.decimal_point, is_diff=is_diff, whitespaces=whitespaces)
return self.config.format_amount(x, is_diff=is_diff, whitespaces=whitespaces)
def format_amount_and_units(self, amount):
text = self.format_amount(amount) + ' '+ self.base_unit()
text = self.config.format_amount_and_units(amount)
x = self.fx.format_amount_and_units(amount) if self.fx else None
if text and x:
text += ' (%s)'%x
return text
def format_fee_rate(self, fee_rate):
# fee_rate is in sat/kB
return format_fee_satoshis(fee_rate/1000, num_zeros=self.num_zeros) + ' sat/byte'
return self.config.format_fee_rate(fee_rate)
def get_decimal_point(self):
return self.decimal_point
return self.config.get_decimal_point()
def base_unit(self):
return decimal_point_to_base_unit_name(self.decimal_point)
return self.config.get_base_unit()
def connect_fields(self, window, btc_e, fiat_e, fee_e):

15
electrum/gui/qt/settings_dialog.py

@ -33,7 +33,7 @@ from PyQt5.QtWidgets import (QComboBox, QTabWidget,
from electrum.i18n import _
from electrum import util, coinchooser, paymentrequest
from electrum.util import base_units_list, base_unit_name_to_decimal_point
from electrum.util import base_units_list
from .util import (ColorScheme, WindowModalDialog, HelpLabel, Buttons,
CloseButton)
@ -89,14 +89,14 @@ class SettingsDialog(WindowModalDialog):
nz_label = HelpLabel(_('Zeros after decimal point') + ':', nz_help)
nz = QSpinBox()
nz.setMinimum(0)
nz.setMaximum(self.window.decimal_point)
nz.setValue(self.window.num_zeros)
nz.setMaximum(self.config.decimal_point)
nz.setValue(self.config.num_zeros)
if not self.config.is_modifiable('num_zeros'):
for w in [nz, nz_label]: w.setEnabled(False)
def on_nz():
value = nz.value()
if self.window.num_zeros != value:
self.window.num_zeros = value
if self.config.num_zeros != value:
self.config.num_zeros = value
self.config.set_key('num_zeros', value, True)
self.window.history_list.update()
self.window.address_list.update()
@ -209,9 +209,8 @@ you close all your wallet windows. Use this to keep your local watchtower runnin
return
edits = self.window.amount_e, self.window.receive_amount_e
amounts = [edit.get_amount() for edit in edits]
self.window.decimal_point = base_unit_name_to_decimal_point(unit_result)
self.config.set_key('decimal_point', self.window.decimal_point, True)
nz.setMaximum(self.window.decimal_point)
self.config.set_base_unit(unit_result)
nz.setMaximum(self.config.decimal_point)
self.window.history_list.update()
self.window.request_list.update()
self.window.address_list.update()

33
electrum/simple_config.py

@ -13,8 +13,9 @@ from aiorpcx import NetAddress
from . import util
from . import constants
from .util import (user_dir, make_dir,
NoDynamicFeeEstimates, format_fee_satoshis, quantize_feerate)
from .util import base_units, base_unit_name_to_decimal_point
from .util import format_satoshis, format_fee_satoshis, decimal_point_to_base_unit_name, DECIMAL_POINT_DEFAULT
from .util import user_dir, make_dir, NoDynamicFeeEstimates, quantize_feerate
from .i18n import _
from .logging import get_logger, Logger
@ -103,6 +104,14 @@ class SimpleConfig(Logger):
self._check_dependent_keys()
# units and formatting
self.decimal_point = self.get('decimal_point', DECIMAL_POINT_DEFAULT)
try:
decimal_point_to_base_unit_name(self.decimal_point)
except UnknownBaseUnit:
self.decimal_point = DECIMAL_POINT_DEFAULT
self.num_zeros = int(self.get('num_zeros', 0))
def electrum_path(self):
# Read electrum_path from command line
# Otherwise use the user's default data directory.
@ -604,6 +613,26 @@ class SimpleConfig(Logger):
except:
pass
def format_amount(self, x, is_diff=False, whitespaces=False):
return format_satoshis(x, self.num_zeros, self.decimal_point, is_diff=is_diff, whitespaces=whitespaces)
def format_amount_and_units(self, amount):
return self.format_amount(amount) + ' '+ self.base_unit()
def format_fee_rate(self, fee_rate):
return format_fee_satoshis(fee_rate/1000, num_zeros=self.num_zeros) + ' sat/byte'
def get_base_unit(self):
return decimal_point_to_base_unit_name(self.decimal_point)
def set_base_unit(self, unit):
assert unit in base_units.keys()
self.decimal_point = base_unit_name_to_decimal_point(unit)
self.set_key('decimal_point', self.decimal_point, True)
def get_decimal_point(self):
return self.decimal_point
def read_user_config(path):
"""Parse and store the user config settings in electrum.conf into user_config[]."""

Loading…
Cancel
Save