qutebrowser/qutebrowser/models/downloadmodel.py

109 lines
3.7 KiB
Python
Raw Normal View History

2014-06-19 09:04:37 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
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
2014-08-26 19:10:14 +02:00
from qutebrowser.config import config
from qutebrowser.utils import usertypes, qtutils, utils
2014-08-26 19:10:14 +02:00
Role = usertypes.enum('Role', 'item', start=Qt.UserRole, is_int=True)
2014-06-12 21:43:30 +02:00
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)
2014-09-23 23:05:55 +02:00
download_manager = utils.get_object('download-manager')
download_manager.download_about_to_be_added.connect(
lambda idx: self.beginInsertRows(QModelIndex(), idx, idx))
2014-09-23 23:05:55 +02:00
download_manager.download_added.connect(self.endInsertRows)
download_manager.download_about_to_be_finished.connect(
lambda idx: self.beginRemoveRows(QModelIndex(), idx, idx))
2014-09-23 23:05:55 +02:00
download_manager.download_finished.connect(self.endRemoveRows)
download_manager.data_changed.connect(self.on_data_changed)
def __repr__(self):
return '<{}>'.format(self.__class__.__name__)
@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)
qtutils.ensure_valid(model_idx)
self.dataChanged.emit(model_idx, model_idx)
2014-06-12 08:02:44 +02:00
def last_index(self):
2014-06-22 23:32:49 +02:00
"""Get the last index in the model.
Return:
A (possibly invalid) QModelIndex.
"""
idx = self.index(self.rowCount() - 1)
return 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."""
qtutils.ensure_valid(index)
2014-06-21 16:42:58 +02:00
if index.parent().isValid() or index.column() != 0:
2014-06-12 08:02:44 +02:00
return QVariant()
2014-06-12 21:43:30 +02:00
2014-09-23 23:05:55 +02:00
item = utils.get_object('download-manager').downloads[index.row()]
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()
elif role == Role.item:
data = item
else:
2014-06-12 21:43:30 +02:00
data = QVariant()
return data
2014-06-12 08:02:44 +02:00
2014-06-13 23:09:24 +02:00
def flags(self, _index):
2014-06-13 17:47:03 +02:00
"""Override flags so items aren't selectable.
The default would be Qt.ItemIsEnabled | Qt.ItemIsSelectable."""
return Qt.ItemIsEnabled
def rowCount(self, parent=QModelIndex()):
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
2014-09-23 23:05:55 +02:00
return len(utils.get_object('download-manager').downloads)