Started to implement session support for the new web browser.

Wed, 28 Jun 2017 20:05:15 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 28 Jun 2017 20:05:15 +0200
changeset 5774
a559df54a729
parent 5772
b5ef56fa3a48
child 5775
3a8bedba97ab

Started to implement session support for the new web browser.

UI/Previewers/PreviewerHTML.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserPage.py file | annotate | diff | comparison | revisions
--- a/UI/Previewers/PreviewerHTML.py	Tue Jun 27 19:15:41 2017 +0200
+++ b/UI/Previewers/PreviewerHTML.py	Wed Jun 28 20:05:15 2017 +0200
@@ -246,6 +246,7 @@
                 frame.scrollBarMaximum(Qt.Vertical) == pos.y()
         else:
             from PyQt5.QtCore import QPoint
+            # TODO: replace this by QWebEnginePage.scrollPosition()
             pos = self.__execJavaScript(
                 "(function() {"
                 "var res = {"
--- a/WebBrowser/WebBrowserPage.py	Tue Jun 27 19:15:41 2017 +0200
+++ b/WebBrowser/WebBrowserPage.py	Wed Jun 28 20:05:15 2017 +0200
@@ -14,7 +14,8 @@
 except NameError:
     pass
 
-from PyQt5.QtCore import pyqtSlot, QUrl, QTimer, QEventLoop, QPoint
+from PyQt5.QtCore import pyqtSlot, QUrl, QTimer, QEventLoop, QPoint, QPointF, \
+    QByteArray, QDataStream, QIODevice
 from PyQt5.QtGui import QDesktopServices
 from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineSettings, \
     QWebEngineScript
@@ -253,6 +254,18 @@
             WebBrowserPage.SafeJsWorld
         )
     
+    def scrollTo(self, pos):
+        """
+        Public method to scroll to the given position.
+        
+        @param pos position to scroll to
+        @type QPointF
+        """
+        self.runJavaScript(
+            "window.scrollTo({0}, {1});".format(pos.x(), pos.y()),
+            WebBrowserPage.SafeJsWorld
+        )
+    
     def mapToViewport(self, pos):
         """
         Public method to map a position to the viewport.
@@ -362,6 +375,39 @@
         self.view().mainWindow().javascriptConsole().javaScriptConsoleMessage(
             level, message, lineNumber, sourceId)
     
+    ####################################################
+    ## Methods below implement session related functions
+    ####################################################
+    
+    def getSessionData(self):
+        """
+        Public method to populate the session data.
+        
+        @return dictionary containing the session data
+        @rtype dict
+        """
+        sessionData = {}
+        
+        # 1. zoom factor
+        sessionData["ZoomFactor"] = self.zoomFactor()
+        
+        # 2. scroll position
+        scrollPos = self.scrollPosition()
+        sessionData["ScrollPosition"] = {
+            "x": scrollPos.x(),
+            "y": scrollPos.y(),
+        }
+        
+        # 3. page history
+        historyArray = QByteArray()
+        stream = QDataStream(historyArray, QIODevice.WriteOnly)
+        stream << self.history()
+        sessionData["History"] = str(
+            historyArray.toBase64(QByteArray.Base64UrlEncoding),
+            encoding="ascii")
+        
+        return sessionData
+    
     ##################################################
     ## Methods below implement compatibility functions
     ##################################################
@@ -375,3 +421,30 @@
             @rtype QIcon
             """
             return self.view().icon()
+    
+    if not hasattr(QWebEnginePage, "scrollPosition"):
+        def scrollPosition(self):
+            """
+            Public method to get the scroll position of the web page.
+            
+            @return scroll position
+            @rtype QPointF
+            """
+            pos = self.execJavaScript(
+                "(function() {"
+                "var res = {"
+                "    x: 0,"
+                "    y: 0,"
+                "};"
+                "res.x = window.scrollX;"
+                "res.y = window.scrollY;"
+                "return res;"
+                "})()",
+                WebBrowserPage.SafeJsWorld
+            )
+            if pos is not None:
+                pos = QPointF(pos["x"], pos["y"])
+            else:
+                pos = QPointF(0.0, 0.0)
+            
+            return pos

eric ide

mercurial