Continued implementing session support for the new web browser.

Tue, 04 Jul 2017 19:44:30 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 04 Jul 2017 19:44:30 +0200
changeset 5784
362bbcc49ac1
parent 5783
44a9f08de394
child 5785
7c7c5f9e4fad

Continued implementing session support for the new web browser.

WebBrowser/WebBrowserPage.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserTabWidget.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserView.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserWindow.py file | annotate | diff | comparison | revisions
--- a/WebBrowser/WebBrowserPage.py	Mon Jul 03 19:23:54 2017 +0200
+++ b/WebBrowser/WebBrowserPage.py	Tue Jul 04 19:44:30 2017 +0200
@@ -378,70 +378,70 @@
     ####################################################
     ## 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")
-        sessionData["HistoryIndex"] = self.history().currentItemIndex()
-        
-        # 4. current URL
-        sessionData["Url"] = self.url().toString(
-            QUrl.PrettyDecoded | QUrl.RemovePassword)
-        
-        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
-        """
-        # 1. page history
-        if "History" in sessionData:
-            historyArray = QByteArray.fromBase64(
-                sessionData["History"].encode("ascii"),
-                QByteArray.Base64UrlEncoding)
-            stream = QDataStream(historyArray, QIODevice.ReadOnly)
-            stream >> self.history()
-            
-            if "HistoryIndex" in sessionData:
-                item = self.history().itemAt(sessionData["HistoryIndex"])
-                if item is not None:
-                    self.history().goToItem(item)
-        
-        # 2. zoom factor
-        if "ZoomFactor" in sessionData:
-            self.setZoomFactor(sessionData["ZoomFactor"])
-        
-        # 3. scroll position
-        if "ScrollPosition" in sessionData:
-            scrollPos = sessionData["ScrollPosition"]
-            self.scrollTo(QPointF(scrollPos["x"], scrollPos["y"]))
+##    
+##    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")
+##        sessionData["HistoryIndex"] = self.history().currentItemIndex()
+##        
+##        # 4. current URL
+##        sessionData["Url"] = self.url().toString(
+##            QUrl.PrettyDecoded | QUrl.RemovePassword)
+##        
+##        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
+##        """
+##        # 1. page history
+##        if "History" in sessionData:
+##            historyArray = QByteArray.fromBase64(
+##                sessionData["History"].encode("ascii"),
+##                QByteArray.Base64UrlEncoding)
+##            stream = QDataStream(historyArray, QIODevice.ReadOnly)
+##            stream >> self.history()
+##            
+##            if "HistoryIndex" in sessionData:
+##                item = self.history().itemAt(sessionData["HistoryIndex"])
+##                if item is not None:
+##                    self.history().goToItem(item)
+##        
+##        # 2. zoom factor
+##        if "ZoomFactor" in sessionData:
+##            self.setZoomFactor(sessionData["ZoomFactor"])
+##        
+##        # 3. scroll position
+##        if "ScrollPosition" in sessionData:
+##            scrollPos = sessionData["ScrollPosition"]
+##            self.scrollTo(QPointF(scrollPos["x"], scrollPos["y"]))
     
     ##################################################
     ## Methods below implement compatibility functions
--- a/WebBrowser/WebBrowserTabWidget.py	Mon Jul 03 19:23:54 2017 +0200
+++ b/WebBrowser/WebBrowserTabWidget.py	Tue Jul 04 19:44:30 2017 +0200
@@ -457,13 +457,15 @@
         
         if not restoreSession:
             if not linkName:
-                if Preferences.getWebBrowser("NewTabBehavior") == 1:
+                if Preferences.getWebBrowser("NewTabBehavior") == 0:
+                    linkName = "about:blank"
+                elif Preferences.getWebBrowser("NewTabBehavior") == 1:
                     linkName = Preferences.getWebBrowser("HomePage")
                 elif Preferences.getWebBrowser("NewTabBehavior") == 2:
                     linkName = "eric:speeddial"
-            else:
-                if linkName in ["about:blank", "eric:blank"]:
-                    linkName = ""
+        
+        if linkName == "eric:blank":
+            linkName = "about:blank"
         
         if linkName:
             browser.setSource(QUrl(linkName))
@@ -1214,7 +1216,7 @@
         sessionData["Tabs"] = []
         for index in range(self.count()):
             browser = self.widget(index)
-            data = browser.page().getSessionData()
+            data = browser.getSessionData()
             sessionData["Tabs"].append(data)
         
         return sessionData
@@ -1231,11 +1233,16 @@
         
         # 1. load tab data
         if "Tabs" in sessionData:
+            loadTabOnActivate = True
             for data in sessionData["Tabs"]:
-                browser = self.newBrowser(restoreSession=True)
-                browser.page().loadFromSessionData(data)
+                browser = self.newBrowser(link="about:blank", restoreSession=True)
+                if loadTabOnActivate:
+                    browser.storeSessionData(data)
+                else:
+                    browser.loadFromSessionData(data)
         
         # 2. set tab index
         if "CurrentTabIndex" in sessionData:
             index = tabCount + sessionData["CurrentTabIndex"]
             self.setCurrentIndex(index)
+            self.browserAt(index).show()
--- 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"]))
--- a/WebBrowser/WebBrowserWindow.py	Mon Jul 03 19:23:54 2017 +0200
+++ b/WebBrowser/WebBrowserWindow.py	Tue Jul 04 19:44:30 2017 +0200
@@ -374,7 +374,7 @@
                         restoreSession = True
                 else:
                     if Preferences.getWebBrowser("StartupBehavior") == 0:
-                        home = "eric:blank"
+                        home = "about:blank"
                     elif Preferences.getWebBrowser("StartupBehavior") == 1:
                         home = Preferences.getWebBrowser("HomePage")
                     elif Preferences.getWebBrowser("StartupBehavior") == 2:

eric ide

mercurial