Skip to content

Commit

Permalink
Help: Fix rendering internal and external links with WebEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Jun 2, 2016
1 parent f601b43 commit 4b21e1e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
12 changes: 5 additions & 7 deletions spyderlib/plugins/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from qtpy.QtWidgets import (QActionGroup, QComboBox, QGroupBox, QHBoxLayout,
QLabel, QLineEdit, QMenu, QMessageBox, QSizePolicy,
QToolButton, QVBoxLayout, QWidget)
from qtpy.QtWebEngineWidgets import QWebEnginePage
from qtpy.QtWebEngineWidgets import QWebEnginePage, WEBENGINE

# Local imports
from spyderlib import dependencies
Expand Down Expand Up @@ -475,13 +475,11 @@ def __init__(self, parent):
self._on_sphinx_thread_html_ready)
self._sphinx_thread.error_msg.connect(self._on_sphinx_thread_error_msg)

# Render internal links
try:
view = self.rich_text.webview
# Handle internal and external links
view = self.rich_text.webview
if not WEBENGINE:
view.page().setLinkDelegationPolicy(QWebEnginePage.DelegateAllLinks)
view.linkClicked.connect(self.handle_link_clicks)
except AttributeError:
pass
view.linkClicked.connect(self.handle_link_clicks)

self._starting_up = True

Expand Down
38 changes: 28 additions & 10 deletions spyderlib/widgets/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import sys

# Third party imports
from qtpy import PYQT5
from qtpy.QtCore import QUrl, Signal, Slot
from qtpy.QtWidgets import (QFrame, QHBoxLayout, QLabel, QProgressBar, QMenu,
QVBoxLayout, QWidget)
from qtpy.QtWebEngineWidgets import QWebEnginePage, QWebEngineSettings, QWebEngineView
from qtpy.QtWebEngineWidgets import (QWebEnginePage, QWebEngineSettings,
QWebEngineView, WEBENGINE)

# Local imports
from spyderlib.config.base import _, DEV
Expand All @@ -25,8 +27,23 @@
from spyderlib.widgets.findreplace import FindReplace


if WEBENGINE:
class WebPage(QWebEnginePage):
"""Web page"""
linkClicked = Signal(QUrl)

def acceptNavigationRequest(self, url, navigation_type, isMainFrame):
"""
Overloaded method to handle links ourselves
"""
if navigation_type == QWebEnginePage.NavigationTypeLinkClicked:
self.linkClicked.emit(url)
return False
return True


class WebView(QWebEngineView):
"""Web page"""
"""Web view"""
def __init__(self, parent):
QWebEngineView.__init__(self, parent)
self.zoom_factor = 1.
Expand All @@ -36,7 +53,10 @@ def __init__(self, parent):
self.zoom_in_action = create_action(self, _("Zoom in"),
icon=ima.icon('zoom_in'),
triggered=self.zoom_in)

if WEBENGINE:
web_page = WebPage(self)
self.setPage(web_page)

def find_text(self, text, changed=True,
forward=True, case=False, words=False,
regexp=False):
Expand Down Expand Up @@ -106,7 +126,7 @@ def contextMenuEvent(self, event):
self.pageAction(QWebEnginePage.SelectAll),
self.pageAction(QWebEnginePage.Copy), None,
self.zoom_in_action, self.zoom_out_action]
if DEV:
if DEV and not WEBENGINE:
settings = self.page().settings()
settings.setAttribute(QWebEngineSettings.DeveloperExtrasEnabled, True)
actions += [None, self.pageAction(QWebEnginePage.InspectElement)]
Expand Down Expand Up @@ -158,10 +178,8 @@ def __init__(self, parent=None):

self.url_combo = UrlComboBox(self)
self.url_combo.valid.connect(self.url_combo_activated)
try:
if not WEBENGINE:
self.webview.iconChanged.connect(self.icon_changed)
except AttributeError:
pass

self.find_widget = FindReplace(self)
self.find_widget.set_editor(self.webview)
Expand Down Expand Up @@ -263,10 +281,10 @@ def __init__(self, parent):

self.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)

try:
if WEBENGINE:
self._webview.page().linkClicked.connect(self.linkClicked)
else:
self._webview.linkClicked.connect(self.linkClicked)
except AttributeError:
pass

def set_font(self, font, fixed_font=None):
self._webview.set_font(font, fixed_font=fixed_font)
Expand Down

0 comments on commit 4b21e1e

Please sign in to comment.