Move history triggering out of WebView

This commit is contained in:
Florian Bruhin 2016-08-10 13:14:38 +02:00
parent eed3460317
commit 77531d09df
5 changed files with 22 additions and 16 deletions

View File

@ -455,6 +455,7 @@ class AbstractTab(QWidget):
url_changed = pyqtSignal(QUrl)
shutting_down = pyqtSignal()
contents_size_changed = pyqtSignal(QSizeF)
add_history_item = pyqtSignal(QUrl, QUrl, str) # url, requested url, title
def __init__(self, win_id, parent=None):
self.win_id = win_id
@ -533,6 +534,13 @@ class AbstractTab(QWidget):
if not self.title():
self.title_changed.emit(self.url().toDisplayString())
@pyqtSlot()
def _on_history_trigger(self):
"""Emit add_history_item when triggered by backend-specific signal."""
url = self.url()
requested_url = self.url(requested=True)
self.add_history_item.emit(url, requested_url, self.title())
@pyqtSlot(int)
def _on_load_progress(self, perc):
self._progress = perc

View File

@ -22,7 +22,7 @@
import time
import collections
from PyQt5.QtCore import pyqtSignal, QUrl, QObject
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QUrl, QObject
from PyQt5.QtWebKit import QWebHistoryInterface
from qutebrowser.commands import cmdutils
@ -284,6 +284,17 @@ class WebHistory(QObject):
self._saved_count = 0
self.cleared.emit()
@pyqtSlot(QUrl, QUrl, str)
def add_from_tab(self, url, requested_url, title):
"""Add a new history entry as slot, called from a BrowserTab."""
no_formatting = QUrl.UrlFormattingOption(0)
if (requested_url.isValid() and
not requested_url.matches(url, no_formatting)):
# If the url of the page is different than the url of the link
# originally clicked, save them both.
self.add_url(requested_url, title, redirect=True)
self.add_url(url, title)
def add_url(self, url, title="", *, redirect=False, atime=None):
"""Called by WebKit when a URL should be added to the history.

View File

@ -634,3 +634,4 @@ class WebKitTab(browsertab.AbstractTab):
view.iconChanged.connect(self._on_webkit_icon_changed)
page.frameCreated.connect(self._on_frame_created)
frame.contentsSizeChanged.connect(self._on_contents_size_changed)
frame.initialLayoutCompleted.connect(self._on_history_trigger)

View File

@ -94,26 +94,11 @@ class WebView(QWebView):
self.setContextMenuPolicy(Qt.PreventContextMenu)
objreg.get('config').changed.connect(self.on_config_changed)
@pyqtSlot()
def on_initial_layout_completed(self):
"""Add url to history now that we have displayed something."""
history = objreg.get('web-history')
no_formatting = QUrl.UrlFormattingOption(0)
orig_url = self.page().mainFrame().requestedUrl()
if (orig_url.isValid() and
not orig_url.matches(self.url(), no_formatting)):
# If the url of the page is different than the url of the link
# originally clicked, save them both.
history.add_url(orig_url, self.title(), redirect=True)
history.add_url(self.url(), self.title())
def _init_page(self):
"""Initialize the QWebPage used by this view."""
page = webpage.BrowserPage(self.win_id, self._tab_id, self)
self.setPage(page)
page.mainFrame().loadFinished.connect(self.on_load_finished)
page.mainFrame().initialLayoutCompleted.connect(
self.on_initial_layout_completed)
return page
def __repr__(self):

View File

@ -198,6 +198,7 @@ class TabbedBrowser(tabwidget.TabWidget):
tab.window_close_requested.connect(
functools.partial(self.on_window_close_requested, tab))
tab.new_tab_requested.connect(self.tabopen)
tab.add_history_item.connect(objreg.get('web-history').add_from_tab)
def current_url(self):
"""Get the URL of the current tab.