From d7817ca2ef299134e62c97d22f1dcbdc71c44b3a Mon Sep 17 00:00:00 2001 From: mailchuck Date: Thu, 19 Nov 2015 04:05:58 +0100 Subject: [PATCH] Smoother rerenderTabSubscriptions Does not reset it all the time but adds/removes Fixes #109 --- src/bitmessageqt/__init__.py | 84 +++++++++++++++++++++++++----------- src/bitmessageqt/account.py | 20 +++++++++ 2 files changed, 80 insertions(+), 24 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 87eb1083..82dfa5ef 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -390,37 +390,73 @@ class MyForm(settingsmixin.SMainWindow): def rerenderTabTreeSubscriptions(self): treeWidget = self.ui.treeWidgetSubscriptions folders = ['inbox', 'sent', 'trash'] - treeWidget.clear() - treeWidget.setSortingEnabled(False) - treeWidget.header().setSortIndicator(0, Qt.AscendingOrder) - db = {} - queryreturn = sqlQuery('''SELECT fromaddress, folder, count(msgid) as cnt - FROM inbox, subscriptions - WHERE read = 0 AND subscriptions.address = inbox.fromaddress - GROUP BY inbox.fromaddress, folder''') - for row in queryreturn: - fromaddress, folder, cnt = row - if fromaddress not in db: - db[fromaddress] = {} - db[fromaddress][folder] = cnt - queryreturn = sqlQuery('SELECT label, address, enabled FROM subscriptions') - for row in queryreturn: - label, address, enabled = row - newItem = Ui_SubscriptionWidget(treeWidget, 0, address, 0, label, enabled) + # sort ascending when creating + if treeWidget.topLevelItemCount() == 0: + treeWidget.header().setSortIndicator(0, Qt.AscendingOrder) + # init dictionary + + db = getSortedSubscriptions(True) + + if treeWidget.isSortingEnabled(): + treeWidget.setSortingEnabled(False) + + widgets = {} + i = 0 + while i < treeWidget.topLevelItemCount(): + widget = treeWidget.topLevelItem(i) + if widget is not None: + toAddress = widget.address + else: + toAddress = None + + if not toAddress in db: + treeWidget.takeTopLevelItem(i) + # no increment + continue + unread = 0 + j = 0 + while j < widget.childCount(): + subwidget = widget.child(j) + try: + subwidget.setUnreadCount(db[toAddress][subwidget.folderName]['count']) + unread += db[toAddress][subwidget.folderName]['count'] + db[toAddress].pop(subwidget.folderName, None) + except: + widget.takeChild(j) + # no increment + continue + j += 1 + + # add missing folders + if len(db[toAddress]) > 0: + j = 0 + for f, c in db[toAddress].iteritems(): + subwidget = Ui_FolderWidget(widget, j, toAddress, f, c['count']) + j += 1 + widget.setUnreadCount(unread) + db.pop(toAddress, None) + i += 1 + + i = 0 + for toAddress in db: + widget = Ui_SubscriptionWidget(treeWidget, i, toAddress, db[toAddress]["inbox"]['count'], db[toAddress]["inbox"]['label'], db[toAddress]["inbox"]['enabled']) + j = 0 unread = 0 for folder in folders: try: - newSubItem = Ui_FolderWidget(newItem, 0, address, folder, db[address][folder]) - unread += db[address][folder] + subwidget = Ui_FolderWidget(widget, j, toAddress, folder, db[toAddress][folder]['count']) + unread += db[toAddress][folder]['count'] except KeyError: - newSubItem = Ui_FolderWidget(newItem, 0, address, folder, 0) - - newItem.setUnreadCount(unread) - newItem.setFlags (newItem.flags() | QtCore.Qt.ItemIsEditable) - + subwidget = Ui_FolderWidget(widget, j, toAddress, folder, 0) + j += 1 + widget.setUnreadCount(unread) + widget.setFlags (widget.flags() | QtCore.Qt.ItemIsEditable) + i += 1 + treeWidget.setSortingEnabled(True) + def rerenderTabTreeMessages(self): self.rerenderTabTree('messages') diff --git a/src/bitmessageqt/account.py b/src/bitmessageqt/account.py index 6e1083f6..83ce76c9 100644 --- a/src/bitmessageqt/account.py +++ b/src/bitmessageqt/account.py @@ -18,6 +18,26 @@ def getSortedAccounts(): ) return configSections +def getSortedSubscriptions(count = False): + queryreturn = sqlQuery('SELECT label, address, enabled FROM subscriptions ORDER BY label COLLATE NOCASE ASC') + ret = {} + for row in queryreturn: + label, address, enabled = row + ret[address] = {} + ret[address]["inbox"] = {} + ret[address]["inbox"]['label'] = label + ret[address]["inbox"]['enabled'] = enabled + ret[address]["inbox"]['count'] = 0 + if count: + queryreturn = sqlQuery('''SELECT fromaddress, folder, count(msgid) as cnt + FROM inbox, subscriptions + WHERE read = 0 AND subscriptions.address = inbox.fromaddress + GROUP BY inbox.fromaddress, folder''') + for row in queryreturn: + address, folder, cnt = row + ret[address][folder]['count'] = cnt + return ret + def accountClass(address): if not shared.config.has_section(address): if address == str_broadcast_subscribers: