# HG changeset patch # User Dirk Olmes # Date 1272418621 -7200 # Node ID 5fda8bd94fa831a2efab773288d75ef91fd95f50 # Parent 3ffecc709da9ff0d700d58a458329474ed16b733 make the model used to display feeds generic (so it can be used to display FeedEntries, too) diff -r 3ffecc709da9 -r 5fda8bd94fa8 Feed.py --- a/Feed.py Wed Apr 28 03:00:11 2010 +0200 +++ b/Feed.py Wed Apr 28 03:37:01 2010 +0200 @@ -10,3 +10,6 @@ def __repr__(self): return "" % (self.pk, self.title) + + def userPresentableString(self): + return self.title diff -r 3ffecc709da9 -r 5fda8bd94fa8 MainWindowController.py --- a/MainWindowController.py Wed Apr 28 03:00:11 2010 +0200 +++ b/MainWindowController.py Wed Apr 28 03:37:01 2010 +0200 @@ -1,10 +1,7 @@ from Feed import Feed -from PyQt4 import QtGui -from PyQt4.QtCore import QAbstractListModel -from PyQt4.QtCore import QModelIndex -from PyQt4.QtCore import Qt -from PyQt4.QtCore import QVariant +from PyQt4 import QtCore, QtGui +from PyQt4.QtCore import QAbstractListModel, QModelIndex, QVariant, Qt from Ui_MainWindow import Ui_MainWindow class MainWindowController(QtGui.QMainWindow): @@ -13,25 +10,35 @@ self.session = session self.ui = Ui_MainWindow() self.ui.setupUi(self) - self.connectWidgets() + self.setupWidgets() + + def setupWidgets(self): + self.setupFeedList() - def connectWidgets(self): - feedModel = FeedModel(self) + def setupFeedList(self): + self.allFeeds = Feed.all(self.session) + feedModel = DisplayModel(self, self.allFeeds, Feed.userPresentableString) self.ui.feedList.setModel(feedModel) + self.connect(self.ui.feedList, QtCore.SIGNAL("clicked(QModelIndex)"), self.feedSelected) + + def feedSelected(self, index): + feed = self.allFeeds[index.row()] + print("feed selected: " + str(feed)) -class FeedModel(QAbstractListModel): - def __init__(self, parent=None, **args): +class DisplayModel(QAbstractListModel): + def __init__(self, parent=None, list=None, displayFunction=None, **args): QAbstractListModel.__init__(self, parent, *args) - self.session = parent.session - self.allFeeds = Feed.all(parent.session) + self.list = list + self.displayFunction = displayFunction def rowCount(self, parent=QModelIndex()): - return len(self.allFeeds) + return len(self.list) def data(self, index, role): if index.isValid() and role == Qt.DisplayRole: row = index.row() - feed = self.allFeeds[row] - return QVariant(feed.title) + object = self.list[row] + displayString = self.displayFunction(object) + return QVariant(displayString) else: - return QVariant() \ No newline at end of file + return QVariant()