Refactoring foldertree

Colors consistent
Sorting more consistent
Deduplication of methods
Context menu enable/disable affects tree
Fixes #73
This commit is contained in:
mailchuck 2015-10-27 19:24:29 +01:00 committed by Peter Surda
parent 1c4015d359
commit 77382a2424
2 changed files with 82 additions and 91 deletions

View File

@ -529,7 +529,7 @@ class MyForm(QtGui.QMainWindow):
i = 0 i = 0
for toAddress in db: for toAddress in db:
widget = Ui_AddressWidget(treeWidget, i, toAddress, db[toAddress]["inbox"]) widget = Ui_AddressWidget(treeWidget, i, toAddress, db[toAddress]["inbox"], enabled[toAddress])
j = 0 j = 0
unread = 0 unread = 0
for folder in folders: for folder in folders:
@ -3202,7 +3202,8 @@ class MyForm(QtGui.QMainWindow):
sqlExecute( sqlExecute(
'''update subscriptions set enabled=1 WHERE address=?''', '''update subscriptions set enabled=1 WHERE address=?''',
address) address)
self.setCurrentItemColor(QApplication.palette().text().color()) account = self.getCurrentItem()
account.setEnabled(True)
shared.reloadBroadcastSendersForWhichImWatching() shared.reloadBroadcastSendersForWhichImWatching()
def on_action_SubscriptionsDisable(self): def on_action_SubscriptionsDisable(self):
@ -3210,7 +3211,8 @@ class MyForm(QtGui.QMainWindow):
sqlExecute( sqlExecute(
'''update subscriptions set enabled=0 WHERE address=?''', '''update subscriptions set enabled=0 WHERE address=?''',
address) address)
self.setCurrentItemColor(QtGui.QColor(128, 128, 128)) account = self.getCurrentItem()
account.setEnabled(False)
shared.reloadBroadcastSendersForWhichImWatching() shared.reloadBroadcastSendersForWhichImWatching()
def on_context_menuSubscriptions(self, point): def on_context_menuSubscriptions(self, point):
@ -3434,7 +3436,8 @@ class MyForm(QtGui.QMainWindow):
def on_action_Enable(self): def on_action_Enable(self):
addressAtCurrentRow = self.getCurrentAccount() addressAtCurrentRow = self.getCurrentAccount()
self.enableIdentity(addressAtCurrentRow) self.enableIdentity(addressAtCurrentRow)
self.setCurrentItemColor(QApplication.palette().text().color()) account = self.getCurrentItem()
account.setEnabled(True)
def enableIdentity(self, address): def enableIdentity(self, address):
shared.config.set(address, 'enabled', 'true') shared.config.set(address, 'enabled', 'true')
@ -3444,7 +3447,8 @@ class MyForm(QtGui.QMainWindow):
def on_action_Disable(self): def on_action_Disable(self):
address = self.getCurrentAccount() address = self.getCurrentAccount()
self.disableIdentity(address) self.disableIdentity(address)
self.setCurrentItemColor(QtGui.QColor(128, 128, 128)) account = self.getCurrentItem()
account.setEnabled(False)
def disableIdentity(self, address): def disableIdentity(self, address):
shared.config.set(str(address), 'enabled', 'false') shared.config.set(str(address), 'enabled', 'false')

View File

