electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 3965176295364f0af444525206afb49ef983ab36
parent b2c7d2d2ccee4e156d785dbff3d8e211fae0e86b
Author: Johann Bauer <bauerj@bauerj.eu>
Date:   Sun, 28 Jan 2018 13:07:59 +0100

Enable sorting of list widgets

Diffstat:
Mgui/qt/address_list.py | 5+++--
Mgui/qt/history_list.py | 6+++++-
Mgui/qt/util.py | 15+++++++++++++++
Mgui/qt/utxo_list.py | 3++-
4 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/gui/qt/address_list.py b/gui/qt/address_list.py @@ -39,6 +39,7 @@ class AddressList(MyTreeWidget): MyTreeWidget.__init__(self, parent, self.create_menu, [], 2) self.refresh_headers() self.setSelectionMode(QAbstractItemView.ExtendedSelection) + self.setSortingEnabled(True) self.show_change = 0 self.show_used = 0 self.change_button = QComboBox(self) @@ -109,11 +110,11 @@ class AddressList(MyTreeWidget): if fx and fx.get_fiat_address_config(): rate = fx.exchange_rate() fiat_balance = fx.value_str(balance, rate) - address_item = QTreeWidgetItem(['', address, label, balance_text, fiat_balance, "%d"%num]) + address_item = SortableTreeWidgetItem(['', address, label, balance_text, fiat_balance, "%d"%num]) address_item.setTextAlignment(4, Qt.AlignRight) address_item.setFont(4, QFont(MONOSPACE_FONT)) else: - address_item = QTreeWidgetItem(['', address, label, balance_text, "%d"%num]) + address_item = SortableTreeWidgetItem(['', address, label, balance_text, "%d"%num]) address_item.setFont(3, QFont(MONOSPACE_FONT)) if self.wallet.is_change(address): address_item.setText(0, _('change')) diff --git a/gui/qt/history_list.py b/gui/qt/history_list.py @@ -59,6 +59,8 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop): AcceptFileDragDrop.__init__(self, ".txn") self.refresh_headers() self.setColumnHidden(1, True) + self.setSortingEnabled(True) + self.sortByColumn(0, Qt.AscendingOrder) self.start_timestamp = None self.end_timestamp = None self.years = [] @@ -243,9 +245,10 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop): if value < 0: entry.append(fx.format_fiat(tx_item['acquisition_price'].value)) entry.append(fx.format_fiat(tx_item['capital_gain'].value)) - item = QTreeWidgetItem(entry) + item = SortableTreeWidgetItem(entry) item.setIcon(0, icon) item.setToolTip(0, str(conf) + " confirmation" + ("s" if conf != 1 else "")) + item.setData(0, SortableTreeWidgetItem.DataRole, (status, conf)) if has_invoice: item.setIcon(3, QIcon(":icons/seal")) for i in range(len(entry)): @@ -301,6 +304,7 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop): if items: item = items[0] item.setIcon(0, icon) + item.setData(0, SortableTreeWidgetItem.DataRole, (status, conf)) item.setText(2, status_str) def create_menu(self, position): diff --git a/gui/qt/util.py b/gui/qt/util.py @@ -763,6 +763,21 @@ def get_parent_main_window(widget): return widget return None +class SortableTreeWidgetItem(QTreeWidgetItem): + DataRole = Qt.UserRole + 1 + + def __lt__(self, other): + column = self.treeWidget().sortColumn() + if None not in [x.data(column, self.DataRole) for x in [self, other]]: + # We have set custom data to sort by + return self.data(column, self.DataRole) < other.data(column, self.DataRole) + try: + # Is the value something numeric? + return float(self.text(column)) < float(other.text(column)) + except ValueError: + # If not, we will just do string comparison + return self.text(column) < other.text(column) + if __name__ == "__main__": app = QApplication([]) diff --git a/gui/qt/utxo_list.py b/gui/qt/utxo_list.py @@ -32,6 +32,7 @@ class UTXOList(MyTreeWidget): def __init__(self, parent=None): MyTreeWidget.__init__(self, parent, self.create_menu, [ _('Address'), _('Label'), _('Amount'), _('Height'), _('Output point')], 1) self.setSelectionMode(QAbstractItemView.ExtendedSelection) + self.setSortingEnabled(True) def get_name(self, x): return x.get('prevout_hash') + ":%d"%x.get('prevout_n') @@ -47,7 +48,7 @@ class UTXOList(MyTreeWidget): name = self.get_name(x) label = self.wallet.get_label(x.get('prevout_hash')) amount = self.parent.format_amount(x['value'], whitespaces=True) - utxo_item = QTreeWidgetItem([address, label, amount, '%d'%height, name[0:10] + '...' + name[-2:]]) + utxo_item = SortableTreeWidgetItem([address, label, amount, '%d'%height, name[0:10] + '...' + name[-2:]]) utxo_item.setFont(0, QFont(MONOSPACE_FONT)) utxo_item.setFont(2, QFont(MONOSPACE_FONT)) utxo_item.setFont(4, QFont(MONOSPACE_FONT))