|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the whitelist edit dialog. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot |
|
13 from PyQt5.QtWidgets import QDialog, QInputDialog, QLineEdit |
|
14 |
|
15 from .Ui_EditWhiteListDialog import Ui_EditWhiteListDialog |
|
16 |
|
17 |
|
18 class EditWhiteListDialog(QDialog, Ui_EditWhiteListDialog): |
|
19 """ |
|
20 Class implementing the whitelist edit dialog. |
|
21 """ |
|
22 def __init__(self, whitelists, parent=None): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param whitelists dictionary containing the whitelists |
|
27 @type dict of list of str |
|
28 @param parent reference to the parent widget |
|
29 @type QWidget |
|
30 """ |
|
31 super(EditWhiteListDialog, self).__init__(parent) |
|
32 self.setupUi(self) |
|
33 |
|
34 self.__lists = [ |
|
35 self.classesList, |
|
36 self.functionsList, |
|
37 self.attributesList, |
|
38 self.variablesList, |
|
39 self.propertiesList, |
|
40 self.patternsList, |
|
41 ] |
|
42 |
|
43 self.classesList.addItems(whitelists["class"]) |
|
44 self.functionsList.addItems(whitelists["function"]) |
|
45 self.attributesList.addItems(whitelists["attribute"]) |
|
46 self.variablesList.addItems(whitelists["variable"]) |
|
47 self.propertiesList.addItems(whitelists["property"]) |
|
48 self.patternsList.addItems(whitelists["__patterns__"]) |
|
49 |
|
50 self.listsWidget.setCurrentIndex(self.listsWidget.count() - 1) |
|
51 |
|
52 @pyqtSlot() |
|
53 def on_patternsList_itemSelectionChanged(self): |
|
54 """ |
|
55 Private slot to react upon a change of selection in the patterns list. |
|
56 """ |
|
57 self.__setButtonEnabledStates() |
|
58 |
|
59 @pyqtSlot() |
|
60 def on_propertiesList_itemSelectionChanged(self): |
|
61 """ |
|
62 Private slot to react upon a change of selection in the properties |
|
63 list. |
|
64 """ |
|
65 self.__setButtonEnabledStates() |
|
66 |
|
67 @pyqtSlot() |
|
68 def on_variablesList_itemSelectionChanged(self): |
|
69 """ |
|
70 Private slot to react upon a change of selection in the variables list. |
|
71 """ |
|
72 self.__setButtonEnabledStates() |
|
73 |
|
74 @pyqtSlot() |
|
75 def on_attributesList_itemSelectionChanged(self): |
|
76 """ |
|
77 Private slot to react upon a change of selection in the attributes |
|
78 list. |
|
79 """ |
|
80 self.__setButtonEnabledStates() |
|
81 |
|
82 @pyqtSlot() |
|
83 def on_functionsList_itemSelectionChanged(self): |
|
84 """ |
|
85 Private slot to react upon a change of selection in the functions list. |
|
86 """ |
|
87 self.__setButtonEnabledStates() |
|
88 |
|
89 @pyqtSlot() |
|
90 def on_classesList_itemSelectionChanged(self): |
|
91 """ |
|
92 Private slot to react upon a change of selection in the classes list. |
|
93 """ |
|
94 self.__setButtonEnabledStates() |
|
95 |
|
96 def __isPattern(self, name): |
|
97 """ |
|
98 Private method to check, if a name is a wildcard pattern. |
|
99 |
|
100 @param name name to be checked |
|
101 @type str |
|
102 @return flag indicating a wildcard pattern |
|
103 @rtype bool |
|
104 """ |
|
105 isPattern = False |
|
106 for char in "*?[": |
|
107 if char in name: |
|
108 isPattern = True |
|
109 break |
|
110 return isPattern |
|
111 |
|
112 @pyqtSlot() |
|
113 def on_addButton_clicked(self): |
|
114 """ |
|
115 Private slot to add an entry to the current list. |
|
116 """ |
|
117 name, ok = QInputDialog.getText( |
|
118 self, |
|
119 self.tr("Add Whitelist"), |
|
120 self.tr("Enter a name or wildcard pattern to the current" |
|
121 " whitelist:"), |
|
122 QLineEdit.Normal) |
|
123 if ok and bool(name): |
|
124 curr = self.__lists[self.listsWidget.currentIndex()] |
|
125 if curr is self.patternsList or self.__isPattern(name): |
|
126 self.patternsList.addItem(name) |
|
127 else: |
|
128 curr.addItem(name) |
|
129 |
|
130 @pyqtSlot() |
|
131 def on_removeButton_clicked(self): |
|
132 """ |
|
133 Private slot to remove the selected entries from the current list. |
|
134 """ |
|
135 curr = self.__lists[self.listsWidget.currentIndex()] |
|
136 for itm in curr.selectedItems(): |
|
137 row = curr.row(itm) |
|
138 curr.takeItem(row) |
|
139 del itm |
|
140 |
|
141 @pyqtSlot() |
|
142 def on_removeAllButton_clicked(self): |
|
143 """ |
|
144 Private slot to remove all entries from the current list. |
|
145 """ |
|
146 curr = self.__lists[self.listsWidget.currentIndex()] |
|
147 curr.clear() |
|
148 |
|
149 @pyqtSlot(int) |
|
150 def on_listsWidget_currentChanged(self, index): |
|
151 """ |
|
152 Private slot handling the selection of tab. |
|
153 |
|
154 @param index index of the selected tab |
|
155 @type int |
|
156 """ |
|
157 self.__setButtonEnabledStates() |
|
158 |
|
159 def __setButtonEnabledStates(self): |
|
160 """ |
|
161 Private slot to set the state of various buttons. |
|
162 """ |
|
163 curr = self.__lists[self.listsWidget.currentIndex()] |
|
164 self.removeButton.setEnabled(len(curr.selectedItems()) > 0) |
|
165 self.removeAllButton.setEnabled(curr.count() > 0) |
|
166 |
|
167 def __getWhiteList(self, listWidget): |
|
168 """ |
|
169 Private method to get the whitelisted names from a list widget. |
|
170 |
|
171 @param listWidget reference to the list widget |
|
172 @type QListWidget |
|
173 @return whitelisted names |
|
174 @rtype list of str |
|
175 """ |
|
176 whitelist = [] |
|
177 for row in range(listWidget.count()): |
|
178 whitelist.append(listWidget.item(row).text()) |
|
179 return whitelist |
|
180 |
|
181 def getWhiteLists(self): |
|
182 """ |
|
183 Public methods to retrieve the various whitelists. |
|
184 |
|
185 @return dictionary containing the whitelists |
|
186 @rtype dict of list of str |
|
187 """ |
|
188 whitelists = { |
|
189 "class": self.__getWhiteList(self.classesList), |
|
190 "function": self.__getWhiteList(self.functionsList), |
|
191 "attribute": self.__getWhiteList(self.attributesList), |
|
192 "variable": self.__getWhiteList(self.variablesList), |
|
193 "property": self.__getWhiteList(self.propertiesList), |
|
194 "__patterns__": self.__getWhiteList(self.patternsList), |
|
195 } |
|
196 return whitelists |