annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
42a225be7e56 first version of the GUI
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
1
15
b1aeb98824c1 Add a list view displaying all feeds
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 14
diff changeset
2 from Feed import Feed
17
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
3 from PyQt4 import QtCore, QtGui
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
4 from PyQt4.QtCore import QAbstractListModel, QModelIndex, QVariant, Qt
14
42a225be7e56 first version of the GUI
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
5 from Ui_MainWindow import Ui_MainWindow
42a225be7e56 first version of the GUI
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
6
42a225be7e56 first version of the GUI
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
7 class MainWindowController(QtGui.QMainWindow):
42a225be7e56 first version of the GUI
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
8 def __init__(self, session=None):
42a225be7e56 first version of the GUI
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
9 QtGui.QWidget.__init__(self, None)
42a225be7e56 first version of the GUI
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
10 self.session = session
42a225be7e56 first version of the GUI
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
11 self.ui = Ui_MainWindow()
42a225be7e56 first version of the GUI
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
12 self.ui.setupUi(self)
17
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
13 self.setupWidgets()
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
14
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
15 def setupWidgets(self):
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
16 self.setupFeedList()
15
b1aeb98824c1 Add a list view displaying all feeds
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 14
diff changeset
17
17
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
18 def setupFeedList(self):
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
19 self.allFeeds = Feed.all(self.session)
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
20 feedModel = DisplayModel(self, self.allFeeds, Feed.userPresentableString)
15
b1aeb98824c1 Add a list view displaying all feeds
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 14
diff changeset
21 self.ui.feedList.setModel(feedModel)
17
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
22 self.connect(self.ui.feedList, QtCore.SIGNAL("clicked(QModelIndex)"), self.feedSelected)
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
23
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
24 def feedSelected(self, index):
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
25 feed = self.allFeeds[index.row()]
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
26 print("feed selected: " + str(feed))
15
b1aeb98824c1 Add a list view displaying all feeds
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 14
diff changeset
27
17
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
28 class DisplayModel(QAbstractListModel):
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
29 def __init__(self, parent=None, list=None, displayFunction=None, **args):
15
b1aeb98824c1 Add a list view displaying all feeds
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 14
diff changeset
30 QAbstractListModel.__init__(self, parent, *args)
17
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
31 self.list = list
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
32 self.displayFunction = displayFunction
16
3ffecc709da9 move fetch logic into Feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 15
diff changeset
33
15
b1aeb98824c1 Add a list view displaying all feeds
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 14
diff changeset
34 def rowCount(self, parent=QModelIndex()):
17
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
35 return len(self.list)
15
b1aeb98824c1 Add a list view displaying all feeds
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 14
diff changeset
36
b1aeb98824c1 Add a list view displaying all feeds
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 14
diff changeset
37 def data(self, index, role):
b1aeb98824c1 Add a list view displaying all feeds
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 14
diff changeset
38 if index.isValid() and role == Qt.DisplayRole:
b1aeb98824c1 Add a list view displaying all feeds
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 14
diff changeset
39 row = index.row()
17
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
40 object = self.list[row]
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
41 displayString = self.displayFunction(object)
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
42 return QVariant(displayString)
15
b1aeb98824c1 Add a list view displaying all feeds
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 14
diff changeset
43 else:
17
5fda8bd94fa8 make the model used to display feeds generic (so it can be used to display FeedEntries, too)
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 16
diff changeset
44 return QVariant()