2014-06-12 08:02:44 +02:00
|
|
|
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser 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 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser 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 qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Glue code for qutebrowser.{browser,widgets}.download."""
|
|
|
|
|
2014-06-12 10:20:10 +02:00
|
|
|
from PyQt5.QtCore import (pyqtSlot, Qt, QVariant, QAbstractListModel,
|
|
|
|
QModelIndex)
|
2014-06-12 08:02:44 +02:00
|
|
|
from PyQt5.QtWidgets import QApplication
|
|
|
|
|
|
|
|
|
|
|
|
class DownloadModel(QAbstractListModel):
|
|
|
|
|
2014-06-12 17:56:28 +02:00
|
|
|
"""Glue model to show downloads in a QListView.
|
|
|
|
|
|
|
|
Glue between qutebrowser.browser.download (DownloadManager) and
|
|
|
|
qutebrowser.widgets.download (DownloadView).
|
|
|
|
"""
|
|
|
|
|
2014-06-12 08:02:44 +02:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
|
|
|
self.downloadmanager = QApplication.instance().downloadmanager
|
2014-06-12 10:20:10 +02:00
|
|
|
self.downloadmanager.download_about_to_be_added.connect(
|
|
|
|
lambda idx: self.beginInsertRows(QModelIndex(), idx, idx))
|
|
|
|
self.downloadmanager.download_added.connect(self.endInsertRows)
|
|
|
|
self.downloadmanager.download_about_to_be_finished.connect(
|
|
|
|
lambda idx: self.beginRemoveRows(QModelIndex(), idx, idx))
|
|
|
|
self.downloadmanager.download_finished.connect(self.endRemoveRows)
|
|
|
|
self.downloadmanager.data_changed.connect(self.on_data_changed)
|
|
|
|
|
|
|
|
@pyqtSlot(int)
|
|
|
|
def on_data_changed(self, idx):
|
2014-06-12 17:56:28 +02:00
|
|
|
"""Update view when DownloadManager data changed."""
|
2014-06-12 10:20:10 +02:00
|
|
|
model_idx = self.index(idx, 0)
|
|
|
|
self.dataChanged.emit(model_idx, model_idx)
|
2014-06-12 08:02:44 +02:00
|
|
|
|
|
|
|
def headerData(self, section, orientation, role):
|
2014-06-12 17:56:28 +02:00
|
|
|
"""Simple constant header."""
|
2014-06-12 08:02:44 +02:00
|
|
|
if (section == 0 and orientation == Qt.Horizontal and
|
|
|
|
role == Qt.DisplayRole):
|
|
|
|
return "Downloads"
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def data(self, index, role):
|
2014-06-12 17:56:28 +02:00
|
|
|
"""Download data from DownloadManager."""
|
2014-06-12 08:02:44 +02:00
|
|
|
if not index.isValid():
|
|
|
|
return QVariant()
|
|
|
|
elif role != Qt.DisplayRole:
|
|
|
|
return QVariant()
|
|
|
|
elif index.parent().isValid() or index.column() != 0:
|
|
|
|
return QVariant()
|
|
|
|
try:
|
|
|
|
item = self.downloadmanager.downloads[index.row()]
|
|
|
|
except IndexError:
|
|
|
|
return QVariant()
|
2014-06-12 10:20:27 +02:00
|
|
|
perc = item.percentage
|
|
|
|
if perc is None:
|
|
|
|
return QVariant()
|
|
|
|
else:
|
|
|
|
return str(round(perc)) # FIXME
|
2014-06-12 08:02:44 +02:00
|
|
|
|
|
|
|
def rowCount(self, parent):
|
2014-06-12 17:56:28 +02:00
|
|
|
"""Get count of active downloads."""
|
2014-06-12 08:02:44 +02:00
|
|
|
if parent.isValid():
|
|
|
|
# We don't have children
|
|
|
|
return 0
|
|
|
|
return len(self.downloadmanager.downloads)
|