Browse Source

move units and amount formatting to simple_config

master
ThomasV 6 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) self.show_info(_('Payment failed') + '\n\n' + reason)
def _get_bu(self): def _get_bu(self):
decimal_point = self.electrum_config.get('decimal_point', DECIMAL_POINT_DEFAULT) return self.electrum_config.get_base_unit()
try:
return decimal_point_to_base_unit_name(decimal_point)
except UnknownBaseUnit:
return decimal_point_to_base_unit_name(DECIMAL_POINT_DEFAULT)
def _set_bu(self, value): def _set_bu(self, value):
assert value in base_units.keys() self.electrum_config.set_base_unit(value)
decimal_point = base_unit_name_to_decimal_point(value)
self.electrum_config.set_key('decimal_point', decimal_point, True)
self._trigger_update_status() self._trigger_update_status()
self._trigger_update_history() self._trigger_update_history()
@ -279,7 +273,7 @@ class ElectrumWindow(App):
self._trigger_update_history() self._trigger_update_history()
def decimal_point(self): def decimal_point(self):
return base_units[self.base_unit] return self.electrum_config.get_decimal_point()
def btc_to_fiat(self, amount_str): def btc_to_fiat(self, amount_str):
if not 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.create_status_bar()
self.need_update = threading.Event() 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() self.completions = QStringListModel()
@ -859,24 +853,23 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
self.notify_transactions() self.notify_transactions()
def format_amount(self, x, is_diff=False, whitespaces=False): 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): 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 x = self.fx.format_amount_and_units(amount) if self.fx else None
if text and x: if text and x:
text += ' (%s)'%x text += ' (%s)'%x
return text return text
def format_fee_rate(self, fee_rate): def format_fee_rate(self, fee_rate):
# fee_rate is in sat/kB return self.config.format_fee_rate(fee_rate)
return format_fee_satoshis(fee_rate/1000, num_zeros=self.num_zeros) + ' sat/byte'
def get_decimal_point(self): def get_decimal_point(self):
return self.decimal_point return self.config.get_decimal_point()
def base_unit(self): 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): 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.i18n import _
from electrum import util, coinchooser, paymentrequest 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, from .util import (ColorScheme, WindowModalDialog, HelpLabel, Buttons,
CloseButton) CloseButton)
@ -89,14 +89,14 @@ class SettingsDialog(WindowModalDialog):
nz_label = HelpLabel(_('Zeros after decimal point') + ':', nz_help) nz_label = HelpLabel(_('Zeros after decimal point') + ':', nz_help)
nz = QSpinBox() nz = QSpinBox()
nz.setMinimum(0) nz.setMinimum(0)
nz.setMaximum(self.window.decimal_point) nz.setMaximum(self.config.decimal_point)
nz.setValue(self.window.num_zeros) nz.setValue(self.config.num_zeros)
if not self.config.is_modifiable('num_zeros'): if not self.config.is_modifiable('num_zeros'):
for w in [nz, nz_label]: w.setEnabled(False) for w in [nz, nz_label]: w.setEnabled(False)
def on_nz(): def on_nz():
value = nz.value() value = nz.value()
if self.window.num_zeros != value: if self.config.num_zeros != value:
self.window.num_zeros = value self.config.num_zeros = value
self.config.set_key('num_zeros', value, True) self.config.set_key('num_zeros', value, True)
self.window.history_list.update() self.window.history_list.update()
self.window.address_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 return
edits = self.window.amount_e, self.window.receive_amount_e edits = self.window.amount_e, self.window.receive_amount_e
amounts = [edit.get_amount() for edit in edits] amounts = [edit.get_amount() for edit in edits]
self.window.decimal_point = base_unit_name_to_decimal_point(unit_result) self.config.set_base_unit(unit_result)
self.config.set_key('decimal_point', self.window.decimal_point, True) nz.setMaximum(self.config.decimal_point)
nz.setMaximum(self.window.decimal_point)
self.window.history_list.update() self.window.history_list.update()
self.window.request_list.update() self.window.request_list.update()
self.window.address_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 util
from . import constants from . import constants
from .util import (user_dir, make_dir, from .util import base_units, base_unit_name_to_decimal_point
NoDynamicFeeEstimates, format_fee_satoshis, quantize_feerate) 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 .i18n import _
from .logging import get_logger, Logger from .logging import get_logger, Logger
@ -103,6 +104,14 @@ class SimpleConfig(Logger):
self._check_dependent_keys() 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): def electrum_path(self):
# Read electrum_path from command line # Read electrum_path from command line
# Otherwise use the user's default data directory. # Otherwise use the user's default data directory.
@ -604,6 +613,26 @@ class SimpleConfig(Logger):
except: except:
pass 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): def read_user_config(path):
"""Parse and store the user config settings in electrum.conf into user_config[].""" """Parse and store the user config settings in electrum.conf into user_config[]."""

Loading…
Cancel
Save