Skip to content

Commit

Permalink
move fiat columns show/hide settings from settings_dialog to tab tool…
Browse files Browse the repository at this point in the history
…bars
  • Loading branch information
ecdsa committed Mar 12, 2023
1 parent 98f0526 commit 503776c
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 90 deletions.
27 changes: 3 additions & 24 deletions electrum/exchange_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ async def run(self):
await self._trigger.wait()
self._trigger.clear()
# we were manually triggered, so get historical rates
if self.is_enabled() and self.show_history():
if self.is_enabled() and self.has_history():
self.exchange.get_historical_rates(self.ccy, self.cache_dir)
except TaskTimeout:
pass
Expand All @@ -597,26 +597,8 @@ def set_enabled(self, b):
self.config.set_key('use_exchange_rate', bool(b))
self.trigger_update()

def get_history_config(self, *, allow_none=False):
val = self.config.get('history_rates', None)
if val is None and allow_none:
return None
return bool(val)

def set_history_config(self, b):
self.config.set_key('history_rates', bool(b))

def get_history_capital_gains_config(self):
return bool(self.config.get('history_rates_capital_gains', False))

def set_history_capital_gains_config(self, b):
self.config.set_key('history_rates_capital_gains', bool(b))

def get_fiat_address_config(self):
return bool(self.config.get('fiat_address'))

def set_fiat_address_config(self, b):
self.config.set_key('fiat_address', bool(b))
def has_history(self):
return self.is_enabled() and self.ccy in self.exchange.history_ccys()

def get_currency(self) -> str:
'''Use when dynamic fetching is needed'''
Expand All @@ -625,9 +607,6 @@ def get_currency(self) -> str:
def config_exchange(self):
return self.config.get('use_exchange', DEFAULT_EXCHANGE)

def show_history(self):
return self.is_enabled() and self.get_history_config() and self.ccy in self.exchange.history_ccys()

def set_currency(self, ccy: str):
self.ccy = ccy
self.config.set_key('currency', ccy, True)
Expand Down
4 changes: 2 additions & 2 deletions electrum/gui/qml/qefx.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def fiatCurrency(self, currency):
historicRatesChanged = pyqtSignal()
@pyqtProperty(bool, notify=historicRatesChanged)
def historicRates(self):
return self.fx.get_history_config()
return self.fx.config.get('history_rates', True)

@historicRates.setter
def historicRates(self, checked):
if checked != self.historicRates:
self.fx.set_history_config(checked)
self.fx.config.set_key('history_rates', bool(checked))
self.historicRatesChanged.emit()
self.rateSourcesChanged.emit()

Expand Down
7 changes: 5 additions & 2 deletions electrum/gui/qml/qetransactionlistmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ def init_model(self, force: bool = False):
return

self._logger.debug('retrieving history')
history = self.wallet.get_full_history(onchain_domain=self.onchain_domain,
include_lightning=self.include_lightning)
history = self.wallet.get_full_history(
onchain_domain=self.onchain_domain,
include_lightning=self.include_lightning,
include_fiat=False,
)
txs = []
for key, tx in history.items():
txs.append(self.tx_to_model(tx))
Expand Down
14 changes: 9 additions & 5 deletions electrum/gui/qt/address_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def __init__(self, parent):
super().__init__(parent, self.create_menu,
stretch_column=self.Columns.LABEL,
editable_columns=[self.Columns.LABEL])
self.main_window = parent
self.wallet = self.parent.wallet
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.setSortingEnabled(True)
Expand All @@ -111,10 +112,14 @@ def __init__(self, parent):
def create_toolbar(self, config):
toolbar, menu = self.create_toolbar_with_menu('')
menu.addToggle(_("Show Filter"), lambda: self.toggle_toolbar(self.config))
menu.addConfig(_('Show Fiat balances'), 'fiat_address', False, callback=self.main_window.app.update_fiat_signal.emit)
hbox = self.create_toolbar_buttons()
toolbar.insertLayout(1, hbox)
return toolbar

