Module foldertree: simplified a bit and did flake8

This commit is contained in:
Dmitri Bogomolov 2018-03-06 13:15:41 +02:00
parent cbb228db8b
commit 90eca36b5d
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13

View File

@ -1,10 +1,9 @@
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from string import find, rfind, rstrip, lstrip
from tr import _translate from tr import _translate
from bmconfigparser import BMConfigParser from bmconfigparser import BMConfigParser
from helper_sql import * from helper_sql import *
from utils import * from utils import avatarize
from settingsmixin import SettingsMixin from settingsmixin import SettingsMixin
# for pylupdate # for pylupdate
@ -22,7 +21,7 @@ class AccountMixin(object):
SUBSCRIPTION = 4 SUBSCRIPTION = 4
BROADCAST = 5 BROADCAST = 5
def accountColor (self): def accountColor(self):
if not self.isEnabled: if not self.isEnabled:
return QtGui.QColor(128, 128, 128) return QtGui.QColor(128, 128, 128)
elif self.type == self.CHAN: elif self.type == self.CHAN:
@ -31,18 +30,18 @@ class AccountMixin(object):
return QtGui.QColor(137, 04, 177) return QtGui.QColor(137, 04, 177)
else: else:
return QtGui.QApplication.palette().text().color() return QtGui.QApplication.palette().text().color()
def folderColor (self): def folderColor(self):
if not self.parent().isEnabled: if not self.parent().isEnabled:
return QtGui.QColor(128, 128, 128) return QtGui.QColor(128, 128, 128)
else: else:
return QtGui.QApplication.palette().text().color() return QtGui.QApplication.palette().text().color()
def accountBrush(self): def accountBrush(self):
brush = QtGui.QBrush(self.accountColor()) brush = QtGui.QBrush(self.accountColor())
brush.setStyle(QtCore.Qt.NoBrush) brush.setStyle(QtCore.Qt.NoBrush)
return brush return brush
def folderBrush(self): def folderBrush(self):
brush = QtGui.QBrush(self.folderColor()) brush = QtGui.QBrush(self.folderColor())
brush.setStyle(QtCore.Qt.NoBrush) brush.setStyle(QtCore.Qt.NoBrush)
@ -53,18 +52,23 @@ class AccountMixin(object):
self.address = None self.address = None
else: else:
self.address = str(address) self.address = str(address)
def setUnreadCount(self, cnt): def setUnreadCount(self, cnt):
if hasattr(self, "unreadCount") and self.unreadCount == int(cnt): try:
return if self.unreadCount == int(cnt):
return
except AttributeError:
pass
self.unreadCount = int(cnt) self.unreadCount = int(cnt)
if isinstance(self, QtGui.QTreeWidgetItem): if isinstance(self, QtGui.QTreeWidgetItem):
self.emitDataChanged() self.emitDataChanged()
def setEnabled(self, enabled): def setEnabled(self, enabled):
self.isEnabled = enabled self.isEnabled = enabled
if hasattr(self, "setExpanded"): try:
self.setExpanded(enabled) self.setExpanded(enabled)
except AttributeError:
pass
if isinstance(self, Ui_AddressWidget): if isinstance(self, Ui_AddressWidget):
for i in range(self.childCount()): for i in range(self.childCount()):
if isinstance(self.child(i), Ui_FolderWidget): if isinstance(self.child(i), Ui_FolderWidget):
@ -86,13 +90,16 @@ class AccountMixin(object):
self.type = AccountMixin.SUBSCRIPTION self.type = AccountMixin.SUBSCRIPTION
else: else:
self.type = self.NORMAL self.type = self.NORMAL
def defaultLabel(self): def defaultLabel(self):
queryreturn = None queryreturn = None
retval = None retval = None
if self.type in (AccountMixin.NORMAL, AccountMixin.CHAN, AccountMixin.MAILINGLIST): if self.type in (
AccountMixin.NORMAL,
AccountMixin.CHAN, AccountMixin.MAILINGLIST):
try: try:
retval = unicode(BMConfigParser().get(self.address, 'label'), 'utf-8') retval = unicode(
BMConfigParser().get(self.address, 'label'), 'utf-8')
except Exception as e: except Exception as e:
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''select label from addressbook where address=?''', self.address) '''select label from addressbook where address=?''', self.address)
@ -107,15 +114,15 @@ class AccountMixin(object):
elif self.address is None or self.type == AccountMixin.ALL: elif self.address is None or self.type == AccountMixin.ALL:
return unicode( return unicode(
str(_translate("MainWindow", "All accounts")), 'utf-8') str(_translate("MainWindow", "All accounts")), 'utf-8')
if retval is None:
return unicode(self.address, 'utf-8') return retval or unicode(self.address, 'utf-8')
else:
return retval
class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin): class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin):
folderWeight = {"inbox": 1, "new": 2, "sent": 3, "trash": 4} folderWeight = {"inbox": 1, "new": 2, "sent": 3, "trash": 4}
def __init__(self, parent, pos = 0, address = "", folderName = "", unreadCount = 0):
def __init__(
self, parent, pos=0, address="", folderName="", unreadCount=0):
super(QtGui.QTreeWidgetItem, self).__init__() super(QtGui.QTreeWidgetItem, self).__init__()
self.setAddress(address) self.setAddress(address)
self.setFolderName(folderName) self.setFolderName(folderName)
@ -144,7 +151,7 @@ class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin):
# inbox, sent, thrash first, rest alphabetically # inbox, sent, thrash first, rest alphabetically
def __lt__(self, other): def __lt__(self, other):
if (isinstance(other, Ui_FolderWidget)): if isinstance(other, Ui_FolderWidget):
if self.folderName in self.folderWeight: if self.folderName in self.folderWeight:
x = self.folderWeight[self.folderName] x = self.folderWeight[self.folderName]
else: else:
@ -153,19 +160,19 @@ class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin):
y = self.folderWeight[other.folderName] y = self.folderWeight[other.folderName]
else: else:
y = 99 y = 99
reverse = False reverse = QtCore.Qt.DescendingOrder == \
if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder: self.treeWidget().header().sortIndicatorOrder()
reverse = True
if x == y: if x == y:
return self.folderName < other.folderName return self.folderName < other.folderName
else: else:
return (x >= y if reverse else x < y) return (x >= y if reverse else x < y)
return super(QtGui.QTreeWidgetItem, self).__lt__(other) return super(QtGui.QTreeWidgetItem, self).__lt__(other)
class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin): class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
def __init__(self, parent, pos = 0, address = None, unreadCount = 0, enabled = True): def __init__(
self, parent, pos=0, address=None, unreadCount=0, enabled=True):
super(QtGui.QTreeWidgetItem, self).__init__() super(QtGui.QTreeWidgetItem, self).__init__()
parent.insertTopLevelItem(pos, self) parent.insertTopLevelItem(pos, self)
# only set default when creating # only set default when creating
@ -187,14 +194,14 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
except: except:
return unicode(self.address, 'utf-8') return unicode(self.address, 'utf-8')
def _getAddressBracket(self, unreadCount = False): def _getAddressBracket(self, unreadCount=False):
ret = "" ret = ""
if unreadCount: if unreadCount:
ret += " (" + str(self.unreadCount) + ")" ret += " (" + str(self.unreadCount) + ")"
if self.address is not None: if self.address is not None:
ret += " (" + self.address + ")" ret += " (" + self.address + ")"
return ret return ret
def data(self, column, role): def data(self, column, role):
if column == 0: if column == 0:
if role == QtCore.Qt.DisplayRole: if role == QtCore.Qt.DisplayRole:
@ -204,7 +211,7 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
return self._getLabel() + self._getAddressBracket(False) return self._getLabel() + self._getAddressBracket(False)
elif role == QtCore.Qt.EditRole: elif role == QtCore.Qt.EditRole:
return self._getLabel() return self._getLabel()
elif role == QtCore.Qt.ToolTipRole: elif role == QtCore.Qt.ToolTipRole:
return self._getLabel() + self._getAddressBracket(False) return self._getLabel() + self._getAddressBracket(False)
elif role == QtCore.Qt.DecorationRole: elif role == QtCore.Qt.DecorationRole:
if self.address is None: if self.address is None:
@ -218,23 +225,25 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
elif role == QtCore.Qt.ForegroundRole: elif role == QtCore.Qt.ForegroundRole:
return self.accountBrush() return self.accountBrush()
return super(Ui_AddressWidget, self).data(column, role) return super(Ui_AddressWidget, self).data(column, role)
def setData(self, column, role, value): def setData(self, column, role, value):
if role == QtCore.Qt.EditRole and self.type != AccountMixin.SUBSCRIPTION: if role == QtCore.Qt.EditRole \
if isinstance(value, QtCore.QVariant): and self.type != AccountMixin.SUBSCRIPTION:
BMConfigParser().set(str(self.address), 'label', str(value.toString().toUtf8())) BMConfigParser().set(
else: str(self.address), 'label',
BMConfigParser().set(str(self.address), 'label', str(value)) str(value).toString().toUtf8()
if isinstance(value, QtCore.QVariant) else str(value)
)
BMConfigParser().save() BMConfigParser().save()
return super(Ui_AddressWidget, self).setData(column, role, value) return super(Ui_AddressWidget, self).setData(column, role, value)
def setAddress(self, address): def setAddress(self, address):
super(Ui_AddressWidget, self).setAddress(address) super(Ui_AddressWidget, self).setAddress(address)
self.setData(0, QtCore.Qt.UserRole, self.address) self.setData(0, QtCore.Qt.UserRole, self.address)
def setExpanded(self, expand): def setExpanded(self, expand):
super(Ui_AddressWidget, self).setExpanded(expand) super(Ui_AddressWidget, self).setExpanded(expand)
def _getSortRank(self): def _getSortRank(self):
ret = self.type ret = self.type
if not self.isEnabled: if not self.isEnabled:
@ -243,21 +252,25 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
# 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_AddressWidget)): if isinstance(other, Ui_AddressWidget):
reverse = False reverse = QtCore.Qt.DescendingOrder == \
if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder: self.treeWidget().header().sortIndicatorOrder()
reverse = True
if self._getSortRank() == other._getSortRank(): if self._getSortRank() == other._getSortRank():
x = self._getLabel().lower() x = self._getLabel().lower()
y = other._getLabel().lower() y = other._getLabel().lower()
return x < y return x < y
return (not reverse if self._getSortRank() < other._getSortRank() else reverse) return (
not reverse
if self._getSortRank() < other._getSortRank() else reverse
)
return super(QtGui.QTreeWidgetItem, self).__lt__(other) return super(QtGui.QTreeWidgetItem, self).__lt__(other)
class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin): class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
def __init__(self, parent, pos = 0, address = "", unreadCount = 0, label = "", enabled = True): def __init__(
self, parent, pos=0, address="", unreadCount=0, label="",
enabled=True):
super(QtGui.QTreeWidgetItem, self).__init__() super(QtGui.QTreeWidgetItem, self).__init__()
parent.insertTopLevelItem(pos, self) parent.insertTopLevelItem(pos, self)
# only set default when creating # only set default when creating
@ -266,7 +279,7 @@ class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
self.setEnabled(enabled) self.setEnabled(enabled)
self.setType() self.setType()
self.setUnreadCount(unreadCount) self.setUnreadCount(unreadCount)
def _getLabel(self): def _getLabel(self):
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''select label from subscriptions where address=?''', self.address) '''select label from subscriptions where address=?''', self.address)
@ -275,16 +288,16 @@ class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
retval, = row retval, = row
return unicode(retval, 'utf-8', 'ignore') return unicode(retval, 'utf-8', 'ignore')
return unicode(self.address, 'utf-8') return unicode(self.address, 'utf-8')
def setType(self): def setType(self):
super(Ui_SubscriptionWidget, self).setType() # sets it editable super(Ui_SubscriptionWidget, self).setType() # sets it editable
self.type = AccountMixin.SUBSCRIPTION # overrides type self.type = AccountMixin.SUBSCRIPTION # overrides type
def setData(self, column, role, value): def setData(self, column, role, value):
if role == QtCore.Qt.EditRole: if role == QtCore.Qt.EditRole:
from debug import logger
if isinstance(value, QtCore.QVariant): if isinstance(value, QtCore.QVariant):
label = str(value.toString().toUtf8()).decode('utf-8', 'ignore') label = str(
value.toString().toUtf8()).decode('utf-8', 'ignore')
else: else:
label = unicode(value, 'utf-8', 'ignore') label = unicode(value, 'utf-8', 'ignore')
sqlExecute( sqlExecute(
@ -293,12 +306,10 @@ class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
return super(Ui_SubscriptionWidget, self).setData(column, role, value) return super(Ui_SubscriptionWidget, self).setData(column, role, value)
class MessageList_AddressWidget(QtGui.QTableWidgetItem, AccountMixin, SettingsMixin): class MessageList_AddressWidget(
def __init__(self, parent, address = None, label = None, unread = False): QtGui.QTableWidgetItem, AccountMixin, SettingsMixin):
def __init__(self, parent, address=None, label=None, unread=False):
super(QtGui.QTableWidgetItem, self).__init__() super(QtGui.QTableWidgetItem, self).__init__()
#parent.insertTopLevelItem(pos, self)
# only set default when creating
#super(QtGui.QTreeWidgetItem, self).setExpanded(BMConfigParser().getboolean(self.address, 'enabled'))
self.isEnabled = True self.isEnabled = True
self.setAddress(address) self.setAddress(address)
self.setLabel(label) self.setLabel(label)
@ -307,13 +318,17 @@ class MessageList_AddressWidget(QtGui.QTableWidgetItem, AccountMixin, SettingsMi
self.setType() self.setType()
parent.append(self) parent.append(self)
def setLabel(self, label = None): def setLabel(self, label=None):
newLabel = self.address newLabel = self.address
if label is None: if label is None:
queryreturn = None queryreturn = None
if self.type in (AccountMixin.NORMAL, AccountMixin.CHAN, AccountMixin.MAILINGLIST): if self.type in (
AccountMixin.NORMAL,
AccountMixin.CHAN, AccountMixin.MAILINGLIST):
try: try:
newLabel = unicode(BMConfigParser().get(self.address, 'label'), 'utf-8', 'ignore') newLabel = unicode(
BMConfigParser().get(self.address, 'label'),
'utf-8', 'ignore')
except: except:
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''select label from addressbook where address=?''', self.address) '''select label from addressbook where address=?''', self.address)
@ -326,9 +341,11 @@ class MessageList_AddressWidget(QtGui.QTableWidgetItem, AccountMixin, SettingsMi
newLabel = unicode(row[0], 'utf-8', 'ignore') newLabel = unicode(row[0], 'utf-8', 'ignore')
else: else:
newLabel = label newLabel = label
if hasattr(self, 'label') and newLabel == self.label: try:
return if self.label == newLabel:
self.label = newLabel return
except AttributeError:
self.label = newLabel
def setUnread(self, unread): def setUnread(self, unread):
self.unread = unread self.unread = unread
@ -341,7 +358,8 @@ class MessageList_AddressWidget(QtGui.QTableWidgetItem, AccountMixin, SettingsMi
elif role == QtCore.Qt.ToolTipRole: elif role == QtCore.Qt.ToolTipRole:
return self.label + " (" + self.address + ")" return self.label + " (" + self.address + ")"
elif role == QtCore.Qt.DecorationRole: elif role == QtCore.Qt.DecorationRole:
if BMConfigParser().safeGetBoolean('bitmessagesettings', 'useidenticons'): if BMConfigParser().safeGetBoolean(
'bitmessagesettings', 'useidenticons'):
if self.address is None: if self.address is None:
return avatarize(self.label) return avatarize(self.label)
else: else:
@ -355,7 +373,7 @@ class MessageList_AddressWidget(QtGui.QTableWidgetItem, AccountMixin, SettingsMi
elif role == QtCore.Qt.UserRole: elif role == QtCore.Qt.UserRole:
return self.address return self.address
return super(MessageList_AddressWidget, self).data(role) return super(MessageList_AddressWidget, self).data(role)
def setData(self, role, value): def setData(self, role, value):
if role == QtCore.Qt.EditRole: if role == QtCore.Qt.EditRole:
self.setLabel() self.setLabel()
@ -363,17 +381,14 @@ class MessageList_AddressWidget(QtGui.QTableWidgetItem, AccountMixin, SettingsMi
# 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, MessageList_AddressWidget)): if isinstance(other, MessageList_AddressWidget):
return self.label.lower() < other.label.lower() return self.label.lower() < other.label.lower()
return super(QtGui.QTableWidgetItem, self).__lt__(other) return super(QtGui.QTableWidgetItem, self).__lt__(other)
class MessageList_SubjectWidget(QtGui.QTableWidgetItem, SettingsMixin): class MessageList_SubjectWidget(QtGui.QTableWidgetItem, SettingsMixin):
def __init__(self, parent, subject = None, label = None, unread = False): def __init__(self, parent, subject=None, label=None, unread=False):
super(QtGui.QTableWidgetItem, self).__init__() super(QtGui.QTableWidgetItem, self).__init__()
#parent.insertTopLevelItem(pos, self)
# only set default when creating
#super(QtGui.QTreeWidgetItem, self).setExpanded(BMConfigParser().getboolean(self.address, 'enabled'))
self.setSubject(subject) self.setSubject(subject)
self.setLabel(label) self.setLabel(label)
self.setUnread(unread) self.setUnread(unread)
@ -382,7 +397,7 @@ class MessageList_SubjectWidget(QtGui.QTableWidgetItem, SettingsMixin):
def setLabel(self, label): def setLabel(self, label):
self.label = label self.label = label
def setSubject(self, subject): def setSubject(self, subject):
self.subject = subject self.subject = subject
@ -403,19 +418,16 @@ class MessageList_SubjectWidget(QtGui.QTableWidgetItem, SettingsMixin):
elif role == QtCore.Qt.UserRole: elif role == QtCore.Qt.UserRole:
return self.subject return self.subject
return super(MessageList_SubjectWidget, self).data(role) return super(MessageList_SubjectWidget, self).data(role)
def setData(self, role, value):
return super(MessageList_SubjectWidget, self).setData(role, value)
# 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, MessageList_SubjectWidget)): if isinstance(other, MessageList_SubjectWidget):
return self.label.lower() < other.label.lower() return self.label.lower() < other.label.lower()
return super(QtGui.QTableWidgetItem, self).__lt__(other) return super(QtGui.QTableWidgetItem, self).__lt__(other)
class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin): class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
def __init__ (self, text, type = AccountMixin.NORMAL): def __init__(self, text, type=AccountMixin.NORMAL):
super(QtGui.QTableWidgetItem, self).__init__(text) super(QtGui.QTableWidgetItem, self).__init__(text)
self.label = text self.label = text
self.type = type self.type = type
@ -429,11 +441,9 @@ class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
elif role == QtCore.Qt.ToolTipRole: elif role == QtCore.Qt.ToolTipRole:
return self.label + " (" + self.address + ")" return self.label + " (" + self.address + ")"
elif role == QtCore.Qt.DecorationRole: elif role == QtCore.Qt.DecorationRole:
if BMConfigParser().safeGetBoolean('bitmessagesettings', 'useidenticons'): if BMConfigParser().safeGetBoolean(
if self.address is None: 'bitmessagesettings', 'useidenticons'):
return avatarize(self.label) return avatarize(self.address or self.label)
else:
return avatarize(self.address)
elif role == QtCore.Qt.FontRole: elif role == QtCore.Qt.FontRole:
font = QtGui.QFont() font = QtGui.QFont()
return font return font
@ -449,25 +459,26 @@ class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
self.label = str(value.toString().toUtf8()) self.label = str(value.toString().toUtf8())
else: else:
self.label = str(value) self.label = str(value)
if self.type in (AccountMixin.NORMAL, AccountMixin.MAILINGLIST, AccountMixin.CHAN): if self.type in (
AccountMixin.NORMAL,
AccountMixin.MAILINGLIST, AccountMixin.CHAN):
try: try:
a = BMConfigParser().get(self.address, 'label') BMConfigParser().get(self.address, 'label')
BMConfigParser().set(self.address, 'label', self.label) BMConfigParser().set(self.address, 'label', self.label)
BMConfigParser().save() BMConfigParser().save()
except: except:
sqlExecute('''UPDATE addressbook set label=? WHERE address=?''', self.label, self.address) sqlExecute('''UPDATE addressbook set label=? WHERE address=?''', self.label, self.address)
elif self.type == AccountMixin.SUBSCRIPTION: elif self.type == AccountMixin.SUBSCRIPTION:
from debug import logger
sqlExecute('''UPDATE subscriptions set label=? WHERE address=?''', self.label, self.address) sqlExecute('''UPDATE subscriptions set label=? WHERE address=?''', self.label, self.address)
else: else:
pass pass
return super(Ui_AddressBookWidgetItem, self).setData(role, value) return super(Ui_AddressBookWidgetItem, self).setData(role, value)
def __lt__(self, other):
if isinstance(other, Ui_AddressBookWidgetItem):
reverse = QtCore.Qt.DescendingOrder == \
self.tableWidget().horizontalHeader().sortIndicatorOrder()
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: if self.type == other.type:
return self.label.lower() < other.label.lower() return self.label.lower() < other.label.lower()
else: else:
@ -476,8 +487,8 @@ class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem): class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem):
def __init__ (self, address, label, type): def __init__(self, address, label, type):
Ui_AddressBookWidgetItem.__init__(self, label, type) super(Ui_AddressBookWidgetItemLabel, self).__init__(label, type)
self.address = address self.address = address
self.label = label self.label = label
@ -487,8 +498,8 @@ class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem):
class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem): class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem):
def __init__ (self, address, label, type): def __init__(self, address, label, type):
Ui_AddressBookWidgetItem.__init__(self, address, type) super(Ui_AddressBookWidgetItemAddress, self).__init__(address, type)
self.address = address self.address = address
self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
@ -502,26 +513,22 @@ class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem):
class AddressBookCompleter(QtGui.QCompleter): class AddressBookCompleter(QtGui.QCompleter):
def __init__(self): def __init__(self):
super(QtGui.QCompleter, self).__init__() super(AddressBookCompleter, self).__init__()
self.cursorPos = -1 self.cursorPos = -1
def onCursorPositionChanged(self, oldPos, newPos): def onCursorPositionChanged(self, oldPos, newPos):
if oldPos != self.cursorPos: if oldPos != self.cursorPos:
self.cursorPos = -1 self.cursorPos = -1
def splitPath(self, path): def splitPath(self, path):
stringList = [] text = unicode(path.toUtf8(), 'utf-8')
text = unicode(path.toUtf8(), encoding="UTF-8") return [text[:self.widget().cursorPosition()].split(';')[-1].strip()]
splitIndex = rfind(text[0:self.widget().cursorPosition()], ";") + 1
str = text[splitIndex:self.widget().cursorPosition()]
str = rstrip(lstrip(str))
stringList.append(str)
return stringList
def pathFromIndex(self, index): def pathFromIndex(self, index):
autoString = unicode(index.data(QtCore.Qt.EditRole).toString().toUtf8(), encoding="UTF-8") autoString = unicode(
text = unicode(self.widget().text().toUtf8(), encoding="UTF-8") index.data(QtCore.Qt.EditRole).toString().toUtf8(), 'utf-8')
text = unicode(self.widget().text().toUtf8(), 'utf-8')
# If cursor position was saved, restore it, else save it # If cursor position was saved, restore it, else save it
if self.cursorPos != -1: if self.cursorPos != -1:
self.widget().setCursorPosition(self.cursorPos) self.widget().setCursorPosition(self.cursorPos)
@ -530,15 +537,17 @@ class AddressBookCompleter(QtGui.QCompleter):
# Get current prosition # Get current prosition
curIndex = self.widget().cursorPosition() curIndex = self.widget().cursorPosition()
# prev_delimiter_index should actually point at final white space AFTER the delimiter # prev_delimiter_index should actually point at final white space
# AFTER the delimiter
# Get index of last delimiter before current position # Get index of last delimiter before current position
prevDelimiterIndex = rfind(text[0:curIndex], ";") prevDelimiterIndex = text[0:curIndex].rfind(";")
while text[prevDelimiterIndex + 1] == " ": while text[prevDelimiterIndex + 1] == " ":
prevDelimiterIndex += 1 prevDelimiterIndex += 1
# Get index of first delimiter after current position (or EOL if no delimiter after cursor) # Get index of first delimiter after current position
nextDelimiterIndex = find(text, ";", curIndex) # (or EOL if no delimiter after cursor)
nextDelimiterIndex = text.find(";", curIndex)
if nextDelimiterIndex == -1: if nextDelimiterIndex == -1:
nextDelimiterIndex = len(text) nextDelimiterIndex = len(text)
@ -546,9 +555,9 @@ class AddressBookCompleter(QtGui.QCompleter):
part1 = text[0:prevDelimiterIndex + 1] part1 = text[0:prevDelimiterIndex + 1]
# Get string value from before auto finished string is selected # Get string value from before auto finished string is selected
pre = text[prevDelimiterIndex + 1:curIndex - 1]; # pre = text[prevDelimiterIndex + 1:curIndex - 1]
# Get part of string that occurs AFTER cursor # Get part of string that occurs AFTER cursor
part2 = text[nextDelimiterIndex:] part2 = text[nextDelimiterIndex:]
return part1 + autoString + part2; return part1 + autoString + part2