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