# HG changeset patch # User Dirk Olmes # Date 1401503033 -7200 # Node ID 94a902de5266fceb31dd80709fb848268405c4e1 # Parent 6ce0e081094ada192b15d83ef9463aa961d9f24d use GenericTableModel instead of FeedEntryTableModel diff -r 6ce0e081094a -r 94a902de5266 FeedEntryItemDelegate.py --- a/FeedEntryItemDelegate.py Wed May 28 02:47:40 2014 +0200 +++ b/FeedEntryItemDelegate.py Sat May 31 04:23:53 2014 +0200 @@ -6,11 +6,7 @@ super(FeedEntryItemDelegate, self).__init__(None) def paint(self, painter, style, index): - feedEntryTableModel = index.model() - row = index.row() - # this makes assumptions about the type and the properties of the model but there's no - # way to get to the feedEntry using proper Qt API - entry = feedEntryTableModel.feedEntries[row] + entry = index.internalPointer() if entry.read: style.font.setWeight(QFont.Normal) else: diff -r 6ce0e081094a -r 94a902de5266 FeedEntryTableModel.py --- a/FeedEntryTableModel.py Wed May 28 02:47:40 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -# -*- coding: utf-8 -*- -from PyQt4.QtCore import QAbstractTableModel, Qt - -class TableModel(QAbstractTableModel): - ''' - This is the abstract super class for table models in Python. It deals with the Qt specifics so - that subclasses only need to care about implementing the functionality - ''' - def __init__(self, *args): - QAbstractTableModel.__init__(self, None, *args) - - def headerData(self, section, orientation, role): - if orientation == Qt.Horizontal and role == Qt.DisplayRole: - return self.headerDataForColumn(section) - else: - return None - - def data(self, index, role): - if not index.isValid(): - return None - elif role != Qt.DisplayRole: - return None - else: - value = self.dataForRowAndColumn(index.row(), index.column()) - return value - - -class FeedEntryTableModel(TableModel): - def __init__(self, feedEntries, *args): - TableModel.__init__(self, *args) - self.feedEntries = feedEntries - - def rowCount(self, parent): - return len(self.feedEntries) - - def columnCount(self, parent): - return 2 - - def dataForRowAndColumn(self, row, column): - feedEntry = self.feedEntries[row] - if column == 0: - return feedEntry.title - else: - return str(feedEntry.updated) - - def headerDataForColumn(self, column): - if column == 0: - return "Title" - else: - return "Date" diff -r 6ce0e081094a -r 94a902de5266 MainWindow.py --- a/MainWindow.py Wed May 28 02:47:40 2014 +0200 +++ b/MainWindow.py Sat May 31 04:23:53 2014 +0200 @@ -2,8 +2,8 @@ import subprocess from AddFeed import AddFeed from PyQtLib.GenericListModel import GenericListModel +from PyQtLib.GenericTableModel import GenericTableModel from FeedEntryItemDelegate import FeedEntryItemDelegate -from FeedEntryTableModel import FeedEntryTableModel from FeedSettings import FeedSettings from PreferencesDialog import PreferencesDialog from PyQt4.QtCore import QUrl @@ -20,7 +20,7 @@ self.ui = Ui_MainWindow() self.ui.setupUi(self) self.updateFeedList() - self.initFeedEntryList() + self.initFeedEntryTable() def updateFeedList(self): allFeeds = self.backend.getFeeds() @@ -28,13 +28,22 @@ self.ui.feedList.setModel(feedModel) self.ui.feedList.update() - def initFeedEntryList(self): + def initFeedEntryTable(self): # setup the default row height. This must be called before a table model is set - self.ui.feedEntryList.verticalHeader().setDefaultSectionSize(20) + self.ui.feedEntryTable.verticalHeader().setDefaultSectionSize(20) - self.ui.feedEntryList.setItemDelegate(FeedEntryItemDelegate()) - self.ui.feedEntryList.setModel(FeedEntryTableModel([])) - self.ui.feedEntryList.horizontalHeader().setStretchLastSection(True) + self.ui.feedEntryTable.setItemDelegate(FeedEntryItemDelegate()) + model = self.__buildFeedEntryTableModel() + self.ui.feedEntryTable.setModel(model) + self.ui.feedEntryTable.horizontalHeader().setStretchLastSection(True) + + def __buildFeedEntryTableModel(self): + model = GenericTableModel(self, []) + titleDisplay = lambda feedEntry: feedEntry.title + model.defineColumn('Title', titleDisplay) + dateDisplay = lambda feedEntry: feedEntry.updated.strftime('%d.%m.%Y') + model.defineColumn('Date', dateDisplay) + return model def feedSelected(self, index): self.backend.selectFeed(index.row()) @@ -49,24 +58,24 @@ def setupFeedEntries(self): self.setupFeedEntriesTableModel() - self.ui.feedEntryList.update() + self.ui.feedEntryTable.update() self.setupFeedEntriesTableHeaderWidths() self.scrollFirstRowInFeedEntriesTableToVisible() def setupFeedEntriesTableModel(self): feedEntries = self.backend.entriesForSelectedFeed() - model = FeedEntryTableModel(feedEntries) - self.ui.feedEntryList.setModel(model) + model = self.ui.feedEntryTable.model() + model.setDisplayedObjects(feedEntries) def setupFeedEntriesTableHeaderWidths(self): - width = self.ui.feedEntryList.width() + width = self.ui.feedEntryTable.width() firstColumnWidth = int(width * 0.8) - self.ui.feedEntryList.setColumnWidth(0, firstColumnWidth) + self.ui.feedEntryTable.setColumnWidth(0, firstColumnWidth) def scrollFirstRowInFeedEntriesTableToVisible(self): - model = self.ui.feedEntryList.model() + model = self.ui.feedEntryTable.model() visibleIndex = model.createIndex(0, 0) - self.ui.feedEntryList.scrollTo(visibleIndex) + self.ui.feedEntryTable.scrollTo(visibleIndex) def feedEntrySelected(self, index): self.ui.webView.setZoomFactor(1.0) @@ -115,15 +124,15 @@ def toggleReadOnSelectedEntry(self): self.backend.toggleSelectedFeedEntryRead() - self.ui.feedEntryList.doItemsLayout() + self.ui.feedEntryTable.doItemsLayout() def markSelectedFeedRead(self): self.backend.markSelectedFeedAsRead() - self.ui.feedEntryList.doItemsLayout() + self.ui.feedEntryTable.doItemsLayout() def markSelectedEntriesRead(self): indexes = [] - selectedIndexes = self.ui.feedEntryList.selectedIndexes() + selectedIndexes = self.ui.feedEntryTable.selectedIndexes() for index in selectedIndexes: # selectedIndexes returns one QModelIndex instance per row/column combo. # We are only interested in the rows here so just operate on the first @@ -132,7 +141,7 @@ row = index.row() indexes.append(row) self.backend.markFeedEntriesAsRead(indexes) - self.ui.feedEntryList.doItemsLayout() + self.ui.feedEntryTable.doItemsLayout() def addFeed(self): addFeed = AddFeed(self.backend) diff -r 6ce0e081094a -r 94a902de5266 Ui_MainWindow.ui --- a/Ui_MainWindow.ui Wed May 28 02:47:40 2014 +0200 +++ b/Ui_MainWindow.ui Sat May 31 04:23:53 2014 +0200 @@ -36,7 +36,7 @@ Qt::Vertical - + true @@ -50,8 +50,8 @@ false - - + + about:blank @@ -347,7 +347,7 @@ - feedEntryList + feedEntryTable clicked(QModelIndex) MainWindow feedEntrySelected(QModelIndex) @@ -475,7 +475,7 @@ - feedEntryList + feedEntryTable doubleClicked(QModelIndex) MainWindow feedEntryDoubleClicked()