qutebrowser/qutebrowser/widgets/downloads.py

132 lines
4.3 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/>.
"""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):
"""WORKAROUND
This is a horrible 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 {
2014-08-28 17:54:11 +02:00
{{ color['downloads.bg.bar'] }}
{{ 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)
2014-06-12 08:02:44 +02:00
self.setFlow(QListView.LeftToRight)
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))
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)
def __repr__(self):
model = self.model()
if model is None:
count = 'None'
else:
count = model.rowCount()
return utils.get_repr(self, count=count)
@pyqtSlot('QPoint')
def show_context_menu(self, point):
"""Show the context menu."""
index = self.indexAt(point)
if not index.isValid():
return
item = self.model().data(index, downloads.ModelRole.item)
2014-06-17 07:17:21 +02:00
self._menu = QMenu(self)
cancel = self._menu.addAction("Cancel")
cancel.triggered.connect(item.cancel)
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."""
try:
idx = self.model().last_index()
except RuntimeError:
pass
height = self.visualRect(idx).bottom()
2014-06-12 13:05:43 +02:00
if height != -1:
2014-06-21 16:42:58 +02:00
size = QSize(0, height + 2)
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