From b91d4ee9c2598caa40f1705355d3b58f22e5edea Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 9 May 2017 22:02:30 +0200 Subject: [PATCH] Clean up :debug-webaction --- doc/help/commands.asciidoc | 2 +- qutebrowser/browser/browsertab.py | 17 ++++++++- qutebrowser/browser/commands.py | 35 ++++--------------- qutebrowser/browser/webengine/webenginetab.py | 10 +++--- qutebrowser/browser/webkit/webkittab.py | 3 ++ 5 files changed, 32 insertions(+), 35 deletions(-) diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 1a76e7fc8..c79d83d08 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -1655,7 +1655,7 @@ Syntax: +:debug-webaction 'action'+ Execute a webaction. -See http://doc.qt.io/qt-5/qwebpage.html#WebAction-enum for the available actions. +Available actions: http://doc.qt.io/archives/qt-5.5/qwebpage.html#WebAction-enum (WebKit) http://doc.qt.io/qt-5/qwebenginepage.html#WebAction-enum (WebEngine) ==== positional arguments * +'action'+: The action to execute, e.g. MoveToNextChar. diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index 8e705d9df..62fb67453 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -105,7 +105,15 @@ class TabData: class AbstractAction: - """Attribute of AbstractTab for Qt WebActions.""" + """Attribute of AbstractTab for Qt WebActions. + + Class attributes (overridden by subclasses): + action_class: The class actions are defined on (QWeb{Engine,}Page) + action_base: The type of the actions (QWeb{Engine,}Page.WebAction) + """ + + action_class = None + action_base = None def __init__(self): self._widget = None @@ -118,6 +126,13 @@ class AbstractAction: """Save the current page.""" raise NotImplementedError + def run_string(self, name): + """Run a webaction based on its name.""" + member = getattr(self.action_class, name, None) + if not isinstance(member, self.action_base): + raise WebTabError("{} is not a valid web action!".format(name)) + self._widget.triggerPageAction(member) + class AbstractPrinting: diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 12de21b33..6b34e4d30 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -28,14 +28,6 @@ from PyQt5.QtWidgets import QApplication, QTabBar from PyQt5.QtCore import Qt, QUrl, QEvent, QUrlQuery from PyQt5.QtGui import QKeyEvent from PyQt5.QtPrintSupport import QPrintDialog, QPrintPreviewDialog -try: - from PyQt5.QtWebKitWidgets import QWebPage -except ImportError: - QWebPage = None -try: - from PyQt5.QtWebEngineWidgets import QWebEnginePage -except ImportError: - QWebEnginePage = None import pygments import pygments.lexers import pygments.formatters @@ -1905,33 +1897,20 @@ class CommandDispatcher: def debug_webaction(self, action, count=1): """Execute a webaction. - See http://doc.qt.io/qt-5/qwebpage.html#WebAction-enum for the - available actions. + Available actions: + http://doc.qt.io/archives/qt-5.5/qwebpage.html#WebAction-enum (WebKit) + http://doc.qt.io/qt-5/qwebenginepage.html#WebAction-enum (WebEngine) Args: action: The action to execute, e.g. MoveToNextChar. count: How many times to repeat the action. """ tab = self._current_widget() - - if tab.backend == usertypes.Backend.QtWebKit: - assert QWebPage is not None - member = getattr(QWebPage, action, None) - base = QWebPage.WebAction - elif tab.backend == usertypes.Backend.QtWebEngine: - assert QWebEnginePage is not None - member = getattr(QWebEnginePage, action, None) - base = QWebEnginePage.WebAction - - if not isinstance(member, base): - raise cmdexc.CommandError("{} is not a valid web action!".format( - action)) - for _ in range(count): - # This whole command is backend-specific anyways, so it makes no - # sense to introduce some API for this. - # pylint: disable=protected-access - tab._widget.triggerPageAction(member) + try: + tab.action.run_string(action) + except browsertab.WebTabError as e: + raise cmdexc.CommandError(str(e)) @cmdutils.register(instance='command-dispatcher', scope='window', maxsplit=0, no_cmd_split=True) diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 5d2c171c3..af93786d6 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -79,17 +79,17 @@ _JS_WORLD_MAP = { class WebEngineAction(browsertab.AbstractAction): - """QtWebKit implementations related to web actions.""" + """QtWebEngine implementations related to web actions.""" - def _action(self, action): - self._widget.triggerPageAction(action) + action_class = QWebEnginePage + action_base = QWebEnginePage.WebAction def exit_fullscreen(self): - self._action(QWebEnginePage.ExitFullScreen) + self._widget.triggerPageAction(QWebEnginePage.ExitFullScreen) def save_page(self): """Save the current page.""" - self._action(QWebEnginePage.SavePage) + self._widget.triggerPageAction(QWebEnginePage.SavePage) class WebEnginePrinting(browsertab.AbstractPrinting): diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index 3de652887..daf46a503 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -57,6 +57,9 @@ class WebKitAction(browsertab.AbstractAction): """QtWebKit implementations related to web actions.""" + action_class = QWebPage + action_base = QWebPage.WebAction + def exit_fullscreen(self): raise browsertab.UnsupportedOperationError