comparison DisplayModel.py @ 119:04a730f9d07d backend

move all sqlalchemy related classes to the respective sub-package. use a backend to abstract from access to the data
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sun, 21 Aug 2011 03:55:16 +0200
parents c8bb3cee7935
children c5a427d46703
comparison
equal deleted inserted replaced
118:0e73adb2dec4 119:04a730f9d07d
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