diff --git a/res/ui/currency_tab.ui b/res/ui/currency_tab.ui index 065353e5286e38fd4f9d4b16304bf39ba34db8ea..35f6249b12eab281d4aef6d019225bee48f7342d 100644 --- a/res/ui/currency_tab.ui +++ b/res/ui/currency_tab.ui @@ -69,6 +69,9 @@ <layout class="QVBoxLayout" name="verticalLayout_3"> <item> <widget class="QTableView" name="table_history"> + <attribute name="horizontalHeaderShowSortIndicator" stdset="0"> + <bool>true</bool> + </attribute> <attribute name="horizontalHeaderStretchLastSection"> <bool>true</bool> </attribute> diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py index c60ae4b2946851844c40b5404da3ebcb742781c9..8fe7efaef0d6983aca50a1c4cf0098a12ec2a275 100644 --- a/src/cutecoin/core/community.py +++ b/src/cutecoin/core/community.py @@ -132,6 +132,8 @@ class Community(object): pass except TimeoutError: pass + except ConnectionError: + pass except ValueError: pass diff --git a/src/cutecoin/core/person.py b/src/cutecoin/core/person.py index 3b3ad2a02c4b6bb73178a9c6805956f89c025d1f..37cec6fb9bc7a4a950ceb270cd610542b9e444f0 100644 --- a/src/cutecoin/core/person.py +++ b/src/cutecoin/core/person.py @@ -32,8 +32,6 @@ class Person(object): ''' data = community.request(bma.wot.Lookup, req_args={'search': pubkey}, cached=cached) - results = data['results'] - logging.debug(results) timestamp = 0 for result in data['results']: diff --git a/src/cutecoin/gui/currency_tab.py b/src/cutecoin/gui/currency_tab.py index 0e1c66ddffc84439b1e98210fce0a4be06a81823..12347ee99da01a6ee6fb01cf58b67215b0981e65 100644 --- a/src/cutecoin/gui/currency_tab.py +++ b/src/cutecoin/gui/currency_tab.py @@ -88,6 +88,7 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget): self.table_history.setModel( HistoryTableModel(self.app.current_account, self.community)) + self.table_history.setSortingEnabled(True) self.tab_community = CommunityTabWidget(self.app.current_account, self.community, self.password_asker) diff --git a/src/cutecoin/models/txhistory.py b/src/cutecoin/models/txhistory.py index de77341417187f141990ea1b2eeafeb44bc5fbb7..934a8c8bc4eb878b2c685e90d03fbab5f76bf220 100644 --- a/src/cutecoin/models/txhistory.py +++ b/src/cutecoin/models/txhistory.py @@ -7,7 +7,7 @@ Created on 5 févr. 2014 import logging from ..core.person import Person from ..tools.exceptions import PersonNotFoundError -from PyQt5.QtCore import QAbstractTableModel, Qt, QVariant +from PyQt5.QtCore import QAbstractTableModel, Qt, QVariant, QSortFilterProxyModel from PyQt5.QtGui import QFont from operator import itemgetter import datetime @@ -27,12 +27,14 @@ class HistoryTableModel(QAbstractTableModel): self.account = account self.community = community self.columns = ('Date', 'UID/Public key', 'Payment', 'Deposit', 'Comment') - - def rowCount(self, parent): - transactions = self.account.transactions_sent(self.community) + \ + self.section_sorting = 0 + self.reversed = True + self.transactions = self.account.transactions_sent(self.community) + \ self.account.transactions_awaiting(self.community) + \ self.account.transactions_received(self.community) - return len(transactions) + + def rowCount(self, parent): + return len(self.transactions ) def columnCount(self, parent): return len(self.columns) @@ -84,29 +86,76 @@ class HistoryTableModel(QAbstractTableModel): def data(self, index, role): row = index.row() col = index.column() - transactions = self.account.transactions_sent(self.community) + \ - self.account.transactions_awaiting(self.community) + \ - self.account.transactions_received(self.community) - transactions = sorted(transactions, reverse=True, key=itemgetter(0)) if not index.isValid(): return QVariant() if role == Qt.DisplayRole: - if transactions[row] in self.account.transactions_sent(self.community) \ - or transactions[row] in self.account.transactions_awaiting(self.community): - return self.data_sent(transactions[row])[col] + if self.transactions[row] in self.account.transactions_sent(self.community) \ + or self.transactions[row] in self.account.transactions_awaiting(self.community): + return self.data_sent(self.transactions[row])[col] - if transactions[row] in self.account.transactions_received(self.community): - return self.data_received(transactions[row])[col] + if self.transactions[row] in self.account.transactions_received(self.community): + return self.data_received(self.transactions[row])[col] if role == Qt.FontRole: font = QFont() - if transactions[row] in self.account.transactions_awaiting(self.community): + if self.transactions[row] in self.account.transactions_awaiting(self.community): font.setItalic(True) else: font.setItalic(False) return font + def sort(self, section, order): + """Sort table by given column number. + """ + self.layoutAboutToBeChanged.emit() + + def uid(tx): + if tx in self.account.transactions_received(self.community): + pubkey = tx[1].issuers[0] + else: + pubkey = tx[1].outputs[0].pubkey + + try: + receiver = Person.lookup(pubkey, self.community).name + except PersonNotFoundError: + receiver = pubkey + return receiver + + def amount_received(tx): + amount = 0 + if tx in self.account.transactions_received(self.community): + for o in tx[1].outputs: + pubkeys = [w.pubkey for w in self.account.wallets] + if o.pubkey not in pubkeys: + amount += o.amount + return amount + + def amount_sent(tx): + amount = 0 + if tx in self.account.transactions_sent(self.community) \ + or tx in self.account.transactions_awaiting(self.community): + for o in tx[1].outputs: + pubkeys = [w.pubkey for w in self.account.wallets] + if o.pubkey not in pubkeys: + amount += o.amount + return amount + + def comment(tx): + return tx[1].comment + + key_getter = {0: itemgetter(0), + 1: uid, + 2: amount_sent, + 3: amount_received, + 4: comment} + + self.transactions = sorted(self.transactions, + reverse=(order == Qt.DescendingOrder), + key=key_getter[section]) + + self.layoutChanged.emit() + def flags(self, index): return Qt.ItemIsSelectable | Qt.ItemIsEnabled