qutebrowser/qutebrowser/browser/downloadview.py

183 lines
6.2 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:
2016-01-04 07:12:39 +01:00
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-06-12 08:02:44 +02:00
#
# 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/>.
"""The ListView to display downloads in."""
import functools
import sip
from PyQt5.QtCore import pyqtSlot, QSize, Qt, QTimer
from PyQt5.QtWidgets import QListView, QSizePolicy, QMenu
2014-06-12 08:02:44 +02:00
from qutebrowser.browser import downloads
2014-08-26 19:10:14 +02:00
from qutebrowser.config import style
2014-10-08 22:20:38 +02:00
from qutebrowser.utils import qtutils, utils, objreg
def update_geometry(obj):
2015-03-26 07:08:13 +01:00
"""Weird WORKAROUND for some weird PyQt bug (probably).
This actually should be a method of DownloadView, but for some reason the
rowsInserted/rowsRemoved signals don't get disconnected from this method
when the DownloadView is deleted from Qt (e.g. by closing a window).
Here we check if obj ("self") was deleted and just ignore the event if so.
Original bug: https://github.com/The-Compiler/qutebrowser/issues/167
Workaround bug: https://github.com/The-Compiler/qutebrowser/issues/171
"""
def _update_geometry():
"""Actually update the geometry if the object still exists."""
if sip.isdeleted(obj):
return
obj.updateGeometry()
# If we don't use a singleShot QTimer, the geometry isn't updated correctly
# and won't include the new item.
QTimer.singleShot(0, _update_geometry)
2014-06-12 08:02:44 +02:00
class DownloadView(QListView):
"""QListView which shows currently running downloads as a bar.
Attributes:
_menu: The QMenu which is currently displayed.
_model: The currently set model.
"""
2014-06-12 17:56:28 +02:00
2014-06-13 07:39:47 +02:00
STYLESHEET = """
2014-08-28 17:47:40 +02:00
QListView {
2015-10-16 17:52:02 +02:00
background-color: {{ color['downloads.bg.bar'] }};
2015-10-16 19:02:57 +02:00
font: {{ font['downloads'] }};
2014-08-28 17:47:40 +02:00
}
2014-06-13 23:06:20 +02:00
2014-08-28 17:47:40 +02:00
QListView::item {
2014-06-13 23:06:20 +02:00
padding-right: 2px;
2014-08-28 17:47:40 +02:00
}
2014-06-13 07:39:47 +02:00
"""
def __init__(self, win_id, parent=None):
2014-06-12 08:02:44 +02:00
super().__init__(parent)
2014-08-26 19:10:14 +02:00
style.set_register_stylesheet(self)
self.setResizeMode(QListView.Adjust)
2014-06-13 22:53:56 +02:00
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
2014-06-12 10:20:42 +02:00
self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)
self.setFocusPolicy(Qt.NoFocus)
2014-06-12 08:02:44 +02:00
self.setFlow(QListView.LeftToRight)
2014-11-14 19:33:18 +01:00
self.setSpacing(1)
self._menu = None
model = objreg.get('download-manager', scope='window', window=win_id)
model.rowsInserted.connect(functools.partial(update_geometry, self))
model.rowsRemoved.connect(functools.partial(update_geometry, self))
2014-11-15 20:07:19 +01:00
model.dataChanged.connect(functools.partial(update_geometry, self))
self.setModel(model)
2014-06-12 13:05:43 +02:00
self.setWrapping(True)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_context_menu)
self.clicked.connect(self.on_clicked)
def __repr__(self):
model = self.model()
if model is None:
count = 'None'
else:
count = model.rowCount()
return utils.get_repr(self, count=count)
@pyqtSlot('QModelIndex')
def on_clicked(self, index):
"""Handle clicking of an item.
Args:
index: The QModelIndex of the clicked item.
"""
if not index.isValid():
return
item = self.model().data(index, downloads.ModelRole.item)
if item.done and item.successful:
item.open_file()
self.model().remove_item(item)
def _get_menu_actions(self, item):
"""Get the available context menu actions for a given DownloadItem.
Args:
item: The DownloadItem to get the actions for, or None.
Return:
A list of either:
- (QAction, callable) tuples.
2015-03-31 20:49:29 +02:00
- (None, None) for a separator
"""
model = self.model()
actions = []
if item is None:
pass
elif item.done:
if item.successful:
actions.append(("Open", item.open_file))
else:
actions.append(("Retry", item.retry))
actions.append(("Remove",
functools.partial(model.remove_item, item)))
else:
actions.append(("Cancel", item.cancel))
if model.can_clear():
actions.append((None, None))
actions.append(("Remove all finished", model.download_clear))
return actions
@pyqtSlot('QPoint')
def show_context_menu(self, point):
"""Show the context menu."""
index = self.indexAt(point)
if index.isValid():
item = self.model().data(index, downloads.ModelRole.item)
else:
item = None
2014-06-17 07:17:21 +02:00
self._menu = QMenu(self)
actions = self._get_menu_actions(item)
for (name, handler) in actions:
if name is None and handler is None:
self._menu.addSeparator()
else:
action = self._menu.addAction(name)
action.triggered.connect(handler)
if actions:
self._menu.popup(self.viewport().mapToGlobal(point))
2014-06-12 13:05:43 +02:00
def minimumSizeHint(self):
2014-06-12 17:56:28 +02:00
"""Override minimumSizeHint so the size is correct in a layout."""
2014-06-12 13:05:43 +02:00
return self.sizeHint()
def sizeHint(self):
2014-06-12 17:56:28 +02:00
"""Return sizeHint based on the view contents."""
2014-11-14 19:33:26 +01:00
idx = self.model().last_index()
bottom = self.visualRect(idx).bottom()
if bottom != -1:
margins = self.contentsMargins()
height = (bottom + margins.top() + margins.bottom() +
2014-12-16 15:09:47 +01:00
2 * self.spacing())
size = QSize(0, height)
2014-06-12 13:05:43 +02:00
else:
2014-06-21 16:42:58 +02:00
size = QSize(0, 0)
qtutils.ensure_valid(size)
2014-06-21 16:42:58 +02:00
return size