qt history: use IntEnum for column indices
This commit is contained in:
@@ -28,6 +28,7 @@ import datetime
|
|||||||
from datetime import date
|
from datetime import date
|
||||||
from typing import TYPE_CHECKING, Tuple, Dict
|
from typing import TYPE_CHECKING, Tuple, Dict
|
||||||
import threading
|
import threading
|
||||||
|
from enum import IntEnum
|
||||||
|
|
||||||
from electrum.address_synchronizer import TX_HEIGHT_LOCAL
|
from electrum.address_synchronizer import TX_HEIGHT_LOCAL
|
||||||
from electrum.i18n import _
|
from electrum.i18n import _
|
||||||
@@ -59,6 +60,17 @@ TX_ICONS = [
|
|||||||
"confirmed.png",
|
"confirmed.png",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
class HistoryColumns(IntEnum):
|
||||||
|
STATUS_ICON = 0
|
||||||
|
STATUS_TEXT = 1
|
||||||
|
DESCRIPTION = 2
|
||||||
|
COIN_VALUE = 3
|
||||||
|
RUNNING_COIN_BALANCE = 4
|
||||||
|
FIAT_VALUE = 5
|
||||||
|
FIAT_ACQ_PRICE = 6
|
||||||
|
FIAT_CAP_GAINS = 7
|
||||||
|
TXID = 8
|
||||||
|
|
||||||
class HistorySortModel(QSortFilterProxyModel):
|
class HistorySortModel(QSortFilterProxyModel):
|
||||||
def lessThan(self, source_left: QModelIndex, source_right: QModelIndex):
|
def lessThan(self, source_left: QModelIndex, source_right: QModelIndex):
|
||||||
item1 = self.sourceModel().data(source_left, Qt.UserRole)
|
item1 = self.sourceModel().data(source_left, Qt.UserRole)
|
||||||
@@ -71,8 +83,6 @@ class HistorySortModel(QSortFilterProxyModel):
|
|||||||
|
|
||||||
class HistoryModel(QAbstractItemModel, PrintError):
|
class HistoryModel(QAbstractItemModel, PrintError):
|
||||||
|
|
||||||
NUM_COLUMNS = 9
|
|
||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
@@ -87,7 +97,7 @@ class HistoryModel(QAbstractItemModel, PrintError):
|
|||||||
self.set_visibility_of_columns()
|
self.set_visibility_of_columns()
|
||||||
|
|
||||||
def columnCount(self, parent: QModelIndex):
|
def columnCount(self, parent: QModelIndex):
|
||||||
return self.NUM_COLUMNS
|
return len(HistoryColumns)
|
||||||
|
|
||||||
def rowCount(self, parent: QModelIndex):
|
def rowCount(self, parent: QModelIndex):
|
||||||
return len(self.transactions)
|
return len(self.transactions)
|
||||||
@@ -113,61 +123,69 @@ class HistoryModel(QAbstractItemModel, PrintError):
|
|||||||
if role == Qt.UserRole:
|
if role == Qt.UserRole:
|
||||||
# for sorting
|
# for sorting
|
||||||
d = {
|
d = {
|
||||||
# height breaks ties for unverified txns
|
HistoryColumns.STATUS_ICON:
|
||||||
# txpos breaks ties for verified same block txns
|
# height breaks ties for unverified txns
|
||||||
0: (status, conf, -height, -txpos),
|
# txpos breaks ties for verified same block txns
|
||||||
1: status_str,
|
(status, conf, -height, -txpos),
|
||||||
2: tx_item['label'],
|
HistoryColumns.STATUS_TEXT: status_str,
|
||||||
3: tx_item['value'].value,
|
HistoryColumns.DESCRIPTION: tx_item['label'],
|
||||||
4: tx_item['balance'].value,
|
HistoryColumns.COIN_VALUE: tx_item['value'].value,
|
||||||
5: tx_item['fiat_value'].value if 'fiat_value' in tx_item else None,
|
HistoryColumns.RUNNING_COIN_BALANCE: tx_item['balance'].value,
|
||||||
6: tx_item['acquisition_price'].value if 'acquisition_price' in tx_item else None,
|
HistoryColumns.FIAT_VALUE:
|
||||||
7: tx_item['capital_gain'].value if 'capital_gain' in tx_item else None,
|
tx_item['fiat_value'].value if 'fiat_value' in tx_item else None,
|
||||||
8: tx_hash,
|
HistoryColumns.FIAT_ACQ_PRICE:
|
||||||
|
tx_item['acquisition_price'].value if 'acquisition_price' in tx_item else None,
|
||||||
|
HistoryColumns.FIAT_CAP_GAINS:
|
||||||
|
tx_item['capital_gain'].value if 'capital_gain' in tx_item else None,
|
||||||
|
HistoryColumns.TXID: tx_hash,
|
||||||
}
|
}
|
||||||
return QVariant(d[col])
|
return QVariant(d[col])
|
||||||
if role not in (Qt.DisplayRole, Qt.EditRole):
|
if role not in (Qt.DisplayRole, Qt.EditRole):
|
||||||
if col == 0 and role == Qt.DecorationRole:
|
if col == HistoryColumns.STATUS_ICON and role == Qt.DecorationRole:
|
||||||
return QVariant(self.view.icon_cache.get(":icons/" + TX_ICONS[status]))
|
return QVariant(self.view.icon_cache.get(":icons/" + TX_ICONS[status]))
|
||||||
elif col == 0 and role == Qt.ToolTipRole:
|
elif col == HistoryColumns.STATUS_ICON and role == Qt.ToolTipRole:
|
||||||
return QVariant(str(conf) + _(" confirmation" + ("s" if conf != 1 else "")))
|
return QVariant(str(conf) + _(" confirmation" + ("s" if conf != 1 else "")))
|
||||||
elif col > 2 and role == Qt.TextAlignmentRole:
|
elif col > HistoryColumns.DESCRIPTION and role == Qt.TextAlignmentRole:
|
||||||
return QVariant(Qt.AlignRight | Qt.AlignVCenter)
|
return QVariant(Qt.AlignRight | Qt.AlignVCenter)
|
||||||
elif col != 1 and role == Qt.FontRole:
|
elif col != HistoryColumns.STATUS_TEXT and role == Qt.FontRole:
|
||||||
monospace_font = QFont(MONOSPACE_FONT)
|
monospace_font = QFont(MONOSPACE_FONT)
|
||||||
return QVariant(monospace_font)
|
return QVariant(monospace_font)
|
||||||
elif col == 2 and role == Qt.DecorationRole and self.parent.wallet.invoices.paid.get(tx_hash):
|
elif col == HistoryColumns.DESCRIPTION and role == Qt.DecorationRole \
|
||||||
|
and self.parent.wallet.invoices.paid.get(tx_hash):
|
||||||
return QVariant(self.view.icon_cache.get(":icons/seal"))
|
return QVariant(self.view.icon_cache.get(":icons/seal"))
|
||||||
elif col in (2, 3) and role == Qt.ForegroundRole and tx_item['value'].value < 0:
|
elif col in (HistoryColumns.DESCRIPTION, HistoryColumns.COIN_VALUE) \
|
||||||
|
and role == Qt.ForegroundRole and tx_item['value'].value < 0:
|
||||||
red_brush = QBrush(QColor("#BC1E1E"))
|
red_brush = QBrush(QColor("#BC1E1E"))
|
||||||
return QVariant(red_brush)
|
return QVariant(red_brush)
|
||||||
elif col == 5 and role == Qt.ForegroundRole and not tx_item.get('fiat_default') and tx_item.get('fiat_value') is not None:
|
elif col == HistoryColumns.FIAT_VALUE and role == Qt.ForegroundRole \
|
||||||
|
and not tx_item.get('fiat_default') and tx_item.get('fiat_value') is not None:
|
||||||
blue_brush = QBrush(QColor("#1E1EFF"))
|
blue_brush = QBrush(QColor("#1E1EFF"))
|
||||||
return QVariant(blue_brush)
|
return QVariant(blue_brush)
|
||||||
return None
|
return None
|
||||||
if col == 1:
|
if col == HistoryColumns.STATUS_TEXT:
|
||||||
return QVariant(status_str)
|
return QVariant(status_str)
|
||||||
elif col == 2:
|
elif col == HistoryColumns.DESCRIPTION:
|
||||||
return QVariant(tx_item['label'])
|
return QVariant(tx_item['label'])
|
||||||
elif col == 3:
|
elif col == HistoryColumns.COIN_VALUE:
|
||||||
value = tx_item['value'].value
|
value = tx_item['value'].value
|
||||||
v_str = self.parent.format_amount(value, is_diff=True, whitespaces=True)
|
v_str = self.parent.format_amount(value, is_diff=True, whitespaces=True)
|
||||||
return QVariant(v_str)
|
return QVariant(v_str)
|
||||||
elif col == 4:
|
elif col == HistoryColumns.RUNNING_COIN_BALANCE:
|
||||||
balance = tx_item['balance'].value
|
balance = tx_item['balance'].value
|
||||||
balance_str = self.parent.format_amount(balance, whitespaces=True)
|
balance_str = self.parent.format_amount(balance, whitespaces=True)
|
||||||
return QVariant(balance_str)
|
return QVariant(balance_str)
|
||||||
elif col == 5 and 'fiat_value' in tx_item:
|
elif col == HistoryColumns.FIAT_VALUE and 'fiat_value' in tx_item:
|
||||||
value_str = self.parent.fx.format_fiat(tx_item['fiat_value'].value)
|
value_str = self.parent.fx.format_fiat(tx_item['fiat_value'].value)
|
||||||
return QVariant(value_str)
|
return QVariant(value_str)
|
||||||
elif col == 6 and tx_item['value'].value < 0 and 'acquisition_price' in tx_item:
|
elif col == HistoryColumns.FIAT_ACQ_PRICE and \
|
||||||
|
tx_item['value'].value < 0 and 'acquisition_price' in tx_item:
|
||||||
# fixme: should use is_mine
|
# fixme: should use is_mine
|
||||||
acq = tx_item['acquisition_price'].value
|
acq = tx_item['acquisition_price'].value
|
||||||
return QVariant(self.parent.fx.format_fiat(acq))
|
return QVariant(self.parent.fx.format_fiat(acq))
|
||||||
elif col == 7 and 'capital_gain' in tx_item:
|
elif col == HistoryColumns.FIAT_CAP_GAINS and 'capital_gain' in tx_item:
|
||||||
cg = tx_item['capital_gain'].value
|
cg = tx_item['capital_gain'].value
|
||||||
return QVariant(self.parent.fx.format_fiat(cg))
|
return QVariant(self.parent.fx.format_fiat(cg))
|
||||||
elif col == 8:
|
elif col == HistoryColumns.TXID:
|
||||||
return QVariant(tx_hash)
|
return QVariant(tx_hash)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -234,25 +252,16 @@ class HistoryModel(QAbstractItemModel, PrintError):
|
|||||||
self.tx_status_cache[txid] = self.parent.wallet.get_tx_status(txid, tx_mined_info)
|
self.tx_status_cache[txid] = self.parent.wallet.get_tx_status(txid, tx_mined_info)
|
||||||
|
|
||||||
def set_visibility_of_columns(self):
|
def set_visibility_of_columns(self):
|
||||||
hide = self.view.hideColumn
|
def set_visible(col: int, b: bool):
|
||||||
show = self.view.showColumn
|
self.view.showColumn(col) if b else self.view.hideColumn(col)
|
||||||
# txid
|
# txid
|
||||||
hide(8)
|
set_visible(HistoryColumns.TXID, False)
|
||||||
# fiat
|
# fiat
|
||||||
history = self.parent.fx.show_history()
|
history = self.parent.fx.show_history()
|
||||||
cap_gains = self.parent.fx.get_history_capital_gains_config()
|
cap_gains = self.parent.fx.get_history_capital_gains_config()
|
||||||
if history and cap_gains:
|
set_visible(HistoryColumns.FIAT_VALUE, history)
|
||||||
show(5)
|
set_visible(HistoryColumns.FIAT_ACQ_PRICE, history and cap_gains)
|
||||||
show(6)
|
set_visible(HistoryColumns.FIAT_CAP_GAINS, history and cap_gains)
|
||||||
show(7)
|
|
||||||
elif history:
|
|
||||||
show(5)
|
|
||||||
hide(6)
|
|
||||||
hide(7)
|
|
||||||
else:
|
|
||||||
hide(5)
|
|
||||||
hide(6)
|
|
||||||
hide(7)
|
|
||||||
|
|
||||||
def update_fiat(self, row, idx):
|
def update_fiat(self, row, idx):
|
||||||
tx_item = self.transactions.value_from_pos(row)
|
tx_item = self.transactions.value_from_pos(row)
|
||||||
@@ -270,7 +279,7 @@ class HistoryModel(QAbstractItemModel, PrintError):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
return
|
return
|
||||||
topLeft = self.createIndex(row, 0)
|
topLeft = self.createIndex(row, 0)
|
||||||
bottomRight = self.createIndex(row, self.NUM_COLUMNS-1)
|
bottomRight = self.createIndex(row, len(HistoryColumns) - 1)
|
||||||
self.dataChanged.emit(topLeft, bottomRight)
|
self.dataChanged.emit(topLeft, bottomRight)
|
||||||
|
|
||||||
def on_fee_histogram(self):
|
def on_fee_histogram(self):
|
||||||
@@ -295,15 +304,15 @@ class HistoryModel(QAbstractItemModel, PrintError):
|
|||||||
fiat_acq_title = '%s '%fx.ccy + _('Acquisition price')
|
fiat_acq_title = '%s '%fx.ccy + _('Acquisition price')
|
||||||
fiat_cg_title = '%s '%fx.ccy + _('Capital Gains')
|
fiat_cg_title = '%s '%fx.ccy + _('Capital Gains')
|
||||||
return {
|
return {
|
||||||
0: '',
|
HistoryColumns.STATUS_ICON: '',
|
||||||
1: _('Date'),
|
HistoryColumns.STATUS_TEXT: _('Date'),
|
||||||
2: _('Description'),
|
HistoryColumns.DESCRIPTION: _('Description'),
|
||||||
3: _('Amount'),
|
HistoryColumns.COIN_VALUE: _('Amount'),
|
||||||
4: _('Balance'),
|
HistoryColumns.RUNNING_COIN_BALANCE: _('Balance'),
|
||||||
5: fiat_title,
|
HistoryColumns.FIAT_VALUE: fiat_title,
|
||||||
6: fiat_acq_title,
|
HistoryColumns.FIAT_ACQ_PRICE: fiat_acq_title,
|
||||||
7: fiat_cg_title,
|
HistoryColumns.FIAT_CAP_GAINS: fiat_cg_title,
|
||||||
8: 'TXID',
|
HistoryColumns.TXID: 'TXID',
|
||||||
}[section]
|
}[section]
|
||||||
|
|
||||||
def flags(self, idx):
|
def flags(self, idx):
|
||||||
@@ -320,7 +329,10 @@ class HistoryModel(QAbstractItemModel, PrintError):
|
|||||||
return tx_mined_info
|
return tx_mined_info
|
||||||
|
|
||||||
class HistoryList(MyTreeView, AcceptFileDragDrop):
|
class HistoryList(MyTreeView, AcceptFileDragDrop):
|
||||||
filter_columns = [1, 2, 3, 8] # Date, Description, Amount, TXID
|
filter_columns = [HistoryColumns.STATUS_TEXT,
|
||||||
|
HistoryColumns.DESCRIPTION,
|
||||||
|
HistoryColumns.COIN_VALUE,
|
||||||
|
HistoryColumns.TXID]
|
||||||
|
|
||||||
def tx_item_from_proxy_row(self, proxy_row):
|
def tx_item_from_proxy_row(self, proxy_row):
|
||||||
hm_idx = self.model().mapToSource(self.model().index(proxy_row, 0))
|
hm_idx = self.model().mapToSource(self.model().index(proxy_row, 0))
|
||||||
@@ -337,7 +349,7 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def __init__(self, parent, model: HistoryModel):
|
def __init__(self, parent, model: HistoryModel):
|
||||||
super().__init__(parent, self.create_menu, 2)
|
super().__init__(parent, self.create_menu, stretch_column=HistoryColumns.DESCRIPTION)
|
||||||
self.hm = model
|
self.hm = model
|
||||||
self.proxy = HistorySortModel(self)
|
self.proxy = HistorySortModel(self)
|
||||||
self.proxy.setSourceModel(model)
|
self.proxy.setSourceModel(model)
|
||||||
@@ -351,11 +363,11 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
|
|||||||
self.years = []
|
self.years = []
|
||||||
self.create_toolbar_buttons()
|
self.create_toolbar_buttons()
|
||||||
self.wallet = self.parent.wallet # type: Abstract_Wallet
|
self.wallet = self.parent.wallet # type: Abstract_Wallet
|
||||||
self.sortByColumn(0, Qt.AscendingOrder)
|
self.sortByColumn(HistoryColumns.STATUS_ICON, Qt.AscendingOrder)
|
||||||
self.editable_columns |= {5}
|
self.editable_columns |= {HistoryColumns.FIAT_VALUE}
|
||||||
|
|
||||||
self.header().setStretchLastSection(False)
|
self.header().setStretchLastSection(False)
|
||||||
for col in range(HistoryModel.NUM_COLUMNS):
|
for col in HistoryColumns:
|
||||||
sm = QHeaderView.Stretch if col == self.stretch_column else QHeaderView.ResizeToContents
|
sm = QHeaderView.Stretch if col == self.stretch_column else QHeaderView.ResizeToContents
|
||||||
self.header().setSectionResizeMode(col, sm)
|
self.header().setSectionResizeMode(col, sm)
|
||||||
|
|
||||||
@@ -489,12 +501,11 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
|
|||||||
row, column = index.row(), index.column()
|
row, column = index.row(), index.column()
|
||||||
tx_item = self.hm.transactions.value_from_pos(row)
|
tx_item = self.hm.transactions.value_from_pos(row)
|
||||||
key = tx_item['txid']
|
key = tx_item['txid']
|
||||||
# fixme
|
if column == HistoryColumns.DESCRIPTION:
|
||||||
if column == 2:
|
|
||||||
if self.wallet.set_label(key, text): #changed
|
if self.wallet.set_label(key, text): #changed
|
||||||
self.hm.update_label(row)
|
self.hm.update_label(row)
|
||||||
self.parent.update_completions()
|
self.parent.update_completions()
|
||||||
elif column == 5:
|
elif column == HistoryColumns.FIAT_VALUE:
|
||||||
self.wallet.set_fiat_value(key, self.parent.fx.ccy, text, self.parent.fx, tx_item['value'].value)
|
self.wallet.set_fiat_value(key, self.parent.fx.ccy, text, self.parent.fx, tx_item['value'].value)
|
||||||
value = tx_item['value'].value
|
value = tx_item['value'].value
|
||||||
if value is not None:
|
if value is not None:
|
||||||
@@ -527,7 +538,7 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
|
|||||||
return
|
return
|
||||||
tx_item = self.hm.transactions.value_from_pos(idx.row())
|
tx_item = self.hm.transactions.value_from_pos(idx.row())
|
||||||
column = idx.column()
|
column = idx.column()
|
||||||
if column == 0:
|
if column == HistoryColumns.STATUS_ICON:
|
||||||
column_title = _('Transaction ID')
|
column_title = _('Transaction ID')
|
||||||
column_data = tx_item['txid']
|
column_data = tx_item['txid']
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user