eric6/WebBrowser/AdBlock/AdBlockIcon.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
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
29 @type WebBrowserWindow
30 """
31 super(AdBlockIcon, self).__init__(parent)
32
33 self.__mw = parent
34 self.__menuAction = None
35 self.__enabled = False
36
37 self.setMaximumHeight(16)
38 self.setCursor(Qt.PointingHandCursor)
39 self.setToolTip(self.tr(
40 "AdBlock lets you block unwanted content on web pages."))
41
42 self.clicked.connect(self.__showMenu)
43
44 def setEnabled(self, enabled):
45 """
46 Public slot to set the enabled state.
47
48 @param enabled enabled state
49 @type bool
50 """
51 self.__enabled = enabled
52 if enabled:
53 self.currentChanged()
54 else:
55 self.setPixmap(
56 UI.PixmapCache.getPixmap("adBlockPlusDisabled16.png"))
57
58 def __createMenu(self, menu):
59 """
60 Private slot to create the context menu.
61
62 @param menu parent menu
63 @type QMenu
64 """
65 menu.clear()
66
67 manager = self.__mw.adBlockManager()
68
69 if manager.isEnabled():
70 act = menu.addAction(
71 UI.PixmapCache.getIcon("adBlockPlusDisabled.png"),
72 self.tr("Disable AdBlock"))
73 act.setData(False)
74 act.triggered.connect(lambda: self.__enableAdBlock(act))
75 else:
76 act = menu.addAction(
77 UI.PixmapCache.getIcon("adBlockPlus.png"),
78 self.tr("Enable AdBlock"))
79 act.setData(True)
80 act.triggered.connect(lambda: self.__enableAdBlock(act))
81 menu.addSeparator()
82 if manager.isEnabled() and self.__mw.currentBrowser().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
103 def menuAction(self):
104 """
105 Public method to get a reference to the menu action.
106
107 @return reference to the menu action
108 @rtype QAction
109 """
110 if not self.__menuAction:
111 self.__menuAction = QAction(self.tr("AdBlock"), self)
112 self.__menuAction.setMenu(QMenu())
113 self.__menuAction.menu().aboutToShow.connect(
114 lambda: self.__createMenu(self.__menuAction.menu()))
115
116 if self.__enabled:
117 self.__menuAction.setIcon(
118 UI.PixmapCache.getIcon("adBlockPlus.png"))
119 else:
120 self.__menuAction.setIcon(
121 UI.PixmapCache.getIcon("adBlockPlusDisabled.png"))
122
123 return self.__menuAction
124
125 def __showMenu(self, pos):
126 """
127 Private slot to show the context menu.
128
129 @param pos position the context menu should be shown
130 @type QPoint
131 """
132 menu = QMenu()
133 self.__createMenu(menu)
134 menu.exec_(pos)
135
136 def __enableAdBlock(self, act):
137 """
138 Private slot to enable or disable AdBlock.
139
140 @param act reference to the action
141 @type QAction
142 """
143 self.__mw.adBlockManager().setEnabled(act.data())
144
145 def __isCurrentHostExcepted(self):
146 """
147 Private method to check, if the host of the current browser is
148 excepted.
149
150 @return flag indicating an exception
151 @rtype bool
152 """
153 browser = self.__mw.currentBrowser()
154 if browser is None:
155 return False
156
157 urlHost = browser.page().url().host()
158
159 return urlHost and \
160 self.__mw.adBlockManager().isHostExcepted(urlHost)
161
162 def currentChanged(self):
163 """
164 Public slot to handle a change of the current browser tab.
165 """
166 if self.__enabled:
167 if self.__isCurrentHostExcepted():
168 self.setPixmap(
169 UI.PixmapCache.getPixmap("adBlockPlusGreen16.png"))
170 else:
171 self.setPixmap(UI.PixmapCache.getPixmap("adBlockPlus16.png"))
172
173 def __setException(self, act):
174 """
175 Private slot to add or remove the current host from the list of
176 exceptions.
177
178 @param act referenced to the action
179 @type QAction
180 """
181 urlHost = self.__mw.currentBrowser().url().host()
182 if act.data():
183 self.__mw.adBlockManager().addException(urlHost)
184 else:
185 self.__mw.adBlockManager().removeException(urlHost)
186 self.currentChanged()
187
188 def sourceChanged(self, browser, url):
189 """
190 Public slot to handle URL changes.
191
192 @param browser reference to the browser
193 @type WebBrowserView
194 @param url new URL
195 @type QUrl
196 """
197 if browser == self.__mw.currentBrowser():
198 self.currentChanged()

eric ide

mercurial