Added support for printing from within a web page.

Mon, 12 Feb 2018 19:50:24 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 12 Feb 2018 19:50:24 +0100
changeset 6142
304a469455c8
parent 6141
f4cecd941096
child 6143
bedf7ca6cea0

Added support for printing from within a web page.

WebBrowser/Tools/Scripts.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserPage.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserView.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserWindow.py file | annotate | diff | comparison | revisions
--- a/WebBrowser/Tools/Scripts.py	Mon Feb 12 19:19:50 2018 +0100
+++ b/WebBrowser/Tools/Scripts.py	Mon Feb 12 19:50:24 2018 +0100
@@ -86,6 +86,27 @@
         match, readAllFileContents(":/javascript/qwebchannel.js"))
 
 
+def setupWindowObject():
+    """
+    Function generating a script to setup window.object add-ons.
+    
+    @return generated script
+    @rtype str
+    """
+    source = """
+(function() {
+var external = {};
+
+window.external = external;
+
+window.print = function() {
+    window.location = 'eric:PrintPage';
+};
+})()"""
+    
+    return source
+
+
 def setStyleSheet(css):
     """
     Function generating a script to set a user style sheet.
--- a/WebBrowser/WebBrowserPage.py	Mon Feb 12 19:19:50 2018 +0100
+++ b/WebBrowser/WebBrowserPage.py	Mon Feb 12 19:50:24 2018 +0100
@@ -41,6 +41,8 @@
         browsing event
     @signal safeBrowsingBad(threatType, threatMessages) emitted to indicate a
         malicious web site as determined by safe browsing
+    @signal printRequested() emitted to indicate a print request of the shown
+        web page
     """
     if qVersionTuple() >= (5, 7, 0):
         SafeJsWorld = QWebEngineScript.ApplicationWorld
@@ -52,6 +54,8 @@
     safeBrowsingAbort = pyqtSignal()
     safeBrowsingBad = pyqtSignal(str, str)
     
+    printRequested = pyqtSignal()
+    
     def __init__(self, parent=None):
         """
         Constructor
@@ -139,6 +143,11 @@
             WebBrowserWindow.greaseMonkeyManager().downloadScript(url)
             return False
         
+        if url.scheme() == "eric":
+            if url.path() == "PrintPage":
+                self.printRequested.emit()
+                return False
+        
         # Safe Browsing
         self.__badSite = False
         from WebBrowser.SafeBrowsing.SafeBrowsingManager import \
--- a/WebBrowser/WebBrowserView.py	Mon Feb 12 19:19:50 2018 +0100
+++ b/WebBrowser/WebBrowserView.py	Mon Feb 12 19:50:24 2018 +0100
@@ -16,9 +16,9 @@
 
 import os
 
-from PyQt5.QtCore import pyqtSignal, PYQT_VERSION, Qt, QUrl, QFileInfo, \
-    QTimer, QEvent, QPoint, QPointF, QDateTime, QStandardPaths, QByteArray, \
-    QIODevice, QDataStream
+from PyQt5.QtCore import pyqtSignal, pyqtSlot, PYQT_VERSION, Qt, QUrl, \
+    QFileInfo, QTimer, QEvent, QPoint, QPointF, QDateTime, QStandardPaths, \
+    QByteArray, QIODevice, QDataStream
 from PyQt5.QtGui import QDesktopServices, QClipboard, QIcon, \
     QContextMenuEvent, QPixmap
 from PyQt5.QtWidgets import qApp, QStyle, QMenu, QApplication
@@ -152,6 +152,7 @@
         
         self.__page.safeBrowsingAbort.connect(self.safeBrowsingAbort)
         self.__page.safeBrowsingBad.connect(self.safeBrowsingBad)
+        self.__page.printRequested.connect(self.__printPage)
     
     def __setRwhvqt(self):
         """
@@ -2156,3 +2157,14 @@
             return self.__page.getSafeBrowsingStatus()
         else:
             return True
+    
+    ###########################################################################
+    ## Methods below implement print support from the page
+    ###########################################################################
+    
+    @pyqtSlot()
+    def __printPage(self):
+        """
+        Private slot to support printing from the web page.
+        """
+        self.__mw.tabWidget.printBrowser(browser=self)
--- a/WebBrowser/WebBrowserWindow.py	Mon Feb 12 19:19:50 2018 +0100
+++ b/WebBrowser/WebBrowserWindow.py	Mon Feb 12 19:50:24 2018 +0100
@@ -4887,9 +4887,10 @@
                 # not yet supported
                 pass
             
-            # Setup QWebChannel user script
+            # Setup QWebChannel user scripts
             from .WebBrowserPage import WebBrowserPage
-
+            
+            # WebChannel for SafeJsWorld
             script = QWebEngineScript()
             script.setName("_eric_webchannel")
             script.setInjectionPoint(QWebEngineScript.DocumentCreation)
@@ -4897,7 +4898,8 @@
             script.setRunsOnSubFrames(True)
             script.setSourceCode(Scripts.setupWebChannel(script.worldId()))
             cls._webProfile.scripts().insert(script)
-
+            
+            # WebChannel for UnsafeJsWorld
             script2 = QWebEngineScript()
             script2.setName("_eric_webchannel2")
             script2.setInjectionPoint(QWebEngineScript.DocumentCreation)
@@ -4905,6 +4907,15 @@
             script2.setRunsOnSubFrames(True)
             script2.setSourceCode(Scripts.setupWebChannel(script2.worldId()))
             cls._webProfile.scripts().insert(script2)
+            
+            # document.window object addons
+            script3 = QWebEngineScript()
+            script3.setName("_eric_window_object")
+            script3.setInjectionPoint(QWebEngineScript.DocumentCreation)
+            script3.setWorldId(WebBrowserPage.UnsafeJsWorld)
+            script3.setRunsOnSubFrames(True)
+            script3.setSourceCode(Scripts.setupWindowObject())
+            cls._webProfile.scripts().insert(script3)
         
         return cls._webProfile
     

eric ide

mercurial