eric6/UI/Previewers/PreviewerHTML.py

branch
maintenance
changeset 7286
7eb04391adf7
parent 6989
8b8cadf8d7e9
parent 7264
bedbe458d792
child 7322
cd8ee889589f
--- a/eric6/UI/Previewers/PreviewerHTML.py	Mon Sep 09 18:52:08 2019 +0200
+++ b/eric6/UI/Previewers/PreviewerHTML.py	Thu Oct 03 11:12:10 2019 +0200
@@ -7,13 +7,6 @@
 Module implementing a previewer widget for HTML, Markdown and ReST files.
 """
 
-from __future__ import unicode_literals
-
-try:  # Only for Py2
-    import StringIO as io   # __IGNORE_EXCEPTION__
-    str = unicode
-except (ImportError, NameError):
-    import io       # __IGNORE_WARNING__
 
 import os
 import threading
@@ -21,11 +14,13 @@
 import shutil
 import tempfile
 import sys
+import io
 
-from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QUrl, QSize, QThread
+from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QUrl, QThread
 from PyQt5.QtGui import QCursor
-from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QCheckBox, \
-    QSizePolicy, QToolTip
+from PyQt5.QtWidgets import (
+    QWidget, QVBoxLayout, QLabel, QCheckBox, QSizePolicy, QToolTip
+)
 
 from E5Gui.E5Application import e5App
 
@@ -58,22 +53,14 @@
             from PyQt5.QtWebEngineWidgets import QWebEngineView
             self.previewView = QWebEngineView(self)
             self.previewView.page().linkHovered.connect(self.__showLink)
-            self.__usesWebKit = False
         except ImportError:
-            try:
-                from PyQt5.QtWebKitWidgets import QWebPage, QWebView
-                self.previewView = QWebView(self)
-                self.previewView.page().setLinkDelegationPolicy(
-                    QWebPage.DelegateAllLinks)
-                self.__usesWebKit = True
-            except ImportError:
-                self.__previewAvailable = False
-                self.titleLabel.setText(self.tr(
-                    "<b>HTML Preview is not available!<br/>"
-                    "Install QtWebEngine or QtWebKit.</b>"))
-                self.titleLabel.setAlignment(Qt.AlignHCenter)
-                self.__layout.addStretch()
-                return
+            self.__previewAvailable = False
+            self.titleLabel.setText(self.tr(
+                "<b>HTML Preview is not available!<br/>"
+                "Install QtWebEngine.</b>"))
+            self.titleLabel.setAlignment(Qt.AlignHCenter)
+            self.__layout.addStretch()
+            return
         
         sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
         sizePolicy.setHorizontalStretch(0)
@@ -99,9 +86,6 @@
         self.jsCheckBox.clicked[bool].connect(self.on_jsCheckBox_clicked)
         self.ssiCheckBox.clicked[bool].connect(self.on_ssiCheckBox_clicked)
         self.previewView.titleChanged.connect(self.on_previewView_titleChanged)
-        if self.__usesWebKit:
-            self.previewView.linkClicked.connect(
-                self.on_previewView_linkClicked)
         
         self.jsCheckBox.setChecked(
             Preferences.getUI("ShowFilePreviewJS"))
@@ -189,17 +173,23 @@
                 extension = os.path.normcase(os.path.splitext(fn)[1][1:])
             else:
                 extension = ""
-            if extension in \
-                Preferences.getEditor("PreviewHtmlFileNameExtensions") or \
-                    editor.getLanguage() == "HTML":
+            if (
+                extension in Preferences.getEditor(
+                    "PreviewHtmlFileNameExtensions") or
+                editor.getLanguage() == "HTML"
+            ):
                 language = "HTML"
-            elif extension in \
-                Preferences.getEditor("PreviewMarkdownFileNameExtensions") or \
-                    editor.getLanguage().lower() == "markdown":
+            elif (
+                extension in Preferences.getEditor(
+                    "PreviewMarkdownFileNameExtensions") or
+                editor.getLanguage().lower() == "markdown"
+            ):
                 language = "Markdown"
-            elif extension in \
-                Preferences.getEditor("PreviewRestFileNameExtensions") or \
-                    editor.getLanguage().lower() == "restructuredtext":
+            elif (
+                extension in Preferences.getEditor(
+                    "PreviewRestFileNameExtensions") or
+                editor.getLanguage().lower() == "restructuredtext"
+            ):
                 language = "ReST"
             else:
                 self.__setHtml(fn, self.tr(
@@ -239,14 +229,10 @@
         self.__previewedPath = Utilities.normcasepath(
             Utilities.fromNativeSeparators(filePath))
         self.__saveScrollBarPositions()
-        if self.__usesWebKit:
-            self.previewView.page().mainFrame().contentsSizeChanged.connect(
-                self.__restoreScrollBarPositions)
-        else:
-            self.previewView.page().loadFinished.connect(
-                self.__restoreScrollBarPositions)
-            if not filePath:
-                filePath = "/"
+        self.previewView.page().loadFinished.connect(
+            self.__restoreScrollBarPositions)
+        if not filePath:
+            filePath = "/"
         if rootPath:
             baseUrl = QUrl.fromLocalFile(rootPath + "/index.html")
         else:
@@ -271,84 +257,39 @@
         """
         Private method to save scroll bar positions for a previewed editor.
         """
-        if self.__usesWebKit:
-            frame = self.previewView.page().mainFrame()
-            if frame.contentsSize() == QSize(0, 0):
-                return  # no valid data, nothing to save
-            
-            pos = frame.scrollPosition()
-            self.__scrollBarPositions[self.__previewedPath] = pos
-            self.__hScrollBarAtEnd[self.__previewedPath] = \
-                frame.scrollBarMaximum(Qt.Horizontal) == pos.x()
-            self.__vScrollBarAtEnd[self.__previewedPath] = \
-                frame.scrollBarMaximum(Qt.Vertical) == pos.y()
-        else:
-            from PyQt5.QtCore import QPoint
-            try:
-                pos = self.previewView.scrollPosition()
-            except AttributeError:
-                pos = self.__execJavaScript(
-                    "(function() {"
-                    "var res = {"
-                    "    x: 0,"
-                    "    y: 0,"
-                    "};"
-                    "res.x = window.scrollX;"
-                    "res.y = window.scrollY;"
-                    "return res;"
-                    "})()"
-                )
-                if pos is not None:
-                    pos = QPoint(pos["x"], pos["y"])
-                else:
-                    pos = QPoint(0, 0)
-            self.__scrollBarPositions[self.__previewedPath] = pos
-            self.__hScrollBarAtEnd[self.__previewedPath] = False
-            self.__vScrollBarAtEnd[self.__previewedPath] = False
+        from PyQt5.QtCore import QPoint
+        try:
+            pos = self.previewView.scrollPosition()
+        except AttributeError:
+            pos = self.__execJavaScript(
+                "(function() {"
+                "var res = {"
+                "    x: 0,"
+                "    y: 0,"
+                "};"
+                "res.x = window.scrollX;"
+                "res.y = window.scrollY;"
+                "return res;"
+                "})()"
+            )
+            if pos is not None:
+                pos = QPoint(pos["x"], pos["y"])
+            else:
+                pos = QPoint(0, 0)
+        self.__scrollBarPositions[self.__previewedPath] = pos
+        self.__hScrollBarAtEnd[self.__previewedPath] = False
+        self.__vScrollBarAtEnd[self.__previewedPath] = False
 
     def __restoreScrollBarPositions(self):
         """
         Private method to restore scroll bar positions for a previewed editor.
         """
-        if self.__usesWebKit:
-            try:
-                self.previewView.page().mainFrame().contentsSizeChanged.\
-                    disconnect(self.__restoreScrollBarPositions)
-            except TypeError:
-                # not connected, simply ignore it
-                pass
-            
-            if self.__previewedPath not in self.__scrollBarPositions:
-                return
-            
-            frame = self.previewView.page().mainFrame()
-            frame.setScrollPosition(
-                self.__scrollBarPositions[self.__previewedPath])
-            
-            if self.__hScrollBarAtEnd[self.__previewedPath]:
-                frame.setScrollBarValue(
-                    Qt.Horizontal, frame.scrollBarMaximum(Qt.Horizontal))
-            
-            if self.__vScrollBarAtEnd[self.__previewedPath]:
-                frame.setScrollBarValue(
-                    Qt.Vertical, frame.scrollBarMaximum(Qt.Vertical))
-        else:
-            if self.__previewedPath not in self.__scrollBarPositions:
-                return
-            
-            pos = self.__scrollBarPositions[self.__previewedPath]
-            self.previewView.page().runJavaScript(
-                "window.scrollTo({0}, {1});".format(pos.x(), pos.y()))
-    
-    @pyqtSlot(QUrl)
-    def on_previewView_linkClicked(self, url):
-        """
-        Private slot handling the clicking of a link.
+        if self.__previewedPath not in self.__scrollBarPositions:
+            return
         
-        @param url URL of the clicked link
-        @type QUrl
-        """
-        e5App().getObject("UserInterface").launchHelpViewer(url)
+        pos = self.__scrollBarPositions[self.__previewedPath]
+        self.previewView.page().runJavaScript(
+            "window.scrollTo({0}, {1});".format(pos.x(), pos.y()))
     
     def __execJavaScript(self, script):
         """
@@ -717,9 +658,11 @@
             extensions = ['fenced_code', 'extra']
         
         # version 2.0 supports only extension names, not instances
-        if markdown.version_info[0] > 2 or \
-                (markdown.version_info[0] == 2 and
-                 markdown.version_info[1] > 0):
+        if (
+            markdown.version_info[0] > 2 or
+            (markdown.version_info[0] == 2 and
+             markdown.version_info[1] > 0)
+        ):
             class _StrikeThroughExtension(markdown.Extension):
                 """
                 Class is placed here, because it depends on imported markdown,

eric ide

mercurial