Helpviewer/History/HistoryFilterModel.py

branch
Py2 comp.
changeset 3057
10516539f238
parent 2525
8b507a9a2d40
parent 3002
6ffc581f00f1
child 3058
0a02c433f52d
equal deleted inserted replaced
3056:9986ec0e559a 3057:10516539f238
42 42
43 def __lt__(self, other): 43 def __lt__(self, other):
44 """ 44 """
45 Special method determining less relation. 45 Special method determining less relation.
46 46
47 Note: Like the actual history entries the index mapping is sorted in reverse 47 Note: Like the actual history entries the index mapping is sorted in
48 order by offset 48 reverse order by offset
49 49
50 @param other reference to the history data object to compare against 50 @param other reference to the history data object to compare against
51 (HistoryEntry) 51 (HistoryEntry)
52 @return flag indicating less (boolean) 52 @return flag indicating less (boolean)
53 """ 53 """
120 @param sourceModel reference to the source model (QAbstractItemModel) 120 @param sourceModel reference to the source model (QAbstractItemModel)
121 """ 121 """
122 if self.sourceModel() is not None: 122 if self.sourceModel() is not None:
123 self.sourceModel().modelReset.disconnect(self.__sourceReset) 123 self.sourceModel().modelReset.disconnect(self.__sourceReset)
124 self.sourceModel().dataChanged.disconnect(self.__sourceDataChanged) 124 self.sourceModel().dataChanged.disconnect(self.__sourceDataChanged)
125 self.sourceModel().rowsInserted.disconnect(self.__sourceRowsInserted) 125 self.sourceModel().rowsInserted.disconnect(
126 self.__sourceRowsInserted)
126 self.sourceModel().rowsRemoved.disconnect(self.__sourceRowsRemoved) 127 self.sourceModel().rowsRemoved.disconnect(self.__sourceRowsRemoved)
127 128
128 super(HistoryFilterModel, self).setSourceModel(sourceModel) 129 super(HistoryFilterModel, self).setSourceModel(sourceModel)
129 130
130 if self.sourceModel() is not None: 131 if self.sourceModel() is not None:
233 self.__load() 234 self.__load()
234 if row < 0 or row >= self.rowCount(parent) or \ 235 if row < 0 or row >= self.rowCount(parent) or \
235 column < 0 or column >= self.columnCount(parent): 236 column < 0 or column >= self.columnCount(parent):
236 return QModelIndex() 237 return QModelIndex()
237 238
238 return self.createIndex(row, column, self.__filteredRows[row].tailOffset) 239 return self.createIndex(row, column,
240 self.__filteredRows[row].tailOffset)
239 241
240 def parent(self, index): 242 def parent(self, index):
241 """ 243 """
242 Public method to get the parent index. 244 Public method to get the parent index.
243 245
265 self.__filteredRows.append( 267 self.__filteredRows.append(
266 HistoryData(sourceOffset, self.__frequencyScore(idx))) 268 HistoryData(sourceOffset, self.__frequencyScore(idx)))
267 self.__historyDict[url] = sourceOffset 269 self.__historyDict[url] = sourceOffset
268 else: 270 else:
269 # the url is known already, so just update the frequency score 271 # the url is known already, so just update the frequency score
270 row = self.__filteredRows.index(HistoryData(self.__historyDict[url], -1)) 272 row = self.__filteredRows.index(
271 self.__filteredRows[row].frequency += self.__frequencyScore(idx) 273 HistoryData(self.__historyDict[url], -1))
274 self.__filteredRows[row].frequency += \
275 self.__frequencyScore(idx)
272 276
273 self.__loaded = True 277 self.__loaded = True
274 278
275 def __sourceRowsInserted(self, parent, start, end): 279 def __sourceRowsInserted(self, parent, start, end):
276 """ 280 """
286 290
287 idx = self.sourceModel().index(start, 0, parent) 291 idx = self.sourceModel().index(start, 0, parent)
288 url = idx.data(HistoryModel.UrlStringRole) 292 url = idx.data(HistoryModel.UrlStringRole)
289 currentFrequency = 0 293 currentFrequency = 0
290 if url in self.__historyDict: 294 if url in self.__historyDict:
291 row = self.__filteredRows.index(HistoryData(self.__historyDict[url], -1)) 295 row = self.__filteredRows.index(
296 HistoryData(self.__historyDict[url], -1))
292 currentFrequency = self.__filteredRows[row].frequency 297 currentFrequency = self.__filteredRows[row].frequency
293 self.beginRemoveRows(QModelIndex(), row, row) 298 self.beginRemoveRows(QModelIndex(), row, row)
294 del self.__filteredRows[row] 299 del self.__filteredRows[row]
295 del self.__historyDict[url] 300 del self.__historyDict[url]
296 self.endRemoveRows() 301 self.endRemoveRows()
297 302
298 self.beginInsertRows(QModelIndex(), 0, 0) 303 self.beginInsertRows(QModelIndex(), 0, 0)
299 self.__filteredRows.insert(0, HistoryData(self.sourceModel().rowCount(), 304 self.__filteredRows.insert(
305 0, HistoryData(self.sourceModel().rowCount(),
300 self.__frequencyScore(idx) + currentFrequency)) 306 self.__frequencyScore(idx) + currentFrequency))
301 self.__historyDict[url] = self.sourceModel().rowCount() 307 self.__historyDict[url] = self.sourceModel().rowCount()
302 self.endInsertRows() 308 self.endInsertRows()
303 309
304 def __sourceRowsRemoved(self, parent, start, end): 310 def __sourceRowsRemoved(self, parent, start, end):
315 """ 321 """
316 Public method to remove entries from the model. 322 Public method to remove entries from the model.
317 323
318 @param row row of the first entry to remove (integer) 324 @param row row of the first entry to remove (integer)
319 @param count number of entries to remove (integer) 325 @param count number of entries to remove (integer)
320 @param index of the parent entry (QModelIndex) 326 @param parent index of the parent entry (QModelIndex)
321 @return flag indicating successful removal (boolean) 327 @return flag indicating successful removal (boolean)
322 """ 328 """
323 if row < 0 or \ 329 if row < 0 or \
324 count <= 0 or \ 330 count <= 0 or \
325 row + count > self.rowCount(parent) or \ 331 row + count > self.rowCount(parent) or \
328 334
329 lastRow = row + count - 1 335 lastRow = row + count - 1
330 self.sourceModel().rowsRemoved.disconnect(self.__sourceRowsRemoved) 336 self.sourceModel().rowsRemoved.disconnect(self.__sourceRowsRemoved)
331 self.beginRemoveRows(parent, row, lastRow) 337 self.beginRemoveRows(parent, row, lastRow)
332 oldCount = self.rowCount() 338 oldCount = self.rowCount()
333 start = self.sourceModel().rowCount() - self.__filteredRows[row].tailOffset 339 start = self.sourceModel().rowCount() - \
334 end = self.sourceModel().rowCount() - self.__filteredRows[lastRow].tailOffset 340 self.__filteredRows[row].tailOffset
341 end = self.sourceModel().rowCount() - \
342 self.__filteredRows[lastRow].tailOffset
335 self.sourceModel().removeRows(start, end - start + 1) 343 self.sourceModel().removeRows(start, end - start + 1)
336 self.endRemoveRows() 344 self.endRemoveRows()
337 self.sourceModel().rowsRemoved.connect(self.__sourceRowsRemoved) 345 self.sourceModel().rowsRemoved.connect(self.__sourceRowsRemoved)
338 self.__loaded = False 346 self.__loaded = False
339 if oldCount - count != self.rowCount(): 347 if oldCount - count != self.rowCount():

eric ide

mercurial