WebBrowser/WebBrowserWindow.py

branch
QtWebEngine
changeset 4725
b19ff70ba509
parent 4717
5841f229baf7
child 4726
c26e2a2dc0cb
equal deleted inserted replaced
4717:5841f229baf7 4725:b19ff70ba509
22 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QSizePolicy, QDockWidget, \ 22 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QSizePolicy, QDockWidget, \
23 QComboBox, QLabel, QSplitter, QMenu, QToolButton, QLineEdit, \ 23 QComboBox, QLabel, QSplitter, QMenu, QToolButton, QLineEdit, \
24 QApplication, QWhatsThis, QDialog, QHBoxLayout, QProgressBar, QAction, \ 24 QApplication, QWhatsThis, QDialog, QHBoxLayout, QProgressBar, QAction, \
25 QInputDialog 25 QInputDialog
26 ##from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest 26 ##from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
27 from PyQt5.QtWebEngineWidgets import QWebEngineSettings, QWebEnginePage 27 from PyQt5.QtWebEngineWidgets import QWebEngineSettings, QWebEnginePage, \
28 QWebEngineProfile, QWebEngineScript
28 try: 29 try:
29 from PyQt5.QtHelp import QHelpEngine, QHelpEngineCore, QHelpSearchQuery 30 from PyQt5.QtHelp import QHelpEngine, QHelpEngineCore, QHelpSearchQuery
30 QTHELP_AVAILABLE = True 31 QTHELP_AVAILABLE = True
31 except ImportError: 32 except ImportError:
32 QTHELP_AVAILABLE = False 33 QTHELP_AVAILABLE = False
33 ## 34
34 ##from .Network.NetworkAccessManager import SSL_AVAILABLE
35 ##
36 ##from .data import icons_rc # __IGNORE_WARNING__
37 ##from .data import html_rc # __IGNORE_WARNING__
38 ##from .data import javascript_rc # __IGNORE_WARNING__
39 ##
40 from E5Gui.E5Action import E5Action 35 from E5Gui.E5Action import E5Action
41 from E5Gui import E5MessageBox, E5FileDialog, E5ErrorMessage 36 from E5Gui import E5MessageBox, E5FileDialog, E5ErrorMessage
42 from E5Gui.E5MainWindow import E5MainWindow 37 from E5Gui.E5MainWindow import E5MainWindow
43 from E5Gui.E5Application import e5App 38 from E5Gui.E5Application import e5App
44 from E5Gui.E5ZoomWidget import E5ZoomWidget 39 from E5Gui.E5ZoomWidget import E5ZoomWidget
51 import Utilities 46 import Utilities
52 47
53 import UI.PixmapCache 48 import UI.PixmapCache
54 import UI.Config 49 import UI.Config
55 from UI.Info import Version 50 from UI.Info import Version
51 ##
52 ##from .Network.NetworkAccessManager import SSL_AVAILABLE
53 ##
54 ##from .data import icons_rc # __IGNORE_WARNING__
55 ##from .data import html_rc # __IGNORE_WARNING__
56 ##from .data import javascript_rc # __IGNORE_WARNING__
57 ##
58
59 from .Tools import Scripts, WebBrowserTools
56 60
57 61
58 class WebBrowserWindow(E5MainWindow): 62 class WebBrowserWindow(E5MainWindow):
59 """ 63 """
60 Class implementing the web browser main window. 64 Class implementing the web browser main window.
72 ## maxMenuFilePathLen = 75 76 ## maxMenuFilePathLen = 75
73 ## 77 ##
74 _fromEric = False 78 _fromEric = False
75 UseQtHelp = QTHELP_AVAILABLE 79 UseQtHelp = QTHELP_AVAILABLE
76 80
77 ## _networkAccessManager = None 81 _networkManager = None
78 ## _cookieJar = None 82 ## _cookieJar = None
79 ## _helpEngine = None 83 ## _helpEngine = None
80 ## _bookmarksManager = None 84 ## _bookmarksManager = None
81 ## _historyManager = None 85 ## _historyManager = None
82 ## _passwordManager = None 86 ## _passwordManager = None
92 ## _featurePermissionManager = None 96 ## _featurePermissionManager = None
93 ## _flashCookieManager = None 97 ## _flashCookieManager = None
94 ## _zoomManager = None 98 ## _zoomManager = None
95 99
96 def __init__(self, home, path, parent, name, fromEric=False, 100 def __init__(self, home, path, parent, name, fromEric=False,
97 initShortcutsOnly=False, searchWord=None): 101 initShortcutsOnly=False, searchWord=None,
102 private=False):
98 """ 103 """
99 Constructor 104 Constructor
100 105
101 @param home the URL to be shown (string) 106 @param home the URL to be shown (string)
102 @param path the path of the working dir (usually '.') (string) 107 @param path the path of the working dir (usually '.') (string)
105 @param fromEric flag indicating whether it was called from within 110 @param fromEric flag indicating whether it was called from within
106 eric6 (boolean) 111 eric6 (boolean)
107 @keyparam initShortcutsOnly flag indicating to just initialize the 112 @keyparam initShortcutsOnly flag indicating to just initialize the
108 keyboard shortcuts (boolean) 113 keyboard shortcuts (boolean)
109 @keyparam searchWord word to search for (string) 114 @keyparam searchWord word to search for (string)
115 @keyparam private flag indicating a private browsing window (bool)
110 """ 116 """
111 super(WebBrowserWindow, self).__init__(parent) 117 super(WebBrowserWindow, self).__init__(parent)
112 self.setObjectName(name) 118 self.setObjectName(name)
113 self.setWindowTitle(self.tr("eric6 Web Browser")) 119 self.setWindowTitle(self.tr("eric6 Web Browser"))
114 120
118 self.setWindowIcon(UI.PixmapCache.getIcon("ericWeb.png")) 124 self.setWindowIcon(UI.PixmapCache.getIcon("ericWeb.png"))
119 125
120 self.__mHistory = [] 126 self.__mHistory = []
121 self.__lastConfigurationPageName = "" 127 self.__lastConfigurationPageName = ""
122 128
129 self.__isPrivate = private
130
123 ## self.__eventMouseButtons = Qt.NoButton 131 ## self.__eventMouseButtons = Qt.NoButton
124 ## self.__eventKeyboardModifiers = Qt.NoModifier 132 ## self.__eventKeyboardModifiers = Qt.NoModifier
125 ## 133 ##
126 if self.__initShortcutsOnly: 134 if self.__initShortcutsOnly:
127 self.__initActions() 135 self.__initActions()
128 else: 136 else:
137 if self.isPrivate():
138 self.__webProfile = QWebEngineProfile(self)
139 else:
140 self.__webProfile = QWebEngineProfile.defaultProfile()
141 self.__webProfile.downloadRequested.connect(
142 self.__downloadRequested)
143
144 # Setup QWebChannel user script
145 script = QWebEngineScript()
146 script.setName("_eric_webchannel")
147 script.setInjectionPoint(QWebEngineScript.DocumentCreation)
148 script.setWorldId(QWebEngineScript.MainWorld)
149 script.setRunsOnSubFrames(True)
150 script.setSourceCode(Scripts.setupWebChannel())
151 self.__webProfile.scripts().insert(script)
152
129 from .SearchWidget import SearchWidget 153 from .SearchWidget import SearchWidget
130 # TODO: QtHelp 154 # TODO: QtHelp
131 ## from .HelpTocWidget import HelpTocWidget 155 ## from .HelpTocWidget import HelpTocWidget
132 ## from .HelpIndexWidget import HelpIndexWidget 156 ## from .HelpIndexWidget import HelpIndexWidget
133 ## from .HelpSearchWidget import HelpSearchWidget 157 ## from .HelpSearchWidget import HelpSearchWidget
363 standardFont.pointSize()) 387 standardFont.pointSize())
364 settings.setFontFamily(QWebEngineSettings.FixedFont, 388 settings.setFontFamily(QWebEngineSettings.FixedFont,
365 fixedFont.family()) 389 fixedFont.family())
366 settings.setFontSize(QWebEngineSettings.DefaultFixedFontSize, 390 settings.setFontSize(QWebEngineSettings.DefaultFixedFontSize,
367 fixedFont.pointSize()) 391 fixedFont.pointSize())
368 392 settings.setFontSize(
369 ## styleSheet = Preferences.getHelp("UserStyleSheet") 393 QWebEngineSettings.MinimumFontSize,
370 ## settings.setUserStyleSheetUrl(self.__userStyleSheet(styleSheet)) 394 Preferences.getWebBrowser("MinimumFontSize"))
371 ## 395 settings.setFontSize(
396 QWebEngineSettings.MinimumLogicalFontSize,
397 Preferences.getWebBrowser("MinimumLogicalFontSize"))
398
399 styleSheet = Preferences.getHelp("UserStyleSheet")
400 self.__setUserStyleSheet(styleSheet)
401
372 settings.setAttribute( 402 settings.setAttribute(
373 QWebEngineSettings.AutoLoadImages, 403 QWebEngineSettings.AutoLoadImages,
374 Preferences.getWebBrowser("AutoLoadImages")) 404 Preferences.getWebBrowser("AutoLoadImages"))
375 ## settings.setAttribute( 405 ## settings.setAttribute(
376 ## QWebSettings.JavaEnabled, 406 ## QWebSettings.JavaEnabled,
417 ## settings.setOfflineWebApplicationCachePath(appCacheDir) 447 ## settings.setOfflineWebApplicationCachePath(appCacheDir)
418 ## settings.setOfflineWebApplicationCacheQuota( 448 ## settings.setOfflineWebApplicationCacheQuota(
419 ## Preferences.getHelp("OfflineWebApplicationCacheQuota") * 449 ## Preferences.getHelp("OfflineWebApplicationCacheQuota") *
420 ## 1024 * 1024) 450 ## 1024 * 1024)
421 ## 451 ##
422 settings.setAttribute( 452 if self.isPrivate():
423 QWebEngineSettings.LocalStorageEnabled, 453 settings.setAttribute(
424 Preferences.getWebBrowser("LocalStorageEnabled")) 454 QWebEngineSettings.LocalStorageEnabled, False)
455 else:
456 settings.setAttribute(
457 QWebEngineSettings.LocalStorageEnabled,
458 Preferences.getWebBrowser("LocalStorageEnabled"))
425 ## localStorageDir = os.path.join( 459 ## localStorageDir = os.path.join(
426 ## Utilities.getConfigDir(), "browser", "weblocalstorage") 460 ## Utilities.getConfigDir(), "browser", "weblocalstorage")
427 ## if not os.path.exists(localStorageDir): 461 ## if not os.path.exists(localStorageDir):
428 ## os.makedirs(localStorageDir) 462 ## os.makedirs(localStorageDir)
429 ## settings.setLocalStoragePath(localStorageDir) 463 ## settings.setLocalStoragePath(localStorageDir)
2501 ## else: 2535 ## else:
2502 ## self.__setIconDatabasePath(True) 2536 ## self.__setIconDatabasePath(True)
2503 ## self.privateBrowsingAct.setChecked(on) 2537 ## self.privateBrowsingAct.setChecked(on)
2504 ## self.privacyChanged.emit(on) 2538 ## self.privacyChanged.emit(on)
2505 2539
2540 def isPrivate(self):
2541 """
2542 Public method to check the private browsing mode.
2543
2544 @return flag indicating private browsing mode
2545 @rtype bool
2546 """
2547 return self.__isPrivate
2548
2506 def currentBrowser(self): 2549 def currentBrowser(self):
2507 """ 2550 """
2508 Public method to get a reference to the current web browser. 2551 Public method to get a reference to the current web browser.
2509 2552
2510 @return reference to the current help browser (WebBrowserView) 2553 @return reference to the current help browser (WebBrowserView)
2673 ## "browser", "eric6help.qhc")) 2716 ## "browser", "eric6help.qhc"))
2674 ## return cls._helpEngine 2717 ## return cls._helpEngine
2675 ## else: 2718 ## else:
2676 ## return None 2719 ## return None
2677 ## 2720 ##
2678 ## @classmethod 2721 @classmethod
2679 ## def networkAccessManager(cls): 2722 def networkManager(cls):
2680 ## """ 2723 """
2681 ## Class method to get a reference to the network access manager. 2724 Class method to get a reference to the network manager object.
2682 ## 2725
2683 ## @return reference to the network access manager (NetworkAccessManager) 2726 @return reference to the network access manager (NetworkManager)
2684 ## """ 2727 """
2685 ## if cls._networkAccessManager is None: 2728 if cls._networkManager is None:
2686 ## from .Network.NetworkAccessManager import NetworkAccessManager 2729 from .Network.NetworkManager import NetworkManager
2687 ## from .CookieJar.CookieJar import CookieJar 2730 cls._networkManager = NetworkManager()
2688 ## cls._networkAccessManager = \ 2731
2689 ## NetworkAccessManager(cls.helpEngine()) 2732 return cls._networkManager
2690 ## cls._cookieJar = CookieJar() 2733
2691 ## cls._networkAccessManager.setCookieJar(cls._cookieJar)
2692 ##
2693 ## return cls._networkAccessManager
2694 ##
2695 ## @classmethod 2734 ## @classmethod
2696 ## def cookieJar(cls): 2735 ## def cookieJar(cls):
2697 ## """ 2736 ## """
2698 ## Class method to get a reference to the cookie jar. 2737 ## Class method to get a reference to the cookie jar.
2699 ## 2738 ##
2700 ## @return reference to the cookie jar (CookieJar) 2739 ## @return reference to the cookie jar (CookieJar)
2701 ## """ 2740 ## """
2741 ## from .CookieJar.CookieJar import CookieJar
2742 ## cls._cookieJar = CookieJar()
2702 ## return cls.networkAccessManager().cookieJar() 2743 ## return cls.networkAccessManager().cookieJar()
2703 ## 2744 ##
2704 ## def __clearIconsDatabase(self): 2745 ## def __clearIconsDatabase(self):
2705 ## """ 2746 ## """
2706 ## Private slot to clear the icons databse. 2747 ## Private slot to clear the icons databse.
3840 ## self.tr("Domain Report"), 3881 ## self.tr("Domain Report"),
3841 ## self.tr("Enter a valid domain name:"), 3882 ## self.tr("Enter a valid domain name:"),
3842 ## QLineEdit.Normal) 3883 ## QLineEdit.Normal)
3843 ## if ok and domain: 3884 ## if ok and domain:
3844 ## self.__virusTotal.getDomainReport(domain) 3885 ## self.__virusTotal.getDomainReport(domain)
3845 ## 3886
3846 ## ########################################################################### 3887 ###########################################################################
3847 ## ## Style sheet handling below ## 3888 ## Style sheet handling below ##
3848 ## ########################################################################### 3889 ###########################################################################
3849 ## 3890
3850 ## def reloadUserStyleSheet(self): 3891 def reloadUserStyleSheet(self):
3851 ## """ 3892 """
3852 ## Public method to reload the user style sheet. 3893 Public method to reload the user style sheet.
3853 ## """ 3894 """
3854 ## settings = QWebSettings.globalSettings() 3895 styleSheet = Preferences.getWebBrowser("UserStyleSheet")
3855 ## styleSheet = Preferences.getHelp("UserStyleSheet") 3896 self.__setUserStyleSheet(styleSheet)
3856 ## settings.setUserStyleSheetUrl(self.__userStyleSheet(styleSheet)) 3897
3857 ## 3898 def __setUserStyleSheet(self, styleSheetFile):
3858 ## def __userStyleSheet(self, styleSheetFile): 3899 """
3859 ## """ 3900 Private method to set a user style sheet.
3860 ## Private method to generate the user style sheet. 3901
3861 ## 3902 @param styleSheetFile name of the user style sheet file (string)
3862 ## @param styleSheetFile name of the user style sheet file (string) 3903 """
3863 ## @return style sheet (QUrl) 3904 # TODO: AdBlock
3864 ## """ 3905 userStyle = ""
3865 ## userStyle = self.adBlockManager().elementHidingRules() + \ 3906 ## userStyle = \
3866 ## "{display:none !important;}" 3907 ## self.adBlockManager().elementHidingRules().replace('"', '\\"')
3867 ## 3908
3868 ## if styleSheetFile: 3909 userStyle += WebBrowserTools.readAllFileContents(styleSheetFile)\
3869 ## try: 3910 .replace("\n", "")
3870 ## f = open(styleSheetFile, "r") 3911 name = "_eric_userstylesheet"
3871 ## fileData = f.read() 3912
3872 ## f.close() 3913 oldScript = self.__webProfile.scripts().findScript(name)
3873 ## fileData = fileData.replace("\n", "") 3914 if not oldScript.isNull():
3874 ## userStyle += fileData 3915 self.__webProfile.scripts().remove(oldScript)
3875 ## except IOError: 3916
3876 ## pass 3917 if userStyle:
3877 ## 3918 script = QWebEngineScript()
3878 ## encodedStyle = bytes(QByteArray(userStyle.encode("utf-8")).toBase64())\ 3919 script.setName(name)
3879 ## .decode() 3920 script.setInjectionPoint(QWebEngineScript.DocumentCreation)
3880 ## dataString = "data:text/css;charset=utf-8;base64,{0}".format( 3921 script.setWorldId(QWebEngineScript.ApplicationWorld)
3881 ## encodedStyle) 3922 script.setRunsOnSubFrames(True)
3882 ## 3923 script.setSourceCode(Scripts.setStyleSheet(userStyle))
3883 ## return QUrl(dataString) 3924 self.__webProfile.scripts().insert(script)
3884 3925
3885 ########################################## 3926 ##########################################
3886 ## Support for desktop notifications below 3927 ## Support for desktop notifications below
3887 ########################################## 3928 ##########################################
3888 3929
3921 """ 3962 """
3922 if cls._fromEric: 3963 if cls._fromEric:
3923 return e5App().getObject("UserInterface").notificationsEnabled() 3964 return e5App().getObject("UserInterface").notificationsEnabled()
3924 else: 3965 else:
3925 return Preferences.getUI("NotificationsEnabled") 3966 return Preferences.getUI("NotificationsEnabled")
3967
3968 ###################################
3969 ## Support for download files below
3970 ###################################
3971
3972 def __downloadRequested(self, download):
3973 """
3974 Private slot to handle a download request.
3975
3976 @param download reference to the download data
3977 @type QWebEngineDownloadItem
3978 """
3979 pass
3980 # TODO: DownloadManager
3981 ## self.downloadManager().download(download, mainWindow=self)

eric ide

mercurial