28 @type WebBrowserWindow |
27 @type WebBrowserWindow |
29 """ |
28 """ |
30 super().__init__(parent) |
29 super().__init__(parent) |
31 |
30 |
32 self.__mw = parent |
31 self.__mw = parent |
33 self.__menuAction = None |
32 self.__menu = QMenu(self.tr("AdBlock")) |
34 self.__enabled = False |
33 self.__enabled = False |
35 |
34 |
36 self.setMaximumHeight(16) |
35 self.setMaximumHeight(16) |
37 self.setCursor(Qt.CursorShape.PointingHandCursor) |
36 self.setCursor(Qt.CursorShape.PointingHandCursor) |
38 self.setToolTip(self.tr( |
37 self.setToolTip(self.tr( |
39 "AdBlock lets you block unwanted content on web pages.")) |
38 "AdBlock lets you block unwanted content on web pages.")) |
40 |
39 |
|
40 self.__menu.aboutToShow.connect(self.__aboutToShowMenu) |
41 self.clicked.connect(self.__showMenu) |
41 self.clicked.connect(self.__showMenu) |
42 |
42 |
43 def setEnabled(self, enabled): |
43 def setEnabled(self, enabled): |
44 """ |
44 """ |
45 Public slot to set the enabled state. |
45 Public slot to set the enabled state. |
52 self.currentChanged() |
52 self.currentChanged() |
53 else: |
53 else: |
54 self.setPixmap( |
54 self.setPixmap( |
55 UI.PixmapCache.getPixmap("adBlockPlusDisabled16")) |
55 UI.PixmapCache.getPixmap("adBlockPlusDisabled16")) |
56 |
56 |
57 def __createMenu(self, menu): |
57 def __aboutToShowMenu(self): |
58 """ |
58 """ |
59 Private slot to create the context menu. |
59 Private slot to show the context menu. |
60 |
|
61 @param menu parent menu |
|
62 @type QMenu |
|
63 """ |
60 """ |
64 menu.clear() |
61 self.__menu.clear() |
65 |
62 |
66 manager = self.__mw.adBlockManager() |
63 manager = self.__mw.adBlockManager() |
67 |
64 |
68 if manager.isEnabled(): |
65 if manager.isEnabled(): |
69 act = menu.addAction( |
66 act = self.__menu.addAction( |
70 UI.PixmapCache.getIcon("adBlockPlusDisabled"), |
67 UI.PixmapCache.getIcon("adBlockPlusDisabled"), |
71 self.tr("Disable AdBlock")) |
68 self.tr("Disable AdBlock")) |
72 act.triggered.connect(lambda: self.__enableAdBlock(False)) |
69 act.triggered.connect(lambda: self.__enableAdBlock(False)) |
73 else: |
70 else: |
74 act = menu.addAction( |
71 act = self.__menu.addAction( |
75 UI.PixmapCache.getIcon("adBlockPlus"), |
72 UI.PixmapCache.getIcon("adBlockPlus"), |
76 self.tr("Enable AdBlock")) |
73 self.tr("Enable AdBlock")) |
77 act.triggered.connect(lambda: self.__enableAdBlock(True)) |
74 act.triggered.connect(lambda: self.__enableAdBlock(True)) |
78 menu.addSeparator() |
75 self.__menu.addSeparator() |
79 if manager.isEnabled() and self.__mw.currentBrowser().url().host(): |
76 if manager.isEnabled() and self.__mw.currentBrowser().url().host(): |
80 if self.__isCurrentHostExcepted(): |
77 if self.__isCurrentHostExcepted(): |
81 act = menu.addAction( |
78 act = self.__menu.addAction( |
82 UI.PixmapCache.getIcon("adBlockPlus"), |
79 UI.PixmapCache.getIcon("adBlockPlus"), |
83 self.tr("Remove AdBlock Exception")) |
80 self.tr("Remove AdBlock Exception")) |
84 act.triggered.connect(lambda: self.__setException(False)) |
81 act.triggered.connect(lambda: self.__setException(False)) |
85 else: |
82 else: |
86 act = menu.addAction( |
83 act = self.__menu.addAction( |
87 UI.PixmapCache.getIcon("adBlockPlusGreen"), |
84 UI.PixmapCache.getIcon("adBlockPlusGreen"), |
88 self.tr("Add AdBlock Exception")) |
85 self.tr("Add AdBlock Exception")) |
89 act.triggered.connect(lambda: self.__setException(True)) |
86 act.triggered.connect(lambda: self.__setException(True)) |
90 menu.addAction( |
87 self.__menu.addAction( |
91 UI.PixmapCache.getIcon("adBlockPlusGreen"), |
88 UI.PixmapCache.getIcon("adBlockPlusGreen"), |
92 self.tr("AdBlock Exceptions..."), manager.showExceptionsDialog) |
89 self.tr("AdBlock Exceptions..."), manager.showExceptionsDialog) |
93 menu.addSeparator() |
90 self.__menu.addSeparator() |
94 menu.addAction( |
91 self.__menu.addAction( |
95 UI.PixmapCache.getIcon("adBlockPlus"), |
92 UI.PixmapCache.getIcon("adBlockPlus"), |
96 self.tr("AdBlock Configuration..."), manager.showDialog) |
93 self.tr("AdBlock Configuration..."), manager.showDialog) |
97 |
94 |
98 # TODO: change this to return a QMenu |
95 def menu(self): |
99 def menuAction(self): |
|
100 """ |
96 """ |
101 Public method to get a reference to the menu action. |
97 Public method to get a reference to the menu. |
102 |
98 |
103 @return reference to the menu action |
99 @return reference to the menu |
104 @rtype QAction |
100 @rtype QMenu |
105 """ |
101 """ |
106 if not self.__menuAction: |
|
107 self.__menuAction = QAction(self.tr("AdBlock"), self) |
|
108 # TODO: replace this obsolete function |
|
109 self.__menuAction.setMenu(QMenu()) |
|
110 self.__menuAction.menu().aboutToShow.connect( |
|
111 lambda: self.__createMenu(self.__menuAction.menu())) |
|
112 |
|
113 if self.__enabled: |
102 if self.__enabled: |
114 self.__menuAction.setIcon( |
103 self.__menu.setIcon( |
115 UI.PixmapCache.getIcon("adBlockPlus")) |
104 UI.PixmapCache.getIcon("adBlockPlus")) |
116 else: |
105 else: |
117 self.__menuAction.setIcon( |
106 self.__menu.setIcon( |
118 UI.PixmapCache.getIcon("adBlockPlusDisabled")) |
107 UI.PixmapCache.getIcon("adBlockPlusDisabled")) |
119 |
108 |
120 return self.__menuAction |
109 return self.__menu |
121 |
110 |
122 def __showMenu(self, pos): |
111 def __showMenu(self, pos): |
123 """ |
112 """ |
124 Private slot to show the context menu. |
113 Private slot to show the context menu. |
125 |
114 |
126 @param pos position the context menu should be shown |
115 @param pos position the context menu should be shown |
127 @type QPoint |
116 @type QPoint |
128 """ |
117 """ |
129 menu = QMenu() |
118 self.__menu.exec(pos) |
130 self.__createMenu(menu) |
|
131 menu.exec(pos) |
|
132 |
119 |
133 def __enableAdBlock(self, enable): |
120 def __enableAdBlock(self, enable): |
134 """ |
121 """ |
135 Private slot to enable or disable AdBlock. |
122 Private slot to enable or disable AdBlock. |
136 |
123 |