@ -3,15 +3,16 @@ from PyQt4 import QtCore, QtGui
from utils import * from utils import *
import shared import shared
class Ui_FolderWidget(QtGui.QTreeWidgetItem): class AccountMixin (object):
folderWeight = {"inbox": 1, "sent": 2, "trash": 3} def accountColor (self):
def __init__(self, parent, pos = 0, address = "", folderName = "", unreadCount = 0): if not self.isEnabled:
super(QtGui.QTreeWidgetItem, self).__init__() return QtGui.QBrush(QtGui.QColor(128, 128, 128))
self.address = address elif self.type == "chan":
self.folderName = folderName return QtGui.QBrush(QtGui.QColor(216, 119, 0))
self.unreadCount = unreadCount elif self.type == "mailinglist" or self.type == "subscription":
parent.insertChild(pos, self) return QtGui.QBrush(QtGui.QColor(137, 04, 177))
self.updateText() else:
return QtGui.QBrush(QtGui.QApplication.palette().text().color())
def setAddress(self, address): def setAddress(self, address):
self.address = str(address) self.address = str(address)
@ -21,11 +22,41 @@ class Ui_FolderWidget(QtGui.QTreeWidgetItem):
self.unreadCount = int(cnt) self.unreadCount = int(cnt)
self.updateText() self.updateText()
def setEnabled(self, enabled):
self.isEnabled = enabled
self.updateText()
def setType(self):
if shared.safeConfigGetBoolean(self.address, 'chan'):
self.type = "chan"
elif shared.safeConfigGetBoolean(self.address, 'mailinglist'):
self.type = "mailinglist"
else:
self.type = "normal"
def updateText(self):
pass
class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin):
folderWeight = {"inbox": 1, "sent": 2, "trash": 3}
def __init__(self, parent, pos = 0, address = "", folderName = "", unreadCount = 0):
super(QtGui.QTreeWidgetItem, self).__init__()
self.initialised = False
self.setAddress(address)
self.setFolderName(folderName)
self.setUnreadCount(unreadCount)
self.initialised = True
self.updateText()
parent.insertChild(pos, self)
def setFolderName(self, fname): def setFolderName(self, fname):
self.folderName = str(fname) self.folderName = str(fname)
self.updateText() self.updateText()
def updateText(self): def updateText(self):
if not self.initialised:
return
text = QtGui.QApplication.translate("MainWindow", self.folderName) text = QtGui.QApplication.translate("MainWindow", self.folderName)
font = QtGui.QFont() font = QtGui.QFont()
if self.unreadCount > 0: if self.unreadCount > 0:
@ -60,34 +91,23 @@ class Ui_FolderWidget(QtGui.QTreeWidgetItem):
return super(QtGui.QTreeWidgetItem, self).__lt__(other) return super(QtGui.QTreeWidgetItem, self).__lt__(other)
class Ui_AddressWidget(QtGui.QTreeWidgetItem): class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin):
def __init__(self, parent, pos = 0, address = "", unreadCount = 0): def __init__(self, parent, pos = 0, address = "", unreadCount = 0, enabled = True):
super(QtGui.QTreeWidgetItem, self).__init__() super(QtGui.QTreeWidgetItem, self).__init__()
self.unreadCount = unreadCount
parent.insertTopLevelItem(pos, self) parent.insertTopLevelItem(pos, self)
# only set default when creating # only set default when creating
#super(QtGui.QTreeWidgetItem, self).setExpanded(shared.config.getboolean(self.address, 'enabled')) #super(QtGui.QTreeWidgetItem, self).setExpanded(shared.config.getboolean(self.address, 'enabled'))
self.initialised = False
self.setAddress(address) self.setAddress(address)
self.setEnabled(enabled)
def setAddress(self, address): self.setUnreadCount(unreadCount)
self.address = str(address)
self.setType() self.setType()
self.setExpanded(shared.safeConfigGetBoolean(self.address, 'enabled')) self.initialised = True
self.updateText() self.setExpanded(enabled) # does updateText
def setType(self):
if shared.safeConfigGetBoolean(self.address, 'chan'):
self.type = "chan"
elif shared.safeConfigGetBoolean(self.address, 'mailinglist'):
self.type = "mailinglist"
else:
self.type = "normal"
def setUnreadCount(self, cnt):
self.unreadCount = int(cnt)
self.updateText()
def updateText(self): def updateText(self):
if not self.initialised:
return
text = unicode(shared.config.get(self.address, 'label'), 'utf-8)') + ' (' + self.address + ')' text = unicode(shared.config.get(self.address, 'label'), 'utf-8)') + ' (' + self.address + ')'
font = QtGui.QFont() font = QtGui.QFont()
@ -101,15 +121,7 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem):
self.setFont(0, font) self.setFont(0, font)
#set text color #set text color
if shared.safeConfigGetBoolean(self.address, 'enabled'): brush = self.accountColor()
if shared.safeConfigGetBoolean(self.address, 'mailinglist'):
brush = QtGui.QBrush(QtGui.QColor(137, 04, 177))
else:
brush = QtGui.QBrush(QtGui.QApplication.palette().text().color())
#self.setExpanded(True)
else:
brush = QtGui.QBrush(QtGui.QColor(128, 128, 128))
#self.setExpanded(False)
brush.setStyle(QtCore.Qt.NoBrush) brush.setStyle(QtCore.Qt.NoBrush)
self.setForeground(0, brush) self.setForeground(0, brush)
@ -132,10 +144,8 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem):
reverse = False reverse = False
if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder: if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
reverse = True reverse = True
if shared.config.getboolean(self.address, 'enabled') == \ if self.isEnabled == other.isEnabled:
shared.config.getboolean(other.address, 'enabled'): if self.type == other.type:
if shared.safeConfigGetBoolean(self.address, 'mailinglist') == \
shared.safeConfigGetBoolean(other.address, 'mailinglist'):
if shared.config.get(self.address, 'label'): if shared.config.get(self.address, 'label'):
x = shared.config.get(self.address, 'label').decode('utf-8').lower() x = shared.config.get(self.address, 'label').decode('utf-8').lower()
else: else:
@ -146,44 +156,37 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem):
y = other.address.decode('utf-8').lower() y = other.address.decode('utf-8').lower()
return x < y return x < y
else: else:
return (reverse if shared.safeConfigGetBoolean(self.address, 'mailinglist') else not reverse) return (reverse if self.type == "mailinglist" else not reverse)
# else: # else:
return (not reverse if shared.config.getboolean(self.address, 'enabled') else reverse) return (not reverse if self.isEnabled else reverse)
return super(QtGui.QTreeWidgetItem, self).__lt__(other) return super(QtGui.QTreeWidgetItem, self).__lt__(other)
class Ui_SubscriptionWidget(Ui_AddressWidget): class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
def __init__(self, parent, pos = 0, address = "", unreadCount = 0, label = "", enabled = ""): def __init__(self, parent, pos = 0, address = "", unreadCount = 0, label = "", enabled = True):
super(QtGui.QTreeWidgetItem, self).__init__() super(QtGui.QTreeWidgetItem, self).__init__()
self.unreadCount = unreadCount
parent.insertTopLevelItem(pos, self) parent.insertTopLevelItem(pos, self)
# only set default when creating # only set default when creating
#super(QtGui.QTreeWidgetItem, self).setExpanded(shared.config.getboolean(self.address, 'enabled')) #super(QtGui.QTreeWidgetItem, self).setExpanded(shared.config.getboolean(self.address, 'enabled'))
self.setEnabled(enabled) self.initialised = False
self.setLabel(label)
self.setAddress(address) self.setAddress(address)
self.setEnabled(enabled)
self.setType()
self.setLabel(label)
self.setUnreadCount (unreadCount)
self.initialised = True
self.setExpanded(enabled) # does updateText
def setLabel(self, label): def setLabel(self, label):
self.label = label self.label = label
def setAddress(self, address):
self.address = str(address)
self.setType()
self.setExpanded(self.isEnabled)
self.updateText()
def setEnabled(self, enabled):
self.isEnabled = enabled
def setType(self): def setType(self):
self.type = "subscription" self.type = "subscription"
def setUnreadCount(self, cnt):
self.unreadCount = int(cnt)
self.updateText()
def updateText(self): def updateText(self):
if not self.initialised:
return
text = unicode(self.label, 'utf-8)') + ' (' + self.address + ')' text = unicode(self.label, 'utf-8)') + ' (' + self.address + ')'
font = QtGui.QFont() font = QtGui.QFont()
@ -197,12 +200,7 @@ class Ui_SubscriptionWidget(Ui_AddressWidget):
self.setFont(0, font) self.setFont(0, font)
#set text color #set text color
if self.isEnabled: brush = self.accountColor()
brush = QtGui.QBrush(QtGui.QColor(137, 04, 177))
#self.setExpanded(True)
else:
brush = QtGui.QBrush(QtGui.QColor(128, 128, 128))
#self.setExpanded(False)
brush.setStyle(QtCore.Qt.NoBrush) brush.setStyle(QtCore.Qt.NoBrush)
self.setForeground(0, brush) self.setForeground(0, brush)
@ -211,14 +209,6 @@ class Ui_SubscriptionWidget(Ui_AddressWidget):
self.setToolTip(0, text) self.setToolTip(0, text)
# self.setData(0, QtCore.Qt.UserRole, [self.address, "inbox"]) # self.setData(0, QtCore.Qt.UserRole, [self.address, "inbox"])
def setExpanded(self, expand):
super(Ui_SubscriptionWidget, self).setExpanded(expand)
self.updateText()
def edit(self):
self.setText(0, self.label)
super(QtGui.QAbstractItemView, self).edit()
# label (or address) alphabetically, disabled at the end # label (or address) alphabetically, disabled at the end
def __lt__(self, other): def __lt__(self, other):
if (isinstance(other, Ui_SubscriptionWidget)): if (isinstance(other, Ui_SubscriptionWidget)):
@ -240,22 +230,19 @@ class Ui_SubscriptionWidget(Ui_AddressWidget):
return super(QtGui.QTreeWidgetItem, self).__lt__(other) return super(QtGui.QTreeWidgetItem, self).__lt__(other)
class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem): class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
_types = {'normal': 0, 'chan': 1, 'subscription': 2} _types = {'normal': 0, 'chan': 1, 'subscription': 2}
def __init__ (self, text, type = 'normal'): def __init__ (self, text, type = 'normal'):
super(QtGui.QTableWidgetItem, self).__init__(text) super(QtGui.QTableWidgetItem, self).__init__(text)
self.label = text self.label = text
self.type = type
try: try:
self.type = self._types[type] self.typeNum = self._types[self.type]
except: except:
self.type = 0 self.type = 0
if self.type == 2: self.setEnabled(True)
brush = QtGui.QBrush(QtGui.QColor(137, 04, 177)) brush = self.accountColor()
elif self.type == 1:
brush = QtGui.QBrush(QtGui.QColor(216, 119, 0))
else:
return
brush.setStyle(QtCore.Qt.NoBrush) brush.setStyle(QtCore.Qt.NoBrush)
self.setForeground(brush) self.setForeground(brush)
@ -264,10 +251,10 @@ class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem):
reverse = False reverse = False
if self.tableWidget().horizontalHeader().sortIndicatorOrder() == QtCore.Qt.DescendingOrder: if self.tableWidget().horizontalHeader().sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
reverse = True reverse = True
if self.type == other.type: if self.typeNum == other.typeNum:
return self.label.decode('utf-8').lower() < other.label.decode('utf-8').lower() return self.label.decode('utf-8').lower() < other.label.decode('utf-8').lower()
else: else:
return (not reverse if self.type < other.type else reverse) return (not reverse if self.typeNum < other.typeNum else reverse)
return super(QtGui.QTableWidgetItem, self).__lt__(other) return super(QtGui.QTableWidgetItem, self).__lt__(other)