Support QWebView::createWindow (opening windows via js)

This commit is contained in:
Florian Bruhin 2014-05-08 20:36:05 +02:00
parent b7eec48037
commit e2a2ed8850
4 changed files with 49 additions and 21 deletions

2
TODO
View File

@ -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

View File

@ -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)

View File

@ -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.

View File

@ -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.