qutebrowser/qutebrowser/browser/webkit/webkittab.py

539 lines
19 KiB
Python
Raw Normal View History

2016-06-13 14:44:41 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Wrapper over our (QtWebKit) WebView."""
2016-06-14 18:08:46 +02:00
import sys
2016-07-04 15:18:49 +02:00
import functools
2016-07-04 13:35:38 +02:00
import xml.etree.ElementTree
2016-06-14 18:08:46 +02:00
2016-07-04 15:18:49 +02:00
from PyQt5.QtCore import pyqtSlot, Qt, QEvent, QUrl, QPoint, QTimer
2016-06-14 17:32:36 +02:00
from PyQt5.QtGui import QKeyEvent
2016-06-14 13:39:51 +02:00
from PyQt5.QtWebKitWidgets import QWebPage
2016-06-14 18:08:46 +02:00
from PyQt5.QtWebKit import QWebSettings
2016-06-13 14:44:41 +02:00
2016-07-10 17:23:08 +02:00
from qutebrowser.browser import browsertab
2016-07-04 15:18:49 +02:00
from qutebrowser.browser.webkit import webview, tabhistory
2016-06-14 18:08:46 +02:00
from qutebrowser.utils import qtutils, objreg, usertypes, utils
2016-07-10 17:23:08 +02:00
class WebKitSearch(browsertab.AbstractSearch):
2016-07-04 11:23:46 +02:00
"""QtWebKit implementations related to searching on the page."""
2016-07-04 11:23:46 +02:00
def clear(self):
# We first clear the marked text, then the highlights
self._widget.search('', 0)
self._widget.search('', QWebPage.HighlightAllOccurrences)
2016-07-04 11:23:46 +02:00
def search(self, text, *, ignore_case=False, wrap=False, reverse=False):
flags = 0
if ignore_case == 'smart':
if not text.islower():
flags |= QWebPage.FindCaseSensitively
elif not ignore_case:
flags |= QWebPage.FindCaseSensitively
if wrap:
flags |= QWebPage.FindWrapsAroundDocument
if reverse:
flags |= QWebPage.FindBackward
# We actually search *twice* - once to highlight everything, then again
# to get a mark so we can navigate.
self._widget.search(text, flags)
self._widget.search(text, flags | QWebPage.HighlightAllOccurrences)
2016-07-04 11:23:46 +02:00
self.text = text
self._flags = flags
def next_result(self):
self._widget.search(self.text, self._flags)
2016-07-04 11:23:46 +02:00
def prev_result(self):
# The int() here serves as a QFlags constructor to create a copy of the
# QFlags instance rather as a reference. I don't know why it works this
# way, but it does.
flags = int(self._flags)
if flags & QWebPage.FindBackward:
flags &= ~QWebPage.FindBackward
else:
flags |= QWebPage.FindBackward
self._widget.search(self.text, flags)
2016-07-04 11:23:46 +02:00
2016-07-10 17:23:08 +02:00
class WebKitCaret(browsertab.AbstractCaret):
2016-06-14 18:08:46 +02:00
"""QtWebKit implementations related to moving the cursor/selection."""
2016-06-14 18:08:46 +02:00
@pyqtSlot(usertypes.KeyMode)
2016-07-07 16:10:35 +02:00
def _on_mode_entered(self, mode):
2016-06-14 18:08:46 +02:00
if mode != usertypes.KeyMode.caret:
return
settings = self._widget.settings()
2016-06-14 18:08:46 +02:00
settings.setAttribute(QWebSettings.CaretBrowsingEnabled, True)
self.selection_enabled = bool(self.selection())
2016-06-14 18:08:46 +02:00
if self._widget.isVisible():
2016-06-14 18:08:46 +02:00
# Sometimes the caret isn't immediately visible, but unfocusing
# and refocusing it fixes that.
self._widget.clearFocus()
self._widget.setFocus(Qt.OtherFocusReason)
2016-06-14 18:08:46 +02:00
# Move the caret to the first element in the viewport if there
# isn't any text which is already selected.
#
# Note: We can't use hasSelection() here, as that's always
# true in caret mode.
if not self.selection():
self._widget.page().currentFrame().evaluateJavaScript(
2016-06-14 18:08:46 +02:00
utils.read_file('javascript/position_caret.js'))
2016-07-05 20:11:42 +02:00
@pyqtSlot()
2016-07-07 16:10:35 +02:00
def _on_mode_left(self):
settings = self._widget.settings()
2016-06-14 18:08:46 +02:00
if settings.testAttribute(QWebSettings.CaretBrowsingEnabled):
if self.selection_enabled and self._widget.hasSelection():
2016-06-14 18:08:46 +02:00
# Remove selection if it exists
self._widget.triggerPageAction(QWebPage.MoveToNextChar)
2016-06-14 18:08:46 +02:00
settings.setAttribute(QWebSettings.CaretBrowsingEnabled, False)
self.selection_enabled = False
def move_to_next_line(self, count=1):
if not self.selection_enabled:
act = QWebPage.MoveToNextLine
else:
act = QWebPage.SelectNextLine
for _ in range(count):
self._widget.triggerPageAction(act)
2016-06-14 18:08:46 +02:00
def move_to_prev_line(self, count=1):
if not self.selection_enabled:
act = QWebPage.MoveToPreviousLine
else:
act = QWebPage.SelectPreviousLine
for _ in range(count):
self._widget.triggerPageAction(act)
2016-06-14 18:08:46 +02:00
def move_to_next_char(self, count=1):
if not self.selection_enabled:
act = QWebPage.MoveToNextChar
else:
act = QWebPage.SelectNextChar
for _ in range(count):
self._widget.triggerPageAction(act)
2016-06-14 18:08:46 +02:00
def move_to_prev_char(self, count=1):
if not self.selection_enabled:
act = QWebPage.MoveToPreviousChar
else:
act = QWebPage.SelectPreviousChar
for _ in range(count):
self._widget.triggerPageAction(act)
2016-06-14 18:08:46 +02:00
def move_to_end_of_word(self, count=1):
if not self.selection_enabled:
act = [QWebPage.MoveToNextWord]
if sys.platform == 'win32': # pragma: no cover
act.append(QWebPage.MoveToPreviousChar)
else:
act = [QWebPage.SelectNextWord]
if sys.platform == 'win32': # pragma: no cover
act.append(QWebPage.SelectPreviousChar)
for _ in range(count):
for a in act:
self._widget.triggerPageAction(a)
2016-06-14 18:08:46 +02:00
def move_to_next_word(self, count=1):
if not self.selection_enabled:
act = [QWebPage.MoveToNextWord]
if sys.platform != 'win32': # pragma: no branch
act.append(QWebPage.MoveToNextChar)
else:
act = [QWebPage.SelectNextWord]
if sys.platform != 'win32': # pragma: no branch
act.append(QWebPage.SelectNextChar)
for _ in range(count):
for a in act:
self._widget.triggerPageAction(a)
2016-06-14 18:08:46 +02:00
def move_to_prev_word(self, count=1):
if not self.selection_enabled:
act = QWebPage.MoveToPreviousWord
else:
act = QWebPage.SelectPreviousWord
for _ in range(count):
self._widget.triggerPageAction(act)
2016-06-14 18:08:46 +02:00
def move_to_start_of_line(self):
if not self.selection_enabled:
act = QWebPage.MoveToStartOfLine
else:
act = QWebPage.SelectStartOfLine
self._widget.triggerPageAction(act)
2016-06-14 18:08:46 +02:00
def move_to_end_of_line(self):
if not self.selection_enabled:
act = QWebPage.MoveToEndOfLine
else:
act = QWebPage.SelectEndOfLine
self._widget.triggerPageAction(act)
2016-06-14 18:08:46 +02:00
def move_to_start_of_next_block(self, count=1):
if not self.selection_enabled:
act = [QWebPage.MoveToNextLine,
QWebPage.MoveToStartOfBlock]
else:
act = [QWebPage.SelectNextLine,
QWebPage.SelectStartOfBlock]
for _ in range(count):
for a in act:
self._widget.triggerPageAction(a)
2016-06-14 18:08:46 +02:00
def move_to_start_of_prev_block(self, count=1):
if not self.selection_enabled:
act = [QWebPage.MoveToPreviousLine,
QWebPage.MoveToStartOfBlock]
else:
act = [QWebPage.SelectPreviousLine,
QWebPage.SelectStartOfBlock]
for _ in range(count):
for a in act:
self._widget.triggerPageAction(a)
2016-06-14 18:08:46 +02:00
def move_to_end_of_next_block(self, count=1):
if not self.selection_enabled:
act = [QWebPage.MoveToNextLine,
QWebPage.MoveToEndOfBlock]
else:
act = [QWebPage.SelectNextLine,
QWebPage.SelectEndOfBlock]
for _ in range(count):
for a in act:
self._widget.triggerPageAction(a)
2016-06-14 18:08:46 +02:00
def move_to_end_of_prev_block(self, count=1):
if not self.selection_enabled:
act = [QWebPage.MoveToPreviousLine, QWebPage.MoveToEndOfBlock]
else:
act = [QWebPage.SelectPreviousLine, QWebPage.SelectEndOfBlock]
for _ in range(count):
for a in act:
self._widget.triggerPageAction(a)
2016-06-14 18:08:46 +02:00
def move_to_start_of_document(self):
if not self.selection_enabled:
act = QWebPage.MoveToStartOfDocument
else:
act = QWebPage.SelectStartOfDocument
self._widget.triggerPageAction(act)
2016-06-14 18:08:46 +02:00
def move_to_end_of_document(self):
if not self.selection_enabled:
act = QWebPage.MoveToEndOfDocument
else:
act = QWebPage.SelectEndOfDocument
self._widget.triggerPageAction(act)
2016-06-14 18:08:46 +02:00
def toggle_selection(self):
self.selection_enabled = not self.selection_enabled
mainwindow = objreg.get('main-window', scope='window',
window=self._win_id)
mainwindow.status.set_mode_active(usertypes.KeyMode.caret, True)
def drop_selection(self):
self._widget.triggerPageAction(QWebPage.MoveToNextChar)
2016-06-13 14:44:41 +02:00
def has_selection(self):
return self._widget.hasSelection()
def selection(self, html=False):
if html:
return self._widget.selectedHtml()
return self._widget.selectedText()
2016-07-04 13:35:38 +02:00
def follow_selected(self, *, tab=False):
if not self.has_selection():
return
if QWebSettings.globalSettings().testAttribute(
QWebSettings.JavascriptEnabled):
if tab:
self._widget.page().open_target = usertypes.ClickTarget.tab
self._tab.run_js_async(
'window.getSelection().anchorNode.parentNode.click()')
else:
selection = self.selection(html=True)
try:
selected_element = xml.etree.ElementTree.fromstring(
'<html>{}</html>'.format(selection)).find('a')
except xml.etree.ElementTree.ParseError:
2016-07-10 17:33:36 +02:00
raise browsertab.WebTabError('Could not parse selected '
'element!')
2016-07-04 13:35:38 +02:00
if selected_element is not None:
try:
url = selected_element.attrib['href']
except KeyError:
2016-07-10 17:33:36 +02:00
raise browsertab.WebTabError('Anchor element without '
'href!')
url = self._tab.url().resolved(QUrl(url))
2016-07-04 13:51:11 +02:00
if tab:
self._tab.new_tab_requested.emit(url)
else:
self._tab.openurl(url)
2016-07-04 13:35:38 +02:00
2016-06-13 14:44:41 +02:00
2016-07-10 17:23:08 +02:00
class WebKitZoom(browsertab.AbstractZoom):
2016-06-15 13:02:24 +02:00
"""QtWebKit implementations related to zooming."""
2016-06-15 13:02:24 +02:00
def _set_factor_internal(self, factor):
self._widget.setZoomFactor(factor)
2016-06-15 13:02:24 +02:00
def factor(self):
return self._widget.zoomFactor()
2016-06-15 13:02:24 +02:00
2016-07-10 17:23:08 +02:00
class WebKitScroller(browsertab.AbstractScroller):
2016-06-14 17:32:36 +02:00
"""QtWebKit implementations related to scrolling."""
2016-07-07 18:03:37 +02:00
# FIXME:qtwebengine When to use the main frame, when the current one?
2016-06-14 17:32:36 +02:00
def pos_px(self):
return self._widget.page().mainFrame().scrollPosition()
2016-06-14 17:32:36 +02:00
def pos_perc(self):
return self._widget.scroll_pos
2016-06-14 17:32:36 +02:00
def to_point(self, point):
self._widget.page().mainFrame().setScrollPosition(point)
2016-06-14 17:32:36 +02:00
2016-06-15 13:04:50 +02:00
def delta(self, x=0, y=0):
2016-06-14 17:32:36 +02:00
qtutils.check_overflow(x, 'int')
qtutils.check_overflow(y, 'int')
self._widget.page().mainFrame().scroll(x, y)
2016-06-14 17:32:36 +02:00
2016-07-05 20:11:42 +02:00
def delta_page(self, x=0.0, y=0.0):
2016-06-14 17:32:36 +02:00
if y.is_integer():
y = int(y)
if y == 0:
pass
elif y < 0:
2016-06-15 13:04:50 +02:00
self.page_up(count=-y)
2016-06-14 17:32:36 +02:00
elif y > 0:
self.page_down(count=y)
y = 0
if x == 0 and y == 0:
return
size = self._widget.page().mainFrame().geometry()
2016-06-14 17:32:36 +02:00
self.delta(x * size.width(), y * size.height())
def to_perc(self, x=None, y=None):
if x is None and y == 0:
self.top()
elif x is None and y == 100:
self.bottom()
else:
for val, orientation in [(x, Qt.Horizontal), (y, Qt.Vertical)]:
2016-06-15 13:04:50 +02:00
if val is not None:
2016-07-05 20:11:42 +02:00
val = qtutils.check_overflow(val, 'int', fatal=False)
frame = self._widget.page().mainFrame()
2016-06-15 13:04:50 +02:00
m = frame.scrollBarMaximum(orientation)
if m == 0:
continue
frame.setScrollBarValue(orientation, int(m * val / 100))
2016-06-14 17:32:36 +02:00
def _key_press(self, key, count=1, getter_name=None, direction=None):
frame = self._widget.page().mainFrame()
2016-06-14 17:32:36 +02:00
press_evt = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier, 0, 0, 0)
release_evt = QKeyEvent(QEvent.KeyRelease, key, Qt.NoModifier, 0, 0, 0)
getter = None if getter_name is None else getattr(frame, getter_name)
2016-07-07 18:03:37 +02:00
# FIXME:qtwebengine needed?
# self._widget.setFocus()
2016-06-14 17:32:36 +02:00
for _ in range(count):
# Abort scrolling if the minimum/maximum was reached.
2016-06-15 13:04:50 +02:00
if (getter is not None and
frame.scrollBarValue(direction) == getter(direction)):
2016-06-14 17:32:36 +02:00
return
self._widget.keyPressEvent(press_evt)
self._widget.keyReleaseEvent(release_evt)
2016-06-14 17:32:36 +02:00
def up(self, count=1):
self._key_press(Qt.Key_Up, count, 'scrollBarMinimum', Qt.Vertical)
def down(self, count=1):
self._key_press(Qt.Key_Down, count, 'scrollBarMaximum', Qt.Vertical)
def left(self, count=1):
self._key_press(Qt.Key_Left, count, 'scrollBarMinimum', Qt.Horizontal)
def right(self, count=1):
self._key_press(Qt.Key_Right, count, 'scrollBarMaximum', Qt.Horizontal)
def top(self):
self._key_press(Qt.Key_Home)
def bottom(self):
self._key_press(Qt.Key_End)
def page_up(self, count=1):
self._key_press(Qt.Key_PageUp, count, 'scrollBarMinimum', Qt.Vertical)
def page_down(self, count=1):
self._key_press(Qt.Key_PageDown, count, 'scrollBarMaximum',
Qt.Vertical)
def at_top(self):
return self.pos_px().y() == 0
def at_bottom(self):
frame = self._widget.page().currentFrame()
2016-06-14 17:32:36 +02:00
return self.pos_px().y() >= frame.scrollBarMaximum(Qt.Vertical)
2016-07-10 17:23:08 +02:00
class WebKitHistory(browsertab.AbstractHistory):
2016-06-14 11:03:34 +02:00
"""QtWebKit implementations related to page history."""
2016-06-14 13:53:35 +02:00
def current_idx(self):
2016-07-04 12:50:15 +02:00
return self._history.currentItemIndex()
2016-06-14 13:53:35 +02:00
2016-06-14 11:03:34 +02:00
def back(self):
2016-07-04 12:50:15 +02:00
self._history.back()
2016-06-14 11:03:34 +02:00
def forward(self):
2016-07-04 12:50:15 +02:00
self._history.forward()
2016-06-14 11:03:34 +02:00
def can_go_back(self):
2016-07-04 12:50:15 +02:00
return self._history.canGoBack()
2016-06-14 11:03:34 +02:00
def can_go_forward(self):
2016-07-04 12:50:15 +02:00
return self._history.canGoForward()
2016-06-14 11:03:34 +02:00
def serialize(self):
2016-07-04 12:50:15 +02:00
return qtutils.serialize(self._history)
2016-06-14 11:03:34 +02:00
def deserialize(self, data):
2016-07-04 12:50:15 +02:00
return qtutils.deserialize(data, self._history)
2016-06-14 11:03:34 +02:00
def load_items(self, items):
stream, _data, user_data = tabhistory.serialize(items)
2016-07-04 12:50:15 +02:00
qtutils.deserialize_stream(stream, self._history)
2016-06-14 11:03:34 +02:00
for i, data in enumerate(user_data):
2016-07-04 12:50:15 +02:00
self._history.itemAt(i).setUserData(data)
cur_data = self._history.currentItem().userData()
2016-06-14 11:03:34 +02:00
if cur_data is not None:
if 'zoom' in cur_data:
2016-06-15 14:23:24 +02:00
self._tab.zoom.set_factor(cur_data['zoom'])
2016-06-14 11:03:34 +02:00
if ('scroll-pos' in cur_data and
2016-06-15 14:23:24 +02:00
self._tab.scroll.pos_px() == QPoint(0, 0)):
2016-06-14 11:03:34 +02:00
QTimer.singleShot(0, functools.partial(
2016-07-04 15:18:49 +02:00
self._tab.scroll.to_point, cur_data['scroll-pos']))
2016-06-14 11:03:34 +02:00
2016-07-10 17:23:08 +02:00
class WebKitTab(browsertab.AbstractTab):
2016-06-13 14:44:41 +02:00
"""A QtWebKit tab in the browser."""
def __init__(self, win_id, mode_manager, parent=None):
2016-06-14 15:20:40 +02:00
super().__init__(win_id)
2016-06-15 13:02:24 +02:00
widget = webview.WebView(win_id, self.tab_id, tab=self)
2016-07-10 17:23:08 +02:00
self.history = WebKitHistory(self)
self.scroll = WebKitScroller(parent=self)
self.caret = WebKitCaret(win_id=win_id, mode_manager=mode_manager,
tab=self, parent=self)
self.zoom = WebKitZoom(win_id=win_id, parent=self)
self.search = WebKitSearch(parent=self)
2016-06-13 15:05:31 +02:00
self._set_widget(widget)
2016-06-13 14:44:41 +02:00
self._connect_signals()
self.zoom.set_default()
self.backend = usertypes.Backend.QtWebKit
2016-06-13 14:44:41 +02:00
2016-06-13 15:05:31 +02:00
def openurl(self, url):
self._openurl_prepare(url)
2016-06-13 15:05:31 +02:00
self._widget.openurl(url)
def url(self):
return self._widget.url()
2016-06-13 15:05:31 +02:00
2016-06-14 18:35:28 +02:00
def dump_async(self, callback, *, plain=False):
2016-06-14 13:26:30 +02:00
frame = self._widget.page().mainFrame()
if plain:
callback(frame.toPlainText())
else:
callback(frame.toHtml())
2016-06-14 18:47:26 +02:00
def run_js_async(self, code, callback=None):
result = self._widget.page().mainFrame().evaluateJavaScript(code)
if callback is not None:
callback(result)
2016-06-14 15:22:22 +02:00
def icon(self):
return self._widget.icon()
2016-06-14 13:31:02 +02:00
def shutdown(self):
self._widget.shutdown()
2016-06-14 13:39:51 +02:00
def reload(self, *, force=False):
if force:
action = QWebPage.ReloadAndBypassCache
else:
action = QWebPage.Reload
self._widget.triggerPageAction(action)
2016-06-14 13:39:51 +02:00
def stop(self):
self._widget.stop()
2016-06-14 13:53:35 +02:00
def title(self):
return self._widget.title()
def clear_ssl_errors(self):
nam = self._widget.page().networkAccessManager()
nam.clear_all_ssl_errors()
2016-07-04 16:33:49 +02:00
def set_html(self, html, base_url):
self._widget.setHtml(html, base_url)
2016-06-13 14:44:41 +02:00
def _connect_signals(self):
view = self._widget
page = view.page()
frame = page.mainFrame()
page.windowCloseRequested.connect(self.window_close_requested)
page.linkHovered.connect(self.link_hovered)
page.loadProgress.connect(self._on_load_progress)
2016-07-04 16:34:07 +02:00
frame.loadStarted.connect(self._on_load_started)
2016-06-14 17:32:36 +02:00
view.scroll_pos_changed.connect(self.scroll.perc_changed)
2016-06-13 14:44:41 +02:00
view.titleChanged.connect(self.title_changed)
2016-07-11 16:05:09 +02:00
view.urlChanged.connect(self._on_url_changed)
2016-06-14 13:31:02 +02:00
view.shutting_down.connect(self.shutting_down)
page.networkAccessManager().sslErrors.connect(self._on_ssl_errors)
2016-06-13 14:44:41 +02:00
2016-07-05 20:11:42 +02:00
# Make sure we emit an appropriate status when loading finished. While
# Qt has a bool "ok" attribute for loadFinished, it always is True when
# using error pages...
2016-06-13 14:44:41 +02:00
# See https://github.com/The-Compiler/qutebrowser/issues/84
frame.loadFinished.connect(lambda:
self._on_load_finished(
2016-06-13 15:05:31 +02:00
not self._widget.page().error_occurred))
2016-06-13 14:44:41 +02:00
# Emit iconChanged with a QIcon like QWebEngineView does.
view.iconChanged.connect(lambda:
self.icon_changed.emit(self._widget.icon()))