qutebrowser/qutebrowser/models/downloadmodel.py

87 lines
3.1 KiB
Python
Raw Normal View History

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."""
from PyQt5.QtCore import (pyqtSlot, Qt, QVariant, QAbstractListModel,
QModelIndex)
2014-06-12 08:02:44 +02:00
from PyQt5.QtWidgets import QApplication
2014-06-12 21:43:30 +02:00
import qutebrowser.config.config as config
2014-06-12 08:02:44 +02:00
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
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."""
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 index.parent().isValid() or index.column() != 0:
return QVariant()
2014-06-12 21:43:30 +02:00
2014-06-12 08:02:44 +02:00
try:
item = self.downloadmanager.downloads[index.row()]
except IndexError:
return QVariant()
2014-06-12 21:43:30 +02:00
if role == Qt.DisplayRole:
2014-06-12 23:29:34 +02:00
data = str(item)
2014-06-12 21:43:30 +02:00
elif role == Qt.ForegroundRole:
2014-06-13 07:41:51 +02:00
data = config.get('colors', 'downloads.fg')
2014-06-12 21:43:30 +02:00
elif role == Qt.BackgroundRole:
data = item.bg_color()
else:
2014-06-12 21:43:30 +02:00
data = QVariant()
return data
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)