WebBrowser/WebBrowserView.py

changeset 5785
7c7c5f9e4fad
parent 5784
362bbcc49ac1
child 5793
4ff73b969ab1
equal deleted inserted replaced
5784:362bbcc49ac1 5785:7c7c5f9e4fad
1902 ## Methods below implement session related functions 1902 ## Methods below implement session related functions
1903 ########################################################################### 1903 ###########################################################################
1904 1904
1905 def storeSessionData(self, data): 1905 def storeSessionData(self, data):
1906 """ 1906 """
1907 1907 Public method to store session data to be restored later on.
1908
1909 @param data dictionary with session data to be restored
1910 @type dict
1908 """ 1911 """
1909 self.__restoreData = data 1912 self.__restoreData = data
1910 1913
1911 def showEvent(self, evt): 1914 def __showEventSlot(self):
1912 """ 1915 """
1913 1916 Private slot to perform actions when the view is shown and the event
1917 loop is running.
1914 """ 1918 """
1915 if self.__restoreData: 1919 if self.__restoreData:
1916 self.loadFromSessionData(self.__restoreData) 1920 self.loadFromSessionData(self.__restoreData)
1917 self.__restoreData = None 1921 self.__restoreData = None
1922
1923 def showEvent(self, evt):
1924 """
1925 Protected method to handle show events.
1926
1927 @param evt reference to the show event object
1928 @type QShowEvent
1929 """
1930 super(WebBrowserView, self).showEvent(evt)
1931 self.activateSession()
1932
1933 def activateSession(self):
1934 """
1935 Private slot to activate a restored session.
1936 """
1937 if self.__restoreData:
1938 QTimer.singleShot(0, self.__showEventSlot)
1918 1939
1919 def getSessionData(self): 1940 def getSessionData(self):
1920 """ 1941 """
1921 Public method to populate the session data. 1942 Public method to populate the session data.
1922 1943
1925 """ 1946 """
1926 if self.__restoreData: 1947 if self.__restoreData:
1927 # page has not been shown yet 1948 # page has not been shown yet
1928 return self.__restoreData 1949 return self.__restoreData
1929 1950
1930
1931 sessionData = {} 1951 sessionData = {}
1932 ## page = self.page() 1952 page = self.page()
1933 1953
1934 # 1. zoom factor 1954 # 1. zoom factor
1935 sessionData["ZoomFactor"] = self.__page.zoomFactor() 1955 sessionData["ZoomFactor"] = page.zoomFactor()
1936 1956
1937 # 2. scroll position 1957 # 2. scroll position
1938 scrollPos = self.__page.scrollPosition() 1958 scrollPos = page.scrollPosition()
1939 sessionData["ScrollPosition"] = { 1959 sessionData["ScrollPosition"] = {
1940 "x": scrollPos.x(), 1960 "x": scrollPos.x(),
1941 "y": scrollPos.y(), 1961 "y": scrollPos.y(),
1942 } 1962 }
1943 1963
1944 # 3. page history 1964 # 3. page history
1945 historyArray = QByteArray() 1965 historyArray = QByteArray()
1946 stream = QDataStream(historyArray, QIODevice.WriteOnly) 1966 stream = QDataStream(historyArray, QIODevice.WriteOnly)
1947 stream << self.__page.history() 1967 stream << page.history()
1948 sessionData["History"] = str( 1968 sessionData["History"] = str(
1949 historyArray.toBase64(QByteArray.Base64UrlEncoding), 1969 historyArray.toBase64(QByteArray.Base64UrlEncoding),
1950 encoding="ascii") 1970 encoding="ascii")
1951 sessionData["HistoryIndex"] = self.__page.history().currentItemIndex() 1971 sessionData["HistoryIndex"] = page.history().currentItemIndex()
1952 1972
1953 # 4. current URL and title 1973 # 4. current URL and title
1954 sessionData["Url"] = self.url().toString() 1974 sessionData["Url"] = self.url().toString()
1955 sessionData["Title"] = self.title() 1975 sessionData["Title"] = self.title()
1956 1976
1957 # 5. web icon 1977 # 5. web icon
1958 iconArray = QByteArray() 1978 iconArray = QByteArray()
1959 stream = QDataStream(iconArray, QIODevice.WriteOnly) 1979 stream = QDataStream(iconArray, QIODevice.WriteOnly)
1960 stream << self.__page.icon() 1980 stream << page.icon()
1961 sessionData["Icon"] = str(iconArray.toBase64(), encoding="ascii") 1981 sessionData["Icon"] = str(iconArray.toBase64(), encoding="ascii")
1962 1982
1963 return sessionData 1983 return sessionData
1964 1984
1965 def loadFromSessionData(self, sessionData): 1985 def loadFromSessionData(self, sessionData):
1968 1988
1969 @param sessionData dictionary containing the session data as 1989 @param sessionData dictionary containing the session data as
1970 generated by getSessionData() 1990 generated by getSessionData()
1971 @type dict 1991 @type dict
1972 """ 1992 """
1973 ## page = self.page() 1993 page = self.page()
1994 # blank the page
1995 page.setUrl(QUrl("about:blank"))
1974 1996
1975 # 1. page history 1997 # 1. page history
1976 if "History" in sessionData: 1998 if "History" in sessionData:
1977 historyArray = QByteArray.fromBase64( 1999 historyArray = QByteArray.fromBase64(
1978 sessionData["History"].encode("ascii"), 2000 sessionData["History"].encode("ascii"),
1979 QByteArray.Base64UrlEncoding) 2001 QByteArray.Base64UrlEncoding)
1980 stream = QDataStream(historyArray, QIODevice.ReadOnly) 2002 stream = QDataStream(historyArray, QIODevice.ReadOnly)
1981 stream >> self.__page.history() 2003 stream >> page.history()
1982 2004
1983 if "HistoryIndex" in sessionData: 2005 if "HistoryIndex" in sessionData:
1984 item = self.__page.history().itemAt(sessionData["HistoryIndex"]) 2006 item = page.history().itemAt(sessionData["HistoryIndex"])
1985 if item is not None: 2007 if item is not None:
1986 self.__page.history().goToItem(item) 2008 page.history().goToItem(item)
1987 2009
1988 # 2. zoom factor 2010 # 2. zoom factor
1989 if "ZoomFactor" in sessionData: 2011 if "ZoomFactor" in sessionData:
1990 self.__page.setZoomFactor(sessionData["ZoomFactor"]) 2012 page.setZoomFactor(sessionData["ZoomFactor"])
1991 2013
1992 # 3. scroll position 2014 # 3. scroll position
1993 if "ScrollPosition" in sessionData: 2015 if "ScrollPosition" in sessionData:
1994 scrollPos = sessionData["ScrollPosition"] 2016 scrollPos = sessionData["ScrollPosition"]
1995 self.__page.scrollTo(QPointF(scrollPos["x"], scrollPos["y"])) 2017 page.scrollTo(QPointF(scrollPos["x"], scrollPos["y"]))
2018
2019 def extractSessionMetaData(self, sessionData):
2020 """
2021 Public method to extract some session meta data elements needed by the
2022 tab widget in case of deferred loading.
2023
2024 @param sessionData dictionary containing the session data as
2025 generated by getSessionData()
2026 @type dict
2027 @return tuple containing the title, URL and web icon
2028 @rtype tuple of (str, str, QIcon)
2029 """
2030 if "Title" in sessionData:
2031 title = sessionData["Title"]
2032 else:
2033 title = ""
2034
2035 if "Url" in sessionData:
2036 urlStr = sessionData["Url"]
2037 else:
2038 urlStr = ""
2039
2040 if "Icon" in sessionData:
2041 iconArray = QByteArray.fromBase64(
2042 sessionData["Icon"].encode("ascii"))
2043 stream = QDataStream(iconArray, QIODevice.ReadOnly)
2044 icon = QIcon()
2045 stream >> icon
2046 else:
2047 from .Tools import WebIconProvider
2048 icon = WebIconProvider.instance().iconForUrl(
2049 QUrl.fromUserInput(urlStr))
2050
2051 return title, urlStr, icon

eric ide

mercurial