15 pass |
15 pass |
16 |
16 |
17 import os |
17 import os |
18 |
18 |
19 from PyQt5.QtCore import pyqtSignal, QUrl, QFileInfo, Qt, QTimer, QEvent, \ |
19 from PyQt5.QtCore import pyqtSignal, QUrl, QFileInfo, Qt, QTimer, QEvent, \ |
20 QPoint, QDateTime, QStandardPaths |
20 QPoint, QPointF, QDateTime, QStandardPaths, QByteArray, QIODevice, \ |
|
21 QDataStream |
21 from PyQt5.QtGui import QDesktopServices, QClipboard, QIcon, \ |
22 from PyQt5.QtGui import QDesktopServices, QClipboard, QIcon, \ |
22 QContextMenuEvent, QPixmap |
23 QContextMenuEvent, QPixmap |
23 from PyQt5.QtWidgets import qApp, QStyle, QMenu, QApplication |
24 from PyQt5.QtWidgets import qApp, QStyle, QMenu, QApplication |
24 from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, \ |
25 from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, \ |
25 QWebEngineDownloadItem |
26 QWebEngineDownloadItem |
1892 def __resetSpeedDials(self): |
1895 def __resetSpeedDials(self): |
1893 """ |
1896 """ |
1894 Private slot to reset all speed dials to the default pages. |
1897 Private slot to reset all speed dials to the default pages. |
1895 """ |
1898 """ |
1896 self.__speedDial.resetDials() |
1899 self.__speedDial.resetDials() |
|
1900 |
|
1901 ########################################################################### |
|
1902 ## Methods below implement session related functions |
|
1903 ########################################################################### |
|
1904 |
|
1905 def storeSessionData(self, data): |
|
1906 """ |
|
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 |
|
1911 """ |
|
1912 self.__restoreData = data |
|
1913 |
|
1914 def __showEventSlot(self): |
|
1915 """ |
|
1916 Private slot to perform actions when the view is shown and the event |
|
1917 loop is running. |
|
1918 """ |
|
1919 if self.__restoreData: |
|
1920 sessionData, self.__restoreData = self.__restoreData, None |
|
1921 self.loadFromSessionData(sessionData) |
|
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 Public slot to activate a restored session. |
|
1936 """ |
|
1937 if self.__restoreData and not self.__mw.isClosing(): |
|
1938 QTimer.singleShot(0, self.__showEventSlot) |
|
1939 |
|
1940 def getSessionData(self): |
|
1941 """ |
|
1942 Public method to populate the session data. |
|
1943 |
|
1944 @return dictionary containing the session data |
|
1945 @rtype dict |
|
1946 """ |
|
1947 if self.__restoreData: |
|
1948 # page has not been shown yet |
|
1949 return self.__restoreData |
|
1950 |
|
1951 sessionData = {} |
|
1952 page = self.page() |
|
1953 |
|
1954 # 1. zoom factor |
|
1955 sessionData["ZoomFactor"] = page.zoomFactor() |
|
1956 |
|
1957 # 2. scroll position |
|
1958 scrollPos = page.scrollPosition() |
|
1959 sessionData["ScrollPosition"] = { |
|
1960 "x": scrollPos.x(), |
|
1961 "y": scrollPos.y(), |
|
1962 } |
|
1963 |
|
1964 # 3. page history |
|
1965 historyArray = QByteArray() |
|
1966 stream = QDataStream(historyArray, QIODevice.WriteOnly) |
|
1967 stream << page.history() |
|
1968 sessionData["History"] = str( |
|
1969 historyArray.toBase64(QByteArray.Base64UrlEncoding), |
|
1970 encoding="ascii") |
|
1971 sessionData["HistoryIndex"] = page.history().currentItemIndex() |
|
1972 |
|
1973 # 4. current URL and title |
|
1974 sessionData["Url"] = self.url().toString() |
|
1975 sessionData["Title"] = self.title() |
|
1976 |
|
1977 # 5. web icon |
|
1978 iconArray = QByteArray() |
|
1979 stream = QDataStream(iconArray, QIODevice.WriteOnly) |
|
1980 stream << page.icon() |
|
1981 sessionData["Icon"] = str(iconArray.toBase64(), encoding="ascii") |
|
1982 |
|
1983 return sessionData |
|
1984 |
|
1985 def loadFromSessionData(self, sessionData): |
|
1986 """ |
|
1987 Public method to load the session data. |
|
1988 |
|
1989 @param sessionData dictionary containing the session data as |
|
1990 generated by getSessionData() |
|
1991 @type dict |
|
1992 """ |
|
1993 page = self.page() |
|
1994 # blank the page |
|
1995 page.setUrl(QUrl("about:blank")) |
|
1996 |
|
1997 # 1. page history |
|
1998 if "History" in sessionData: |
|
1999 historyArray = QByteArray.fromBase64( |
|
2000 sessionData["History"].encode("ascii"), |
|
2001 QByteArray.Base64UrlEncoding) |
|
2002 stream = QDataStream(historyArray, QIODevice.ReadOnly) |
|
2003 stream >> page.history() |
|
2004 |
|
2005 if "HistoryIndex" in sessionData: |
|
2006 item = page.history().itemAt(sessionData["HistoryIndex"]) |
|
2007 if item is not None: |
|
2008 page.history().goToItem(item) |
|
2009 |
|
2010 # 2. zoom factor |
|
2011 if "ZoomFactor" in sessionData: |
|
2012 page.setZoomFactor(sessionData["ZoomFactor"]) |
|
2013 |
|
2014 # 3. scroll position |
|
2015 if "ScrollPosition" in sessionData: |
|
2016 scrollPos = sessionData["ScrollPosition"] |
|
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 |