From 503776c0dee2a544c0d746e9b853f29fe0b54279 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Sun, 12 Mar 2023 13:30:11 +0100 Subject: [PATCH] move fiat columns show/hide settings from settings_dialog to tab toolbars --- electrum/exchange_rate.py | 27 ++--------- electrum/gui/qml/qefx.py | 4 +- electrum/gui/qml/qetransactionlistmodel.py | 7 ++- electrum/gui/qt/address_list.py | 14 ++++-- electrum/gui/qt/history_list.py | 38 +++++++++++---- electrum/gui/qt/main_window.py | 1 + electrum/gui/qt/settings_dialog.py | 55 ++++------------------ electrum/wallet.py | 6 +-- 8 files changed, 62 insertions(+), 90 deletions(-) diff --git a/electrum/exchange_rate.py b/electrum/exchange_rate.py index 480b5030ec28..fd3280c9cbf3 100644 --- a/electrum/exchange_rate.py +++ b/electrum/exchange_rate.py @@ -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 @@ -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''' @@ -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) diff --git a/electrum/gui/qml/qefx.py b/electrum/gui/qml/qefx.py index ba1e0d1c7e8d..689481cfab7c 100644 --- a/electrum/gui/qml/qefx.py +++ b/electrum/gui/qml/qefx.py @@ -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() diff --git a/electrum/gui/qml/qetransactionlistmodel.py b/electrum/gui/qml/qetransactionlistmodel.py index 9081af66da90..2f98fbd1d5ce 100644 --- a/electrum/gui/qml/qetransactionlistmodel.py +++ b/electrum/gui/qml/qetransactionlistmodel.py @@ -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)) diff --git a/electrum/gui/qt/address_list.py b/electrum/gui/qt/address_list.py index 379b2dc9fdbb..130dfdc57702 100644 --- a/electrum/gui/qt/address_list.py +++ b/electrum/gui/qt/address_list.py @@ -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) @@ -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 @@ -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 = { @@ -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) @@ -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: diff --git a/electrum/gui/qt/history_list.py b/electrum/gui/qt/history_list.py index 5325e394a426..6a79f14770af 100644 --- a/electrum/gui/qt/history_list.py +++ b/electrum/gui/qt/history_list.py @@ -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}") @@ -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() @@ -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) @@ -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') @@ -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) @@ -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 @@ -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, diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py index 68649f32ddec..cf2c803344d6 100644 --- a/electrum/gui/qt/main_window.py +++ b/electrum/gui/qt/main_window.py @@ -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() diff --git a/electrum/gui/qt/settings_dialog.py b/electrum/gui/qt/settings_dialog.py index 26492715914e..f00ec6c4735f 100644 --- a/electrum/gui/qt/settings_dialog.py +++ b/electrum/gui/qt/settings_dialog.py @@ -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: @@ -365,7 +349,6 @@ 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() @@ -373,35 +356,17 @@ 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 = [] @@ -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)) diff --git a/electrum/wallet.py b/electrum/wallet.py index af42eba51863..441e1e6a7b74 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -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) @@ -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']) @@ -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