23 |
23 |
24 class JavaScriptIcon(StatusBarIcon): |
24 class JavaScriptIcon(StatusBarIcon): |
25 """ |
25 """ |
26 Class implementing the JavaScript status bar icon. |
26 Class implementing the JavaScript status bar icon. |
27 """ |
27 """ |
|
28 |
28 def __init__(self, window): |
29 def __init__(self, window): |
29 """ |
30 """ |
30 Constructor |
31 Constructor |
31 |
32 |
32 @param window reference to the web browser window |
33 @param window reference to the web browser window |
33 @type WebBrowserWindow |
34 @type WebBrowserWindow |
34 """ |
35 """ |
35 super().__init__(window) |
36 super().__init__(window) |
36 |
37 |
37 self.setToolTip(self.tr("Modify JavaScript settings temporarily for" |
38 self.setToolTip( |
38 " a site or globally")) |
39 self.tr("Modify JavaScript settings temporarily for" " a site or globally") |
|
40 ) |
39 self.__icon = UI.PixmapCache.getPixmap("fileJavascript").scaled(16, 16) |
41 self.__icon = UI.PixmapCache.getPixmap("fileJavascript").scaled(16, 16) |
40 self.setPixmap(self.__icon) |
42 self.setPixmap(self.__icon) |
41 |
43 |
42 self.__settings = {} |
44 self.__settings = {} |
43 |
45 |
44 self._window.tabWidget().currentChanged.connect(self.__updateIcon) |
46 self._window.tabWidget().currentChanged.connect(self.__updateIcon) |
45 self._window.tabWidget().currentUrlChanged.connect(self.__updateIcon) |
47 self._window.tabWidget().currentUrlChanged.connect(self.__updateIcon) |
46 self.clicked.connect(self.__showMenu) |
48 self.clicked.connect(self.__showMenu) |
47 |
49 |
48 self.__updateIcon() |
50 self.__updateIcon() |
49 |
51 |
50 def preferencesChanged(self): |
52 def preferencesChanged(self): |
51 """ |
53 """ |
52 Public method to handle changes of the settings. |
54 Public method to handle changes of the settings. |
53 """ |
55 """ |
54 self.__updateIcon() |
56 self.__updateIcon() |
55 |
57 |
56 @pyqtSlot(QPoint) |
58 @pyqtSlot(QPoint) |
57 def __showMenu(self, pos): |
59 def __showMenu(self, pos): |
58 """ |
60 """ |
59 Private slot to show the menu. |
61 Private slot to show the menu. |
60 |
62 |
61 @param pos position to show the menu at |
63 @param pos position to show the menu at |
62 @type QPoint |
64 @type QPoint |
63 """ |
65 """ |
64 boldFont = self.font() |
66 boldFont = self.font() |
65 boldFont.setBold(True) |
67 boldFont.setBold(True) |
66 |
68 |
67 menu = QMenu() |
69 menu = QMenu() |
68 menu.addAction(self.tr("Current Page Settings")).setFont(boldFont) |
70 menu.addAction(self.tr("Current Page Settings")).setFont(boldFont) |
69 |
71 |
70 act = ( |
72 act = ( |
71 menu.addAction(self.tr("Disable JavaScript (temporarily)"), |
73 menu.addAction( |
72 self.__toggleJavaScript) |
74 self.tr("Disable JavaScript (temporarily)"), self.__toggleJavaScript |
|
75 ) |
73 if self._testCurrentPageWebAttribute( |
76 if self._testCurrentPageWebAttribute( |
74 QWebEngineSettings.WebAttribute.JavascriptEnabled) else |
77 QWebEngineSettings.WebAttribute.JavascriptEnabled |
75 menu.addAction(self.tr("Enable JavaScript (temporarily)"), |
78 ) |
76 self.__toggleJavaScript) |
79 else menu.addAction( |
|
80 self.tr("Enable JavaScript (temporarily)"), self.__toggleJavaScript |
|
81 ) |
77 ) |
82 ) |
78 if ( |
83 if ( |
79 self._currentPage() is not None and |
84 self._currentPage() is not None |
80 self._currentPage().url().scheme() == "eric" |
85 and self._currentPage().url().scheme() == "eric" |
81 ): |
86 ): |
82 # JavaScript is needed for eric: scheme |
87 # JavaScript is needed for eric: scheme |
83 act.setEnabled(False) |
88 act.setEnabled(False) |
84 |
89 |
85 menu.addSeparator() |
90 menu.addSeparator() |
86 menu.addAction(self.tr("Global Settings")).setFont(boldFont) |
91 menu.addAction(self.tr("Global Settings")).setFont(boldFont) |
87 menu.addAction(self.tr("Manage JavaScript Settings"), |
92 menu.addAction( |
88 self.__showJavaScriptSettingsDialog) |
93 self.tr("Manage JavaScript Settings"), self.__showJavaScriptSettingsDialog |
|
94 ) |
89 menu.exec(pos) |
95 menu.exec(pos) |
90 |
96 |
91 @pyqtSlot() |
97 @pyqtSlot() |
92 def __updateIcon(self): |
98 def __updateIcon(self): |
93 """ |
99 """ |
94 Private slot to update the icon. |
100 Private slot to update the icon. |
95 """ |
101 """ |
96 if self._testCurrentPageWebAttribute( |
102 if self._testCurrentPageWebAttribute( |
97 QWebEngineSettings.WebAttribute.JavascriptEnabled): |
103 QWebEngineSettings.WebAttribute.JavascriptEnabled |
|
104 ): |
98 self.setGraphicsEffect(None) |
105 self.setGraphicsEffect(None) |
99 else: |
106 else: |
100 effect = QGraphicsColorizeEffect(self) |
107 effect = QGraphicsColorizeEffect(self) |
101 effect.setColor(Qt.GlobalColor.gray) |
108 effect.setColor(Qt.GlobalColor.gray) |
102 self.setGraphicsEffect(effect) |
109 self.setGraphicsEffect(effect) |
103 |
110 |
104 @pyqtSlot() |
111 @pyqtSlot() |
105 def __toggleJavaScript(self): |
112 def __toggleJavaScript(self): |
106 """ |
113 """ |
107 Private slot to toggle the JavaScript setting. |
114 Private slot to toggle the JavaScript setting. |
108 """ |
115 """ |
109 page = self._currentPage() |
116 page = self._currentPage() |
110 if page is None: |
117 if page is None: |
111 return |
118 return |
112 |
119 |
113 current = self._testCurrentPageWebAttribute( |
120 current = self._testCurrentPageWebAttribute( |
114 QWebEngineSettings.WebAttribute.JavascriptEnabled) |
121 QWebEngineSettings.WebAttribute.JavascriptEnabled |
|
122 ) |
115 self._setCurrentPageWebAttribute( |
123 self._setCurrentPageWebAttribute( |
116 QWebEngineSettings.WebAttribute.JavascriptEnabled, not current) |
124 QWebEngineSettings.WebAttribute.JavascriptEnabled, not current |
117 |
125 ) |
|
126 |
118 self.__settings[page] = not current |
127 self.__settings[page] = not current |
119 page.navigationRequestAccepted.connect( |
128 page.navigationRequestAccepted.connect( |
120 lambda u, t, mf: self.__navigationRequestAccepted(u, t, mf, page)) |
129 lambda u, t, mf: self.__navigationRequestAccepted(u, t, mf, page) |
121 |
130 ) |
|
131 |
122 self._window.currentBrowser().reload() |
132 self._window.currentBrowser().reload() |
123 |
133 |
124 self.__updateIcon() |
134 self.__updateIcon() |
125 |
135 |
126 @pyqtSlot() |
136 @pyqtSlot() |
127 def __showJavaScriptSettingsDialog(self): |
137 def __showJavaScriptSettingsDialog(self): |
128 """ |
138 """ |
129 Private slot to show the JavaScript settings dialog. |
139 Private slot to show the JavaScript settings dialog. |
130 |
140 |
131 Note: This is the JavaScript subset of the web browser configuration |
141 Note: This is the JavaScript subset of the web browser configuration |
132 page. |
142 page. |
133 """ |
143 """ |
134 from .JavaScriptSettingsDialog import JavaScriptSettingsDialog |
144 from .JavaScriptSettingsDialog import JavaScriptSettingsDialog |
|
145 |
135 dlg = JavaScriptSettingsDialog(self._window) |
146 dlg = JavaScriptSettingsDialog(self._window) |
136 if dlg.exec() == QDialog.DialogCode.Accepted: |
147 if dlg.exec() == QDialog.DialogCode.Accepted: |
137 self._window.preferencesChanged() |
148 self._window.preferencesChanged() |
138 QTimer.singleShot(500, self.__updateIcon) |
149 QTimer.singleShot(500, self.__updateIcon) |
139 |
150 |
140 def __navigationRequestAccepted(self, url, navigationType, isMainFrame, |
151 def __navigationRequestAccepted(self, url, navigationType, isMainFrame, page): |
141 page): |
|
142 """ |
152 """ |
143 Private method to handle the navigationRequestAccepted signal. |
153 Private method to handle the navigationRequestAccepted signal. |
144 |
154 |
145 @param url URL being loaded |
155 @param url URL being loaded |
146 @type QUrl |
156 @type QUrl |
147 @param navigationType type of navigation request |
157 @param navigationType type of navigation request |
148 @type QWebEnginePage.NavigationType |
158 @type QWebEnginePage.NavigationType |
149 @param isMainFrame flag indicating a navigation request of the |
159 @param isMainFrame flag indicating a navigation request of the |
150 main frame |
160 main frame |
151 @type bool |
161 @type bool |
152 @param page reference to the web page |
162 @param page reference to the web page |
153 @type WebBrowserPage |
163 @type WebBrowserPage |
154 """ |
164 """ |
155 enable = (True if url.scheme() in ("eric", "qthelp") |
165 enable = True if url.scheme() in ("eric", "qthelp") else self.__settings[page] |
156 else self.__settings[page]) |
|
157 if isMainFrame: |
166 if isMainFrame: |
158 page.settings().setAttribute( |
167 page.settings().setAttribute( |
159 QWebEngineSettings.WebAttribute.JavascriptEnabled, enable) |
168 QWebEngineSettings.WebAttribute.JavascriptEnabled, enable |
|
169 ) |