def should_show_fiat(self):
return self.parent.fx and self.parent.fx.is_enabled() and self.config.get('fiat_address', False)

def get_toolbar_buttons(self):
return QLabel(_("Filter:")), self.change_button, self.used_button

Expand All @@ -124,9 +129,8 @@ def on_hide_toolbar(self):
self.update()

def refresh_headers(self):
fx = self.parent.fx
if fx and fx.get_fiat_address_config():
ccy = fx.get_currency()
if self.should_show_fiat():
ccy = self.parent.fx.get_currency()
else:
ccy = _('Fiat')
headers = {
Expand Down Expand Up @@ -211,7 +215,7 @@ def update(self):
set_address = QPersistentModelIndex(address_idx)
self.set_current_idx(set_address)
# show/hide columns
if fx and fx.get_fiat_address_config():
if self.should_show_fiat():
self.showColumn(self.Columns.FIAT_BALANCE)
else:
self.hideColumn(self.Columns.FIAT_BALANCE)
Expand All @@ -228,7 +232,7 @@ def refresh_row(self, key, row):
balance_text = self.parent.format_amount(balance, whitespaces=True)
# create item
fx = self.parent.fx
if fx and fx.get_fiat_address_config():
if self.should_show_fiat():
rate = fx.exchange_rate()
fiat_balance_str = fx.value_str(balance, rate)
else:
Expand Down
38 changes: 30 additions & 8 deletions electrum/gui/qt/history_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ def should_include_lightning_payments(self) -> bool:
"""Overridden in address_dialog.py"""
return True

def should_show_fiat(self):
if not self.window.config.get('history_rates', False):
return False
fx = self.window.fx
if not fx or not fx.is_enabled():
return False
return fx.has_history()

def should_show_capital_gains(self):
return self.should_show_fiat() and self.window.config.get('history_rates_capital_gains', False)

@profiler
def refresh(self, reason: str):
self.logger.info(f"refreshing... reason: {reason}")
Expand All @@ -280,7 +291,9 @@ def refresh(self, reason: str):
transactions = wallet.get_full_history(
self.window.fx,
onchain_domain=self.get_domain(),
include_lightning=self.should_include_lightning_payments())
include_lightning=self.should_include_lightning_payments(),
include_fiat=self.should_show_fiat(),
)
if transactions == self.transactions:
return
old_length = self._root.childCount()
Expand Down Expand Up @@ -361,8 +374,8 @@ def set_visible(col: int, b: bool):
set_visible(HistoryColumns.TXID, False)
set_visible(HistoryColumns.SHORT_ID, False)
# fiat
history = self.window.fx.show_history()
cap_gains = self.window.fx.get_history_capital_gains_config()
history = self.should_show_fiat()
cap_gains = self.should_show_capital_gains()
set_visible(HistoryColumns.FIAT_VALUE, history)
set_visible(HistoryColumns.FIAT_ACQ_PRICE, history and cap_gains)
set_visible(HistoryColumns.FIAT_CAP_GAINS, history and cap_gains)
Expand Down Expand Up @@ -412,7 +425,7 @@ def headerData(self, section: int, orientation: Qt.Orientation, role: Qt.ItemDat
fiat_title = 'n/a fiat value'
fiat_acq_title = 'n/a fiat acquisition price'
fiat_cg_title = 'n/a fiat capital gains'
if fx and fx.show_history():
if self.should_show_fiat():
fiat_title = '%s '%fx.ccy + _('Value')
fiat_acq_title = '%s '%fx.ccy + _('Acquisition price')
fiat_cg_title = '%s '%fx.ccy + _('Capital Gains')
Expand Down Expand Up @@ -473,6 +486,7 @@ def __init__(self, parent, model: HistoryModel):
super().__init__(parent, self.create_menu,
stretch_column=HistoryColumns.DESCRIPTION,
editable_columns=[HistoryColumns.DESCRIPTION, HistoryColumns.FIAT_VALUE])
self.main_window = parent
self.config = parent.config
self.hm = model
self.proxy = HistorySortModel(self)
Expand Down Expand Up @@ -529,14 +543,23 @@ def on_combo(self, x):

def create_toolbar(self, config):
toolbar, menu = self.create_toolbar_with_menu('')
menu.addToggle(_("&Filter Period"), lambda: self.toggle_toolbar(self.config))
menu.addToggle(_("Filter by Date"), lambda: self.toggle_toolbar(self.config))
self.menu_fiat = menu.addConfig(_('Show Fiat Values'), 'history_rates', False, callback=self.main_window.app.update_fiat_signal.emit)
self.menu_capgains = menu.addConfig(_('Show Capital Gains'), 'history_rates_capital_gains', False, callback=self.main_window.app.update_fiat_signal.emit)
menu.addAction(_("&Summary"), self.show_summary)
menu.addAction(_("&Plot"), self.plot_history_dialog)
menu.addAction(_("&Export"), self.export_history_dialog)
hbox = self.create_toolbar_buttons()
toolbar.insertLayout(1, hbox)
self.update_toolbar_menu()
return toolbar

def update_toolbar_menu(self):
fx = self.main_window.fx
b = fx and fx.is_enabled() and fx.has_history()
self.menu_fiat.setEnabled(b)
self.menu_capgains.setEnabled(b)

def get_toolbar_buttons(self):
return self.period_combo, self.start_button, self.end_button

Expand Down Expand Up @@ -574,11 +597,10 @@ def on_date(date):
return datetime.datetime(date.year, date.month, date.day)

def show_summary(self):
fx = self.parent.fx
show_fiat = fx and fx.is_enabled() and fx.get_history_config()
if not show_fiat:
if not self.hm.should_show_fiat():
self.parent.show_message(_("Enable fiat exchange rate with history."))
return
fx = self.parent.fx
h = self.wallet.get_detailed_history(
from_timestamp = time.mktime(self.start_date.timetuple()) if self.start_date else None,
to_timestamp = time.mktime(self.end_date.timetuple()) if self.end_date else None,
Expand Down
1 change: 1 addition & 0 deletions electrum/gui/qt/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -2463,6 +2463,7 @@ def update_fiat(self):
self.send_tab.fiat_send_e.setVisible(b)
self.receive_tab.fiat_receive_e.setVisible(b)
self.history_model.refresh('update_fiat')
self.history_list.update_toolbar_menu()
self.address_list.refresh_headers()
self.address_list.update()
self.update_status()
Expand Down
55 changes: 9 additions & 46 deletions electrum/gui/qt/settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,41 +313,25 @@ def on_be_edit():
block_ex_hbox_w.setLayout(block_ex_hbox)

# Fiat Currency
hist_checkbox = QCheckBox()
hist_capgains_checkbox = QCheckBox()
fiat_address_checkbox = QCheckBox()
self.require_history_checkbox = QCheckBox()
ccy_combo = QComboBox()
ex_combo = QComboBox()

def update_currencies():
if not self.fx:
return
currencies = sorted(self.fx.get_currencies(self.fx.get_history_config()))
currencies = sorted(self.fx.get_currencies(self.require_history_checkbox.isChecked()))
ccy_combo.clear()
ccy_combo.addItems([_('None')] + currencies)
if self.fx.is_enabled():
ccy_combo.setCurrentIndex(ccy_combo.findText(self.fx.get_currency()))

def update_history_cb():
if not self.fx: return
hist_checkbox.setChecked(self.fx.get_history_config())
hist_checkbox.setEnabled(self.fx.is_enabled())

def update_fiat_address_cb():
if not self.fx: return
fiat_address_checkbox.setChecked(self.fx.get_fiat_address_config())

def update_history_capgains_cb():
if not self.fx: return
hist_capgains_checkbox.setChecked(self.fx.get_history_capital_gains_config())
hist_capgains_checkbox.setEnabled(hist_checkbox.isChecked())

def update_exchanges():
if not self.fx: return
b = self.fx.is_enabled()
ex_combo.setEnabled(b)
if b:
h = self.fx.get_history_config()
h = self.require_history_checkbox.isChecked()
c = self.fx.get_currency()
exchanges = self.fx.get_exchanges_by_ccy(c, h)
else:
Expand All @@ -365,43 +349,24 @@ def on_currency(hh):
self.fx.set_enabled(b)
if b and ccy != self.fx.ccy:
self.fx.set_currency(ccy)
update_history_cb()
update_exchanges()
self.app.update_fiat_signal.emit()

def on_exchange(idx):
exchange = str(ex_combo.currentText())
if self.fx and self.fx.is_enabled() and exchange and exchange != self.fx.exchange.name():
self.fx.set_exchange(exchange)

def on_history(checked):
if not self.fx: return
self.fx.set_history_config(checked)
update_exchanges()
if self.fx.is_enabled() and checked:
self.fx.trigger_update()
update_history_capgains_cb()
self.app.update_fiat_signal.emit()

def on_history_capgains(checked):
if not self.fx: return
self.fx.set_history_capital_gains_config(checked)
self.app.update_fiat_signal.emit()

def on_fiat_address(checked):
if not self.fx: return
self.fx.set_fiat_address_config(checked)
self.app.update_fiat_signal.emit()
def on_require_history(checked):
if not self.fx:
return
update_exchanges()

update_currencies()
update_history_cb()
update_history_capgains_cb()
update_fiat_address_cb()
update_exchanges()
ccy_combo.currentIndexChanged.connect(on_currency)
hist_checkbox.stateChanged.connect(on_history)
hist_capgains_checkbox.stateChanged.connect(on_history_capgains)
fiat_address_checkbox.stateChanged.connect(on_fiat_address)
self.require_history_checkbox.stateChanged.connect(on_require_history)
ex_combo.currentIndexChanged.connect(on_exchange)

gui_widgets = []
Expand All @@ -419,9 +384,7 @@ def on_fiat_address(checked):
fiat_widgets = []
fiat_widgets.append((QLabel(_('Fiat currency')), ccy_combo))
fiat_widgets.append((QLabel(_('Source')), ex_combo))
fiat_widgets.append((QLabel(_('Show history rates')), hist_checkbox))
fiat_widgets.append((QLabel(_('Show capital gains in history')), hist_capgains_checkbox))
fiat_widgets.append((QLabel(_('Show Fiat balance for addresses')), fiat_address_checkbox))
fiat_widgets.append((QLabel(_('Show sources with historical data')), self.require_history_checkbox))
misc_widgets = []
misc_widgets.append((updatecheck_cb, None))
misc_widgets.append((filelogging_cb, None))
Expand Down
6 changes: 3 additions & 3 deletions electrum/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ def is_onchain_invoice_paid(self, invoice: BaseInvoice) -> Tuple[bool, Optional[
return is_paid, conf_needed

@profiler
def get_full_history(self, fx=None, *, onchain_domain=None, include_lightning=True):
def get_full_history(self, fx=None, *, onchain_domain=None, include_lightning=True, include_fiat=False):
transactions_tmp = OrderedDictWithIndex()
# add on-chain txns
onchain_history = self.get_onchain_history(domain=onchain_domain)
Expand Down Expand Up @@ -1245,7 +1245,7 @@ def get_full_history(self, fx=None, *, onchain_domain=None, include_lightning=Tr
item['value'] = Satoshis(value)
balance += value
item['balance'] = Satoshis(balance)
if fx and fx.is_enabled() and fx.get_history_config():
if include_fiat:
txid = item.get('txid')
if not item.get('lightning') and txid:
fiat_fields = self.get_tx_item_fiat(tx_hash=txid, amount_sat=value, fx=fx, tx_fee=item['fee_sat'])
Expand All @@ -1272,7 +1272,7 @@ def get_detailed_history(
and (from_height is not None or to_height is not None):
raise Exception('timestamp and block height based filtering cannot be used together')

show_fiat = fx and fx.is_enabled() and fx.get_history_config()
show_fiat = fx and fx.is_enabled() and fx.has_history()
out = []
income = 0
expenditures = 0
Expand Down

0 comments on commit 503776c

Please sign in to comment.