Initial history implementation

This commit is contained in:
Florian Bruhin 2016-06-14 11:03:34 +02:00
parent 37c3dbbc7d
commit 5b9ae8bc85
7 changed files with 111 additions and 30 deletions

View File

@ -358,9 +358,7 @@ class CommandDispatcher:
new_tabbed_browser.window().setWindowIcon(curtab.icon())
newtab.keep_icon = True
newtab.setZoomFactor(curtab.zoomFactor())
history = qtutils.serialize(curtab.history())
qtutils.deserialize(history, newtab.history())
return newtab
newtab.history.deserialize(history = curtab.history.serialize())
@cmdutils.register(instance='command-dispatcher', scope='window')
def tab_detach(self):

View File

@ -55,6 +55,39 @@ class WrapperLayout(QLayout):
self._widget.setGeometry(r)
class AbstractHistory:
"""The history attribute of a AbstractTab."""
def __init__(self, tab):
self.tab = tab
self.widget = None
def back(self):
raise NotImplementedError
def forward(self):
raise NotImplementedError
def can_go_back(self):
raise NotImplementedError
def can_go_forward(self):
raise NotImplementedError
def serialize(self):
"""Serialize into an opaque format understood by self.deserialize."""
raise NotImplementedError
def deserialize(self, data):
"""Serialize from a format produced by self.serialize."""
raise NotImplementedError
def load_items(self, items):
"""Deserialize from a list of WebHistoryItems."""
raise NotImplementedError
class AbstractTab(QWidget):
"""A wrapper over the given widget to hide its API and expose another one.
@ -64,6 +97,7 @@ class AbstractTab(QWidget):
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.
@ -86,6 +120,7 @@ class AbstractTab(QWidget):
def __init__(self, parent=None):
self.tab_id = next(tab_id_gen)
super().__init__(parent)
self.history = AbstractHistory(self)
self._layout = None
self._widget = None
self.keep_icon = False # FIXME:refactor get rid of this?
@ -93,6 +128,7 @@ class AbstractTab(QWidget):
def _set_widget(self, widget):
self._layout = WrapperLayout(widget, self)
self._widget = widget
self.history.history = widget.history()
widget.setParent(self)
@property

View File

@ -26,16 +26,41 @@ try:
except ImportError:
QWebEngineView = None
from qutebrowser.browser.tab import AbstractTab
from qutebrowser.browser.webkit.webview import WebView
from qutebrowser.utils import usertypes
from qutebrowser.browser import tab
from qutebrowser.utils import usertypes, qtutils
class WebEngineViewTab(AbstractTab):
class WebEngineHistory(tab.AbstractHistory):
def back(self):
self.history.back()
def forward(self):
self.history.forward()
def can_go_back(self):
return self.history.canGoBack()
def can_go_forward(self):
return self.history.canGoForward()
def serialize(self):
return qtutils.serialize(self.history)
def deserialize(self, data):
return qtutils.deserialize(self.history)
def load_items(self, items):
# TODO
raise NotImplementedError
class WebEngineViewTab(tab.AbstractTab):
def __init__(self, win_id, parent=None):
super().__init__()
widget = QWebEngineView()
self.history = WebEngineHistory(self)
self._set_widget(widget)
self._connect_signals()

View File

@ -21,15 +21,52 @@
from PyQt5.QtCore import pyqtSlot
from qutebrowser.browser.tab import AbstractTab
from qutebrowser.browser.webkit.webview import WebView
from qutebrowser.browser import tab
from qutebrowser.browser.webkit import webview
from qutebrowser.utils import qtutils
class WebViewTab(AbstractTab):
class WebViewHistory(tab.AbstractHistory):
def back(self):
self.history.back()
def forward(self):
self.history.forward()
def can_go_back(self):
return self.history.canGoBack()
def can_go_forward(self):
return self.history.canGoForward()
def serialize(self):
return qtutils.serialize(self.history)
def deserialize(self, data):
return qtutils.deserialize(self.history)
def load_items(self, items):
stream, _data, user_data = tabhistory.serialize(items)
qtutils.deserialize_stream(stream, self.history)
for i, data in enumerate(user_data):
self.history.itemAt(i).setUserData(data)
cur_data = self.history.currentItem().userData()
if cur_data is not None:
if 'zoom' in cur_data:
self.tab.zoom_perc(cur_data['zoom'] * 100)
if ('scroll-pos' in cur_data and
self.tab.scroll_position() == QPoint(0, 0)):
QTimer.singleShot(0, functools.partial(
self.tab.scroll, cur_data['scroll-pos']))
class WebViewTab(tab.AbstractTab):
def __init__(self, win_id, parent=None):
super().__init__()
widget = WebView(win_id, self.tab_id)
widget = webview.WebView(win_id, self.tab_id)
self.history = WebViewHistory(self)
self._set_widget(widget)
self._connect_signals()

View File

@ -243,23 +243,6 @@ class BrowserPage(QWebPage):
else:
nam.shutdown()
def load_history(self, entries):
"""Load the history from a list of TabHistoryItem objects."""
stream, _data, user_data = tabhistory.serialize(entries)
history = self.history()
qtutils.deserialize_stream(stream, history)
for i, data in enumerate(user_data):
history.itemAt(i).setUserData(data)
cur_data = history.currentItem().userData()
if cur_data is not None:
frame = self.mainFrame()
if 'zoom' in cur_data:
frame.page().view().zoom_perc(cur_data['zoom'] * 100)
if ('scroll-pos' in cur_data and
frame.scrollPosition() == QPoint(0, 0)):
QTimer.singleShot(0, functools.partial(
frame.setScrollPosition, cur_data['scroll-pos']))
def display_content(self, reply, mimetype):
"""Display a QNetworkReply with an explicitly set mimetype."""
self.mainFrame().setContent(reply.readAll(), mimetype, reply.url())

View File

@ -266,7 +266,7 @@ class TabbedBrowser(tabwidget.TabWidget):
objreg.delete('last-focused-tab', scope='window',
window=self._win_id)
if tab.cur_url.isValid():
history_data = qtutils.serialize(tab.history())
history_data = tab.history.serialize()
entry = UndoEntry(tab.cur_url, history_data)
self._undo_stack.append(entry)
elif tab.cur_url.isEmpty():
@ -312,7 +312,7 @@ class TabbedBrowser(tabwidget.TabWidget):
else:
newtab = self.tabopen(url, background=False)
qtutils.deserialize(history_data, newtab.history())
newtab.history.deserialize(history_data)
@pyqtSlot('QUrl', bool)
def openurl(self, url, newtab):

View File

@ -30,4 +30,6 @@ def test_tab(qtbot):
assert tab_w._widget is None
tab_w._set_widget(w)
assert tab_w._widget is w
assert tab_w.history.tab is tab_w
assert tab_w.history.history is w.history()
assert w.parent() is tab_w