changeset 228:94a902de5266

use GenericTableModel instead of FeedEntryTableModel
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sat, 31 May 2014 04:23:53 +0200
parents 6ce0e081094a
children f0b5bdce1189
files FeedEntryItemDelegate.py FeedEntryTableModel.py MainWindow.py Ui_MainWindow.ui
diffstat 4 files changed, 33 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- 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:
--- 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"
--- 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)
--- 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 @@
        <property name="orientation">
         <enum>Qt::Vertical</enum>
        </property>
-       <widget class="QTableView" name="feedEntryList">
+       <widget class="QTableView" name="feedEntryTable">
         <property name="alternatingRowColors">
          <bool>true</bool>
         </property>
@@ -50,8 +50,8 @@
          <bool>false</bool>
         </attribute>
        </widget>
-       <widget class="QWebView" name="webView">
-        <property name="url">
+       <widget class="QWebView" name="webView" native="true">
+        <property name="url" stdset="0">
          <url>
           <string>about:blank</string>
          </url>
@@ -347,7 +347,7 @@
    </hints>
   </connection>
   <connection>
-   <sender>feedEntryList</sender>
+   <sender>feedEntryTable</sender>
    <signal>clicked(QModelIndex)</signal>
    <receiver>MainWindow</receiver>
    <slot>feedEntrySelected(QModelIndex)</slot>
@@ -475,7 +475,7 @@
    </hints>
   </connection>
   <connection>
-   <sender>feedEntryList</sender>
+   <sender>feedEntryTable</sender>
    <signal>doubleClicked(QModelIndex)</signal>
    <receiver>MainWindow</receiver>
    <slot>feedEntryDoubleClicked()</slot>