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-28 14:56:42 +02:00
|
|
|
from PyQt5.QtCore import (pyqtSlot, Qt, QEvent, QUrl, QPoint, QTimer, QSizeF,
|
|
|
|
QSize)
|
2016-06-14 17:32:36 +02:00
|
|
|
from PyQt5.QtGui import QKeyEvent
|
2016-10-30 22:50:10 +01:00
|
|
|
from PyQt5.QtWidgets import QApplication
|
2016-07-27 15:34:28 +02:00
|
|
|
from PyQt5.QtWebKitWidgets import QWebPage, QWebFrame
|
2016-06-14 18:08:46 +02:00
|
|
|
from PyQt5.QtWebKit import QWebSettings
|
2016-07-12 12:54:11 +02:00
|
|
|
from PyQt5.QtPrintSupport import QPrinter
|
2016-06-13 14:44:41 +02:00
|
|
|
|
2016-07-10 17:23:08 +02:00
|
|
|
from qutebrowser.browser import browsertab
|
2016-10-31 07:27:30 +01:00
|
|
|
from qutebrowser.browser.webkit import webview, tabhistory, webkitelem
|
2016-10-30 22:50:10 +01:00
|
|
|
from qutebrowser.browser.webkit.network import proxy, webkitqutescheme
|
2016-10-31 07:27:30 +01:00
|
|
|
from qutebrowser.utils import qtutils, objreg, usertypes, utils, log
|
2016-06-14 18:08:46 +02:00
|
|
|
|
|
|
|
|
2016-09-07 17:27:21 +02:00
|
|
|
def init():
|
|
|
|
"""Initialize QtWebKit-specific modules."""
|
2016-10-30 22:50:10 +01:00
|
|
|
qapp = QApplication.instance()
|
|
|
|
|
|
|
|
log.init.debug("Initializing proxy...")
|
|
|
|
proxy.init()
|
|
|
|
|
|
|
|
log.init.debug("Initializing js-bridge...")
|
|
|
|
js_bridge = webkitqutescheme.JSBridge(qapp)
|
|
|
|
objreg.register('js-bridge', js_bridge)
|
2016-09-07 17:27:21 +02:00
|
|
|
|
|
|
|
|
2016-07-12 12:54:11 +02:00
|
|
|
class WebKitPrinting(browsertab.AbstractPrinting):
|
|
|
|
|
|
|
|
"""QtWebKit implementations related to printing."""
|
|
|
|
|
|
|
|
def _do_check(self):
|
|
|
|
if not qtutils.check_print_compat():
|
|
|
|
# WORKAROUND (remove this when we bump the requirements to 5.3.0)
|
|
|
|
raise browsertab.WebTabError(
|
|
|
|
"Printing on Qt < 5.3.0 on Windows is broken, please upgrade!")
|
|
|
|
|
|
|
|
def check_pdf_support(self):
|
|
|
|
self._do_check()
|
|
|
|
|
|
|
|
def check_printer_support(self):
|
|
|
|
self._do_check()
|
|
|
|
|
|
|
|
def to_pdf(self, filename):
|
|
|
|
printer = QPrinter()
|
|
|
|
printer.setOutputFileName(filename)
|
|
|
|
self.to_printer(printer)
|
|
|
|
|
|
|
|
def to_printer(self, printer):
|
|
|
|
self._widget.print(printer)
|
|
|
|
|
|
|
|
|
2016-07-10 17:23:08 +02:00
|
|
|
class WebKitSearch(browsertab.AbstractSearch):
|
2016-07-04 11:23:46 +02:00
|
|
|
|
2016-07-07 11:28:57 +02:00
|
|
|
"""QtWebKit implementations related to searching on the page."""
|
|
|
|
|
2016-07-12 16:10:35 +02:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
|
|
|
self._flags = QWebPage.FindFlags(0)
|
|
|
|
|
|
|
|
def _call_cb(self, callback, found):
|
|
|
|
"""Call the given callback if it's non-None.
|
|
|
|
|
|
|
|
Delays the call via a QTimer so the website is re-rendered in between.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
callback: What to call
|
|
|
|
found: If the text was found
|
|
|
|
"""
|
|
|
|
if callback is not None:
|
|
|
|
QTimer.singleShot(0, functools.partial(callback, found))
|
|
|
|
|
2016-07-04 11:23:46 +02:00
|
|
|
def clear(self):
|
|
|
|
# We first clear the marked text, then the highlights
|
2016-07-12 16:10:35 +02:00
|
|
|
self._widget.findText('')
|
|
|
|
self._widget.findText('', QWebPage.HighlightAllOccurrences)
|
2016-07-04 11:23:46 +02:00
|
|
|
|
2016-07-12 16:47:57 +02:00
|
|
|
def search(self, text, *, ignore_case=False, reverse=False,
|
2016-07-12 16:10:35 +02:00
|
|
|
result_cb=None):
|
2016-07-12 17:28:59 +02:00
|
|
|
flags = QWebPage.FindWrapsAroundDocument
|
2016-07-04 11:23:46 +02:00
|
|
|
if ignore_case == 'smart':
|
|
|
|
if not text.islower():
|
|
|
|
flags |= QWebPage.FindCaseSensitively
|
|
|
|
elif not ignore_case:
|
|
|
|
flags |= QWebPage.FindCaseSensitively
|
|
|
|
if reverse:
|
|
|
|
flags |= QWebPage.FindBackward
|
|
|
|
# We actually search *twice* - once to highlight everything, then again
|
|
|
|
# to get a mark so we can navigate.
|
2016-07-12 16:10:35 +02:00
|
|
|
found = self._widget.findText(text, flags)
|
|
|
|
self._widget.findText(text, flags | QWebPage.HighlightAllOccurrences)
|
2016-07-04 11:23:46 +02:00
|
|
|
self.text = text
|
|
|
|
self._flags = flags
|
2016-07-12 16:10:35 +02:00
|
|
|
self._call_cb(result_cb, found)
|
2016-07-04 11:23:46 +02:00
|
|
|
|
2016-07-12 16:10:35 +02:00
|
|
|
def next_result(self, *, result_cb=None):
|
|
|
|
found = self._widget.findText(self.text, self._flags)
|
|
|
|
self._call_cb(result_cb, found)
|
2016-07-04 11:23:46 +02:00
|
|
|
|
2016-07-12 16:10:35 +02:00
|
|
|
def prev_result(self, *, result_cb=None):
|
|
|
|
# The int() here makes sure we get a copy of the flags.
|
|
|
|
flags = QWebPage.FindFlags(int(self._flags))
|
2016-07-04 11:23:46 +02:00
|
|
|
if flags & QWebPage.FindBackward:
|
|
|
|
flags &= ~QWebPage.FindBackward
|
|
|
|
else:
|
|
|
|
flags |= QWebPage.FindBackward
|
2016-07-12 16:10:35 +02:00
|
|
|
found = self._widget.findText(self.text, flags)
|
|
|
|
self._call_cb(result_cb, found)
|
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
|
|
|
|
2016-07-07 11:28:57 +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
|
|
|
|
|
2016-07-04 11:33:15 +02:00
|
|
|
settings = self._widget.settings()
|
2016-06-14 18:08:46 +02:00
|
|
|
settings.setAttribute(QWebSettings.CaretBrowsingEnabled, True)
|
2016-07-04 07:52:49 +02:00
|
|
|
self.selection_enabled = bool(self.selection())
|
2016-06-14 18:08:46 +02:00
|
|
|
|
2016-07-04 11:33:15 +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.
|
2016-07-04 11:33:15 +02:00
|
|
|
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.
|
2016-07-04 07:52:49 +02:00
|
|
|
if not self.selection():
|
2016-07-04 11:33:15 +02:00
|
|
|
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):
|
2016-07-04 11:33:15 +02:00
|
|
|
settings = self._widget.settings()
|
2016-06-14 18:08:46 +02:00
|
|
|
if settings.testAttribute(QWebSettings.CaretBrowsingEnabled):
|
2016-07-04 11:33:15 +02:00
|
|
|
if self.selection_enabled and self._widget.hasSelection():
|
2016-06-14 18:08:46 +02:00
|
|
|
# Remove selection if it exists
|
2016-07-04 11:33:15 +02:00
|
|
|
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):
|
2016-07-04 11:33:15 +02:00
|
|
|
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):
|
2016-07-04 11:33:15 +02:00
|
|
|
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):
|
2016-07-04 11:33:15 +02:00
|
|
|
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):
|
2016-07-04 11:33:15 +02:00
|
|
|
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:
|
2016-07-04 11:33:15 +02:00
|
|
|
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:
|
2016-07-04 11:33:15 +02:00
|
|
|
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):
|
2016-07-04 11:33:15 +02:00
|
|
|
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
|
2016-07-04 11:33:15 +02:00
|
|
|
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
|
2016-07-04 11:33:15 +02:00
|
|
|
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:
|
2016-07-04 11:33:15 +02:00
|
|
|
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:
|
2016-07-04 11:33:15 +02:00
|
|
|
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:
|
2016-07-04 11:33:15 +02:00
|
|
|
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:
|
2016-07-04 11:33:15 +02:00
|
|
|
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
|
2016-07-04 11:33:15 +02:00
|
|
|
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
|
2016-07-04 11:33:15 +02:00
|
|
|
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):
|
2016-07-04 11:33:15 +02:00
|
|
|
self._widget.triggerPageAction(QWebPage.MoveToNextChar)
|
2016-06-13 14:44:41 +02:00
|
|
|
|
2016-07-04 07:52:49 +02:00
|
|
|
def has_selection(self):
|
2016-07-04 11:33:15 +02:00
|
|
|
return self._widget.hasSelection()
|
2016-07-04 07:52:49 +02:00
|
|
|
|
|
|
|
def selection(self, html=False):
|
|
|
|
if html:
|
2016-07-04 11:33:15 +02:00
|
|
|
return self._widget.selectedHtml()
|
|
|
|
return self._widget.selectedText()
|
2016-07-04 07:52:49 +02:00
|
|
|
|
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:
|
2016-11-14 06:45:52 +01:00
|
|
|
self._tab.data.override_target = usertypes.ClickTarget.tab
|
2016-07-04 13:35:38 +02:00
|
|
|
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!')
|
2016-07-08 10:05:46 +02:00
|
|
|
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
|
|
|
|
2016-07-07 11:28:57 +02:00
|
|
|
"""QtWebKit implementations related to zooming."""
|
|
|
|
|
2016-06-15 13:02:24 +02:00
|
|
|
def _set_factor_internal(self, factor):
|
2016-07-04 11:33:15 +02:00
|
|
|
self._widget.setZoomFactor(factor)
|
2016-06-15 13:02:24 +02:00
|
|
|
|
|
|
|
def factor(self):
|
2016-07-04 11:33:15 +02:00
|
|
|
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
|
|
|
|
2016-07-07 11:28:57 +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):
|
2016-07-04 11:33:15 +02:00
|
|
|
return self._widget.page().mainFrame().scrollPosition()
|
2016-06-14 17:32:36 +02:00
|
|
|
|
|
|
|
def pos_perc(self):
|
2016-07-04 11:33:15 +02:00
|
|
|
return self._widget.scroll_pos
|
2016-06-14 17:32:36 +02:00
|
|
|
|
|
|
|
def to_point(self, point):
|
2016-07-04 11:33:15 +02:00
|
|
|
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')
|
2016-07-04 11:33:15 +02:00
|
|
|
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
|
2016-07-04 11:33:15 +02:00
|
|
|
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)
|
2016-07-04 11:33:15 +02:00
|
|
|
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):
|
2016-07-04 11:33:15 +02:00
|
|
|
frame = self._widget.page().mainFrame()
|
2016-06-14 17:32:36 +02:00
|
|
|
getter = None if getter_name is None else getattr(frame, getter_name)
|
|
|
|
|
2016-07-07 18:03:37 +02:00
|
|
|
# FIXME:qtwebengine needed?
|
2016-07-07 12:47:48 +02:00
|
|
|
# self._widget.setFocus()
|
|
|
|
|
2016-06-14 17:32:36 +02:00
|
|
|
for _ in range(count):
|
2016-09-11 19:37:09 +02:00
|
|
|
press_evt = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier, 0, 0, 0)
|
|
|
|
release_evt = QKeyEvent(QEvent.KeyRelease, key, Qt.NoModifier,
|
|
|
|
0, 0, 0)
|
2016-06-14 17:32:36 +02:00
|
|
|
# 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
|
2016-07-07 12:47:48 +02:00
|
|
|
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):
|
2016-07-04 11:33:15 +02:00
|
|
|
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
|
|
|
|
2016-07-07 11:28:57 +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-07-26 13:19:07 +02:00
|
|
|
self._tab.scroller.pos_px() == QPoint(0, 0)):
|
2016-06-14 11:03:34 +02:00
|
|
|
QTimer.singleShot(0, functools.partial(
|
2016-07-26 13:19:07 +02:00
|
|
|
self._tab.scroller.to_point, cur_data['scroll-pos']))
|
2016-06-14 11:03:34 +02:00
|
|
|
|
|
|
|
|
2016-08-18 14:01:27 +02:00
|
|
|
class WebKitElements(browsertab.AbstractElements):
|
|
|
|
|
|
|
|
"""QtWebKit implemementations related to elements on the page."""
|
|
|
|
|
|
|
|
def find_css(self, selector, callback, *, only_visible=False):
|
|
|
|
mainframe = self._widget.page().mainFrame()
|
|
|
|
if mainframe is None:
|
|
|
|
raise browsertab.WebTabError("No frame focused!")
|
|
|
|
|
|
|
|
elems = []
|
|
|
|
frames = webkitelem.get_child_frames(mainframe)
|
|
|
|
for f in frames:
|
|
|
|
for elem in f.findAllElements(selector):
|
2016-08-18 14:32:19 +02:00
|
|
|
elems.append(webkitelem.WebKitElement(elem, tab=self._tab))
|
2016-08-18 14:01:27 +02:00
|
|
|
|
|
|
|
if only_visible:
|
2016-09-07 11:58:39 +02:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
elems = [e for e in elems if e._is_visible(mainframe)]
|
|
|
|
# pylint: enable=protected-access
|
2016-08-18 14:01:27 +02:00
|
|
|
|
|
|
|
callback(elems)
|
|
|
|
|
2016-08-18 14:08:34 +02:00
|
|
|
def find_id(self, elem_id, callback):
|
2016-08-18 15:30:04 +02:00
|
|
|
def find_id_cb(elems):
|
|
|
|
if not elems:
|
|
|
|
callback(None)
|
|
|
|
else:
|
|
|
|
callback(elems[0])
|
|
|
|
self.find_css('#' + elem_id, find_id_cb)
|
2016-08-18 14:08:34 +02:00
|
|
|
|
2016-08-18 14:01:27 +02:00
|
|
|
def find_focused(self, callback):
|
|
|
|
frame = self._widget.page().currentFrame()
|
|
|
|
if frame is None:
|
|
|
|
callback(None)
|
|
|
|
return
|
|
|
|
|
|
|
|
elem = frame.findFirstElement('*:focus')
|
|
|
|
if elem.isNull():
|
|
|
|
callback(None)
|
|
|
|
else:
|
2016-08-18 14:32:19 +02:00
|
|
|
callback(webkitelem.WebKitElement(elem, tab=self._tab))
|
2016-08-18 14:01:27 +02:00
|
|
|
|
|
|
|
def find_at_pos(self, pos, callback):
|
|
|
|
assert pos.x() >= 0
|
|
|
|
assert pos.y() >= 0
|
|
|
|
frame = self._widget.page().frameAt(pos)
|
|
|
|
if frame is None:
|
|
|
|
# This happens when we click inside the webview, but not actually
|
|
|
|
# on the QWebPage - for example when clicking the scrollbar
|
|
|
|
# sometimes.
|
|
|
|
log.webview.debug("Hit test at {} but frame is None!".format(pos))
|
|
|
|
callback(None)
|
|
|
|
return
|
|
|
|
|
|
|
|
# You'd think we have to subtract frame.geometry().topLeft() from the
|
|
|
|
# position, but it seems QWebFrame::hitTestContent wants a position
|
|
|
|
# relative to the QWebView, not to the frame. This makes no sense to
|
|
|
|
# me, but it works this way.
|
|
|
|
hitresult = frame.hitTestContent(pos)
|
|
|
|
if hitresult.isNull():
|
|
|
|
# For some reason, the whole hit result can be null sometimes (e.g.
|
2016-09-07 11:24:28 +02:00
|
|
|
# on doodle menu links).
|
2016-08-18 14:01:27 +02:00
|
|
|
log.webview.debug("Hit test result is null!")
|
|
|
|
callback(None)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2016-08-18 14:32:19 +02:00
|
|
|
elem = webkitelem.WebKitElement(hitresult.element(), tab=self._tab)
|
2016-08-18 14:01:27 +02:00
|
|
|
except webkitelem.IsNullError:
|
|
|
|
# For some reason, the hit result element can be a null element
|
|
|
|
# sometimes (e.g. when clicking the timetable fields on
|
2016-09-07 11:24:28 +02:00
|
|
|
# http://www.sbb.ch/ ).
|
2016-08-18 14:01:27 +02:00
|
|
|
log.webview.debug("Hit test result element is null!")
|
|
|
|
callback(None)
|
|
|
|
return
|
|
|
|
|
|
|
|
callback(elem)
|
|
|
|
|
|
|
|
|
2016-07-10 17:23:08 +02:00
|
|
|
class WebKitTab(browsertab.AbstractTab):
|
2016-06-13 14:44:41 +02:00
|
|
|
|
2016-07-07 11:28:57 +02:00
|
|
|
"""A QtWebKit tab in the browser."""
|
|
|
|
|
2016-08-19 18:38:07 +02:00
|
|
|
WIDGET_CLASS = webview.WebView
|
|
|
|
|
2016-07-08 10:20:28 +02:00
|
|
|
def __init__(self, win_id, mode_manager, parent=None):
|
2016-09-07 10:24:27 +02:00
|
|
|
super().__init__(win_id=win_id, mode_manager=mode_manager,
|
|
|
|
parent=parent)
|
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)
|
2016-07-26 13:19:07 +02:00
|
|
|
self.scroller = WebKitScroller(self, parent=self)
|
2016-07-10 17:23:08 +02:00
|
|
|
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-07-12 12:54:11 +02:00
|
|
|
self.printing = WebKitPrinting()
|
2016-08-18 14:01:27 +02:00
|
|
|
self.elements = WebKitElements(self)
|
2016-06-13 15:05:31 +02:00
|
|
|
self._set_widget(widget)
|
2016-06-13 14:44:41 +02:00
|
|
|
self._connect_signals()
|
2016-07-07 16:09:38 +02:00
|
|
|
self.zoom.set_default()
|
2016-07-10 16:17:19 +02:00
|
|
|
self.backend = usertypes.Backend.QtWebKit
|
2016-06-13 14:44:41 +02:00
|
|
|
|
2016-08-10 19:42:24 +02:00
|
|
|
def _install_event_filter(self):
|
|
|
|
self._widget.installEventFilter(self._mouse_event_filter)
|
2016-08-10 16:37:52 +02:00
|
|
|
|
2016-06-13 15:05:31 +02:00
|
|
|
def openurl(self, url):
|
2016-07-11 16:32:24 +02:00
|
|
|
self._openurl_prepare(url)
|
2016-06-13 15:05:31 +02:00
|
|
|
self._widget.openurl(url)
|
|
|
|
|
2016-08-10 13:07:12 +02:00
|
|
|
def url(self, requested=False):
|
|
|
|
frame = self._widget.page().mainFrame()
|
|
|
|
if requested:
|
|
|
|
return frame.requestedUrl()
|
|
|
|
else:
|
|
|
|
return frame.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-09-12 15:59:03 +02:00
|
|
|
def run_js_async(self, code, callback=None, *, world=None):
|
2016-09-12 18:27:51 +02:00
|
|
|
if world is not None and world != usertypes.JsWorld.jseval:
|
2016-09-12 15:59:03 +02:00
|
|
|
log.webview.warning("Ignoring world ID {}".format(world))
|
2016-10-30 18:49:41 +01:00
|
|
|
document_element = self._widget.page().mainFrame().documentElement()
|
|
|
|
result = document_element.evaluateJavaScript(code)
|
2016-06-14 18:47:26 +02:00
|
|
|
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
|
2016-07-08 10:42:54 +02:00
|
|
|
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()
|
|
|
|
|
2016-07-04 14:01:28 +02:00
|
|
|
def clear_ssl_errors(self):
|
|
|
|
nam = self._widget.page().networkAccessManager()
|
|
|
|
nam.clear_all_ssl_errors()
|
|
|
|
|
2016-08-18 18:05:48 +02:00
|
|
|
@pyqtSlot()
|
|
|
|
def _on_history_trigger(self):
|
|
|
|
url = self.url()
|
|
|
|
requested_url = self.url(requested=True)
|
|
|
|
self.add_history_item.emit(url, requested_url, self.title())
|
|
|
|
|
2016-07-04 16:33:49 +02:00
|
|
|
def set_html(self, html, base_url):
|
|
|
|
self._widget.setHtml(html, base_url)
|
|
|
|
|
2016-07-26 12:55:40 +02:00
|
|
|
@pyqtSlot()
|
|
|
|
def _on_frame_load_finished(self):
|
|
|
|
"""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... See
|
|
|
|
https://github.com/The-Compiler/qutebrowser/issues/84
|
|
|
|
"""
|
|
|
|
self._on_load_finished(not self._widget.page().error_occurred)
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def _on_webkit_icon_changed(self):
|
|
|
|
"""Emit iconChanged with a QIcon like QWebEngineView does."""
|
|
|
|
self.icon_changed.emit(self._widget.icon())
|
|
|
|
|
2016-07-27 15:34:28 +02:00
|
|
|
@pyqtSlot(QWebFrame)
|
|
|
|
def _on_frame_created(self, frame):
|
|
|
|
"""Connect the contentsSizeChanged signal of each frame."""
|
|
|
|
# FIXME:qtwebengine those could theoretically regress:
|
|
|
|
# https://github.com/The-Compiler/qutebrowser/issues/152
|
|
|
|
# https://github.com/The-Compiler/qutebrowser/issues/263
|
2016-07-28 15:29:56 +02:00
|
|
|
frame.contentsSizeChanged.connect(self._on_contents_size_changed)
|
2016-07-27 15:34:28 +02:00
|
|
|
|
2016-07-28 14:56:42 +02:00
|
|
|
@pyqtSlot(QSize)
|
|
|
|
def _on_contents_size_changed(self, size):
|
|
|
|
self.contents_size_changed.emit(QSizeF(size))
|
|
|
|
|
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)
|
2016-07-11 11:31:59 +02:00
|
|
|
page.loadProgress.connect(self._on_load_progress)
|
2016-07-04 16:34:07 +02:00
|
|
|
frame.loadStarted.connect(self._on_load_started)
|
2016-07-26 13:19:07 +02:00
|
|
|
view.scroll_pos_changed.connect(self.scroller.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)
|
2016-07-11 12:08:54 +02:00
|
|
|
page.networkAccessManager().sslErrors.connect(self._on_ssl_errors)
|
2016-07-26 12:55:40 +02:00
|
|
|
frame.loadFinished.connect(self._on_frame_load_finished)
|
|
|
|
view.iconChanged.connect(self._on_webkit_icon_changed)
|
2016-07-28 14:56:42 +02:00
|
|
|
page.frameCreated.connect(self._on_frame_created)
|
|
|
|
frame.contentsSizeChanged.connect(self._on_contents_size_changed)
|
2016-08-10 13:14:38 +02:00
|
|
|
frame.initialLayoutCompleted.connect(self._on_history_trigger)
|
2016-08-17 18:02:24 +02:00
|
|
|
|
2016-08-19 10:10:40 +02:00
|
|
|
def _event_target(self):
|
|
|
|
return self._widget
|