diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index 996edc842..9bf9bcc69 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -23,11 +23,12 @@ import itertools from PyQt5.QtCore import pyqtSignal, pyqtSlot, QUrl, QObject, QPoint, QSizeF from PyQt5.QtGui import QIcon -from PyQt5.QtWidgets import QWidget, QLayout +from PyQt5.QtWidgets import QWidget from qutebrowser.keyinput import modeman from qutebrowser.config import config from qutebrowser.utils import utils, objreg, usertypes, message, log, qtutils +from qutebrowser.misc import miscwidgets tab_id_gen = itertools.count(0) @@ -57,35 +58,6 @@ class WebTabError(Exception): """Base class for various errors.""" -class WrapperLayout(QLayout): - - """A Qt layout which simply wraps a single widget. - - This is used so the widget is hidden behind a AbstractTab API and can't - easily be accidentally accessed. - """ - - def __init__(self, widget, parent=None): - super().__init__(parent) - self._widget = widget - - def addItem(self, _widget): - raise AssertionError("Should never be called!") - - def sizeHint(self): - return self._widget.sizeHint() - - def itemAt(self, _index): # pragma: no cover - # For some reason this sometimes gets called by Qt. - return None - - def takeAt(self, _index): - raise AssertionError("Should never be called!") - - def setGeometry(self, rect): - self._widget.setGeometry(rect) - - class TabData: """A simple namespace with a fixed set of attributes. @@ -512,7 +484,7 @@ class AbstractTab(QWidget): def _set_widget(self, widget): # pylint: disable=protected-access - self._layout = WrapperLayout(widget, self) + self._layout = miscwidgets.WrapperLayout(widget, self) self._widget = widget self.history._history = widget.history() self.scroller._init_widget(widget) diff --git a/qutebrowser/misc/miscwidgets.py b/qutebrowser/misc/miscwidgets.py index 208cef5ec..2ad184193 100644 --- a/qutebrowser/misc/miscwidgets.py +++ b/qutebrowser/misc/miscwidgets.py @@ -21,7 +21,7 @@ from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QSize from PyQt5.QtWidgets import (QLineEdit, QWidget, QHBoxLayout, QLabel, - QStyleOption, QStyle) + QStyleOption, QStyle, QLayout) from PyQt5.QtGui import QValidator, QPainter from qutebrowser.utils import utils @@ -225,3 +225,32 @@ class _FoldArrow(QWidget): def minimumSizeHint(self): """Return a sensible size.""" return QSize(8, 8) + + +class WrapperLayout(QLayout): + + """A Qt layout which simply wraps a single widget. + + This is used so the widget is hidden behind a defined API and can't + easily be accidentally accessed. + """ + + def __init__(self, widget, parent=None): + super().__init__(parent) + self._widget = widget + + def addItem(self, _widget): + raise AssertionError("Should never be called!") + + def sizeHint(self): + return self._widget.sizeHint() + + def itemAt(self, _index): # pragma: no cover + # For some reason this sometimes gets called by Qt. + return None + + def takeAt(self, _index): + raise AssertionError("Should never be called!") + + def setGeometry(self, rect): + self._widget.setGeometry(rect) diff --git a/tests/unit/misc/test_miscwidgets.py b/tests/unit/misc/test_miscwidgets.py index 5ecf7e37f..8e92176f9 100644 --- a/tests/unit/misc/test_miscwidgets.py +++ b/tests/unit/misc/test_miscwidgets.py @@ -20,11 +20,11 @@ """Test widgets in miscwidgets module.""" from unittest import mock -from PyQt5.QtCore import Qt -from PyQt5.QtWidgets import QApplication +from PyQt5.QtCore import Qt, QSize +from PyQt5.QtWidgets import QApplication, QWidget import pytest -from qutebrowser.misc.miscwidgets import CommandLineEdit +from qutebrowser.misc import miscwidgets class TestCommandLineEdit: @@ -34,7 +34,7 @@ class TestCommandLineEdit: @pytest.yield_fixture def cmd_edit(self, qtbot): """Fixture to initialize a CommandLineEdit.""" - cmd_edit = CommandLineEdit(None) + cmd_edit = miscwidgets.CommandLineEdit(None) cmd_edit.set_prompt(':') qtbot.add_widget(cmd_edit) assert cmd_edit.text() == '' @@ -73,3 +73,24 @@ class TestCommandLineEdit: """Test preventing of an invalid prompt being entered.""" qtbot.keyClicks(cmd_edit, '$hello') assert cmd_edit.text() == '' + + +class WrappedWidget(QWidget): + + def sizeHint(self): + return QSize(23, 42) + + +class TestWrapperLayout: + + @pytest.fixture + def container(self, qtbot): + wrapped = WrappedWidget() + parent = QWidget() + qtbot.add_widget(wrapped) + qtbot.add_widget(parent) + miscwidgets.WrapperLayout(wrapped, parent) + return parent + + def test_size_hint(self, container): + assert container.sizeHint() == QSize(23, 42)