From 2cc7506619a7dad3b4fa7bdf15bee9d752f0d512 Mon Sep 17 00:00:00 2001 From: mailchuck Date: Fri, 23 Oct 2015 19:14:01 +0200 Subject: [PATCH] Addressbook contains chans, subscriptions and is sorted Fixes #5 --- src/bitmessageqt/__init__.py | 36 ++++++++++++++++++++------ src/bitmessageqt/foldertree.py | 46 +++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 2b217995..acd0ea6d 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -1951,18 +1951,38 @@ class MyForm(QtGui.QMainWindow): i, 0).setText(unicode(toLabel, 'utf-8')) def rerenderAddressBook(self): + def addRow (address, label, type): + self.ui.tableWidgetAddressBook.insertRow(0) + newItem = Ui_AddressBookWidgetItemLabel(address, unicode(label, 'utf-8'), type) + self.ui.tableWidgetAddressBook.setItem(0, 0, newItem) + newItem = Ui_AddressBookWidgetItemAddress(address, unicode(label, 'utf-8'), type) + self.ui.tableWidgetAddressBook.setItem(0, 1, newItem) + + self.ui.tableWidgetAddressBook.setSortingEnabled(False) self.ui.tableWidgetAddressBook.setRowCount(0) + + # subscriptions + queryreturn = sqlQuery('SELECT label, address FROM subscriptions WHERE enabled = 1') + for row in queryreturn: + label, address = row + addRow(address, label, 'subscription') + + # chans + addresses = getSortedAccounts() + for address in addresses: + account = accountClass(address) + if (account.type == 'chan'): + addRow(address, account.getLabel(), 'chan') + + # normal accounts queryreturn = sqlQuery('SELECT * FROM addressbook') for row in queryreturn: label, address = row - self.ui.tableWidgetAddressBook.insertRow(0) - newItem = QtGui.QTableWidgetItem(unicode(label, 'utf-8')) - newItem.setIcon(avatarize(address)) - self.ui.tableWidgetAddressBook.setItem(0, 0, newItem) - newItem = QtGui.QTableWidgetItem(address) - newItem.setFlags( - QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) - self.ui.tableWidgetAddressBook.setItem(0, 1, newItem) + addRow(address, label, 'normal') + + # sort + self.ui.tableWidgetAddressBook.sortItems(0, QtCore.Qt.AscendingOrder) + self.ui.tableWidgetAddressBook.setSortingEnabled(True) def rerenderSubscriptions(self): self.rerenderTabTreeSubscriptions() diff --git a/src/bitmessageqt/foldertree.py b/src/bitmessageqt/foldertree.py index 71caae66..08cd7ff2 100644 --- a/src/bitmessageqt/foldertree.py +++ b/src/bitmessageqt/foldertree.py @@ -238,4 +238,48 @@ class Ui_SubscriptionWidget(Ui_AddressWidget): # else: return (not reverse if self.isEnabled else reverse) - return super(QtGui.QTreeWidgetItem, self).__lt__(other) \ No newline at end of file + return super(QtGui.QTreeWidgetItem, self).__lt__(other) + +class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem): + _types = {'normal': 0, 'chan': 1, 'subscription': 2} + + def __init__ (self, text, type = 'normal'): + super(QtGui.QTableWidgetItem, self).__init__(text) + self.label = text + try: + self.type = self._types[type] + except: + self.type = 0 + if self.type == 2: + brush = QtGui.QBrush(QtGui.QColor(137, 04, 177)) + elif self.type == 1: + brush = QtGui.QBrush(QtGui.QColor(216, 119, 0)) + else: + return + brush.setStyle(QtCore.Qt.NoBrush) + self.setForeground(brush) + + def __lt__ (self, other): + if (isinstance(other, Ui_AddressBookWidgetItem)): + reverse = False + if self.tableWidget().horizontalHeader().sortIndicatorOrder() == QtCore.Qt.DescendingOrder: + reverse = True + if self.type == other.type: + return self.label.decode('utf-8').lower() < other.label.decode('utf-8').lower() + else: + return (not reverse if self.type < other.type else reverse) + return super(QtGui.QTableWidgetItem, self).__lt__(other) + + +class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem): + def __init__ (self, address, label, type): + Ui_AddressBookWidgetItem.__init__(self, label, type) + self.label = label + self.setIcon(avatarize(address)) + + +class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem): + def __init__ (self, address, label, type): + Ui_AddressBookWidgetItem.__init__(self, address, type) + self.label = label + self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) \ No newline at end of file