|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog for entering character classes. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals # __IGNORE_WARNING__ |
|
11 |
|
12 from PyQt4.QtCore import QRegExp |
|
13 from PyQt4.QtGui import QWidget, QDialog, QVBoxLayout, QHBoxLayout, QScrollArea, \ |
|
14 QPushButton, QSpacerItem, QSizePolicy, QComboBox, QRegExpValidator, QLineEdit, QLabel |
|
15 |
|
16 from .Ui_QRegularExpressionWizardCharactersDialog import \ |
|
17 Ui_QRegularExpressionWizardCharactersDialog |
|
18 |
|
19 |
|
20 class QRegularExpressionWizardCharactersDialog( |
|
21 QDialog, Ui_QRegularExpressionWizardCharactersDialog): |
|
22 """ |
|
23 Class implementing a dialog for entering character classes. |
|
24 """ |
|
25 def __init__(self, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param parent reference to the parent widget (QWidget) |
|
30 """ |
|
31 super(QRegularExpressionWizardCharactersDialog, self).__init__(parent) |
|
32 self.setupUi(self) |
|
33 |
|
34 self.__initCharacterSelectors() |
|
35 |
|
36 self.comboItems = [] |
|
37 self.singleComboItems = [] # these are in addition to the above |
|
38 self.comboItems.append((self.trUtf8("Normal character"), "-c")) |
|
39 self.comboItems.append((self.trUtf8( |
|
40 "Unicode character in hexadecimal notation"), "-h")) |
|
41 self.comboItems.append((self.trUtf8( |
|
42 "ASCII/Latin1 character in octal notation"), "-o")) |
|
43 self.singleComboItems.extend([ |
|
44 ("---", "-i"), |
|
45 (self.trUtf8("Bell character (\\a)"), "\\a"), |
|
46 (self.trUtf8("Escape character (\\e)"), "\\e"), |
|
47 (self.trUtf8("Page break (\\f)"), "\\f"), |
|
48 (self.trUtf8("Line feed (\\n)"), "\\n"), |
|
49 (self.trUtf8("Carriage return (\\r)"), "\\r"), |
|
50 (self.trUtf8("Horizontal tabulator (\\t)"), "\\t"), |
|
51 ("---", "-i"), |
|
52 (self.trUtf8("Character Category"), "-ccp"), |
|
53 (self.trUtf8("Special Character Category"), "-csp"), |
|
54 (self.trUtf8("Character Block"), "-cbp"), |
|
55 (self.trUtf8("POSIX Named Set"), "-psp"), |
|
56 (self.trUtf8("Not Character Category"), "-ccn"), |
|
57 (self.trUtf8("Not Character Block"), "-cbn"), |
|
58 (self.trUtf8("Not Special Character Category"), "-csn"), |
|
59 (self.trUtf8("Not POSIX Named Set"), "-psn"), |
|
60 ]) |
|
61 |
|
62 self.charValidator = QRegExpValidator(QRegExp(".{0,1}"), self) |
|
63 self.hexValidator = QRegExpValidator(QRegExp("[0-9a-fA-F]{0,4}"), self) |
|
64 self.octValidator = QRegExpValidator(QRegExp("[0-3]?[0-7]{0,2}"), self) |
|
65 |
|
66 # generate dialog part for single characters |
|
67 self.singlesBoxLayout = QVBoxLayout(self.singlesBox) |
|
68 self.singlesBoxLayout.setObjectName("singlesBoxLayout") |
|
69 self.singlesBoxLayout.setSpacing(6) |
|
70 self.singlesBoxLayout.setMargin(6) |
|
71 self.singlesBox.setLayout(self.singlesBoxLayout) |
|
72 self.singlesView = QScrollArea(self.singlesBox) |
|
73 self.singlesView.setObjectName("singlesView") |
|
74 self.singlesBoxLayout.addWidget(self.singlesView) |
|
75 |
|
76 self.singlesItemsBox = QWidget(self) |
|
77 self.singlesView.setWidget(self.singlesItemsBox) |
|
78 self.singlesItemsBox.setObjectName("singlesItemsBox") |
|
79 self.singlesItemsBox.setMinimumWidth(1000) |
|
80 self.singlesItemsBoxLayout = QVBoxLayout(self.singlesItemsBox) |
|
81 self.singlesItemsBoxLayout.setMargin(6) |
|
82 self.singlesItemsBoxLayout.setSpacing(6) |
|
83 self.singlesItemsBox.setLayout(self.singlesItemsBoxLayout) |
|
84 self.singlesEntries = [] |
|
85 self.__addSinglesLine() |
|
86 |
|
87 hlayout0 = QHBoxLayout() |
|
88 hlayout0.setMargin(0) |
|
89 hlayout0.setSpacing(6) |
|
90 hlayout0.setObjectName("hlayout0") |
|
91 self.moreSinglesButton = QPushButton(self.trUtf8("Additional Entries"), |
|
92 self.singlesBox) |
|
93 self.moreSinglesButton.setObjectName("moreSinglesButton") |
|
94 hlayout0.addWidget(self.moreSinglesButton) |
|
95 hspacer0 = QSpacerItem(30, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) |
|
96 hlayout0.addItem(hspacer0) |
|
97 self.singlesBoxLayout.addLayout(hlayout0) |
|
98 self.moreSinglesButton.clicked[()].connect(self.__addSinglesLine) |
|
99 |
|
100 # generate dialog part for character ranges |
|
101 self.rangesBoxLayout = QVBoxLayout(self.rangesBox) |
|
102 self.rangesBoxLayout.setObjectName("rangesBoxLayout") |
|
103 self.rangesBoxLayout.setSpacing(6) |
|
104 self.rangesBoxLayout.setMargin(6) |
|
105 self.rangesBox.setLayout(self.rangesBoxLayout) |
|
106 self.rangesView = QScrollArea(self.rangesBox) |
|
107 self.rangesView.setObjectName("rangesView") |
|
108 self.rangesBoxLayout.addWidget(self.rangesView) |
|
109 |
|
110 self.rangesItemsBox = QWidget(self) |
|
111 self.rangesView.setWidget(self.rangesItemsBox) |
|
112 self.rangesItemsBox.setObjectName("rangesItemsBox") |
|
113 self.rangesItemsBox.setMinimumWidth(1000) |
|
114 self.rangesItemsBoxLayout = QVBoxLayout(self.rangesItemsBox) |
|
115 self.rangesItemsBoxLayout.setMargin(6) |
|
116 self.rangesItemsBoxLayout.setSpacing(6) |
|
117 self.rangesItemsBox.setLayout(self.rangesItemsBoxLayout) |
|
118 self.rangesEntries = [] |
|
119 self.__addRangesLine() |
|
120 |
|
121 hlayout1 = QHBoxLayout() |
|
122 hlayout1.setMargin(0) |
|
123 hlayout1.setSpacing(6) |
|
124 hlayout1.setObjectName("hlayout1") |
|
125 self.moreRangesButton = QPushButton(self.trUtf8("Additional Entries"), |
|
126 self.rangesBox) |
|
127 self.moreSinglesButton.setObjectName("moreRangesButton") |
|
128 hlayout1.addWidget(self.moreRangesButton) |
|
129 hspacer1 = QSpacerItem(30, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) |
|
130 hlayout1.addItem(hspacer1) |
|
131 self.rangesBoxLayout.addLayout(hlayout1) |
|
132 self.moreRangesButton.clicked[()].connect(self.__addRangesLine) |
|
133 |
|
134 def __initCharacterSelectors(self): |
|
135 """ |
|
136 Private method to initialize the W3C character selector entries. |
|
137 """ |
|
138 self.__characterCategories = ( |
|
139 # display name code |
|
140 (self.trUtf8("Letter, Any"), "L"), |
|
141 (self.trUtf8("Letter, Lower case"), "Ll"), |
|
142 (self.trUtf8("Letter, Modifier"), "Lm"), |
|
143 (self.trUtf8("Letter, Other"), "Lo"), |
|
144 (self.trUtf8("Letter, Title case"), "Lt"), |
|
145 (self.trUtf8("Letter, Upper case"), "Lu"), |
|
146 (self.trUtf8("Letter, Lower, Upper or Title"), "L&"), |
|
147 (self.trUtf8("Mark, Any"), "M"), |
|
148 (self.trUtf8("Mark, Spacing"), "Mc"), |
|
149 (self.trUtf8("Mark, Enclosing"), "Me"), |
|
150 (self.trUtf8("Mark, Non-spacing"), "Mn"), |
|
151 (self.trUtf8("Number, Any"), "N"), |
|
152 (self.trUtf8("Number, Decimal"), "Nd"), |
|
153 (self.trUtf8("Number, Letter"), "Nl"), |
|
154 (self.trUtf8("Number, Other"), "No"), |
|
155 (self.trUtf8("Punctuation, Any"), "P"), |
|
156 (self.trUtf8("Punctuation, Connector"), "Pc"), |
|
157 (self.trUtf8("Punctuation, Dash"), "Pd"), |
|
158 (self.trUtf8("Punctuation, Close"), "Pe"), |
|
159 (self.trUtf8("Punctuation, Final"), "Pf"), |
|
160 (self.trUtf8("Punctuation, Initial"), "Pi"), |
|
161 (self.trUtf8("Punctuation, Other"), "Po"), |
|
162 (self.trUtf8("Punctuation, Open"), "Ps"), |
|
163 (self.trUtf8("Symbol, Any"), "S"), |
|
164 (self.trUtf8("Symbol, Currency"), "Sc"), |
|
165 (self.trUtf8("Symbol, Modifier"), "Sk"), |
|
166 (self.trUtf8("Symbol, Mathematical"), "Sm"), |
|
167 (self.trUtf8("Symbol, Other"), "So"), |
|
168 (self.trUtf8("Separator, Any"), "Z"), |
|
169 (self.trUtf8("Separator, Line"), "Zl"), |
|
170 (self.trUtf8("Separator, Paragraph"), "Zp"), |
|
171 (self.trUtf8("Separator, Space"), "Zs"), |
|
172 (self.trUtf8("Other, Any"), "C"), |
|
173 (self.trUtf8("Other, Control"), "Cc"), |
|
174 (self.trUtf8("Other, Format"), "Cf"), |
|
175 (self.trUtf8("Other, Unassigned"), "Cn"), |
|
176 (self.trUtf8("Other, Private Use"), "Co"), |
|
177 (self.trUtf8("Other, Surrogat"), "Cn"), |
|
178 ) |
|
179 |
|
180 self.__specialCharacterCategories = ( |
|
181 # display name code |
|
182 (self.trUtf8("Alphanumeric"), "Xan"), |
|
183 (self.trUtf8("POSIX Space"), "Xps"), |
|
184 (self.trUtf8("Perl Space"), "Xsp"), |
|
185 (self.trUtf8("Universal Character"), "Xuc"), |
|
186 (self.trUtf8("Perl Word"), "Xan"), |
|
187 ) |
|
188 |
|
189 self.__characterBlocks = ( |
|
190 # display name code |
|
191 (self.trUtf8("Arabic"), "Arabic"), |
|
192 (self.trUtf8("Armenian"), "Armenian"), |
|
193 (self.trUtf8("Avestan"), "Avestan"), |
|
194 (self.trUtf8("Balinese"), "Balinese"), |
|
195 (self.trUtf8("Bamum"), "Bamum"), |
|
196 (self.trUtf8("Batak"), "Batak"), |
|
197 (self.trUtf8("Bengali"), "Bengali"), |
|
198 (self.trUtf8("Bopomofo"), "Bopomofo"), |
|
199 (self.trUtf8("Brahmi"), "Brahmi"), |
|
200 (self.trUtf8("Braille"), "Braille"), |
|
201 (self.trUtf8("Buginese"), "Buginese"), |
|
202 (self.trUtf8("Buhid"), "Buhid"), |
|
203 (self.trUtf8("Canadian Aboriginal"), "Canadian_Aboriginal"), |
|
204 (self.trUtf8("Carian"), "Carian"), |
|
205 (self.trUtf8("Chakma"), "Chakma"), |
|
206 (self.trUtf8("Cham"), "Cham"), |
|
207 (self.trUtf8("Cherokee"), "Cherokee"), |
|
208 (self.trUtf8("Common"), "Common"), |
|
209 (self.trUtf8("Coptic"), "Coptic"), |
|
210 (self.trUtf8("Cuneiform"), "Cuneiform"), |
|
211 (self.trUtf8("Cypriot"), "Cypriot"), |
|
212 (self.trUtf8("Cyrillic"), "Cyrillic"), |
|
213 (self.trUtf8("Deseret"), "Deseret,"), |
|
214 (self.trUtf8("Devanagari"), "Devanagari"), |
|
215 (self.trUtf8("Egyptian Hieroglyphs"), "Egyptian_Hieroglyphs"), |
|
216 (self.trUtf8("Ethiopic"), "Ethiopic"), |
|
217 (self.trUtf8("Georgian"), "Georgian"), |
|
218 (self.trUtf8("Glagolitic"), "Glagolitic"), |
|
219 (self.trUtf8("Gothic"), "Gothic"), |
|
220 (self.trUtf8("Greek"), "Greek"), |
|
221 (self.trUtf8("Gujarati"), "Gujarati"), |
|
222 (self.trUtf8("Gurmukhi"), "Gurmukhi"), |
|
223 (self.trUtf8("Han"), "Han"), |
|
224 (self.trUtf8("Hangul"), "Hangul"), |
|
225 (self.trUtf8("Hanunoo"), "Hanunoo"), |
|
226 (self.trUtf8("Hebrew"), "Hebrew"), |
|
227 (self.trUtf8("Hiragana"), "Hiragana"), |
|
228 (self.trUtf8("Imperial Aramaic"), "Imperial_Aramaic"), |
|
229 (self.trUtf8("Inherited"), "Inherited"), |
|
230 (self.trUtf8("Inscriptional Pahlavi"), "Inscriptional_Pahlavi"), |
|
231 (self.trUtf8("Inscriptional Parthian"), "Inscriptional_Parthian"), |
|
232 (self.trUtf8("Javanese"), "Javanese"), |
|
233 (self.trUtf8("Kaithi"), "Kaithi"), |
|
234 (self.trUtf8("Kannada"), "Kannada"), |
|
235 (self.trUtf8("Katakana"), "Katakana"), |
|
236 (self.trUtf8("Kayah Li"), "Kayah_Li"), |
|
237 (self.trUtf8("Kharoshthi"), "Kharoshthi"), |
|
238 (self.trUtf8("Khmer"), "Khmer"), |
|
239 (self.trUtf8("Lao"), "Lao"), |
|
240 (self.trUtf8("Latin"), "Latin"), |
|
241 (self.trUtf8("Lepcha"), "Lepcha"), |
|
242 (self.trUtf8("Limbu"), "Limbu"), |
|
243 (self.trUtf8("Linear B"), "Linear_B"), |
|
244 (self.trUtf8("Lisu"), "Lisu"), |
|
245 (self.trUtf8("Lycian"), "Lycian"), |
|
246 (self.trUtf8("Lydian"), "Lydian"), |
|
247 (self.trUtf8("Malayalam"), "Malayalam"), |
|
248 (self.trUtf8("Mandaic"), "Mandaic"), |
|
249 (self.trUtf8("Meetei Mayek"), "Meetei_Mayek"), |
|
250 (self.trUtf8("Meroitic Cursive"), "Meroitic_Cursive"), |
|
251 (self.trUtf8("Meroitic Hieroglyphs"), "Meroitic_Hieroglyphs"), |
|
252 (self.trUtf8("Miao"), "Miao"), |
|
253 (self.trUtf8("Mongolian"), "Mongolian"), |
|
254 (self.trUtf8("Myanmar"), "Myanmar"), |
|
255 (self.trUtf8("New Tai Lue"), "New_Tai_Lue"), |
|
256 (self.trUtf8("N'Ko"), "Nko"), |
|
257 (self.trUtf8("Ogham"), "Ogham"), |
|
258 (self.trUtf8("Old Italic"), "Old_Italic"), |
|
259 (self.trUtf8("Old Persian"), "Old_Persian"), |
|
260 (self.trUtf8("Old South Arabian"), "Old_South_Arabian"), |
|
261 (self.trUtf8("Old Turkic"), "Old_Turkic,"), |
|
262 (self.trUtf8("Ol Chiki"), "Ol_Chiki"), |
|
263 (self.trUtf8("Oriya"), "Oriya"), |
|
264 (self.trUtf8("Osmanya"), "Osmanya"), |
|
265 (self.trUtf8("Phags-pa"), "Phags_Pa"), |
|
266 (self.trUtf8("Phoenician"), "Phoenician"), |
|
267 (self.trUtf8("Rejang"), "Rejang"), |
|
268 (self.trUtf8("Runic"), "Runic"), |
|
269 (self.trUtf8("Samaritan"), "Samaritan"), |
|
270 (self.trUtf8("Saurashtra"), "Saurashtra"), |
|
271 (self.trUtf8("Sharada"), "Sharada"), |
|
272 (self.trUtf8("Shavian"), "Shavian"), |
|
273 (self.trUtf8("Sinhala"), "Sinhala"), |
|
274 (self.trUtf8("Sora Sompeng"), "Sora_Sompeng"), |
|
275 (self.trUtf8("Sundanese"), "Sundanese"), |
|
276 (self.trUtf8("Syloti Nagri"), "Syloti_Nagri"), |
|
277 (self.trUtf8("Syriac"), "Syriac"), |
|
278 (self.trUtf8("Tagalog"), "Tagalog"), |
|
279 (self.trUtf8("Tagbanwa"), "Tagbanwa"), |
|
280 (self.trUtf8("Tai Le"), "Tai_Le"), |
|
281 (self.trUtf8("Tai Tham"), "Tai_Tham"), |
|
282 (self.trUtf8("Tai Viet"), "Tai_Viet"), |
|
283 (self.trUtf8("Takri"), "Takri"), |
|
284 (self.trUtf8("Tamil"), "Tamil"), |
|
285 (self.trUtf8("Telugu"), "Telugu"), |
|
286 (self.trUtf8("Thaana"), "Thaana"), |
|
287 (self.trUtf8("Thai"), "Thai"), |
|
288 (self.trUtf8("Tibetan"), "Tibetan"), |
|
289 (self.trUtf8("Tifinagh"), "Tifinagh"), |
|
290 (self.trUtf8("Ugaritic"), "Ugaritic"), |
|
291 (self.trUtf8("Vai"), "Vai"), |
|
292 (self.trUtf8("Yi"), "Yi"), |
|
293 ) |
|
294 |
|
295 self.__posixNamedSets = ( |
|
296 # display name code |
|
297 (self.trUtf8("Alphanumeric"), "alnum"), |
|
298 (self.trUtf8("Alphabetic"), "alpha"), |
|
299 (self.trUtf8("ASCII"), "ascii"), |
|
300 (self.trUtf8("Word Letter"), "word"), |
|
301 (self.trUtf8("Lower Case Letter"), "lower"), |
|
302 (self.trUtf8("Upper Case Letter"), "upper"), |
|
303 (self.trUtf8("Decimal Digit"), "digit"), |
|
304 (self.trUtf8("Hexadecimal Digit"), "xdigit"), |
|
305 (self.trUtf8("Space or Tab"), "blank"), |
|
306 (self.trUtf8("White Space"), "space"), |
|
307 (self.trUtf8("Printing (excl. space)"), "graph"), |
|
308 (self.trUtf8("Printing (incl. space)"), "print"), |
|
309 (self.trUtf8("Printing (excl. alphanumeric)"), "punct"), |
|
310 (self.trUtf8("Control Character"), "cntrl"), |
|
311 ) |
|
312 |
|
313 def __populateCharTypeCombo(self, combo, isSingle): |
|
314 """ |
|
315 Private method to populate a given character type selection combo box. |
|
316 |
|
317 @param combo reference to the combo box to be populated (QComboBox) |
|
318 @param isSingle flag indicating a singles combo (boolean) |
|
319 """ |
|
320 for txt, value in self.comboItems: |
|
321 combo.addItem(txt, value) |
|
322 if isSingle: |
|
323 for txt, value in self.singleComboItems: |
|
324 combo.addItem(txt, value) |
|
325 |
|
326 def __addSinglesLine(self): |
|
327 """ |
|
328 Private slot to add a line of entry widgets for single characters. |
|
329 """ |
|
330 hbox = QWidget(self.singlesItemsBox) |
|
331 hboxLayout = QHBoxLayout(hbox) |
|
332 hboxLayout.setMargin(0) |
|
333 hboxLayout.setSpacing(6) |
|
334 hbox.setLayout(hboxLayout) |
|
335 cb1 = QComboBox(hbox) |
|
336 cb1.setEditable(False) |
|
337 self.__populateCharTypeCombo(cb1, True) |
|
338 hboxLayout.addWidget(cb1) |
|
339 le1 = QLineEdit(hbox) |
|
340 le1.setValidator(self.charValidator) |
|
341 hboxLayout.addWidget(le1) |
|
342 cb1a = QComboBox(hbox) |
|
343 cb1a.setEditable(False) |
|
344 cb1a.setSizeAdjustPolicy(QComboBox.AdjustToContents) |
|
345 hboxLayout.addWidget(cb1a) |
|
346 cb1a.hide() |
|
347 cb2 = QComboBox(hbox) |
|
348 cb2.setEditable(False) |
|
349 self.__populateCharTypeCombo(cb2, True) |
|
350 hboxLayout.addWidget(cb2) |
|
351 le2 = QLineEdit(hbox) |
|
352 le2.setValidator(self.charValidator) |
|
353 hboxLayout.addWidget(le2) |
|
354 cb2a = QComboBox(hbox) |
|
355 cb2a.setEditable(False) |
|
356 cb2a.setSizeAdjustPolicy(QComboBox.AdjustToContents) |
|
357 hboxLayout.addWidget(cb2a) |
|
358 cb2a.hide() |
|
359 self.singlesItemsBoxLayout.addWidget(hbox) |
|
360 |
|
361 cb1.activated[int].connect(self.__singlesCharTypeSelected) |
|
362 cb2.activated[int].connect(self.__singlesCharTypeSelected) |
|
363 hbox.show() |
|
364 |
|
365 self.singlesItemsBox.adjustSize() |
|
366 |
|
367 self.singlesEntries.append([cb1, le1, cb1a]) |
|
368 self.singlesEntries.append([cb2, le2, cb2a]) |
|
369 |
|
370 def __addRangesLine(self): |
|
371 """ |
|
372 Private slot to add a line of entry widgets for character ranges. |
|
373 """ |
|
374 hbox = QWidget(self.rangesItemsBox) |
|
375 hboxLayout = QHBoxLayout(hbox) |
|
376 hboxLayout.setMargin(0) |
|
377 hboxLayout.setSpacing(6) |
|
378 hbox.setLayout(hboxLayout) |
|
379 cb1 = QComboBox(hbox) |
|
380 cb1.setEditable(False) |
|
381 self.__populateCharTypeCombo(cb1, False) |
|
382 hboxLayout.addWidget(cb1) |
|
383 l1 = QLabel(self.trUtf8("Between:"), hbox) |
|
384 hboxLayout.addWidget(l1) |
|
385 le1 = QLineEdit(hbox) |
|
386 le1.setValidator(self.charValidator) |
|
387 hboxLayout.addWidget(le1) |
|
388 l2 = QLabel(self.trUtf8("And:"), hbox) |
|
389 hboxLayout.addWidget(l2) |
|
390 le2 = QLineEdit(hbox) |
|
391 le2.setValidator(self.charValidator) |
|
392 hboxLayout.addWidget(le2) |
|
393 self.rangesItemsBoxLayout.addWidget(hbox) |
|
394 |
|
395 cb1.activated[int].connect(self.__rangesCharTypeSelected) |
|
396 hbox.show() |
|
397 |
|
398 self.rangesItemsBox.adjustSize() |
|
399 |
|
400 self.rangesEntries.append([cb1, le1, le2]) |
|
401 |
|
402 def __populateCharacterCombo(self, combo, format): |
|
403 """ |
|
404 Private method to populate a character selection combo. |
|
405 |
|
406 @param combo combo box to be populated (QComboBox) |
|
407 @param format format identifier (one of "-ccp", "-ccn", |
|
408 "-cbp", "-cbn", "-csp", "-csn", "-psp", "-psn") |
|
409 """ |
|
410 combo.clear() |
|
411 |
|
412 if format in ["-ccp", "-ccn"]: |
|
413 items = self.__characterCategories |
|
414 elif format in ["-csp", "-csn"]: |
|
415 items = self.__specialCharacterCategories |
|
416 elif format in ["-cbp", "-cbn"]: |
|
417 items = self.__characterBlocks |
|
418 elif format in ["-psp", "-psn"]: |
|
419 items = self.__posixNamedSets |
|
420 |
|
421 comboLen = 0 |
|
422 for txt, code in items: |
|
423 combo.addItem(txt, code) |
|
424 comboLen = max(comboLen, len(txt)) |
|
425 combo.setMinimumContentsLength(comboLen) |
|
426 |
|
427 def __performSelectedAction(self, format, lineedit, combo): |
|
428 """ |
|
429 Private method performing some actions depending on the input. |
|
430 |
|
431 @param format format of the selected entry (string) |
|
432 @param lineedit line edit widget to act on (QLineEdit) |
|
433 @param combo combo box widget to act on (QComboBox) |
|
434 """ |
|
435 if format == "-i": |
|
436 return |
|
437 |
|
438 if format in ["-c", "-h", "-o"]: |
|
439 lineedit.show() |
|
440 lineedit.setEnabled(True) |
|
441 if combo is not None: |
|
442 combo.hide() |
|
443 if format == "-c": |
|
444 lineedit.setValidator(self.charValidator) |
|
445 elif format == "-h": |
|
446 lineedit.setValidator(self.hexValidator) |
|
447 elif format == "-o": |
|
448 lineedit.setValidator(self.octValidator) |
|
449 elif format in ["-ccp", "-ccn", "-cbp", "-cbn", "-csp", "-csn", "-psp", "-psn"]: |
|
450 lineedit.setEnabled(False) |
|
451 lineedit.hide() |
|
452 if combo is not None: |
|
453 combo.show() |
|
454 self.__populateCharacterCombo(combo, format) |
|
455 else: |
|
456 lineedit.setEnabled(False) |
|
457 lineedit.hide() |
|
458 if combo is not None: |
|
459 combo.hide() |
|
460 lineedit.clear() |
|
461 |
|
462 def __singlesCharTypeSelected(self, index): |
|
463 """ |
|
464 Private slot to handle the activated(int) signal of the single chars combo boxes. |
|
465 |
|
466 @param index selected list index (integer) |
|
467 """ |
|
468 combo = self.sender() |
|
469 for entriesList in self.singlesEntries: |
|
470 if combo == entriesList[0]: |
|
471 format = combo.itemData(index) |
|
472 self.__performSelectedAction(format, entriesList[1], entriesList[2]) |
|
473 break |
|
474 |
|
475 def __rangesCharTypeSelected(self, index): |
|
476 """ |
|
477 Private slot to handle the activated(int) signal of the char ranges combo boxes. |
|
478 |
|
479 @param index selected list index (integer) |
|
480 """ |
|
481 combo = self.sender() |
|
482 for entriesList in self.rangesEntries: |
|
483 if combo == entriesList[0]: |
|
484 format = combo.itemData(index) |
|
485 self.__performSelectedAction(format, entriesList[1], None) |
|
486 self.__performSelectedAction(format, entriesList[2], None) |
|
487 break |
|
488 |
|
489 def __formatCharacter(self, char, format): |
|
490 """ |
|
491 Private method to format the characters entered into the dialog. |
|
492 |
|
493 @param char character string entered into the dialog (string) |
|
494 @param format string giving a special format (-c, -h, -i or -o) or |
|
495 the already formatted character (string) |
|
496 @return formatted character string (string) |
|
497 """ |
|
498 if format == "-c": |
|
499 return char |
|
500 elif format == "-i": |
|
501 return "" |
|
502 |
|
503 if format == "-h": |
|
504 while len(char) < 2: |
|
505 char = "0" + char |
|
506 if len(char) > 2: |
|
507 return "\\x{{{0}}}".format(char.lower()) |
|
508 else: |
|
509 return "\\x{0}".format(char.lower()) |
|
510 elif format == "-o": |
|
511 while len(char) < 3: |
|
512 char = "0" + char |
|
513 if len(char) > 3: |
|
514 char = char[:3] |
|
515 return "\\{0}".format(char) |
|
516 elif format in ["-ccp", "-cbp", "-csp"]: |
|
517 return "\\p{{{0}}}".format(char) |
|
518 elif format in ["-ccn", "-cbn", "-csn"]: |
|
519 return "\\P{{{0}}}".format(char) |
|
520 elif format == "-psp": |
|
521 return "[:{0}:]".format(char) |
|
522 elif format == "-psn": |
|
523 return "[:^{0}:]".format(char) |
|
524 else: |
|
525 return format |
|
526 |
|
527 def getCharacters(self): |
|
528 """ |
|
529 Public method to return the character string assembled via the dialog. |
|
530 |
|
531 @return formatted string for character classes (string) |
|
532 """ |
|
533 regexp = "" |
|
534 |
|
535 # negative character range |
|
536 if self.negativeCheckBox.isChecked(): |
|
537 regexp += "^" |
|
538 |
|
539 # predefined character ranges |
|
540 if self.wordCharCheckBox.isChecked(): |
|
541 regexp += "\\w" |
|
542 if self.nonWordCharCheckBox.isChecked(): |
|
543 regexp += "\\W" |
|
544 if self.digitsCheckBox.isChecked(): |
|
545 regexp += "\\d" |
|
546 if self.nonDigitsCheckBox.isChecked(): |
|
547 regexp += "\\D" |
|
548 if self.newlineCheckBox.isChecked(): |
|
549 regexp += "\\R" |
|
550 if self.nonNewlineCheckBox.isChecked(): |
|
551 regexp += "\\N" |
|
552 if self.whitespaceCheckBox.isChecked(): |
|
553 regexp += "\\s" |
|
554 if self.nonWhitespaceCheckBox.isChecked(): |
|
555 regexp += "\\S" |
|
556 if self.horizontalWhitespaceCheckBox.isChecked(): |
|
557 regexp += "\\h" |
|
558 if self.nonHorizontalWhitespaceCheckBox.isChecked(): |
|
559 regexp += "\\H" |
|
560 if self.verticalWhitespaceCheckBox.isChecked(): |
|
561 regexp += "\\v" |
|
562 if self.nonVerticalWhitespaceCheckBox.isChecked(): |
|
563 regexp += "\\V" |
|
564 |
|
565 # single characters |
|
566 for entrieslist in self.singlesEntries: |
|
567 format = entrieslist[0].itemData(entrieslist[0].currentIndex()) |
|
568 if format in ["-ccp", "-ccn", "-cbp", "-cbn", "-csp", "-csn", "-psp", "-psn"]: |
|
569 char = entrieslist[2].itemData(entrieslist[2].currentIndex()) |
|
570 else: |
|
571 char = entrieslist[1].text() |
|
572 regexp += self.__formatCharacter(char, format) |
|
573 |
|
574 # character ranges |
|
575 for entrieslist in self.rangesEntries: |
|
576 if not entrieslist[1].text() or \ |
|
577 not entrieslist[2].text(): |
|
578 continue |
|
579 format = entrieslist[0].itemData(entrieslist[0].currentIndex()) |
|
580 char1 = entrieslist[1].text() |
|
581 char2 = entrieslist[2].text() |
|
582 regexp += "{0}-{1}".format( |
|
583 self.__formatCharacter(char1, format), |
|
584 self.__formatCharacter(char2, format)) |
|
585 |
|
586 if regexp: |
|
587 if (regexp.startswith("\\") and \ |
|
588 regexp.count("\\") == 1 and \ |
|
589 "-" not in regexp) or \ |
|
590 len(regexp) == 1: |
|
591 return regexp |
|
592 else: |
|
593 return "[{0}]".format(regexp) |
|
594 else: |
|
595 return "" |