From e2a2ed8850c31b8700018516b4a3fefa11777ec4 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 8 May 2014 20:36:05 +0200 Subject: [PATCH] Support QWebView::createWindow (opening windows via js) --- TODO | 2 -- qutebrowser/browser/curcommand.py | 2 +- qutebrowser/widgets/_tabbedbrowser.py | 25 ++++++++++------ qutebrowser/widgets/webview.py | 41 ++++++++++++++++++++------- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/TODO b/TODO index 3c350d713..8c3b7c112 100644 --- a/TODO +++ b/TODO @@ -79,8 +79,6 @@ Bugs https://paste.xinu.at/vw6r/ This seems to happen only with ttf-ms-fonts installed. -- Links to articles on https://plus.google.com/+SPIEGELONLINE/posts do not work - - Hinting problems on https://bugreports.qt-project.org/secure/Dashboard.jspa Style diff --git a/qutebrowser/browser/curcommand.py b/qutebrowser/browser/curcommand.py index b2815a064..04d1ceec5 100644 --- a/qutebrowser/browser/curcommand.py +++ b/qutebrowser/browser/curcommand.py @@ -121,7 +121,7 @@ class CurCommandDispatcher(QObject): newtab: True to open URL in a new tab, False otherwise. """ if newtab: - self._tabs.tabopen(url) + self._tabs.tabopen(url, background=False) else: self._tabs.currentWidget().openurl(url) diff --git a/qutebrowser/widgets/_tabbedbrowser.py b/qutebrowser/widgets/_tabbedbrowser.py index 6bb210962..48412e4d5 100644 --- a/qutebrowser/widgets/_tabbedbrowser.py +++ b/qutebrowser/widgets/_tabbedbrowser.py @@ -136,11 +136,10 @@ class TabbedBrowser(TabWidget): tab.hintmanager.openurl.connect(self.cur.openurl_slot) # misc tab.titleChanged.connect(self.on_title_changed) - tab.open_tab.connect(self.tabopen) tab.iconChanged.connect(self.on_icon_changed) @pyqtSlot(str, bool) - def tabopen(self, url, background=False): + def tabopen(self, url=None, background=None): """Open a new tab with a given url. Inner logic for tabopen and backtabopen. @@ -148,18 +147,28 @@ class TabbedBrowser(TabWidget): Args: url: The URL to open. - background: Whether to oepn the tab in the background. + background: Whether to open the tab in the background. + if None, the background-tabs setting decides. + + Return: + The opened WebView instance. """ - logging.debug("Opening {}".format(url)) - url = urlutils.qurl(url) + logging.debug("Creating new tab with url {}".format(url)) tab = WebView(self) self._connect_tab_signals(tab) self._tabs.append(tab) - self.addTab(tab, urlutils.urlstring(url)) - tab.show() - tab.openurl(url) + if url is not None: + url = urlutils.qurl(url) + self.addTab(tab, urlutils.urlstring(url)) + tab.openurl(url) + else: + self.addTab(tab, "") + if background is None: + background = config.get('general', 'background-tabs') if not background: self.setCurrentWidget(tab) + tab.show() + return tab def cntwidget(self, count=None): """Return a widget based on a count/idx. diff --git a/qutebrowser/widgets/webview.py b/qutebrowser/widgets/webview.py index 38f52bd4c..d6e0d3a22 100644 --- a/qutebrowser/widgets/webview.py +++ b/qutebrowser/widgets/webview.py @@ -49,6 +49,9 @@ class WebView(QWebView): page_: The QWebPage behind the view signal_cache: The signal cache associated with the view. hintmanager: The HintManager instance for this view. + tabbedbrowser: The TabbedBrowser this WebView is part of. + We need this rather than signals to make createWindow + work. _zoom: A NeighborList with the zoom levels. _scroll_pos: The old scroll position. _shutdown_callback: Callback to be called after shutdown. @@ -61,18 +64,15 @@ class WebView(QWebView): scroll_pos_changed: Scroll percentage of current tab changed. arg 1: x-position in %. arg 2: y-position in %. - open_tab: A new tab should be opened. - arg 1: The address to open - arg 2: Whether to open the tab in the background linkHovered: QWebPages linkHovered signal exposed. """ scroll_pos_changed = pyqtSignal(int, int) - open_tab = pyqtSignal('QUrl', bool) linkHovered = pyqtSignal(str, str, str) - def __init__(self, parent=None): + def __init__(self, parent): super().__init__(parent) + self.tabbedbrowser = parent self._scroll_pos = (-1, -1) self._shutdown_callback = None self._open_target = Target.normal @@ -219,14 +219,11 @@ class WebView(QWebView): Args: url: The url to handle, as string or QUrl. - - Emit: - open_tab: Emitted if window should be opened in a new tab. """ if self._open_target == Target.tab: - self.open_tab.emit(url, False) + self.tabbedbrowser.tabopen(url, False) elif self._open_target == Target.bgtab: - self.open_tab.emit(url, True) + self.tabbedbrowser.tabopen(url, True) else: self.openurl(url) @@ -267,6 +264,30 @@ class WebView(QWebView): logging.debug("Setting force target to {}/{}".format(target, t)) self._force_open_target = t + def createWindow(self, wintype): + """Called by Qt when a page wants to create a new window. + + This function is called from the createWindow() method of the + associated QWebPage, each time the page wants to create a new window of + the given type. This might be the result, for example, of a JavaScript + request to open a document in a new window. + + Args: + wintype: This enum describes the types of window that can be + created by the createWindow() function. + + QWebPage::WebBrowserWindow: The window is a regular web + browser window. + QWebPage::WebModalDialog: The window acts as modal dialog. + + Return: + The new QWebView object. + """ + if wintype == QWebPage.WebModalDialog: + logging.warn("WebModalDialog requested, but we don't support " + "that!") + return self.tabbedbrowser.tabopen() + def paintEvent(self, e): """Extend paintEvent to emit a signal if the scroll position changed.