eric7/WebBrowser/AdBlock/AdBlockIcon.py

branch
eric7
changeset 8345
b2e19966263a
parent 8318
962bce857696
child 8356
68ec9c3d4de5
--- a/eric7/WebBrowser/AdBlock/AdBlockIcon.py	Fri May 21 17:49:43 2021 +0200
+++ b/eric7/WebBrowser/AdBlock/AdBlockIcon.py	Fri May 21 17:51:12 2021 +0200
@@ -8,7 +8,6 @@
 """
 
 from PyQt6.QtCore import Qt
-from PyQt6.QtGui import QAction
 from PyQt6.QtWidgets import QMenu
 
 from E5Gui.E5ClickableLabel import E5ClickableLabel
@@ -30,7 +29,7 @@
         super().__init__(parent)
         
         self.__mw = parent
-        self.__menuAction = None
+        self.__menu = QMenu(self.tr("AdBlock"))
         self.__enabled = False
         
         self.setMaximumHeight(16)
@@ -38,6 +37,7 @@
         self.setToolTip(self.tr(
             "AdBlock lets you block unwanted content on web pages."))
         
+        self.__menu.aboutToShow.connect(self.__aboutToShowMenu)
         self.clicked.connect(self.__showMenu)
     
     def setEnabled(self, enabled):
@@ -54,70 +54,59 @@
             self.setPixmap(
                 UI.PixmapCache.getPixmap("adBlockPlusDisabled16"))
     
-    def __createMenu(self, menu):
+    def __aboutToShowMenu(self):
         """
-        Private slot to create the context menu.
-        
-        @param menu parent menu
-        @type QMenu
+        Private slot to show the context menu.
         """
-        menu.clear()
+        self.__menu.clear()
         
         manager = self.__mw.adBlockManager()
         
         if manager.isEnabled():
-            act = menu.addAction(
+            act = self.__menu.addAction(
                 UI.PixmapCache.getIcon("adBlockPlusDisabled"),
                 self.tr("Disable AdBlock"))
             act.triggered.connect(lambda: self.__enableAdBlock(False))
         else:
-            act = menu.addAction(
+            act = self.__menu.addAction(
                 UI.PixmapCache.getIcon("adBlockPlus"),
                 self.tr("Enable AdBlock"))
             act.triggered.connect(lambda: self.__enableAdBlock(True))
-        menu.addSeparator()
+        self.__menu.addSeparator()
         if manager.isEnabled() and self.__mw.currentBrowser().url().host():
             if self.__isCurrentHostExcepted():
-                act = menu.addAction(
+                act = self.__menu.addAction(
                     UI.PixmapCache.getIcon("adBlockPlus"),
                     self.tr("Remove AdBlock Exception"))
                 act.triggered.connect(lambda: self.__setException(False))
             else:
-                act = menu.addAction(
+                act = self.__menu.addAction(
                     UI.PixmapCache.getIcon("adBlockPlusGreen"),
                     self.tr("Add AdBlock Exception"))
                 act.triggered.connect(lambda: self.__setException(True))
-        menu.addAction(
+        self.__menu.addAction(
             UI.PixmapCache.getIcon("adBlockPlusGreen"),
             self.tr("AdBlock Exceptions..."), manager.showExceptionsDialog)
-        menu.addSeparator()
-        menu.addAction(
+        self.__menu.addSeparator()
+        self.__menu.addAction(
             UI.PixmapCache.getIcon("adBlockPlus"),
             self.tr("AdBlock Configuration..."), manager.showDialog)
     
-    # TODO: change this to return a QMenu
-    def menuAction(self):
-        """
-        Public method to get a reference to the menu action.
-        
-        @return reference to the menu action
-        @rtype QAction
+    def menu(self):
         """
-        if not self.__menuAction:
-            self.__menuAction = QAction(self.tr("AdBlock"), self)
-            # TODO: replace this obsolete function
-            self.__menuAction.setMenu(QMenu())
-            self.__menuAction.menu().aboutToShow.connect(
-                lambda: self.__createMenu(self.__menuAction.menu()))
+        Public method to get a reference to the menu.
         
+        @return reference to the menu
+        @rtype QMenu
+        """
         if self.__enabled:
-            self.__menuAction.setIcon(
+            self.__menu.setIcon(
                 UI.PixmapCache.getIcon("adBlockPlus"))
         else:
-            self.__menuAction.setIcon(
+            self.__menu.setIcon(
                 UI.PixmapCache.getIcon("adBlockPlusDisabled"))
         
-        return self.__menuAction
+        return self.__menu
     
     def __showMenu(self, pos):
         """
@@ -126,9 +115,7 @@
         @param pos position the context menu should be shown
         @type QPoint
         """
-        menu = QMenu()
-        self.__createMenu(menu)
-        menu.exec(pos)
+        self.__menu.exec(pos)
     
     def __enableAdBlock(self, enable):
         """

eric ide

mercurial