-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedmodel.py
122 lines (106 loc) · 4.2 KB
/
feedmodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
# Copyright 2011 by Andreas Pakulat <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA.
import initsip
initsip.setupSipApi()
from PyQt4.QtCore import QAbstractTableModel, Qt, QModelIndex
from PyQt4.QtGui import QFont
# Disable 'method can be used as function' as it triggers on columnCount which
# indeed does not need the self, but we can't change that due to inheritance
# from Qt
#pylint: disable=R0201
class FeedModel(QAbstractTableModel):
def __init__(self, feedmgr, parent=None):
QAbstractTableModel.__init__(self, parent)
self._feedmgr = feedmgr
def rowCount(self, parent=QModelIndex()):
if parent.isValid():
return 0
return len(self._feedmgr.feeds)
def columnCount(self, parent=QModelIndex()):
if parent.isValid():
return 0
return 3
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():
return None
if role != Qt.DisplayRole and role != Qt.FontRole:
return None
feed = list(self._feedmgr.feeds)[idx.row()]
if role == Qt.DisplayRole:
if idx.column() == 0:
return feed.title
elif idx.column() == 1:
return feed.unread
elif idx.column() == 2:
return len(feed.entries)
elif role == Qt.FontRole:
fnt = QFont()
if feed.unread > 0:
fnt.setBold(True)
return fnt
return None
def headerData(self, col, orient, role=Qt.DisplayRole):
if col < 0 or col >= self.columnCount() \
or role != Qt.DisplayRole \
or orient != Qt.Horizontal:
return None
if col == 0:
return "Title"
elif col == 1:
return "New"
elif col == 2:
return "All"
def feedFromIndex(self, idx):
if idx.row() < 0 \
or idx.row() >= self.rowCount(idx.parent()) \
or idx.parent().isValid():
return None
return self._feedmgr.feeds[idx.row()]
def deleteFeed(self, feed):
assert (feed in self._feedmgr.feeds)
idx = self._feedmgr.feeds.index(feed)
self.beginRemoveRows(QModelIndex(), idx, idx)
self._feedmgr.feeds.remove(feed)
self.endRemoveRows()
def addFeed(self, feed):
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
self._feedmgr.feeds.append(feed)
self.endInsertRows()
def feedsUpdated(self, updated):
titles = [ info["title"] for info in updated]
for feed in self._feedmgr.feeds:
if feed.title in titles:
self.dataChanged.emit(self.index(0, 0, QModelIndex()), self.index(self.columnCount()-1, self.rowCount()-1, QModelIndex()))
def entriesUpdated(self, feed):
if feed in self._feedmgr.feeds:
idx1 = self.index(self._feedmgr.feeds.index(feed), 0, QModelIndex())
idx2 = self.index(idx1.row(), 2, QModelIndex())
self.dataChanged.emit(idx1, idx2)
def indexForFeed(self, feed):
for i in range(0, len(self._feedmgr.feeds)):
if self._feedmgr.feeds[i] == feed:
return self.index(i, 0, QModelIndex())
return None
if __name__ == "__main__":
import sys
print "Cannot run this module"
sys.exit(1)