WebBrowser/WebBrowserView.py

changeset 5784
362bbcc49ac1
parent 5740
292538236234
child 5785
7c7c5f9e4fad
--- a/WebBrowser/WebBrowserView.py	Mon Jul 03 19:23:54 2017 +0200
+++ b/WebBrowser/WebBrowserView.py	Tue Jul 04 19:44:30 2017 +0200
@@ -17,7 +17,8 @@
 import os
 
 from PyQt5.QtCore import pyqtSignal, QUrl, QFileInfo, Qt, QTimer, QEvent, \
-    QPoint, QDateTime, QStandardPaths
+    QPoint, QPointF, QDateTime, QStandardPaths, QByteArray, QIODevice, \
+    QDataStream
 from PyQt5.QtGui import QDesktopServices, QClipboard, QIcon, \
     QContextMenuEvent, QPixmap
 from PyQt5.QtWidgets import qApp, QStyle, QMenu, QApplication
@@ -123,6 +124,8 @@
         self.__inspector = None
         WebInspector.registerView(self)
         
+        self.__restoreData = None
+        
         if qVersionTuple() >= (5, 8, 0):
             if self.parentWidget() is not None:
                 self.parentWidget().installEventFilter(self)
@@ -1894,3 +1897,99 @@
         Private slot to reset all speed dials to the default pages.
         """
         self.__speedDial.resetDials()
+    
+    ###########################################################################
+    ## Methods below implement session related functions
+    ###########################################################################
+    
+    def storeSessionData(self, data):
+        """
+        
+        """
+        self.__restoreData = data
+    
+    def showEvent(self, evt):
+        """
+        
+        """
+        if self.__restoreData:
+            self.loadFromSessionData(self.__restoreData)
+        self.__restoreData = None
+    
+    def getSessionData(self):
+        """
+        Public method to populate the session data.
+        
+        @return dictionary containing the session data
+        @rtype dict
+        """
+        if self.__restoreData:
+            # page has not been shown yet
+            return self.__restoreData
+        
+        
+        sessionData = {}
+##        page = self.page()
+        
+        # 1. zoom factor
+        sessionData["ZoomFactor"] = self.__page.zoomFactor()
+        
+        # 2. scroll position
+        scrollPos = self.__page.scrollPosition()
+        sessionData["ScrollPosition"] = {
+            "x": scrollPos.x(),
+            "y": scrollPos.y(),
+        }
+        
+        # 3. page history
+        historyArray = QByteArray()
+        stream = QDataStream(historyArray, QIODevice.WriteOnly)
+        stream << self.__page.history()
+        sessionData["History"] = str(
+            historyArray.toBase64(QByteArray.Base64UrlEncoding),
+            encoding="ascii")
+        sessionData["HistoryIndex"] = self.__page.history().currentItemIndex()
+        
+        # 4. current URL and title
+        sessionData["Url"] = self.url().toString()
+        sessionData["Title"] = self.title()
+        
+        # 5. web icon
+        iconArray = QByteArray()
+        stream = QDataStream(iconArray, QIODevice.WriteOnly)
+        stream << self.__page.icon()
+        sessionData["Icon"] = str(iconArray.toBase64(), encoding="ascii")
+        
+        return sessionData
+    
+    def loadFromSessionData(self, sessionData):
+        """
+        Public method to load the session data.
+        
+        @param sessionData dictionary containing the session data as
+            generated by getSessionData()
+        @type dict
+        """
+##        page = self.page()
+        
+        # 1. page history
+        if "History" in sessionData:
+            historyArray = QByteArray.fromBase64(
+                sessionData["History"].encode("ascii"),
+                QByteArray.Base64UrlEncoding)
+            stream = QDataStream(historyArray, QIODevice.ReadOnly)
+            stream >> self.__page.history()
+            
+            if "HistoryIndex" in sessionData:
+                item = self.__page.history().itemAt(sessionData["HistoryIndex"])
+                if item is not None:
+                    self.__page.history().goToItem(item)
+        
+        # 2. zoom factor
+        if "ZoomFactor" in sessionData:
+            self.__page.setZoomFactor(sessionData["ZoomFactor"])
+        
+        # 3. scroll position
+        if "ScrollPosition" in sessionData:
+            scrollPos = sessionData["ScrollPosition"]
+            self.__page.scrollTo(QPointF(scrollPos["x"], scrollPos["y"]))

eric ide

mercurial