implement requested changes for PR #3521.

This commit is contained in:
Marc Jauvin 2018-01-24 18:04:05 -05:00
parent 2e912eeadf
commit d7c51f7fc4
4 changed files with 35 additions and 25 deletions

View File

@ -94,7 +94,6 @@ class TabData:
keep_icon: Whether the (e.g. cloned) icon should not be cleared on page keep_icon: Whether the (e.g. cloned) icon should not be cleared on page
load. load.
inspector: The QWebInspector used for this webview. inspector: The QWebInspector used for this webview.
viewing_source: Set if we're currently showing a source view.
override_target: Override for open_target for fake clicks (like hints). override_target: Override for open_target for fake clicks (like hints).
Only used for QtWebKit. Only used for QtWebKit.
pinned: Flag to pin the tab. pinned: Flag to pin the tab.
@ -102,7 +101,6 @@ class TabData:
""" """
keep_icon = attr.ib(False) keep_icon = attr.ib(False)
viewing_source = attr.ib(False)
inspector = attr.ib(None) inspector = attr.ib(None)
override_target = attr.ib(None) override_target = attr.ib(None)
pinned = attr.ib(False) pinned = attr.ib(False)
@ -139,7 +137,7 @@ class AbstractAction:
raise WebTabError("{} is not a valid web action!".format(name)) raise WebTabError("{} is not a valid web action!".format(name))
self._widget.triggerPageAction(member) self._widget.triggerPageAction(member)
def show_source(self, dispatcher, url): def show_source(self, win_id, url):
"""Show the source of the current page in a new tab.""" """Show the source of the current page in a new tab."""
raise NotImplementedError raise NotImplementedError
@ -727,7 +725,6 @@ class AbstractTab(QWidget):
def _on_load_started(self): def _on_load_started(self):
self._progress = 0 self._progress = 0
self._has_ssl_errors = False self._has_ssl_errors = False
self.data.viewing_source = False
self._set_load_status(usertypes.LoadStatus.loading) self._set_load_status(usertypes.LoadStatus.loading)
self.load_started.emit() self.load_started.emit()

View File

@ -29,9 +29,6 @@ from PyQt5.QtWidgets import QApplication, QTabBar, QDialog
from PyQt5.QtCore import Qt, QUrl, QEvent, QUrlQuery from PyQt5.QtCore import Qt, QUrl, QEvent, QUrlQuery
from PyQt5.QtGui import QKeyEvent from PyQt5.QtGui import QKeyEvent
from PyQt5.QtPrintSupport import QPrintDialog, QPrintPreviewDialog from PyQt5.QtPrintSupport import QPrintDialog, QPrintPreviewDialog
import pygments
import pygments.lexers
import pygments.formatters
from qutebrowser.commands import userscripts, cmdexc, cmdutils, runners from qutebrowser.commands import userscripts, cmdexc, cmdutils, runners
from qutebrowser.config import config, configdata from qutebrowser.config import config, configdata
@ -758,6 +755,7 @@ class CommandDispatcher:
else: else:
x = None x = None
y = perc y = perc
self._current_widget().scroller.to_perc(x, y) self._current_widget().scroller.to_perc(x, y)
@cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.register(instance='command-dispatcher', scope='window')
@ -1503,15 +1501,15 @@ class CommandDispatcher:
def view_source(self): def view_source(self):
"""Show the source of the current page in a new tab.""" """Show the source of the current page in a new tab."""
tab = self._current_widget() tab = self._current_widget()
if tab.data.viewing_source:
raise cmdexc.CommandError("Already viewing source!")
try: try:
current_url = self._current_url() current_url = self._current_url()
except cmdexc.CommandError as e: except cmdexc.CommandError as e:
message.error(str(e)) message.error(str(e))
return return
tab.action.show_source(self, current_url) if current_url.scheme() == 'view-source':
raise cmdexc.CommandError("Already viewing source!")
tab.action.show_source(self._win_id, current_url)
@cmdutils.register(instance='command-dispatcher', scope='window', @cmdutils.register(instance='command-dispatcher', scope='window',
debug=True) debug=True)

View File

@ -99,12 +99,18 @@ class WebEngineAction(browsertab.AbstractAction):
"""Save the current page.""" """Save the current page."""
self._widget.triggerPageAction(QWebEnginePage.SavePage) self._widget.triggerPageAction(QWebEnginePage.SavePage)
def show_source(self, dispatcher, url): def show_source(self, win_id, url):
# use view-source: scheme to show page source for webengine try:
url = QUrl('view-source:{}'.format(url.toString())) self._widget.triggerPageAction(QWebEnginePage.ViewSource)
new_tab = dispatcher._tabbed_browser.tabopen( except AttributeError:
url, background=True, related=True) # Qt < 5.8
new_tab.data.viewing_source = True # note: it's not possible to build the QUrl object by setting the
# scheme using setScheme('view-source').
# QUrl does the right thing when URL prefix is "view-source:".
new_url = QUrl('view-source:' + url.toString())
tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=win_id)
tabbed_browser.tabopen(new_url, background=False, related=True)
class WebEnginePrinting(browsertab.AbstractPrinting): class WebEnginePrinting(browsertab.AbstractPrinting):

View File

@ -54,7 +54,15 @@ class WebKitAction(browsertab.AbstractAction):
"""Save the current page.""" """Save the current page."""
raise browsertab.UnsupportedOperationError raise browsertab.UnsupportedOperationError
def show_source(self, dispatcher, url): def show_source(self, win_id, url):
def format_url(url):
"""emulate what WebEnginePage::ViewSource does."""
s = url.toString()
s = s.split('//')[-1] # strip scheme
s = s.split('@')[-1] # strip userinfo
return s
def show_source_cb(source): def show_source_cb(source):
"""Show source as soon as it's ready.""" """Show source as soon as it's ready."""
# WORKAROUND for https://github.com/PyCQA/pylint/issues/491 # WORKAROUND for https://github.com/PyCQA/pylint/issues/491
@ -62,16 +70,17 @@ class WebKitAction(browsertab.AbstractAction):
lexer = pygments.lexers.HtmlLexer() lexer = pygments.lexers.HtmlLexer()
formatter = pygments.formatters.HtmlFormatter( formatter = pygments.formatters.HtmlFormatter(
full=True, linenos='table', full=True, linenos='table',
title='view-source:{}'.format(url.toDisplayString())) title='view-source:' + format_url(url))
# pylint: enable=no-member # pylint: enable=no-member
highlighted = pygments.highlight(source, lexer, formatter) highlighted = pygments.highlight(source, lexer, formatter)
new_tab = dispatcher._tabbed_browser.tabopen() base_url = QUrl('view-source:' + url.toString())
new_tab.set_html(highlighted) new_tab = tabbed_browser.tabopen(background=False, related=True)
new_tab.data.viewing_source = True new_tab.set_html(highlighted, base_url)
new_tab.url = lambda requested=False: QUrl(
'view-source:' + url.toDisplayString()) tabbed_browser = objreg.get('tabbed-browser', scope='window',
dispatcher._current_widget().dump_async(show_source_cb) window=win_id)
tabbed_browser.currentWidget().dump_async(show_source_cb)
class WebKitPrinting(browsertab.AbstractPrinting): class WebKitPrinting(browsertab.AbstractPrinting):