Make DownloadModel a sequence

This commit is contained in:
Florian Bruhin 2016-09-10 22:45:14 +02:00
parent b949e4d73a
commit b332d22967

View File

@ -1138,9 +1138,19 @@ class DownloadModel(QAbstractListModel):
downloader.begin_remove_rows.connect(self.beginRemoveRows) downloader.begin_remove_rows.connect(self.beginRemoveRows)
downloader.end_remove_rows.connect(self.endRemoveRows) downloader.end_remove_rows.connect(self.endRemoveRows)
def _all_downloads(self):
"""Combine downloads from both downloaders."""
return self._downloader.downloads[:]
def __len__(self): def __len__(self):
return len(self._all_downloads()) return len(self._all_downloads())
def __iter__(self):
return iter(self._all_downloads())
def __getitem__(self, idx):
return self._all_downloads()[idx]
@pyqtSlot(int, int) @pyqtSlot(int, int)
def _on_data_changed(self, start, end): def _on_data_changed(self, start, end):
"""Called when a downloader's data changed. """Called when a downloader's data changed.
@ -1159,10 +1169,6 @@ class DownloadModel(QAbstractListModel):
qtutils.ensure_valid(end_index) qtutils.ensure_valid(end_index)
self.dataChanged.emit(start_index, end_index) self.dataChanged.emit(start_index, end_index)
def _all_downloads(self):
"""Combine downloads from both downloaders."""
return self._downloader.downloads[:]
def _raise_no_download(self, count): def _raise_no_download(self, count):
"""Raise an exception that the download doesn't exist. """Raise an exception that the download doesn't exist.
@ -1194,7 +1200,7 @@ class DownloadModel(QAbstractListModel):
self._raise_no_download(count) self._raise_no_download(count)
if download.done: if download.done:
if not count: if not count:
count = len(self._all_downloads()) count = len(self)
raise cmdexc.CommandError("Download {} is already done!" raise cmdexc.CommandError("Download {} is already done!"
.format(count)) .format(count))
download.cancel() download.cancel()
@ -1208,7 +1214,7 @@ class DownloadModel(QAbstractListModel):
count: The index of the download to delete. count: The index of the download to delete.
""" """
try: try:
download = self._all_downloads()[count - 1] download = self[count - 1]
except IndexError: except IndexError:
self._raise_no_download(count) self._raise_no_download(count)
if not download.successful: if not download.successful:
@ -1235,7 +1241,7 @@ class DownloadModel(QAbstractListModel):
count: The index of the download to open. count: The index of the download to open.
""" """
try: try:
download = self._all_downloads()[count - 1] download = self[count - 1]
except IndexError: except IndexError:
self._raise_no_download(count) self._raise_no_download(count)
if not download.successful: if not download.successful:
@ -1254,15 +1260,14 @@ class DownloadModel(QAbstractListModel):
""" """
if count: if count:
try: try:
download = self._all_downloads()[count - 1] download = self[count - 1]
except IndexError: except IndexError:
self._raise_no_download(count) self._raise_no_download(count)
if download.successful or not download.done: if download.successful or not download.done:
raise cmdexc.CommandError("Download {} did not fail!".format( raise cmdexc.CommandError("Download {} did not fail!".format(
count)) count))
else: else:
to_retry = [d for d in self._all_downloads() to_retry = [d for d in self if d.done and not d.successful]
if d.done and not d.successful]
if not to_retry: if not to_retry:
raise cmdexc.CommandError("No failed downloads!") raise cmdexc.CommandError("No failed downloads!")
else: else:
@ -1271,12 +1276,12 @@ class DownloadModel(QAbstractListModel):
def can_clear(self): def can_clear(self):
"""Check if there are finished downloads to clear.""" """Check if there are finished downloads to clear."""
return any(download.done for download in self._all_downloads()) return any(download.done for download in self)
@cmdutils.register(instance='download-model', scope='window') @cmdutils.register(instance='download-model', scope='window')
def download_clear(self): def download_clear(self):
"""Remove all finished downloads from the list.""" """Remove all finished downloads from the list."""
finished_items = [d for d in self._all_downloads() if d.done] finished_items = [d for d in self if d.done]
self.remove_items(finished_items) self.remove_items(finished_items)
@cmdutils.register(instance='download-model', scope='window') @cmdutils.register(instance='download-model', scope='window')
@ -1292,7 +1297,7 @@ class DownloadModel(QAbstractListModel):
self.download_clear() self.download_clear()
else: else:
try: try:
download = self._all_downloads()[count - 1] download = self[count - 1]
except IndexError: except IndexError:
self._raise_no_download(count) self._raise_no_download(count)
if not download.done: if not download.done:
@ -1308,8 +1313,7 @@ class DownloadModel(QAbstractListModel):
Return: Return:
The number of unfinished downloads. The number of unfinished downloads.
""" """
return sum(1 for download in self._all_downloads() return sum(1 for download in self if not download.done)
if not download.done)
def last_index(self): def last_index(self):
"""Get the last index in the model. """Get the last index in the model.
@ -1336,7 +1340,7 @@ class DownloadModel(QAbstractListModel):
if index.parent().isValid() or index.column() != 0: if index.parent().isValid() or index.column() != 0:
return None return None
item = self._downloader.downloads[index.row()] item = self[index.row()]
if role == Qt.DisplayRole: if role == Qt.DisplayRole:
data = str(item) data = str(item)
elif role == Qt.ForegroundRole: elif role == Qt.ForegroundRole: