Sun, 29 Jul 2012 15:41:58 +0200
Added an icon to the status bar to give direct access to AdBlock.
# -*- coding: utf-8 -*- # Copyright (c) 2009 - 2012 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the AdBlock configuration dialog. """ from PyQt4.QtCore import pyqtSlot, Qt, QUrl from PyQt4.QtGui import QDialog, QMenu, QToolButton, QApplication, QDesktopServices from E5Gui.E5TreeSortFilterProxyModel import E5TreeSortFilterProxyModel import Helpviewer.HelpWindow from .Ui_AdBlockDialog import Ui_AdBlockDialog from .AdBlockModel import AdBlockModel from .AdBlockRule import AdBlockRule import UI.PixmapCache import Preferences class AdBlockDialog(QDialog, Ui_AdBlockDialog): """ Class implementing the AdBlock configuration dialog. """ def __init__(self, parent=None): """ Constructor """ super().__init__(parent) self.setupUi(self) self.clearButton.setIcon(UI.PixmapCache.getIcon("clearLeft.png")) self.iconLabel.setPixmap(UI.PixmapCache.getPixmap("adBlockPlus48.png")) self.updateSpinBox.setValue(Preferences.getHelp("AdBlockUpdatePeriod")) self.__adBlockModel = AdBlockModel(self) self.__proxyModel = E5TreeSortFilterProxyModel(self) self.__proxyModel.setSourceModel(self.__adBlockModel) self.subscriptionsTree.setModel(self.__proxyModel) self.searchEdit.textChanged.connect(self.__proxyModel.setFilterFixedString) manager = Helpviewer.HelpWindow.HelpWindow.adblockManager() self.adBlockGroup.setChecked(manager.isEnabled()) self.adBlockGroup.toggled[bool].connect(manager.setEnabled) menu = QMenu(self) menu.aboutToShow.connect(self.__aboutToShowActionMenu) self.actionButton.setMenu(menu) self.actionButton.setIcon(UI.PixmapCache.getIcon("adBlockAction.png")) self.actionButton.setPopupMode(QToolButton.InstantPopup) if self.adBlockGroup.isChecked(): subscription = manager.customRules() subscriptionIndex = self.__adBlockModel.subscriptionIndex(subscription) self.subscriptionsTree.expand( self.__proxyModel.mapFromSource(subscriptionIndex)) def model(self): """ Public method to return a reference to the subscriptions tree model. """ return self.subscriptionsTree.model() def setCurrentIndex(self, index): """ Private slot to set the current index of the subscriptions tree. @param index index to be set (QModelIndex) """ self.subscriptionsTree.setCurrentIndex(index) def __aboutToShowActionMenu(self): """ Private slot to show the actions menu. """ menu = self.actionButton.menu() menu.clear() menu.addAction(self.trUtf8("Add Custom Rule"), self.addCustomRule) menu.addAction(self.trUtf8("Learn more about writing rules..."), self.__learnAboutWritingFilters) menu.addSeparator() idx = self.__proxyModel.mapToSource(self.subscriptionsTree.currentIndex()) act = menu.addAction(self.trUtf8("Update Subscription"), self.__updateSubscription) act.setEnabled(idx.isValid()) menu.addAction(self.trUtf8("Browse Subscriptions..."), self.__browseSubscriptions) menu.addSeparator() act = menu.addAction(self.trUtf8("Remove Subscription"), self.__removeSubscription) act.setEnabled(idx.isValid()) def addCustomRule(self, rule=""): """ Public slot to add a custom AdBlock rule. @param rule string defining the rule to be added (string) """ manager = Helpviewer.HelpWindow.HelpWindow.adblockManager() subscription = manager.customRules() assert subscription is not None subscription.addRule(AdBlockRule(rule, subscription)) QApplication.processEvents() parent = self.__adBlockModel.subscriptionIndex(subscription) cnt = self.__adBlockModel.rowCount(parent) ruleIndex = self.__adBlockModel.index(cnt - 1, 0, parent) self.subscriptionsTree.expand(self.__proxyModel.mapFromSource(parent)) self.subscriptionsTree.edit(self.__proxyModel.mapFromSource(ruleIndex)) def __updateSubscription(self): """ Private slot to update the selected subscription. """ idx = self.__proxyModel.mapToSource(self.subscriptionsTree.currentIndex()) if not idx.isValid(): return if idx.parent().isValid(): idx = idx.parent() subscription = self.__adBlockModel.subscription(idx) subscription.updateNow() def __browseSubscriptions(self): """ Private slot to browse the list of available AdBlock subscriptions. """ QDesktopServices.openUrl(QUrl("http://adblockplus.org/en/subscriptions")) def __learnAboutWritingFilters(self): """ Private slot to show the web page about how to write filters. """ QDesktopServices.openUrl(QUrl("http://adblockplus.org/en/filters")) def __removeSubscription(self): """ Private slot to remove the selected subscription. """ idx = self.__proxyModel.mapToSource(self.subscriptionsTree.currentIndex()) if not idx.isValid(): return if idx.parent().isValid(): idx = idx.parent() subscription = self.__adBlockModel.subscription(idx) manager = Helpviewer.HelpWindow.HelpWindow.adblockManager() manager.removeSubscription(subscription) @pyqtSlot(int) def on_updateSpinBox_valueChanged(self, value): """ Private slot to handle changes of the update period. @param value update period (integer) """ if value != Preferences.getHelp("AdBlockUpdatePeriod"): Preferences.setHelp("AdBlockUpdatePeriod", value) manager = Helpviewer.HelpWindow.HelpWindow.adblockManager() for subscription in manager.subscriptions(): subscription.checkForUpdate() def showRule(self, rule): """ Public slot to show the given rule. @param rule rule to be shown (AdBlockRule) """ if rule is None: return subscription = rule.subscription() if subscription is None: return self.searchEdit.clear() subscriptionIndex = self.__adBlockModel.subscriptionIndex(subscription) subscriptionProxyIndex = self.__proxyModel.mapFromSource(subscriptionIndex) filter = rule.filter() if filter: indexes = self.__proxyModel.match( subscriptionProxyIndex, Qt.DisplayRole, filter, 1, Qt.MatchStartsWith | Qt.MatchRecursive) if indexes: self.subscriptionsTree.expand(subscriptionProxyIndex) self.subscriptionsTree.scrollTo(indexes[0]) self.subscriptionsTree.setCurrentIndex(indexes[0]) self.raise_()