Add "Remove finished" to the download context menu

Closes #344.
This commit is contained in:
Florian Bruhin 2014-12-16 14:30:47 +01:00
parent ed013ac3cf
commit 965a1256a3
2 changed files with 42 additions and 0 deletions

View File

@ -761,6 +761,17 @@ class DownloadManager(QAbstractListModel):
return True return True
return False return False
def can_clear(self):
"""Check if there are finished downloads to clear."""
if self.downloads:
return any(download.done for download in self.downloads)
else:
return False
def clear(self):
"""Remove all finished downloads."""
self.remove_items(d for d in self.downloads if d.done)
def last_index(self): def last_index(self):
"""Get the last index in the model. """Get the last index in the model.
@ -782,6 +793,34 @@ class DownloadManager(QAbstractListModel):
self.endRemoveRows() self.endRemoveRows()
download.deleteLater() download.deleteLater()
def remove_items(self, downloads):
"""Remove an iterable of downloads."""
# On the first pass, we only generate the indices so we get the
# first/last one for beginRemoveRows.
indices = []
# We need to iterate over downloads twice, which won't work if it's a
# generator.
downloads = list(downloads)
for download in downloads:
try:
indices.append(self.downloads.index(download))
except ValueError:
# already removed
pass
if not indices:
return
indices.sort()
self.beginRemoveRows(QModelIndex(), indices[0], indices[-1])
for download in downloads:
try:
self.downloads.remove(download)
except ValueError:
# already removed
pass
else:
download.deleteLater()
self.endRemoveRows()
def headerData(self, section, orientation, role): def headerData(self, section, orientation, role):
"""Simple constant header.""" """Simple constant header."""
if (section == 0 and orientation == Qt.Horizontal and if (section == 0 and orientation == Qt.Horizontal and

View File

@ -140,6 +140,9 @@ class DownloadView(QListView):
functools.partial(self.model().remove_item, item))) functools.partial(self.model().remove_item, item)))
else: else:
actions.append(("Cancel", item.cancel)) actions.append(("Cancel", item.cancel))
if self.model().can_clear():
actions.append((None, None))
actions.append(("Remove all finished", self.model().clear))
return actions return actions
@pyqtSlot('QPoint') @pyqtSlot('QPoint')