def data(self, idx, role=Qt.DisplayRole): if not idx.isValid() or \ idx.row() < 0 or \ idx.row() >= self.rowCount() or \ idx.column() < 0 or \ idx.column() >= self.columnCount() or \ self._feed is None: return None if role != Qt.DisplayRole and role != Qt.FontRole: return None entry = list(self._feed.entries)[idx.row()] if role == Qt.DisplayRole: if idx.column() == 0: if entry.important: return "!" return "" elif idx.column() == 1: return entry.title elif idx.column() == 2: return entry.author elif idx.column() == 3: return qDateTimeFromTimeStruct(entry.updated) elif role == Qt.FontRole: fnt = QFont() if not entry.read: fnt.setBold(True) if entry.important: fnt.setItalic(True) return fnt return None
def testData(self): self.assertEqual(self.entryModel.rowCount(), 2) self.assertEqual(self.entryModel.columnCount(), 4) self.assertEqual(self.entryModel.data( self.entryModel.index(0, 0, QModelIndex()), Qt.DisplayRole), "") self.assertEqual(self.entryModel.data( self.entryModel.index(0, 0, QModelIndex()), Qt.FontRole).italic(), False) self.assertEqual(self.entryModel.data( self.entryModel.index(0, 1, QModelIndex()), Qt.DisplayRole), "Title1") self.assertEqual(self.entryModel.data( self.entryModel.index(0, 2, QModelIndex()), Qt.DisplayRole), "Author1") self.assertEqual(self.entryModel.data( self.entryModel.index(0, 3, QModelIndex()), Qt.DisplayRole), qDateTimeFromTimeStruct( list(self.feed.entries)[0].updated)) self.assertEqual(self.entryModel.data( self.entryModel.index(0, 1, QModelIndex()), Qt.FontRole).bold(), True) self.assertEqual(self.entryModel.data( self.entryModel.index(1, 1, QModelIndex()), Qt.DisplayRole), "Title2") self.assertEqual(self.entryModel.data( self.entryModel.index(1, 0, QModelIndex()), Qt.DisplayRole), "!") self.assertEqual(self.entryModel.data( self.entryModel.index(1, 0, QModelIndex()), Qt.FontRole).italic(), True) self.assertEqual(self.entryModel.data( self.entryModel.index(1, 2, QModelIndex()), Qt.DisplayRole), "Author2") self.assertEqual(self.entryModel.data( self.entryModel.index(1, 3, QModelIndex()), Qt.DisplayRole), qDateTimeFromTimeStruct( list(self.feed.entries)[1].updated)) self.assertEqual(self.entryModel.data( self.entryModel.index(1, 1, QModelIndex()), Qt.FontRole).bold(), False)