qutebrowser/qutebrowser/widgets/downloads.py

81 lines
2.8 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/>.
"""The ListView to display downloads in."""
from PyQt5.QtCore import pyqtSlot, QSize, Qt
from PyQt5.QtWidgets import QListView, QSizePolicy, QMenu
2014-06-12 08:02:44 +02:00
from qutebrowser.models.downloadmodel import DownloadModel, Role
2014-06-13 07:39:47 +02:00
from qutebrowser.config.style import set_register_stylesheet
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 = """
QListView {{
2014-06-13 07:41:51 +02:00
{color[downloads.bg.bar]}
{font[downloads]}
2014-06-13 07:39:47 +02:00
}}
"""
2014-06-12 08:02:44 +02:00
def __init__(self, parent=None):
super().__init__(parent)
2014-06-13 07:39:47 +02:00
set_register_stylesheet(self)
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
2014-06-12 08:02:44 +02:00
self._model = DownloadModel()
2014-06-12 13:05:43 +02:00
self._model.rowsInserted.connect(self.updateGeometry)
self._model.rowsRemoved.connect(self.updateGeometry)
2014-06-12 08:02:44 +02:00
self.setModel(self._model)
2014-06-12 13:05:43 +02:00
self.setWrapping(True)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_context_menu)
@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, Role.item)
self._menu = QMenu()
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."""
idx = self.model().last_index()
height = self.visualRect(idx).bottom()
2014-06-12 13:05:43 +02:00
if height != -1:
return QSize(0, height + 2)
else:
return QSize(0, 0)