eric7/WebBrowser/StatusBar/JavaScriptIcon.py

branch
eric7
changeset 8312
800c432b34c8
parent 8260
2161475d9639
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2016 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the JavaScript status bar icon.
8 """
9
10 #
11 # This is modeled after the code found in Qupzilla
12 # Copyright (C) 2014 David Rosca <nowrep@gmail.com>
13 #
14
15 from PyQt5.QtCore import pyqtSlot, Qt, QPoint, QTimer
16 from PyQt5.QtWidgets import QGraphicsColorizeEffect, QMenu, QDialog
17 from PyQt5.QtWebEngineWidgets import QWebEngineSettings
18
19 from .StatusBarIcon import StatusBarIcon
20
21 import UI.PixmapCache
22
23
24 class JavaScriptIcon(StatusBarIcon):
25 """
26 Class implementing the JavaScript status bar icon.
27 """
28 def __init__(self, window):
29 """
30 Constructor
31
32 @param window reference to the web browser window
33 @type WebBrowserWindow
34 """
35 super().__init__(window)
36
37 self.setToolTip(self.tr("Modify JavaScript settings temporarily for"
38 " a site or globally"))
39 self.__icon = UI.PixmapCache.getPixmap("fileJavascript").scaled(16, 16)
40 self.setPixmap(self.__icon)
41
42 self.__settings = {}
43
44 self._window.tabWidget().currentChanged.connect(self.__updateIcon)
45 self._window.tabWidget().currentUrlChanged.connect(self.__updateIcon)
46 self.clicked.connect(self.__showMenu)
47
48 self.__updateIcon()
49
50 def preferencesChanged(self):
51 """
52 Public method to handle changes of the settings.
53 """
54 self.__updateIcon()
55
56 @pyqtSlot(QPoint)
57 def __showMenu(self, pos):
58 """
59 Private slot to show the menu.
60
61 @param pos position to show the menu at
62 @type QPoint
63 """
64 boldFont = self.font()
65 boldFont.setBold(True)
66
67 menu = QMenu()
68 menu.addAction(self.tr("Current Page Settings")).setFont(boldFont)
69
70 act = (
71 menu.addAction(self.tr("Disable JavaScript (temporarily)"),
72 self.__toggleJavaScript)
73 if self._testCurrentPageWebAttribute(
74 QWebEngineSettings.WebAttribute.JavascriptEnabled) else
75 menu.addAction(self.tr("Enable JavaScript (temporarily)"),
76 self.__toggleJavaScript)
77 )
78 if (
79 self._currentPage() is not None and
80 self._currentPage().url().scheme() == "eric"
81 ):
82 # JavaScript is needed for eric: scheme
83 act.setEnabled(False)
84
85 menu.addSeparator()
86 menu.addAction(self.tr("Global Settings")).setFont(boldFont)
87 menu.addAction(self.tr("Manage JavaScript Settings"),
88 self.__showJavaScriptSettingsDialog)
89 menu.exec(pos)
90
91 @pyqtSlot()
92 def __updateIcon(self):
93 """
94 Private slot to update the icon.
95 """
96 if self._testCurrentPageWebAttribute(
97 QWebEngineSettings.WebAttribute.JavascriptEnabled):
98 self.setGraphicsEffect(None)
99 else:
100 effect = QGraphicsColorizeEffect(self)
101 effect.setColor(Qt.GlobalColor.gray)
102 self.setGraphicsEffect(effect)
103
104 @pyqtSlot()
105 def __toggleJavaScript(self):
106 """
107 Private slot to toggle the JavaScript setting.
108 """
109 page = self._currentPage()
110 if page is None:
111 return
112
113 current = self._testCurrentPageWebAttribute(
114 QWebEngineSettings.WebAttribute.JavascriptEnabled)
115 self._setCurrentPageWebAttribute(
116 QWebEngineSettings.WebAttribute.JavascriptEnabled, not current)
117
118 self.__settings[page] = not current
119 page.navigationRequestAccepted.connect(
120 lambda u, t, mf: self.__navigationRequestAccepted(u, t, mf, page))
121
122 self._window.currentBrowser().reload()
123
124 self.__updateIcon()
125
126 @pyqtSlot()
127 def __showJavaScriptSettingsDialog(self):
128 """
129 Private slot to show the JavaScript settings dialog.
130
131 Note: This is the JavaScript subset of the web browser configuration
132 page.
133 """
134 from .JavaScriptSettingsDialog import JavaScriptSettingsDialog
135 dlg = JavaScriptSettingsDialog(self._window)
136 if dlg.exec() == QDialog.DialogCode.Accepted:
137 self._window.preferencesChanged()
138 QTimer.singleShot(500, self.__updateIcon)
139
140 def __navigationRequestAccepted(self, url, navigationType, isMainFrame,
141 page):
142 """
143 Private method to handle the navigationRequestAccepted signal.
144
145 @param url URL being loaded
146 @type QUrl
147 @param navigationType type of navigation request
148 @type QWebEnginePage.NavigationType
149 @param isMainFrame flag indicating a navigation request of the
150 main frame
151 @type bool
152 @param page reference to the web page
153 @type WebBrowserPage
154 """
155 enable = (True if url.scheme() in ("eric", "qthelp")
156 else self.__settings[page])
157 if isMainFrame:
158 page.settings().setAttribute(
159 QWebEngineSettings.WebAttribute.JavascriptEnabled, enable)

eric ide

mercurial