comparison DisplayModel.py @ 130:1e6274cca035

merge with "backend" branch
author Dirk Olmes <dirk@xanthippe.ping.de>
date Mon, 22 Aug 2011 15:30:33 +0200
parents 04a730f9d07d
children c5a427d46703
comparison
equal deleted inserted replaced
117:dc0a82841a60 130:1e6274cca035
1 1
2 from PyQt4.QtCore import QAbstractListModel, QModelIndex, QVariant, Qt 2 from PyQt4.QtCore import QAbstractListModel, QModelIndex, QVariant, Qt
3 3
4 class DisplayModel(QAbstractListModel): 4 class DisplayModel(QAbstractListModel):
5 def __init__(self, parent=None, list=None, displayFunction=None, **args): 5 def __init__(self, parent=None, list=None, displayAttribute=None, **args):
6 QAbstractListModel.__init__(self, parent, *args) 6 QAbstractListModel.__init__(self, parent, *args)
7 self.list = list 7 self.list = list
8 self.displayFunction = displayFunction 8 self.displayAttribute = displayAttribute
9 9
10 def rowCount(self, parent=QModelIndex()): 10 def rowCount(self, parent=QModelIndex()):
11 return len(self.list) 11 return len(self.list)
12 12
13 def data(self, index, role): 13 def data(self, index, role):
14 if index.isValid() and role == Qt.DisplayRole: 14 if index.isValid() and role == Qt.DisplayRole:
15 row = index.row() 15 row = index.row()
16 object = self.list[row] 16 object = self.list[row]
17 displayString = self.displayFunction(object) 17 displayString = self._stringToDisplay(object)
18 return QVariant(displayString) 18 return QVariant(displayString)
19 else: 19 else:
20 return QVariant() 20 return QVariant()
21
22 def _stringToDisplay(self, object):
23 if hasattr(object, self.displayAttribute):
24 return getattr(object, self.displayAttribute)
25 else:
26 return "invalid display attribute: " + self.displayAttribute