Add miscwidgets.WrapperLayout.wrap

This makes it easier for an user of WrapperLayout to wrap a widget.
This commit is contained in:
Florian Bruhin 2016-08-04 18:35:35 +02:00
parent 57ed5ae876
commit 029ea2e5a7
6 changed files with 25 additions and 13 deletions

View File

@ -475,7 +475,7 @@ class AbstractTab(QWidget):
# self.search = AbstractSearch(parent=self)
# self.printing = AbstractPrinting()
self.data = TabData()
self._layout = None
self._layout = miscwidgets.WrapperLayout(self)
self._widget = None
self._progress = 0
self._has_ssl_errors = False
@ -484,8 +484,8 @@ class AbstractTab(QWidget):
def _set_widget(self, widget):
# pylint: disable=protected-access
self._layout = miscwidgets.WrapperLayout(widget, self)
self._widget = widget
self._layout.wrap(self, widget)
self.history._history = widget.history()
self.scroller._init_widget(widget)
self.caret._widget = widget
@ -493,8 +493,6 @@ class AbstractTab(QWidget):
self.search._widget = widget
self.printing._widget = widget
widget.mouse_wheel_zoom.connect(self.zoom._on_mouse_wheel_zoom)
widget.setParent(self)
self.setFocusProxy(widget)
def _set_load_status(self, val):
"""Setter for load_status."""

View File

@ -59,14 +59,12 @@ class AbstractWebInspector(QWebInspector):
def __init__(self, parent=None):
super().__init__(parent)
self._widget = None
self._layout = None
self._layout = miscwidgets.WrapperLayout(self)
self._load_state_geometry()
def _set_widget(self, widget):
self._widget = widget
self._layout = miscwidgets.WrapperLayout(self._widget, self)
self.setFocusProxy(self._widget)
self._widget.setParent(self)
self._layout.wrap(self, widget)
def _load_state_geometry(self):
"""Load the geometry from the state file."""

View File

@ -235,9 +235,9 @@ class WrapperLayout(QLayout):
easily be accidentally accessed.
"""
def __init__(self, widget, parent=None):
def __init__(self, parent=None):
super().__init__(parent)
self._widget = widget
self._widget = None
def addItem(self, _widget):
raise AssertionError("Should never be called!")
@ -254,3 +254,9 @@ class WrapperLayout(QLayout):
def setGeometry(self, rect):
self._widget.setGeometry(rect)
def wrap(self, container, widget):
"""Wrap the given widget in the given container."""
self._widget = widget
container.setFocusProxy(widget)
widget.setParent(container)

View File

@ -27,7 +27,7 @@ from unittest import mock
from PyQt5.QtCore import pyqtSignal, QPoint, QProcess, QObject
from PyQt5.QtNetwork import (QNetworkRequest, QAbstractNetworkCache,
QNetworkCacheMetaData)
from PyQt5.QtWidgets import QCommonStyle, QLineEdit
from PyQt5.QtWidgets import QCommonStyle, QLineEdit, QWidget
from qutebrowser.browser import browsertab
from qutebrowser.browser.webkit import history
@ -253,6 +253,8 @@ class FakeWebTab(browsertab.AbstractTab):
self._url = url
self._progress = progress
self.scroller = FakeWebTabScroller(self, scroll_pos_perc)
wrapped = QWidget()
self._layout.wrap(self, wrapped)
def url(self):
return self._url

View File

@ -96,7 +96,6 @@ def tab(request, default_config, qtbot, tab_registry, cookiejar_and_cache):
def test_tab(qtbot, view, config_stub, tab_registry):
tab_w = browsertab.AbstractTab(win_id=0)
qtbot.add_widget(tab_w)
tab_w.show()
assert tab_w.win_id == 0
assert tab_w._widget is None
@ -118,6 +117,9 @@ def test_tab(qtbot, view, config_stub, tab_registry):
assert tab_w.history._history is view.history()
assert view.parent() is tab_w
tab_w.show()
qtbot.waitForWindowShown(tab_w)
class TestJs:

View File

@ -89,8 +89,14 @@ class TestWrapperLayout:
parent = QWidget()
qtbot.add_widget(wrapped)
qtbot.add_widget(parent)
miscwidgets.WrapperLayout(wrapped, parent)
layout = miscwidgets.WrapperLayout(parent)
layout.wrap(parent, wrapped)
parent.wrapped = wrapped
return parent
def test_size_hint(self, container):
assert container.sizeHint() == QSize(23, 42)
def test_wrapped(self, container):
assert container.wrapped.parent() is container
assert container.focusProxy() is container.wrapped