eric6/Plugins/WizardPlugins/QRegularExpressionWizard/QRegularExpressionWizardCharactersDialog.py

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

eric ide

mercurial