eric6/Helpviewer/AdBlock/AdBlockIcon.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the AdBlock icon for the main window status bar.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import Qt
13 from PyQt5.QtWidgets import QAction, QMenu
14
15 from E5Gui.E5ClickableLabel import E5ClickableLabel
16
17 import UI.PixmapCache
18
19
20 class AdBlockIcon(E5ClickableLabel):
21 """
22 Class implementing the AdBlock icon for the main window status bar.
23 """
24 def __init__(self, parent):
25 """
26 Constructor
27
28 @param parent reference to the parent widget (HelpWindow)
29 """
30 super(AdBlockIcon, self).__init__(parent)
31
32 self.__mw = parent
33 self.__menuAction = None
34 self.__enabled = False
35
36 self.setMaximumHeight(16)
37 self.setCursor(Qt.PointingHandCursor)
38 self.setToolTip(self.tr(
39 "AdBlock lets you block unwanted content on web pages."))
40
41 self.clicked.connect(self.__showMenu)
42
43 def setEnabled(self, enabled):
44 """
45 Public slot to set the enabled state.
46
47 @param enabled enabled state (boolean)
48 """
49 self.__enabled = enabled
50 if enabled:
51 self.currentChanged()
52 else:
53 self.setPixmap(
54 UI.PixmapCache.getPixmap("adBlockPlusDisabled16.png"))
55
56 def __createMenu(self, menu):
57 """
58 Private slot to create the context menu.
59
60 @param menu parent menu
61 @type QMenu
62 """
63 menu.clear()
64
65 import Helpviewer.HelpWindow
66 manager = Helpviewer.HelpWindow.HelpWindow.adBlockManager()
67
68 if manager.isEnabled():
69 act = menu.addAction(
70 UI.PixmapCache.getIcon("adBlockPlusDisabled.png"),
71 self.tr("Disable AdBlock"))
72 act.setData(False)
73 act.triggered.connect(lambda: self.__enableAdBlock(act))
74 else:
75 act = menu.addAction(
76 UI.PixmapCache.getIcon("adBlockPlus.png"),
77 self.tr("Enable AdBlock"))
78 act.setData(True)
79 act.triggered.connect(lambda: self.__enableAdBlock(act))
80 menu.addSeparator()
81 if manager.isEnabled() and \
82 self.__mw.currentBrowser().page().url().host():
83 if self.__isCurrentHostExcepted():
84 act = menu.addAction(
85 UI.PixmapCache.getIcon("adBlockPlus.png"),
86 self.tr("Remove AdBlock Exception"))
87 act.setData(False)
88 act.triggered.connect(lambda: self.__setException(act))
89 else:
90 act = menu.addAction(
91 UI.PixmapCache.getIcon("adBlockPlusGreen.png"),
92 self.tr("Add AdBlock Exception"))
93 act.setData(True)
94 act.triggered.connect(lambda: self.__setException(act))
95 menu.addAction(
96 UI.PixmapCache.getIcon("adBlockPlusGreen.png"),
97 self.tr("AdBlock Exceptions..."), manager.showExceptionsDialog)
98 menu.addSeparator()
99 menu.addAction(
100 UI.PixmapCache.getIcon("adBlockPlus.png"),
101 self.tr("AdBlock Configuration..."), manager.showDialog)
102 menu.addSeparator()
103
104 entries = self.__mw.currentBrowser().page().getAdBlockedPageEntries()
105 if entries:
106 menu.addAction(self.tr(
107 "Blocked URL (AdBlock Rule) - click to edit rule"))\
108 .setEnabled(False)
109 for entry in entries:
110 address = entry.urlString()[-55:]
111 actionText = self.tr("{0} with ({1})").format(
112 address, entry.rule.filter()).replace("&", "&&")
113 act = menu.addAction(actionText)
114 act.setData(entry.rule)
115 act.triggered.connect(lambda: manager.showRule(act))
116 else:
117 menu.addAction(self.tr("No content blocked")).setEnabled(False)
118
119 def menuAction(self):
120 """
121 Public method to get a reference to the menu action.
122
123 @return reference to the menu action (QAction)
124 """
125 if not self.__menuAction:
126 self.__menuAction = QAction(self.tr("AdBlock"), self)
127 self.__menuAction.setMenu(QMenu())
128 self.__menuAction.menu().aboutToShow.connect(
129 lambda: self.__createMenu(self.__menuAction.menu()))
130
131 if self.__enabled:
132 self.__menuAction.setIcon(
133 UI.PixmapCache.getIcon("adBlockPlus.png"))
134 else:
135 self.__menuAction.setIcon(
136 UI.PixmapCache.getIcon("adBlockPlusDisabled.png"))
137
138 return self.__menuAction
139
140 def __showMenu(self, pos):
141 """
142 Private slot to show the context menu.
143
144 @param pos position the context menu should be shown (QPoint)
145 """
146 menu = QMenu()
147 self.__createMenu(menu)
148 menu.exec_(pos)
149
150 def __enableAdBlock(self, act):
151 """
152 Private slot to enable or disable AdBlock.
153
154 @param act reference to the action
155 @type QAction
156 """
157 import Helpviewer.HelpWindow
158 Helpviewer.HelpWindow.HelpWindow.adBlockManager().setEnabled(
159 act.data())
160
161 def __isCurrentHostExcepted(self):
162 """
163 Private method to check, if the host of the current browser is
164 excepted.
165
166 @return flag indicating an exception (boolean)
167 """
168 browser = self.__mw.currentBrowser()
169 if browser is None:
170 return False
171
172 urlHost = browser.page().url().host()
173
174 import Helpviewer.HelpWindow
175 return urlHost and \
176 Helpviewer.HelpWindow.HelpWindow.adBlockManager()\
177 .isHostExcepted(urlHost)
178
179 def currentChanged(self):
180 """
181 Public slot to handle a change of the current browser tab.
182 """
183 if self.__enabled:
184 if self.__isCurrentHostExcepted():
185 self.setPixmap(
186 UI.PixmapCache.getPixmap("adBlockPlusGreen16.png"))
187 else:
188 self.setPixmap(UI.PixmapCache.getPixmap("adBlockPlus16.png"))
189
190 def __setException(self, act):
191 """
192 Private slot to add or remove the current host from the list of
193 exceptions.
194
195 @param act referenced to the action
196 @type QAction
197 """
198 import Helpviewer.HelpWindow
199 urlHost = self.__mw.currentBrowser().page().url().host()
200 if act.data():
201 Helpviewer.HelpWindow.HelpWindow.adBlockManager()\
202 .addException(urlHost)
203 else:
204 Helpviewer.HelpWindow.HelpWindow.adBlockManager()\
205 .removeException(urlHost)
206 self.currentChanged()
207
208 def sourceChanged(self, browser, url):
209 """
210 Public slot to handle URL changes.
211
212 @param browser reference to the browser (HelpBrowser)
213 @param url new URL (QUrl)
214 """
215 if browser == self.__mw.currentBrowser():
216 self.currentChanged()

eric ide

mercurial