From 78f425c98b9bfe483bed16b45cf511bcf78c8809 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 4 Jul 2016 16:12:58 +0200 Subject: [PATCH] Add AbstractTab.data --- qutebrowser/browser/commands.py | 2 +- qutebrowser/browser/tab.py | 32 ++++++++++++++++++++++--- qutebrowser/browser/webkit/webview.py | 1 - qutebrowser/mainwindow/tabbedbrowser.py | 4 ++-- tests/unit/browser/test_tab.py | 16 +++++++++++++ 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index f0e80d702..a0a550204 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -357,7 +357,7 @@ class CommandDispatcher: new_tabbed_browser.setTabIcon(idx, curtab.icon()) if config.get('tabs', 'tabs-are-windows'): new_tabbed_browser.window().setWindowIcon(curtab.icon()) - newtab.keep_icon = True + newtab.data.keep_icon = True newtab.zoom.set_factor(curtab.zoom.factor()) newtab.history.deserialize(curtab.history.serialize()) return newtab diff --git a/qutebrowser/browser/tab.py b/qutebrowser/browser/tab.py index 2585e462d..384e38282 100644 --- a/qutebrowser/browser/tab.py +++ b/qutebrowser/browser/tab.py @@ -63,6 +63,34 @@ class WrapperLayout(QLayout): self._widget.setGeometry(r) +class TabData(QObject): + + """A simple namespace with a fixed set of attributes. + + Attributes: + keep_icon: Whether the (e.g. cloned) icon should not be cleared on page + load. + """ + + def __init__(self): + self._data = {'keep_icon': False} + + def __getattr__(self, attr): + if attr.startswith('_'): + return super().__getattr__(attr) + try: + return self._data[attr] + except KeyError: + raise AttributeError(attr) + + def __setattr__(self, attr, value): + if attr.startswith('_'): + return super().__setattr__(attr, value) + if attr not in self._data: + raise AttributeError("Can't set unknown attribute {!r}".format(attr)) + self._data[attr] = value + + class AbstractSearch(QObject): """Attribute of AbstractTab for doing searches. @@ -381,8 +409,6 @@ class AbstractTab(QWidget): We use this to unify QWebView and QWebEngineView. Attributes: - keep_icon: Whether the (e.g. cloned) icon should not be cleared on page - load. history: The AbstractHistory for the current tab. for properties, see WebView/WebEngineView docs. @@ -416,9 +442,9 @@ class AbstractTab(QWidget): # self.caret = AbstractCaret(win_id=win_id, tab=self, parent=self) # self.zoom = AbstractZoom(win_id=win_id) # self.search = AbstractSearch(parent=self) + self.data = TabData() self._layout = None self._widget = None - self.keep_icon = False # FIXME:refactor get rid of this? self.backend = None def _set_widget(self, widget): diff --git a/qutebrowser/browser/webkit/webview.py b/qutebrowser/browser/webkit/webview.py index 9da45ed8a..f5450cbee 100644 --- a/qutebrowser/browser/webkit/webview.py +++ b/qutebrowser/browser/webkit/webview.py @@ -98,7 +98,6 @@ class WebView(QWebView): self._old_scroll_pos = (-1, -1) self._has_ssl_errors = False self._ignore_wheel_event = False - self.keep_icon = False self._set_bg_color() self.cur_url = QUrl() self.progress = 0 diff --git a/qutebrowser/mainwindow/tabbedbrowser.py b/qutebrowser/mainwindow/tabbedbrowser.py index fe2f5d61a..1ff3656a4 100644 --- a/qutebrowser/mainwindow/tabbedbrowser.py +++ b/qutebrowser/mainwindow/tabbedbrowser.py @@ -464,8 +464,8 @@ class TabbedBrowser(tabwidget.TabWidget): # We can get signals for tabs we already deleted... return self.update_tab_title(idx) - if tab.keep_icon: - tab.keep_icon = False + if tab.data.keep_icon: + tab.data.keep_icon = False else: self.setTabIcon(idx, QIcon()) if (config.get('tabs', 'tabs-are-windows') and diff --git a/tests/unit/browser/test_tab.py b/tests/unit/browser/test_tab.py index 2ac749df3..73850fd21 100644 --- a/tests/unit/browser/test_tab.py +++ b/tests/unit/browser/test_tab.py @@ -47,3 +47,19 @@ def test_tab(qtbot, view): assert tab_w.history.tab is tab_w assert tab_w.history.history is w.history() assert w.parent() is tab_w + + +class TestTabData: + + def test_known_attr(self): + data = tab.TabData() + assert data.keep_icon == False + data.keep_icon = True + assert data.keep_icon == True + + def test_unknown_attr(self): + data = tab.TabData() + with pytest.raises(AttributeError): + data.bar = 42 + with pytest.raises(AttributeError): + data.bar