comparison MainWindowController.py @ 17:5fda8bd94fa8

make the model used to display feeds generic (so it can be used to display FeedEntries, too)
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 28 Apr 2010 03:37:01 +0200
parents 3ffecc709da9
children 35225588b895
comparison
equal deleted inserted replaced
16:3ffecc709da9 17:5fda8bd94fa8
1 1
2 from Feed import Feed 2 from Feed import Feed
3 from PyQt4 import QtGui 3 from PyQt4 import QtCore, QtGui
4 from PyQt4.QtCore import QAbstractListModel 4 from PyQt4.QtCore import QAbstractListModel, QModelIndex, QVariant, Qt
5 from PyQt4.QtCore import QModelIndex
6 from PyQt4.QtCore import Qt
7 from PyQt4.QtCore import QVariant
8 from Ui_MainWindow import Ui_MainWindow 5 from Ui_MainWindow import Ui_MainWindow
9 6
10 class MainWindowController(QtGui.QMainWindow): 7 class MainWindowController(QtGui.QMainWindow):
11 def __init__(self, session=None): 8 def __init__(self, session=None):
12 QtGui.QWidget.__init__(self, None) 9 QtGui.QWidget.__init__(self, None)
13 self.session = session 10 self.session = session
14 self.ui = Ui_MainWindow() 11 self.ui = Ui_MainWindow()
15 self.ui.setupUi(self) 12 self.ui.setupUi(self)
16 self.connectWidgets() 13 self.setupWidgets()
17 14
18 def connectWidgets(self): 15 def setupWidgets(self):
19 feedModel = FeedModel(self) 16 self.setupFeedList()
17
18 def setupFeedList(self):
19 self.allFeeds = Feed.all(self.session)
20 feedModel = DisplayModel(self, self.allFeeds, Feed.userPresentableString)
20 self.ui.feedList.setModel(feedModel) 21 self.ui.feedList.setModel(feedModel)
22 self.connect(self.ui.feedList, QtCore.SIGNAL("clicked(QModelIndex)"), self.feedSelected)
23
24 def feedSelected(self, index):
25 feed = self.allFeeds[index.row()]
26 print("feed selected: " + str(feed))
21 27
22 class FeedModel(QAbstractListModel): 28 class DisplayModel(QAbstractListModel):
23 def __init__(self, parent=None, **args): 29 def __init__(self, parent=None, list=None, displayFunction=None, **args):
24 QAbstractListModel.__init__(self, parent, *args) 30 QAbstractListModel.__init__(self, parent, *args)
25 self.session = parent.session 31 self.list = list
26 self.allFeeds = Feed.all(parent.session) 32 self.displayFunction = displayFunction
27 33
28 def rowCount(self, parent=QModelIndex()): 34 def rowCount(self, parent=QModelIndex()):
29 return len(self.allFeeds) 35 return len(self.list)
30 36
31 def data(self, index, role): 37 def data(self, index, role):
32 if index.isValid() and role == Qt.DisplayRole: 38 if index.isValid() and role == Qt.DisplayRole:
33 row = index.row() 39 row = index.row()
34 feed = self.allFeeds[row] 40 object = self.list[row]
35 return QVariant(feed.title) 41 displayString = self.displayFunction(object)
42 return QVariant(displayString)
36 else: 43 else:
37 return QVariant() 44 return QVariant()