Qt MyTreeView: rm usages of Qt.UserRole, use explicit roles instead
This is a bit more verbose but it explicitly shows what data is being used where. Also rm implicitly setting editable_columns based on stretch_column.
This commit is contained in:
@@ -79,9 +79,12 @@ class AddressList(MyTreeView):
|
||||
filter_columns = [Columns.TYPE, Columns.ADDRESS, Columns.LABEL, Columns.COIN_BALANCE]
|
||||
|
||||
ROLE_SORT_ORDER = Qt.UserRole + 1000
|
||||
ROLE_ADDRESS_STR = Qt.UserRole + 1001
|
||||
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent, self.create_menu, stretch_column=self.Columns.LABEL)
|
||||
super().__init__(parent, self.create_menu,
|
||||
stretch_column=self.Columns.LABEL,
|
||||
editable_columns=[self.Columns.LABEL])
|
||||
self.wallet = self.parent.wallet
|
||||
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
|
||||
self.setSortingEnabled(True)
|
||||
@@ -145,7 +148,7 @@ class AddressList(MyTreeView):
|
||||
def update(self):
|
||||
if self.maybe_defer_update():
|
||||
return
|
||||
current_address = self.current_item_user_role(col=self.Columns.LABEL)
|
||||
current_address = self.get_role_data_for_current_item(col=self.Columns.LABEL, role=self.ROLE_ADDRESS_STR)
|
||||
if self.show_change == AddressTypeFilter.RECEIVING:
|
||||
addr_list = self.wallet.get_receiving_addresses()
|
||||
elif self.show_change == AddressTypeFilter.CHANGE:
|
||||
@@ -193,7 +196,7 @@ class AddressList(MyTreeView):
|
||||
else:
|
||||
address_item[self.Columns.TYPE].setText(_('receiving'))
|
||||
address_item[self.Columns.TYPE].setBackground(ColorScheme.GREEN.as_color(True))
|
||||
address_item[self.Columns.LABEL].setData(address, Qt.UserRole)
|
||||
address_item[self.Columns.LABEL].setData(address, self.ROLE_ADDRESS_STR)
|
||||
address_path = self.wallet.get_address_index(address)
|
||||
address_item[self.Columns.TYPE].setData(address_path, self.ROLE_SORT_ORDER)
|
||||
address_path_str = self.wallet.get_address_path_str(address)
|
||||
@@ -276,3 +279,14 @@ class AddressList(MyTreeView):
|
||||
self.parent.show_error(str(e))
|
||||
raise
|
||||
super().place_text_on_clipboard(text, title=title)
|
||||
|
||||
def get_edit_key_from_coordinate(self, row, col):
|
||||
if col != self.Columns.LABEL:
|
||||
return None
|
||||
return self.get_role_data_from_coordinate(row, col, role=self.ROLE_ADDRESS_STR)
|
||||
|
||||
def on_edited(self, idx, edit_key, *, text):
|
||||
self.parent.wallet.set_label(edit_key, text)
|
||||
self.parent.history_model.refresh('address label edited')
|
||||
self.parent.utxo_list.update()
|
||||
self.parent.update_completions()
|
||||
|
||||
@@ -17,6 +17,9 @@ _logger = get_logger(__name__)
|
||||
|
||||
|
||||
class Bip39RecoveryDialog(WindowModalDialog):
|
||||
|
||||
ROLE_ACCOUNT = Qt.UserRole
|
||||
|
||||
def __init__(self, parent: QWidget, get_account_xpub, on_account_select):
|
||||
self.get_account_xpub = get_account_xpub
|
||||
self.on_account_select = on_account_select
|
||||
@@ -41,7 +44,7 @@ class Bip39RecoveryDialog(WindowModalDialog):
|
||||
|
||||
def on_ok_button_click(self):
|
||||
item = self.list.currentItem()
|
||||
account = item.data(Qt.UserRole)
|
||||
account = item.data(self.ROLE_ACCOUNT)
|
||||
self.on_account_select(account)
|
||||
|
||||
def recovery(self):
|
||||
@@ -58,7 +61,7 @@ class Bip39RecoveryDialog(WindowModalDialog):
|
||||
self.list = QListWidget()
|
||||
for account in accounts:
|
||||
item = QListWidgetItem(account['description'])
|
||||
item.setData(Qt.UserRole, account)
|
||||
item.setData(self.ROLE_ACCOUNT, account)
|
||||
self.list.addItem(item)
|
||||
self.list.clicked.connect(lambda: self.ok_button.setEnabled(True))
|
||||
self.content.addWidget(self.list)
|
||||
|
||||
@@ -62,8 +62,7 @@ class ChannelsList(MyTreeView):
|
||||
_default_item_bg_brush = None # type: Optional[QBrush]
|
||||
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent, self.create_menu, stretch_column=self.Columns.NODE_ALIAS,
|
||||
editable_columns=[])
|
||||
super().__init__(parent, self.create_menu, stretch_column=self.Columns.NODE_ALIAS)
|
||||
self.setModel(QtGui.QStandardItemModel(self))
|
||||
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
|
||||
self.main_window = parent
|
||||
|
||||
@@ -49,6 +49,8 @@ class ContactList(MyTreeView):
|
||||
}
|
||||
filter_columns = [Columns.NAME, Columns.ADDRESS]
|
||||
|
||||
ROLE_CONTACT_KEY = Qt.UserRole + 1000
|
||||
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent, self.create_menu,
|
||||
stretch_column=self.Columns.NAME,
|
||||
@@ -58,9 +60,9 @@ class ContactList(MyTreeView):
|
||||
self.setSortingEnabled(True)
|
||||
self.update()
|
||||
|
||||
def on_edited(self, idx, user_role, text):
|
||||
_type, prior_name = self.parent.contacts.pop(user_role)
|
||||
self.parent.set_contact(text, user_role)
|
||||
def on_edited(self, idx, edit_key, *, text):
|
||||
_type, prior_name = self.parent.contacts.pop(edit_key)
|
||||
self.parent.set_contact(text, edit_key)
|
||||
self.update()
|
||||
|
||||
def create_menu(self, position):
|
||||
@@ -69,7 +71,7 @@ class ContactList(MyTreeView):
|
||||
column = idx.column() or self.Columns.NAME
|
||||
selected_keys = []
|
||||
for s_idx in self.selected_in_column(self.Columns.NAME):
|
||||
sel_key = self.model().itemFromIndex(s_idx).data(Qt.UserRole)
|
||||
sel_key = self.model().itemFromIndex(s_idx).data(self.ROLE_CONTACT_KEY)
|
||||
selected_keys.append(sel_key)
|
||||
if not selected_keys or not idx.isValid():
|
||||
menu.addAction(_("New contact"), lambda: self.parent.new_contact_dialog())
|
||||
@@ -98,7 +100,7 @@ class ContactList(MyTreeView):
|
||||
def update(self):
|
||||
if self.maybe_defer_update():
|
||||
return
|
||||
current_key = self.current_item_user_role(col=self.Columns.NAME)
|
||||
current_key = self.get_role_data_for_current_item(col=self.Columns.NAME, role=self.ROLE_CONTACT_KEY)
|
||||
self.model().clear()
|
||||
self.update_headers(self.__class__.headers)
|
||||
set_current = None
|
||||
@@ -107,7 +109,7 @@ class ContactList(MyTreeView):
|
||||
items = [QStandardItem(x) for x in (name, key)]
|
||||
items[self.Columns.NAME].setEditable(contact_type != 'openalias')
|
||||
items[self.Columns.ADDRESS].setEditable(False)
|
||||
items[self.Columns.NAME].setData(key, Qt.UserRole)
|
||||
items[self.Columns.NAME].setData(key, self.ROLE_CONTACT_KEY)
|
||||
row_count = self.model().rowCount()
|
||||
self.model().insertRow(row_count, items)
|
||||
if key == current_key:
|
||||
@@ -118,3 +120,8 @@ class ContactList(MyTreeView):
|
||||
self.sortByColumn(self.Columns.NAME, Qt.AscendingOrder)
|
||||
self.filter()
|
||||
run_hook('update_contacts_tab', self)
|
||||
|
||||
def get_edit_key_from_coordinate(self, row, col):
|
||||
if col != self.Columns.NAME:
|
||||
return None
|
||||
return self.get_role_data_from_coordinate(row, col, role=self.ROLE_CONTACT_KEY)
|
||||
|
||||
@@ -81,6 +81,10 @@ TX_ICONS = [
|
||||
"confirmed.png",
|
||||
]
|
||||
|
||||
|
||||
ROLE_SORT_ORDER = Qt.UserRole + 1000
|
||||
|
||||
|
||||
class HistoryColumns(IntEnum):
|
||||
STATUS = 0
|
||||
DESCRIPTION = 1
|
||||
@@ -93,8 +97,8 @@ class HistoryColumns(IntEnum):
|
||||
|
||||
class HistorySortModel(QSortFilterProxyModel):
|
||||
def lessThan(self, source_left: QModelIndex, source_right: QModelIndex):
|
||||
item1 = self.sourceModel().data(source_left, Qt.UserRole)
|
||||
item2 = self.sourceModel().data(source_right, Qt.UserRole)
|
||||
item1 = self.sourceModel().data(source_left, ROLE_SORT_ORDER)
|
||||
item2 = self.sourceModel().data(source_right, ROLE_SORT_ORDER)
|
||||
if item1 is None or item2 is None:
|
||||
raise Exception(f'UserRole not set for column {source_left.column()}')
|
||||
v1 = item1.value()
|
||||
@@ -136,8 +140,7 @@ class HistoryNode(CustomNode):
|
||||
tx_mined_info = self.model.tx_mined_info_from_tx_item(tx_item)
|
||||
status, status_str = window.wallet.get_tx_status(tx_hash, tx_mined_info)
|
||||
|
||||
if role == Qt.UserRole:
|
||||
# for sorting
|
||||
if role == ROLE_SORT_ORDER:
|
||||
d = {
|
||||
HistoryColumns.STATUS:
|
||||
# respect sort order of self.transactions (wallet.get_full_history)
|
||||
@@ -158,6 +161,8 @@ class HistoryNode(CustomNode):
|
||||
HistoryColumns.TXID: tx_hash if not is_lightning else None,
|
||||
}
|
||||
return QVariant(d[col])
|
||||
if role == MyTreeView.ROLE_EDIT_KEY:
|
||||
return QVariant(get_item_key(tx_item))
|
||||
if role not in (Qt.DisplayRole, Qt.EditRole):
|
||||
if col == HistoryColumns.STATUS and role == Qt.DecorationRole:
|
||||
icon = "lightning" if is_lightning else TX_ICONS[status]
|
||||
@@ -450,7 +455,9 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
|
||||
return False
|
||||
|
||||
def __init__(self, parent, model: HistoryModel):
|
||||
super().__init__(parent, self.create_menu, stretch_column=HistoryColumns.DESCRIPTION)
|
||||
super().__init__(parent, self.create_menu,
|
||||
stretch_column=HistoryColumns.DESCRIPTION,
|
||||
editable_columns=[HistoryColumns.DESCRIPTION, HistoryColumns.FIAT_VALUE])
|
||||
self.config = parent.config
|
||||
self.hm = model
|
||||
self.proxy = HistorySortModel(self)
|
||||
@@ -464,7 +471,6 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
|
||||
self.create_toolbar_buttons()
|
||||
self.wallet = self.parent.wallet # type: Abstract_Wallet
|
||||
self.sortByColumn(HistoryColumns.STATUS, Qt.AscendingOrder)
|
||||
self.editable_columns |= {HistoryColumns.FIAT_VALUE}
|
||||
self.setRootIsDecorated(True)
|
||||
self.header().setStretchLastSection(False)
|
||||
for col in HistoryColumns:
|
||||
@@ -634,8 +640,8 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
|
||||
except NothingToPlotException as e:
|
||||
self.parent.show_message(str(e))
|
||||
|
||||
def on_edited(self, index, user_role, text):
|
||||
index = self.model().mapToSource(index)
|
||||
def on_edited(self, idx, edit_key, *, text):
|
||||
index = self.model().mapToSource(idx)
|
||||
tx_item = index.internalPointer().get_data()
|
||||
column = index.column()
|
||||
key = get_item_key(tx_item)
|
||||
@@ -834,7 +840,9 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
|
||||
from electrum.util import json_encode
|
||||
f.write(json_encode(txns))
|
||||
|
||||
def get_text_and_userrole_from_coordinate(self, row, col):
|
||||
def get_text_from_coordinate(self, row, col):
|
||||
return self.get_role_data_from_coordinate(row, col, role=Qt.DisplayRole)
|
||||
|
||||
def get_role_data_from_coordinate(self, row, col, *, role):
|
||||
idx = self.model().mapToSource(self.model().index(row, col))
|
||||
tx_item = idx.internalPointer().get_data()
|
||||
return self.hm.data(idx, Qt.DisplayRole).value(), get_item_key(tx_item)
|
||||
return self.hm.data(idx, role).value()
|
||||
|
||||
@@ -65,8 +65,7 @@ class InvoiceList(MyTreeView):
|
||||
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent, self.create_menu,
|
||||
stretch_column=self.Columns.DESCRIPTION,
|
||||
editable_columns=[])
|
||||
stretch_column=self.Columns.DESCRIPTION)
|
||||
self.std_model = QStandardItemModel(self)
|
||||
self.proxy = MySortModel(self, sort_role=ROLE_SORT_ORDER)
|
||||
self.proxy.setSourceModel(self.std_model)
|
||||
|
||||
@@ -65,8 +65,7 @@ class RequestList(MyTreeView):
|
||||
|
||||
def __init__(self, parent: 'ElectrumWindow'):
|
||||
super().__init__(parent, self.create_menu,
|
||||
stretch_column=self.Columns.DESCRIPTION,
|
||||
editable_columns=[])
|
||||
stretch_column=self.Columns.DESCRIPTION)
|
||||
self.wallet = self.parent.wallet
|
||||
self.std_model = QStandardItemModel(self)
|
||||
self.proxy = MySortModel(self, sort_role=ROLE_SORT_ORDER)
|
||||
|
||||
@@ -507,10 +507,9 @@ class ElectrumItemDelegate(QStyledItemDelegate):
|
||||
new_text = editor.text()
|
||||
idx = QModelIndex(self.opened)
|
||||
row, col = idx.row(), idx.column()
|
||||
_prior_text, user_role = self.tv.get_text_and_userrole_from_coordinate(row, col)
|
||||
# check that we didn't forget to set UserRole on an editable field
|
||||
assert user_role is not None, (row, col)
|
||||
self.tv.on_edited(idx, user_role, new_text)
|
||||
edit_key = self.tv.get_edit_key_from_coordinate(row, col)
|
||||
assert edit_key is not None, (idx.row(), idx.column())
|
||||
self.tv.on_edited(idx, edit_key=edit_key, text=new_text)
|
||||
self.closeEditor.connect(on_closeEditor)
|
||||
self.commitData.connect(on_commitData)
|
||||
|
||||
@@ -551,6 +550,7 @@ class ElectrumItemDelegate(QStyledItemDelegate):
|
||||
class MyTreeView(QTreeView):
|
||||
ROLE_CLIPBOARD_DATA = Qt.UserRole + 100
|
||||
ROLE_CUSTOM_PAINT = Qt.UserRole + 101
|
||||
ROLE_EDIT_KEY = Qt.UserRole + 102
|
||||
|
||||
filter_columns: Iterable[int]
|
||||
|
||||
@@ -565,13 +565,9 @@ class MyTreeView(QTreeView):
|
||||
self.setUniformRowHeights(True)
|
||||
|
||||
# Control which columns are editable
|
||||
if editable_columns is not None:
|
||||
editable_columns = set(editable_columns)
|
||||
elif stretch_column is not None:
|
||||
editable_columns = {stretch_column}
|
||||
else:
|
||||
editable_columns = {}
|
||||
self.editable_columns = editable_columns
|
||||
if editable_columns is None:
|
||||
editable_columns = []
|
||||
self.editable_columns = set(editable_columns)
|
||||
self.setItemDelegate(ElectrumItemDelegate(self))
|
||||
self.current_filter = ""
|
||||
self.is_editor_open = False
|
||||
@@ -597,12 +593,12 @@ class MyTreeView(QTreeView):
|
||||
items = self.selectionModel().selectedIndexes()
|
||||
return list(x for x in items if x.column() == column)
|
||||
|
||||
def current_item_user_role(self, col) -> Any:
|
||||
def get_role_data_for_current_item(self, *, col, role) -> Any:
|
||||
idx = self.selectionModel().currentIndex()
|
||||
idx = idx.sibling(idx.row(), col)
|
||||
item = self.item_from_index(idx)
|
||||
if item:
|
||||
return item.data(Qt.UserRole)
|
||||
return item.data(role)
|
||||
|
||||
def item_from_index(self, idx: QModelIndex) -> Optional[QStandardItem]:
|
||||
model = self.model()
|
||||
@@ -658,11 +654,8 @@ class MyTreeView(QTreeView):
|
||||
"""
|
||||
return super().edit(idx, trigger, event)
|
||||
|
||||
def on_edited(self, idx: QModelIndex, user_role, text):
|
||||
self.parent.wallet.set_label(user_role, text)
|
||||
self.parent.history_model.refresh('on_edited in MyTreeView')
|
||||
self.parent.utxo_list.update()
|
||||
self.parent.update_completions()
|
||||
def on_edited(self, idx: QModelIndex, edit_key, *, text: str) -> None:
|
||||
raise NotImplementedError()
|
||||
|
||||
def should_hide(self, row):
|
||||
"""
|
||||
@@ -671,11 +664,20 @@ class MyTreeView(QTreeView):
|
||||
"""
|
||||
return False
|
||||
|
||||
def get_text_and_userrole_from_coordinate(self, row_num, column):
|
||||
idx = self.model().index(row_num, column)
|
||||
def get_text_from_coordinate(self, row, col) -> str:
|
||||
idx = self.model().index(row, col)
|
||||
item = self.item_from_index(idx)
|
||||
user_role = item.data(Qt.UserRole)
|
||||
return item.text(), user_role
|
||||
return item.text()
|
||||
|
||||
def get_role_data_from_coordinate(self, row, col, *, role) -> Any:
|
||||
idx = self.model().index(row, col)
|
||||
item = self.item_from_index(idx)
|
||||
role_data = item.data(role)
|
||||
return role_data
|
||||
|
||||
def get_edit_key_from_coordinate(self, row, col) -> Any:
|
||||
# overriding this might allow avoiding storing duplicate data
|
||||
return self.get_role_data_from_coordinate(row, col, role=self.ROLE_EDIT_KEY)
|
||||
|
||||
def hide_row(self, row_num):
|
||||
"""
|
||||
@@ -688,7 +690,7 @@ class MyTreeView(QTreeView):
|
||||
self.setRowHidden(row_num, QModelIndex(), False)
|
||||
return
|
||||
for column in self.filter_columns:
|
||||
txt, _ = self.get_text_and_userrole_from_coordinate(row_num, column)
|
||||
txt = self.get_text_from_coordinate(row_num, column)
|
||||
txt = txt.lower()
|
||||
if self.current_filter in txt:
|
||||
# the filter matched, but the date filter might apply
|
||||
|
||||
@@ -58,10 +58,11 @@ class UTXOList(MyTreeView):
|
||||
filter_columns = [Columns.ADDRESS, Columns.LABEL, Columns.OUTPOINT]
|
||||
stretch_column = Columns.LABEL
|
||||
|
||||
ROLE_PREVOUT_STR = Qt.UserRole + 1000
|
||||
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent, self.create_menu,
|
||||
stretch_column=self.stretch_column,
|
||||
editable_columns=[])
|
||||
stretch_column=self.stretch_column)
|
||||
self._spend_set = None
|
||||
self._utxo_dict = {}
|
||||
self.wallet = self.parent.wallet
|
||||
@@ -104,10 +105,10 @@ class UTXOList(MyTreeView):
|
||||
utxo_item = [QStandardItem(x) for x in labels]
|
||||
self.set_editability(utxo_item)
|
||||
utxo_item[self.Columns.OUTPOINT].setData(name, self.ROLE_CLIPBOARD_DATA)
|
||||
utxo_item[self.Columns.OUTPOINT].setData(name, self.ROLE_PREVOUT_STR)
|
||||
utxo_item[self.Columns.ADDRESS].setFont(QFont(MONOSPACE_FONT))
|
||||
utxo_item[self.Columns.AMOUNT].setFont(QFont(MONOSPACE_FONT))
|
||||
utxo_item[self.Columns.OUTPOINT].setFont(QFont(MONOSPACE_FONT))
|
||||
utxo_item[self.Columns.ADDRESS].setData(name, Qt.UserRole)
|
||||
SELECTED_TO_SPEND_TOOLTIP = _('Coin selected to be spent')
|
||||
if name in (self._spend_set or set()):
|
||||
for col in utxo_item:
|
||||
@@ -128,8 +129,8 @@ class UTXOList(MyTreeView):
|
||||
def get_selected_outpoints(self) -> Optional[List[str]]:
|
||||
if not self.model():
|
||||
return None
|
||||
items = self.selected_in_column(self.Columns.ADDRESS)
|
||||
return [x.data(Qt.UserRole) for x in items]
|
||||
items = self.selected_in_column(self.Columns.OUTPOINT)
|
||||
return [x.data(self.ROLE_PREVOUT_STR) for x in items]
|
||||
|
||||
def _filter_frozen_coins(self, coins: List[PartialTxInput]) -> List[PartialTxInput]:
|
||||
coins = [utxo for utxo in coins
|
||||
|
||||
@@ -33,7 +33,7 @@ from .util import MyTreeView, Buttons
|
||||
|
||||
class WatcherList(MyTreeView):
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent, self.create_menu, stretch_column=0, editable_columns=[])
|
||||
super().__init__(parent, self.create_menu, stretch_column=0)
|
||||
self.setModel(QStandardItemModel(self))
|
||||
self.setSortingEnabled(True)
|
||||
self.update()
|
||||
|
||||
Reference in New Issue
Block a user