WebBrowser/StatusBar/JavaScriptIcon.py

changeset 4964
a894e8c92369
child 4965
a9a4b632fe48
equal deleted inserted replaced
4963:e5e31c75ce00 4964:a894e8c92369
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2016 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the JavaScript status bar icon.
8 """
9
10 #
11 # This is modelled 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
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._window.tabWidget().currentChanged.connect(self.__updateIcon)
45 self.clicked.connect(self.__showMenu)
46
47 self.__updateIcon()
48
49 def preferencesChanged(self):
50 """
51 Public method to handle changes of the settings.
52 """
53 self.__updateIcon()
54
55 @pyqtSlot(QPoint)
56 def __showMenu(self, pos):
57 """
58 Private slot to show the menu.
59
60 @param pos position to show the menu at
61 @type QPoint
62 """
63 boldFont = self.font()
64 boldFont.setBold(True)
65
66 menu = QMenu()
67 menu.addAction(self.tr("Current Page Settings")).setFont(boldFont)
68
69 if self._testCurrentPageWebAttribute(
70 QWebEngineSettings.JavascriptEnabled):
71 act = menu.addAction(self.tr("Disable JavaScript (temporarily)"),
72 self.__toggleJavaScript)
73 else:
74 act = menu.addAction(self.tr("Enable JavaScript (temporarily)"),
75 self.__toggleJavaScript)
76 if self._currentPage() is not None and \
77 self._currentPage().url().scheme() == "eric":
78 # JavaScript is needed for eric: scheme
79 act.setEnabled(False)
80
81 menu.addSeparator()
82 menu.addAction(self.tr("Global Settings")).setFont(boldFont)
83 menu.addAction(self.tr("Manage JavaScript Settings"),
84 self.__showJavaScriptSettingsDialog)
85 menu.exec(pos)
86
87 @pyqtSlot()
88 def __updateIcon(self):
89 """
90 Private slot to update the icon.
91 """
92 if self._testCurrentPageWebAttribute(
93 QWebEngineSettings.JavascriptEnabled):
94 self.setGraphicsEffect(None)
95 else:
96 effect = QGraphicsColorizeEffect(self)
97 effect.setColor(Qt.gray)
98 self.setGraphicsEffect(effect)
99
100 @pyqtSlot()
101 def __toggleJavaScript(self):
102 """
103 Private slot to toggle the JavaScript setting.
104 """
105 if self._currentPage() is None:
106 return
107
108 current = self._testCurrentPageWebAttribute(
109 QWebEngineSettings.JavascriptEnabled)
110 self._currentPage().setJavaScriptEnabled(not current)
111
112 self._window.currentBrowser().reload()
113
114 self.__updateIcon()
115
116 @pyqtSlot()
117 def __showJavaScriptSettingsDialog(self):
118 """
119 Private slot to show the JavaScript settings dialog.
120
121 Note: This is the JavaScript subset of the web browser configuration
122 page.
123 """
124 from .JavaScriptSettingsDialog import JavaScriptSettingsDialog
125 dlg = JavaScriptSettingsDialog(self._window)
126 if dlg.exec_() == QDialog.Accepted:
127 self._window.preferencesChanged()

eric ide

mercurial