"Fossies" - the Fresh Open Source Software Archive

Member "eric6-20.8/eric/eric6/WebBrowser/AdBlock/AdBlockIcon.py" (2 May 2020, 6176 Bytes) of package /linux/misc/eric6-20.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "AdBlockIcon.py" see the Fossies "Dox" file reference documentation.

    1 # -*- coding: utf-8 -*-
    2 
    3 # Copyright (c) 2012 - 2020 Detlev Offenbach <detlev@die-offenbachs.de>
    4 #
    5 
    6 """
    7 Module implementing the AdBlock icon for the main window status bar.
    8 """
    9 
   10 
   11 from PyQt5.QtCore import Qt
   12 from PyQt5.QtWidgets import QAction, QMenu
   13 
   14 from E5Gui.E5ClickableLabel import E5ClickableLabel
   15 
   16 import UI.PixmapCache
   17 
   18 
   19 class AdBlockIcon(E5ClickableLabel):
   20     """
   21     Class implementing the AdBlock icon for the main window status bar.
   22     """
   23     def __init__(self, parent):
   24         """
   25         Constructor
   26         
   27         @param parent reference to the parent widget
   28         @type WebBrowserWindow
   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
   48         @type bool
   49         """
   50         self.__enabled = enabled
   51         if enabled:
   52             self.currentChanged()
   53         else:
   54             self.setPixmap(
   55                 UI.PixmapCache.getPixmap("adBlockPlusDisabled16"))
   56     
   57     def __createMenu(self, menu):
   58         """
   59         Private slot to create the context menu.
   60         
   61         @param menu parent menu
   62         @type QMenu
   63         """
   64         menu.clear()
   65         
   66         manager = self.__mw.adBlockManager()
   67         
   68         if manager.isEnabled():
   69             act = menu.addAction(
   70                 UI.PixmapCache.getIcon("adBlockPlusDisabled"),
   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"),
   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 self.__mw.currentBrowser().url().host():
   82             if self.__isCurrentHostExcepted():
   83                 act = menu.addAction(
   84                     UI.PixmapCache.getIcon("adBlockPlus"),
   85                     self.tr("Remove AdBlock Exception"))
   86                 act.setData(False)
   87                 act.triggered.connect(lambda: self.__setException(act))
   88             else:
   89                 act = menu.addAction(
   90                     UI.PixmapCache.getIcon("adBlockPlusGreen"),
   91                     self.tr("Add AdBlock Exception"))
   92                 act.setData(True)
   93                 act.triggered.connect(lambda: self.__setException(act))
   94         menu.addAction(
   95             UI.PixmapCache.getIcon("adBlockPlusGreen"),
   96             self.tr("AdBlock Exceptions..."), manager.showExceptionsDialog)
   97         menu.addSeparator()
   98         menu.addAction(
   99             UI.PixmapCache.getIcon("adBlockPlus"),
  100             self.tr("AdBlock Configuration..."), manager.showDialog)
  101     
  102     def menuAction(self):
  103         """
  104         Public method to get a reference to the menu action.
  105         
  106         @return reference to the menu action
  107         @rtype QAction
  108         """
  109         if not self.__menuAction:
  110             self.__menuAction = QAction(self.tr("AdBlock"), self)
  111             self.__menuAction.setMenu(QMenu())
  112             self.__menuAction.menu().aboutToShow.connect(
  113                 lambda: self.__createMenu(self.__menuAction.menu()))
  114         
  115         if self.__enabled:
  116             self.__menuAction.setIcon(
  117                 UI.PixmapCache.getIcon("adBlockPlus"))
  118         else:
  119             self.__menuAction.setIcon(
  120                 UI.PixmapCache.getIcon("adBlockPlusDisabled"))
  121         
  122         return self.__menuAction
  123     
  124     def __showMenu(self, pos):
  125         """
  126         Private slot to show the context menu.
  127         
  128         @param pos position the context menu should be shown
  129         @type QPoint
  130         """
  131         menu = QMenu()
  132         self.__createMenu(menu)
  133         menu.exec_(pos)
  134     
  135     def __enableAdBlock(self, act):
  136         """
  137         Private slot to enable or disable AdBlock.
  138         
  139         @param act reference to the action
  140         @type QAction
  141         """
  142         self.__mw.adBlockManager().setEnabled(act.data())
  143     
  144     def __isCurrentHostExcepted(self):
  145         """
  146         Private method to check, if the host of the current browser is
  147         excepted.
  148         
  149         @return flag indicating an exception
  150         @rtype bool
  151         """
  152         browser = self.__mw.currentBrowser()
  153         if browser is None:
  154             return False
  155         
  156         urlHost = browser.page().url().host()
  157         
  158         return (
  159             urlHost and
  160             self.__mw.adBlockManager().isHostExcepted(urlHost)
  161         )
  162     
  163     def currentChanged(self):
  164         """
  165         Public slot to handle a change of the current browser tab.
  166         """
  167         if self.__enabled:
  168             if self.__isCurrentHostExcepted():
  169                 self.setPixmap(
  170                     UI.PixmapCache.getPixmap("adBlockPlusGreen16"))
  171             else:
  172                 self.setPixmap(UI.PixmapCache.getPixmap("adBlockPlus16"))
  173     
  174     def __setException(self, act):
  175         """
  176         Private slot to add or remove the current host from the list of
  177         exceptions.
  178         
  179         @param act referenced to the action
  180         @type QAction
  181         """
  182         urlHost = self.__mw.currentBrowser().url().host()
  183         if act.data():
  184             self.__mw.adBlockManager().addException(urlHost)
  185         else:
  186             self.__mw.adBlockManager().removeException(urlHost)
  187         self.currentChanged()
  188     
  189     def sourceChanged(self, browser, url):
  190         """
  191         Public slot to handle URL changes.
  192         
  193         @param browser reference to the browser
  194         @type WebBrowserView
  195         @param url new URL
  196         @type QUrl
  197         """
  198         if browser == self.__mw.currentBrowser():
  199             self.currentChanged()