|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the AdBlock configuration dialog. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 |
|
13 from E4Gui.E4TreeSortFilterProxyModel import E4TreeSortFilterProxyModel |
|
14 |
|
15 import Helpviewer.HelpWindow |
|
16 |
|
17 from Ui_AdBlockDialog import Ui_AdBlockDialog |
|
18 |
|
19 from AdBlockModel import AdBlockModel |
|
20 from AdBlockRule import AdBlockRule |
|
21 |
|
22 import UI.PixmapCache |
|
23 |
|
24 class AdBlockDialog(QDialog, Ui_AdBlockDialog): |
|
25 """ |
|
26 Class implementing the AdBlock configuration dialog. |
|
27 """ |
|
28 def __init__(self, parent = None): |
|
29 """ |
|
30 Constructor |
|
31 """ |
|
32 QDialog.__init__(self, parent) |
|
33 self.setupUi(self) |
|
34 |
|
35 self.clearButton.setIcon(UI.PixmapCache.getIcon("clearLeft.png")) |
|
36 |
|
37 self.__adBlockModel = AdBlockModel(self) |
|
38 self.__proxyModel = E4TreeSortFilterProxyModel(self) |
|
39 self.__proxyModel.setSourceModel(self.__adBlockModel) |
|
40 self.subscriptionsTree.setModel(self.__proxyModel) |
|
41 |
|
42 self.connect(self.searchEdit, SIGNAL("textChanged(QString)"), |
|
43 self.__proxyModel.setFilterFixedString) |
|
44 |
|
45 manager = Helpviewer.HelpWindow.HelpWindow.adblockManager() |
|
46 self.adBlockGroup.setChecked(manager.isEnabled()) |
|
47 self.connect(self.adBlockGroup, SIGNAL("toggled(bool)"), |
|
48 manager.setEnabled) |
|
49 |
|
50 menu = QMenu(self) |
|
51 self.connect(menu, SIGNAL("aboutToShow()"), self.__aboutToShowActionMenu) |
|
52 self.actionButton.setMenu(menu) |
|
53 self.actionButton.setIcon(UI.PixmapCache.getIcon("adBlockAction.png")) |
|
54 self.actionButton.setPopupMode(QToolButton.InstantPopup) |
|
55 |
|
56 subscription = manager.customRules() |
|
57 subscriptionIndex = self.__adBlockModel.subscriptionIndex(subscription) |
|
58 self.subscriptionsTree.expand(self.__proxyModel.mapFromSource(subscriptionIndex)) |
|
59 |
|
60 def model(self): |
|
61 """ |
|
62 Public method to return a reference to the subscriptions tree model. |
|
63 """ |
|
64 return self.subscriptionsTree.model() |
|
65 |
|
66 def setCurrentIndex(self, index): |
|
67 """ |
|
68 Private slot to set the current index of the subscriptions tree. |
|
69 |
|
70 @param index index to be set (QModelIndex) |
|
71 """ |
|
72 self.subscriptionsTree.setCurrentIndex(index) |
|
73 |
|
74 def __aboutToShowActionMenu(self): |
|
75 """ |
|
76 Private slot to show the actions menu. |
|
77 """ |
|
78 menu = self.actionButton.menu() |
|
79 menu.clear() |
|
80 |
|
81 menu.addAction(self.trUtf8("Add Custom Rule"), self.addCustomRule) |
|
82 |
|
83 menu.addAction(self.trUtf8("Learn more about writing rules..."), |
|
84 self.__learnAboutWritingFilters) |
|
85 |
|
86 menu.addSeparator() |
|
87 |
|
88 idx = self.__proxyModel.mapToSource(self.subscriptionsTree.currentIndex()) |
|
89 |
|
90 act = menu.addAction(self.trUtf8("Update Subscription"), |
|
91 self.__updateSubscription) |
|
92 act.setEnabled(idx.isValid()) |
|
93 |
|
94 menu.addAction(self.trUtf8("Browse Subscriptions..."), self.__browseSubscriptions) |
|
95 |
|
96 menu.addSeparator() |
|
97 |
|
98 act = menu.addAction(self.trUtf8("Remove Subscription"), |
|
99 self.__removeSubscription) |
|
100 act.setEnabled(idx.isValid()) |
|
101 |
|
102 def addCustomRule(self, rule): |
|
103 """ |
|
104 Public slot to add a custom AdBlock rule. |
|
105 |
|
106 @param rule string defining the rule to be added (string) |
|
107 """ |
|
108 manager = Helpviewer.HelpWindow.HelpWindow.adblockManager() |
|
109 subscription = manager.customRules() |
|
110 assert subscription is not None |
|
111 subscription.addRule(AdBlockRule(rule)) |
|
112 QApplication.processEvents() |
|
113 |
|
114 parent = self.__adBlockModel.subscriptionIndex(subscription) |
|
115 cnt = self.__adBlockModel.rowCount(parent) |
|
116 ruleIndex = self.__adBlockModel.index(cnt - 1, 0, parent) |
|
117 self.subscriptionsTree.expand(self.__proxyModel.mapFromSource(parent)) |
|
118 self.subscriptionsTree.edit(self.__proxyModel.mapFromSource(ruleIndex)) |
|
119 |
|
120 def __updateSubscription(self): |
|
121 """ |
|
122 Private slot to update the selected subscription. |
|
123 """ |
|
124 idx = self.__proxyModel.mapToSource(self.subscriptionsTree.currentIndex()) |
|
125 if not idx.isValid(): |
|
126 return |
|
127 if idx.parent().isValid(): |
|
128 idx = idx.parent() |
|
129 subscription = self.__adBlockModel.subscription(idx) |
|
130 subscription.updateNow() |
|
131 |
|
132 def __browseSubscriptions(self): |
|
133 """ |
|
134 Private slot to browse the list of available AdBlock subscriptions. |
|
135 """ |
|
136 QDesktopServices.openUrl(QUrl("http://adblockplus.org/en/subscriptions")) |
|
137 |
|
138 def __learnAboutWritingFilters(self): |
|
139 """ |
|
140 Private slot to show the web page about how to write filters. |
|
141 """ |
|
142 QDesktopServices.openUrl(QUrl("http://adblockplus.org/en/filters")) |
|
143 |
|
144 def __removeSubscription(self): |
|
145 """ |
|
146 Private slot to remove the selected subscription. |
|
147 """ |
|
148 idx = self.__proxyModel.mapToSource(self.subscriptionsTree.currentIndex()) |
|
149 if not idx.isValid(): |
|
150 return |
|
151 if idx.parent().isValid(): |
|
152 idx = idx.parent() |
|
153 subscription = self.__adBlockModel.subscription(idx) |
|
154 manager = Helpviewer.HelpWindow.HelpWindow.adblockManager() |
|
155 manager.removeSubscription(subscription) |