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."""
|
|
|
|
|
2014-10-08 20:29:29 +02:00
|
|
|
import functools
|
|
|
|
|
|
|
|
import sip
|
2014-10-15 21:30:04 +02:00
|
|
|
from PyQt5.QtCore import pyqtSlot, QSize, Qt, QTimer
|
2014-06-13 12:19:30 +02:00
|
|
|
from PyQt5.QtWidgets import QListView, QSizePolicy, QMenu
|
2014-06-12 08:02:44 +02:00
|
|
|
|
2014-10-08 20:18: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
|
2014-10-08 20:29:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
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).
|
|
|
|
|
2014-10-08 21:12:21 +02:00
|
|
|
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
|
2014-10-08 20:29:29 +02:00
|
|
|
"""
|
2014-10-15 21:30:04 +02:00
|
|
|
|
|
|
|
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):
|
|
|
|
|
2014-06-13 12:19:30 +02:00
|
|
|
"""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
|
|
|
"""
|
|
|
|
|
2014-11-11 21:36:47 +01: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)
|
2014-06-13 20:19:27 +02:00
|
|
|
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)
|
2014-06-13 12:19:30 +02:00
|
|
|
self._menu = None
|
2014-11-11 21:36:47 +01:00
|
|
|
model = objreg.get('download-manager', scope='window', window=win_id)
|
2014-10-08 20:29:29 +02:00
|
|
|
model.rowsInserted.connect(functools.partial(update_geometry, self))
|
|
|
|
model.rowsRemoved.connect(functools.partial(update_geometry, self))
|
2014-10-08 20:18:44 +02:00
|
|
|
self.setModel(model)
|
2014-06-12 13:05:43 +02:00
|
|
|
self.setWrapping(True)
|
2014-06-13 12:19:30 +02:00
|
|
|
self.setContextMenuPolicy(Qt.CustomContextMenu)
|
|
|
|
self.customContextMenuRequested.connect(self.show_context_menu)
|
|
|
|
|
2014-06-17 06:37:56 +02:00
|
|
|
def __repr__(self):
|
2014-10-08 17:19:51 +02:00
|
|
|
model = self.model()
|
|
|
|
if model is None:
|
|
|
|
count = 'None'
|
|
|
|
else:
|
|
|
|
count = model.rowCount()
|
|
|
|
return utils.get_repr(self, count=count)
|
2014-06-17 06:37:56 +02:00
|
|
|
|
2014-06-13 12:19:30 +02:00
|
|
|
@pyqtSlot('QPoint')
|
|
|
|
def show_context_menu(self, point):
|
|
|
|
"""Show the context menu."""
|
|
|
|
index = self.indexAt(point)
|
|
|
|
if not index.isValid():
|
|
|
|
return
|
2014-10-08 20:18:44 +02:00
|
|
|
item = self.model().data(index, downloads.ModelRole.item)
|
2014-06-17 07:17:21 +02:00
|
|
|
self._menu = QMenu(self)
|
2014-06-13 12:19:30 +02:00
|
|
|
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."""
|
2014-10-08 20:18:44 +02:00
|
|
|
try:
|
|
|
|
idx = self.model().last_index()
|
|
|
|
except RuntimeError:
|
|
|
|
pass
|
2014-06-13 20:19:00 +02:00
|
|
|
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)
|
2014-08-26 19:23:06 +02:00
|
|
|
qtutils.ensure_valid(size)
|
2014-06-21 16:42:58 +02:00
|
|
|
return size
|