Helpviewer/History/HistoryTreeModel.py

changeset 0
de9c2efb9d02
child 7
c679fb30c8f3
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the history tree model.
8 """
9
10 import bisect
11
12 from PyQt4.QtCore import *
13 from PyQt4.QtGui import QAbstractProxyModel
14
15 from HistoryModel import HistoryModel
16
17 import UI.PixmapCache
18
19 class HistoryTreeModel(QAbstractProxyModel):
20 """
21 Class implementing the history tree model.
22 """
23 def __init__(self, sourceModel, parent = None):
24 """
25 Constructor
26
27 @param sourceModel reference to the source model (QAbstractItemModel)
28 @param parent reference to the parent object (QObject)
29 """
30 QAbstractProxyModel.__init__(self, parent)
31
32 self.__sourceRowCache = []
33 self.__removingDown = False
34
35 self.setSourceModel(sourceModel)
36
37 def headerData(self, section, orientation, role = Qt.DisplayRole):
38 """
39 Public method to get the header data.
40
41 @param section section number (integer)
42 @param orientation header orientation (Qt.Orientation)
43 @param role data role (integer)
44 @return header data (QVariant)
45 """
46 return self.sourceModel().headerData(section, orientation, role)
47
48 def data(self, index, role = Qt.DisplayRole):
49 """
50 Public method to get data from the model.
51
52 @param index index of history entry to get data for (QModelIndex)
53 @param role data role (integer)
54 @return history entry data (QVariant)
55 """
56 if role in [Qt.DisplayRole, Qt.EditRole]:
57 start = index.internalId()
58 if start == 0:
59 offset = self.__sourceDateRow(index.row())
60 if index.column() == 0:
61 idx = self.sourceModel().index(offset, 0)
62 date = idx.data(HistoryModel.DateRole).toDate()
63 if date == QDate.currentDate():
64 return QVariant(self.trUtf8("Earlier Today"))
65 return QVariant(date.toString("yyyy-MM-dd"))
66 if index.column() == 1:
67 return QVariant(self.trUtf8(
68 "%n item(s)", "", self.rowCount(index.sibling(index.row(), 0))))
69
70 elif role == Qt.DecorationRole:
71 if index.column() == 0 and not index.parent().isValid():
72 return QVariant(UI.PixmapCache.getIcon("history.png"))
73
74 elif role == HistoryModel.DateRole:
75 if index.column() == 0 and index.internalId() == 0:
76 offset = self.__sourceDateRow(index.row())
77 idx = self.sourceModel().index(offset, 0)
78 return idx.data(HistoryModel.DateRole)
79
80 return QAbstractProxyModel.data(self, index, role)
81
82 def columnCount(self, parent = QModelIndex()):
83 """
84 Public method to get the number of columns.
85
86 @param parent index of parent (QModelIndex)
87 @return number of columns (integer)
88 """
89 return self.sourceModel().columnCount(self.mapToSource(parent))
90
91 def rowCount(self, parent = QModelIndex()):
92 """
93 Public method to determine the number of rows.
94
95 @param parent index of parent (QModelIndex)
96 @return number of rows (integer)
97 """
98 if parent.internalId() != 0 or \
99 parent.column() > 0 or \
100 self.sourceModel() is None:
101 return 0
102
103 # row count OF dates
104 if not parent.isValid():
105 if self.__sourceRowCache:
106 return len(self.__sourceRowCache)
107
108 currentDate = QDate()
109 rows = 0
110 totalRows = self.sourceModel().rowCount()
111
112 for row in range(totalRows):
113 rowDate = \
114 self.sourceModel().index(row, 0).data(HistoryModel.DateRole).toDate()
115 if rowDate != currentDate:
116 self.__sourceRowCache.append(row)
117 currentDate = rowDate
118 rows += 1
119 return rows
120
121 # row count FOR a date
122 start = self.__sourceDateRow(parent.row())
123 end = self.__sourceDateRow(parent.row() + 1)
124 return end - start
125
126 def __sourceDateRow(self, row):
127 """
128 Private method to translate the top level date row into the offset
129 where that date starts.
130
131 @param row row number of the date (integer)
132 @return offset where that date starts (integer)
133 """
134 if row <= 0:
135 return 0
136
137 if len(self.__sourceRowCache) == 0:
138 self.rowCount(QModelIndex())
139
140 if row >= len(self.__sourceRowCache):
141 if self.sourceModel() is None:
142 return 0
143 return self.sourceModel().rowCount()
144
145 return self.__sourceRowCache[row]
146
147 def mapToSource(self, proxyIndex):
148 """
149 Public method to map an index to the source model index.
150
151 @param proxyIndex reference to a proxy model index (QModelIndex)
152 @return source model index (QModelIndex)
153 """
154 offset = proxyIndex.internalId()
155 if offset == 0:
156 return QModelIndex()
157 startDateRow = self.__sourceDateRow(offset - 1)
158 return self.sourceModel().index(
159 startDateRow + proxyIndex.row(), proxyIndex.column())
160
161 def index(self, row, column, parent = QModelIndex()):
162 """
163 Public method to create an index.
164
165 @param row row number for the index (integer)
166 @param column column number for the index (integer)
167 @param parent index of the parent item (QModelIndex)
168 @return requested index (QModelIndex)
169 """
170 if row < 0 or \
171 column < 0 or \
172 column >= self.columnCount(parent) or \
173 parent.column() > 0:
174 return QModelIndex()
175
176 if not parent.isValid():
177 return self.createIndex(row, column, 0)
178 return self.createIndex(row, column, parent.row() + 1)
179
180 def parent(self, index):
181 """
182 Public method to get the parent index.
183
184 @param index index of item to get parent (QModelIndex)
185 @return index of parent (QModelIndex)
186 """
187 offset = index.internalId()
188 if offset == 0 or not index.isValid():
189 return QModelIndex()
190 return self.createIndex(offset - 1, 0, 0)
191
192 def hasChildren(self, parent = QModelIndex()):
193 """
194 Public method to check, if an entry has some children.
195
196 @param parent index of the entry to check (QModelIndex)
197 @return flag indicating the presence of children (boolean)
198 """
199 grandparent = parent.parent()
200 if not grandparent.isValid():
201 return True
202 return False
203
204 def flags(self, index):
205 """
206 Public method to get the item flags.
207
208 @param index index of the item (QModelIndex)
209 @return flags (Qt.ItemFlags)
210 """
211 if not index.isValid():
212 return Qt.ItemFlags(Qt.NoItemFlags)
213 return Qt.ItemFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
214
215 def setSourceModel(self, sourceModel):
216 """
217 Public method to set the source model.
218
219 @param sourceModel reference to the source model (QAbstractItemModel)
220 """
221 if self.sourceModel() is not None:
222 self.disconnect(self.sourceModel(),
223 SIGNAL("modelReset()"),
224 self.__sourceReset)
225 self.disconnect(self.sourceModel(),
226 SIGNAL("layoutChanged()"),
227 self.__sourceReset)
228 self.disconnect(self.sourceModel(),
229 SIGNAL("rowsInserted(const QModelIndex &, int, int)"),
230 self.__sourceRowsInserted)
231 self.disconnect(self.sourceModel(),
232 SIGNAL("rowsRemoved(const QModelIndex &, int, int)"),
233 self.__sourceRowsRemoved)
234
235 QAbstractProxyModel.setSourceModel(self, sourceModel)
236
237 if self.sourceModel() is not None:
238 self.__loaded = False
239 self.connect(self.sourceModel(),
240 SIGNAL("modelReset()"),
241 self.__sourceReset)
242 self.connect(self.sourceModel(),
243 SIGNAL("layoutChanged()"),
244 self.__sourceReset)
245 self.connect(self.sourceModel(),
246 SIGNAL("rowsInserted(const QModelIndex &, int, int)"),
247 self.__sourceRowsInserted)
248 self.connect(self.sourceModel(),
249 SIGNAL("rowsRemoved(const QModelIndex &, int, int)"),
250 self.__sourceRowsRemoved)
251
252 self.reset()
253
254 def __sourceReset(self):
255 """
256 Private slot to handle a reset of the source model.
257 """
258 self.__sourceRowCache = []
259 self.reset()
260
261 def __sourceRowsInserted(self, parent, start, end):
262 """
263 Private slot to handle the insertion of data in the source model.
264
265 @param parent reference to the parent index (QModelIndex)
266 @param start start row (integer)
267 @param end end row (integer)
268 """
269 if not parent.isValid():
270 if start != 0 or start != end:
271 self.__sourceRowCache = []
272 self.reset()
273 return
274
275 self.__sourceRowCache = []
276 treeIndex = self.mapFromSource(self.sourceModel().index(start, 0))
277 treeParent = treeIndex.parent()
278 if self.rowCount(treeParent) == 1:
279 self.beginInsertRows(QModelIndex(), 0, 0)
280 self.endInsertRows()
281 else:
282 self.beginInsertRows(treeParent, treeIndex.row(), treeIndex.row())
283 self.endInsertRows()
284
285 def mapFromSource(self, sourceIndex):
286 """
287 Public method to map an index to the proxy model index.
288
289 @param sourceIndex reference to a source model index (QModelIndex)
290 @return proxy model index (QModelIndex)
291 """
292 if not sourceIndex.isValid():
293 return QModelIndex()
294
295 if len(self.__sourceRowCache) == 0:
296 self.rowCount(QModelIndex())
297
298 try:
299 row = self.__sourceRowCache.index(sourceIndex.row())
300 except ValueError:
301 row = bisect.bisect_left(self.__sourceRowCache, sourceIndex.row())
302 if row == len(self.__sourceRowCache) or \
303 self.__sourceRowCache[row] != sourceIndex.row():
304 row -= 1
305 dateRow = max(0, row)
306 row = sourceIndex.row() - self.__sourceRowCache[dateRow]
307 return self.createIndex(row, sourceIndex.column(), dateRow + 1)
308
309 def removeRows(self, row, count, parent = QModelIndex()):
310 """
311 Public method to remove entries from the model.
312
313 @param row row of the first entry to remove (integer)
314 @param count number of entries to remove (integer)
315 @param index of the parent entry (QModelIndex)
316 @return flag indicating successful removal (boolean)
317 """
318 if row < 0 or \
319 count <= 0 or \
320 row + count > self.rowCount(parent):
321 return False
322
323 self.__removingDown = True
324 if parent.isValid() and self.rowCount(parent) == count - row:
325 self.beginRemoveRows(QModelIndex(), parent.row(), parent.row())
326 else:
327 self.beginRemoveRows(parent, row, row + count - 1)
328 if parent.isValid():
329 # removing pages
330 offset = self.__sourceDateRow(parent.row())
331 return self.sourceModel().removeRows(offset + row, count)
332 else:
333 # removing whole dates
334 for i in range(row + count - 1, row - 1, -1):
335 dateParent = self.index(i, 0)
336 offset = self.__sourceDateRow(dateParent.row())
337 if not self.sourceModel().removeRows(offset, self.rowCount(dateParent)):
338 return False
339 return True
340
341 def __sourceRowsRemoved(self, parent, start, end):
342 """
343 Private slot to handle the removal of data in the source model.
344
345 @param parent reference to the parent index (QModelIndex)
346 @param start start row (integer)
347 @param end end row (integer)
348 """
349 if not self.__removingDown:
350 self.__sourceRowCache = []
351 self.reset()
352 return
353
354 if not parent.isValid():
355 if self.__sourceRowCache:
356 i = end
357 while i >= start:
358 try:
359 ind = self.__sourceRowCache.index(i)
360 except ValueError:
361 ind = bisect.bisect_left(self.__sourceRowCache, i)
362 if ind == len(self.__sourceRowCache) or \
363 self.__sourceRowCache[ind] != i:
364 ind -= 1
365 row = max(0, ind)
366 offset = self.__sourceRowCache[row]
367 dateParent = self.index(row, 0)
368 # If we can remove all the rows in the date do that
369 # and skip over them.
370 rc = self.rowCount(dateParent)
371 if i - rc + 1 == offset and start <= i - rc + 1:
372 del self.__sourceRowCache[row]
373 i -= rc + 1
374 else:
375 row += 1
376 i -= 1
377 for j in range(row, len(self.__sourceRowCache)):
378 self.__sourceRowCache[j] -= 1
379
380 if self.__removingDown:
381 self.endRemoveRows()
382 self.__removingDown = False

eric ide

mercurial