Helpviewer/AdBlock/AdBlockIcon.py

changeset 1963
9c5b3235abf9
child 1970
02cf3bac079b
equal deleted inserted replaced
1961:2e7f691cade9 1963:9c5b3235abf9
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 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 PyQt4.QtCore import Qt
11 from PyQt4.QtGui import QAction, QMenu
12
13 from E5Gui.E5ClickableLabel import E5ClickableLabel
14
15 import Helpviewer.HelpWindow
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().__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(
39 self.trUtf8("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 if enabled:
50 self.setPixmap(UI.PixmapCache.getPixmap("adBlockPlus16.png"))
51 else:
52 self.setPixmap(UI.PixmapCache.getPixmap("adBlockPlusDisabled16.png"))
53 self.__enabled = enabled
54
55 def __createMenu(self, menu=None):
56 """
57 Private slot to create the context menu.
58
59 @param menu parent menu (QMenu)
60 """
61 if menu is None:
62 menu = self.sender()
63 if menu is None:
64 return
65
66 menu.clear()
67
68 manager = Helpviewer.HelpWindow.HelpWindow.adblockManager()
69
70 if manager.isEnabled():
71 menu.addAction(UI.PixmapCache.getIcon("adBlockPlusDisabled.png"),
72 self.trUtf8("Disable AdBlock"), self.__enableAdBlock).setData(False)
73 else:
74 menu.addAction(UI.PixmapCache.getIcon("adBlockPlus.png"),
75 self.trUtf8("Enable AdBlock"), self.__enableAdBlock).setData(True)
76 menu.addSeparator()
77 menu.addAction(UI.PixmapCache.getIcon("adBlockPlus.png"),
78 self.trUtf8("AdBlock Configuration"), manager.showDialog)
79 menu.addSeparator()
80
81 entries = self.__mw.currentBrowser().page().getAdBlockedPageEntries()
82 if entries:
83 menu.addAction(
84 self.trUtf8("Blocked URL (AdBlock Rule) - click to edit rule"))\
85 .setEnabled(False)
86 for entry in entries:
87 address = entry.urlString()[-55:]
88 actionText = self.trUtf8("{0} with ({1})").format(
89 address, entry.rule.filter()).replace("&", "&&")
90 act = menu.addAction(actionText, manager.showRule)
91 act.setData(entry.rule)
92 else:
93 menu.addAction(self.trUtf8("No content blocked")).setEnabled(False)
94
95 def menuAction(self):
96 """
97 Public method to get a reference to the menu action.
98
99 @return reference to the menu action (QAction)
100 """
101 if not self.__menuAction:
102 self.__menuAction = QAction(self.trUtf8("AdBlock"))
103 self.__menuAction.setMenu(QMenu())
104 self.__menuAction.menu().aboutToShow.connect(self.__createMenu)
105
106 if self.__enabled:
107 self.__menuAction.setIcon(UI.PixmapCache.getIcon("adBlockPlus.png"))
108 else:
109 self.__menuAction.setIcon(UI.PixmapCache.getIcon("adBlockPlusDisabled.png"))
110
111 return self.__menuAction
112
113 def __showMenu(self, pos):
114 """
115 Private slot to show the context menu.
116
117 @param pos position the context menu should be shown (QPoint)
118 """
119 menu = QMenu()
120 self.__createMenu(menu)
121 menu.exec_(pos)
122
123 def __enableAdBlock(self):
124 """
125 Private slot to enable or disable AdBlock.
126 """
127 act = self.sender()
128 if act is not None:
129 Helpviewer.HelpWindow.HelpWindow.adblockManager().setEnabled(act.data())

eric ide

mercurial