eric7/Plugins/WizardPlugins/QRegularExpressionWizard/QRegularExpressionWizardCharactersDialog.py

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

eric ide

mercurial