--- a/src/eric7/Plugins/WizardPlugins/QRegularExpressionWizard/QRegularExpressionWizardCharactersDialog.py Wed Jul 13 11:16:20 2022 +0200 +++ b/src/eric7/Plugins/WizardPlugins/QRegularExpressionWizard/QRegularExpressionWizardCharactersDialog.py Wed Jul 13 14:55:47 2022 +0200 @@ -10,64 +10,82 @@ from PyQt6.QtCore import QRegularExpression from PyQt6.QtGui import QRegularExpressionValidator from PyQt6.QtWidgets import ( - QWidget, QDialog, QVBoxLayout, QHBoxLayout, QScrollArea, QPushButton, - QSpacerItem, QSizePolicy, QComboBox, QLineEdit, QLabel + QWidget, + QDialog, + QVBoxLayout, + QHBoxLayout, + QScrollArea, + QPushButton, + QSpacerItem, + QSizePolicy, + QComboBox, + QLineEdit, + QLabel, ) from .Ui_QRegularExpressionWizardCharactersDialog import ( - Ui_QRegularExpressionWizardCharactersDialog + Ui_QRegularExpressionWizardCharactersDialog, ) class QRegularExpressionWizardCharactersDialog( - QDialog, Ui_QRegularExpressionWizardCharactersDialog): + QDialog, Ui_QRegularExpressionWizardCharactersDialog +): """ Class implementing a dialog for entering character classes. """ + def __init__(self, parent=None): """ Constructor - + @param parent reference to the parent widget (QWidget) """ super().__init__(parent) self.setupUi(self) - + self.__initCharacterSelectors() - + self.comboItems = [] - self.singleComboItems = [] # these are in addition to the above + self.singleComboItems = [] # these are in addition to the above self.comboItems.append((self.tr("Normal character"), "-c")) - self.comboItems.append((self.tr( - "Unicode character in hexadecimal notation"), "-h")) - self.comboItems.append((self.tr( - "ASCII/Latin1 character in octal notation"), "-o")) - self.singleComboItems.extend([ - ("---", "-i"), - (self.tr("Bell character (\\a)"), "\\a"), - (self.tr("Escape character (\\e)"), "\\e"), - (self.tr("Page break (\\f)"), "\\f"), - (self.tr("Line feed (\\n)"), "\\n"), - (self.tr("Carriage return (\\r)"), "\\r"), - (self.tr("Horizontal tabulator (\\t)"), "\\t"), - ("---", "-i"), - (self.tr("Character Category"), "-ccp"), - (self.tr("Special Character Category"), "-csp"), - (self.tr("Character Block"), "-cbp"), - (self.tr("POSIX Named Set"), "-psp"), - (self.tr("Not Character Category"), "-ccn"), - (self.tr("Not Character Block"), "-cbn"), - (self.tr("Not Special Character Category"), "-csn"), - (self.tr("Not POSIX Named Set"), "-psn"), - ]) - + self.comboItems.append( + (self.tr("Unicode character in hexadecimal notation"), "-h") + ) + self.comboItems.append( + (self.tr("ASCII/Latin1 character in octal notation"), "-o") + ) + self.singleComboItems.extend( + [ + ("---", "-i"), + (self.tr("Bell character (\\a)"), "\\a"), + (self.tr("Escape character (\\e)"), "\\e"), + (self.tr("Page break (\\f)"), "\\f"), + (self.tr("Line feed (\\n)"), "\\n"), + (self.tr("Carriage return (\\r)"), "\\r"), + (self.tr("Horizontal tabulator (\\t)"), "\\t"), + ("---", "-i"), + (self.tr("Character Category"), "-ccp"), + (self.tr("Special Character Category"), "-csp"), + (self.tr("Character Block"), "-cbp"), + (self.tr("POSIX Named Set"), "-psp"), + (self.tr("Not Character Category"), "-ccn"), + (self.tr("Not Character Block"), "-cbn"), + (self.tr("Not Special Character Category"), "-csn"), + (self.tr("Not POSIX Named Set"), "-psn"), + ] + ) + self.charValidator = QRegularExpressionValidator( - QRegularExpression(".{0,1}"), self) + QRegularExpression(".{0,1}"), self + ) self.hexValidator = QRegularExpressionValidator( - QRegularExpression("[0-9a-fA-F]{0,4}"), self) + QRegularExpression("[0-9a-fA-F]{0,4}"), self + ) self.octValidator = QRegularExpressionValidator( - QRegularExpression("[0-3]?[0-7]{0,2}"), self) - + QRegularExpression("[0-3]?[0-7]{0,2}"), self + ) + # generate dialog part for single characters self.singlesBoxLayout = QVBoxLayout(self.singlesBox) self.singlesBoxLayout.setObjectName("singlesBoxLayout") @@ -77,7 +95,7 @@ self.singlesView = QScrollArea(self.singlesBox) self.singlesView.setObjectName("singlesView") self.singlesBoxLayout.addWidget(self.singlesView) - + self.singlesItemsBox = QWidget(self) self.singlesView.setWidget(self.singlesItemsBox) self.singlesItemsBox.setObjectName("singlesItemsBox") @@ -88,21 +106,23 @@ self.singlesItemsBox.setLayout(self.singlesItemsBoxLayout) self.singlesEntries = [] self.__addSinglesLine() - + hlayout0 = QHBoxLayout() hlayout0.setContentsMargins(0, 0, 0, 0) hlayout0.setSpacing(6) hlayout0.setObjectName("hlayout0") self.moreSinglesButton = QPushButton( - self.tr("Additional Entries"), self.singlesBox) + self.tr("Additional Entries"), self.singlesBox + ) self.moreSinglesButton.setObjectName("moreSinglesButton") hlayout0.addWidget(self.moreSinglesButton) hspacer0 = QSpacerItem( - 30, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) + 30, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum + ) hlayout0.addItem(hspacer0) self.singlesBoxLayout.addLayout(hlayout0) self.moreSinglesButton.clicked.connect(self.__addSinglesLine) - + # generate dialog part for character ranges self.rangesBoxLayout = QVBoxLayout(self.rangesBox) self.rangesBoxLayout.setObjectName("rangesBoxLayout") @@ -112,7 +132,7 @@ self.rangesView = QScrollArea(self.rangesBox) self.rangesView.setObjectName("rangesView") self.rangesBoxLayout.addWidget(self.rangesView) - + self.rangesItemsBox = QWidget(self) self.rangesView.setWidget(self.rangesItemsBox) self.rangesItemsBox.setObjectName("rangesItemsBox") @@ -123,21 +143,23 @@ self.rangesItemsBox.setLayout(self.rangesItemsBoxLayout) self.rangesEntries = [] self.__addRangesLine() - + hlayout1 = QHBoxLayout() hlayout1.setContentsMargins(0, 0, 0, 0) hlayout1.setSpacing(6) hlayout1.setObjectName("hlayout1") self.moreRangesButton = QPushButton( - self.tr("Additional Entries"), self.rangesBox) + self.tr("Additional Entries"), self.rangesBox + ) self.moreSinglesButton.setObjectName("moreRangesButton") hlayout1.addWidget(self.moreRangesButton) hspacer1 = QSpacerItem( - 30, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) + 30, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum + ) hlayout1.addItem(hspacer1) self.rangesBoxLayout.addLayout(hlayout1) self.moreRangesButton.clicked.connect(self.__addRangesLine) - + def __initCharacterSelectors(self): """ Private method to initialize the W3C character selector entries. @@ -183,7 +205,7 @@ (self.tr("Other, Private Use"), "Co"), (self.tr("Other, Surrogat"), "Cn"), ) - + self.__specialCharacterCategories = ( # display name code (self.tr("Alphanumeric"), "Xan"), @@ -192,7 +214,7 @@ (self.tr("Universal Character"), "Xuc"), (self.tr("Perl Word"), "Xan"), ) - + self.__characterBlocks = ( # display name code (self.tr("Arabic"), "Arabic"), @@ -298,7 +320,7 @@ (self.tr("Vai"), "Vai"), (self.tr("Yi"), "Yi"), ) - + self.__posixNamedSets = ( # display name code (self.tr("Alphanumeric"), "alnum"), @@ -316,11 +338,11 @@ (self.tr("Printing (excl. alphanumeric)"), "punct"), (self.tr("Control Character"), "cntrl"), ) - + def __populateCharTypeCombo(self, combo, isSingle): """ Private method to populate a given character type selection combo box. - + @param combo reference to the combo box to be populated (QComboBox) @param isSingle flag indicating a singles combo (boolean) """ @@ -364,18 +386,16 @@ hboxLayout.addWidget(cb2a) cb2a.hide() self.singlesItemsBoxLayout.addWidget(hbox) - - cb1.activated[int].connect( - lambda i: self.__singlesCharTypeSelected(i, cb1)) - cb2.activated[int].connect( - lambda i: self.__singlesCharTypeSelected(i, cb2)) + + cb1.activated[int].connect(lambda i: self.__singlesCharTypeSelected(i, cb1)) + cb2.activated[int].connect(lambda i: self.__singlesCharTypeSelected(i, cb2)) hbox.show() - + self.singlesItemsBox.adjustSize() - + self.singlesEntries.append([cb1, le1, cb1a]) self.singlesEntries.append([cb2, le2, cb2a]) - + def __addRangesLine(self): """ Private slot to add a line of entry widgets for character ranges. @@ -400,26 +420,25 @@ le2.setValidator(self.charValidator) hboxLayout.addWidget(le2) self.rangesItemsBoxLayout.addWidget(hbox) - - cb1.activated[int].connect( - lambda i: self.__rangesCharTypeSelected(i, cb1)) - + + cb1.activated[int].connect(lambda i: self.__rangesCharTypeSelected(i, cb1)) + hbox.show() - + self.rangesItemsBox.adjustSize() - + self.rangesEntries.append([cb1, le1, le2]) - + def __populateCharacterCombo(self, combo, formatIdentifier): """ Private method to populate a character selection combo. - + @param combo combo box to be populated (QComboBox) @param formatIdentifier format identifier (one of "-ccp", "-ccn", "-cbp", "-cbn", "-csp", "-csn", "-psp", "-psn") """ combo.clear() - + if formatIdentifier in ["-ccp", "-ccn"]: items = self.__characterCategories elif formatIdentifier in ["-csp", "-csn"]: @@ -428,24 +447,24 @@ items = self.__characterBlocks elif formatIdentifier in ["-psp", "-psn"]: items = self.__posixNamedSets - + comboLen = 0 for txt, code in items: combo.addItem(txt, code) comboLen = max(comboLen, len(txt)) combo.setMinimumContentsLength(comboLen) - + def __performSelectedAction(self, formatIdentifier, lineedit, combo): """ Private method performing some actions depending on the input. - + @param formatIdentifier format of the selected entry (string) @param lineedit line edit widget to act on (QLineEdit) @param combo combo box widget to act on (QComboBox) """ if formatIdentifier == "-i": return - + if formatIdentifier in ["-c", "-h", "-o"]: lineedit.show() lineedit.setEnabled(True) @@ -457,8 +476,16 @@ lineedit.setValidator(self.hexValidator) elif formatIdentifier == "-o": lineedit.setValidator(self.octValidator) - elif formatIdentifier in ["-ccp", "-ccn", "-cbp", "-cbn", "-csp", - "-csn", "-psp", "-psn"]: + elif formatIdentifier in [ + "-ccp", + "-ccn", + "-cbp", + "-cbn", + "-csp", + "-csn", + "-psp", + "-psn", + ]: lineedit.setEnabled(False) lineedit.hide() if combo is not None: @@ -470,12 +497,12 @@ if combo is not None: combo.hide() lineedit.clear() - + def __singlesCharTypeSelected(self, index, combo): """ Private slot to handle the activated(int) signal of the single chars combo boxes. - + @param index selected list index @type int @param combo reference to the combo box @@ -485,14 +512,15 @@ if combo == entriesList[0]: formatIdentifier = combo.itemData(index) self.__performSelectedAction( - formatIdentifier, entriesList[1], entriesList[2]) + formatIdentifier, entriesList[1], entriesList[2] + ) break - + def __rangesCharTypeSelected(self, index, combo): """ Private slot to handle the activated(int) signal of the char ranges combo boxes. - + @param index selected list index @type int @param combo reference to the combo box @@ -501,16 +529,14 @@ for entriesList in self.rangesEntries: if combo == entriesList[0]: formatIdentifier = combo.itemData(index) - self.__performSelectedAction(formatIdentifier, entriesList[1], - None) - self.__performSelectedAction(formatIdentifier, entriesList[2], - None) + self.__performSelectedAction(formatIdentifier, entriesList[1], None) + self.__performSelectedAction(formatIdentifier, entriesList[2], None) break - + def __formatCharacter(self, char, formatIdentifier): """ Private method to format the characters entered into the dialog. - + @param char character string entered into the dialog (string) @param formatIdentifier string giving a special format (-c, -h, -i or -o) or the already formatted character (string) @@ -520,7 +546,7 @@ return char elif formatIdentifier == "-i": return "" - + if formatIdentifier == "-h": while len(char) < 2: char = "0" + char @@ -544,19 +570,19 @@ return "[:^{0}:]".format(char) else: return formatIdentifier - + def getCharacters(self): """ Public method to return the character string assembled via the dialog. - + @return formatted string for character classes (string) """ regexp = "" - + # negative character range if self.negativeCheckBox.isChecked(): regexp += "^" - + # predefined character ranges if self.wordCharCheckBox.isChecked(): regexp += "\\w" @@ -582,41 +608,36 @@ regexp += "\\v" if self.nonVerticalWhitespaceCheckBox.isChecked(): regexp += "\\V" - + # single characters for entrieslist in self.singlesEntries: - formatIdentifier = entrieslist[0].itemData( - entrieslist[0].currentIndex()) + formatIdentifier = entrieslist[0].itemData(entrieslist[0].currentIndex()) char = ( entrieslist[2].itemData(entrieslist[2].currentIndex()) - if formatIdentifier in ["-ccp", "-ccn", "-cbp", "-cbn", "-csp", - "-csn", "-psp", "-psn"] else - entrieslist[1].text() + if formatIdentifier + in ["-ccp", "-ccn", "-cbp", "-cbn", "-csp", "-csn", "-psp", "-psn"] + else entrieslist[1].text() ) regexp += self.__formatCharacter(char, formatIdentifier) - + # character ranges for entrieslist in self.rangesEntries: - if ( - not entrieslist[1].text() or - not entrieslist[2].text() - ): + if not entrieslist[1].text() or not entrieslist[2].text(): continue - formatIdentifier = entrieslist[0].itemData( - entrieslist[0].currentIndex()) + formatIdentifier = entrieslist[0].itemData(entrieslist[0].currentIndex()) char1 = entrieslist[1].text() char2 = entrieslist[2].text() regexp += "{0}-{1}".format( self.__formatCharacter(char1, formatIdentifier), - self.__formatCharacter(char2, formatIdentifier)) - + self.__formatCharacter(char2, formatIdentifier), + ) + if regexp: if ( - (regexp.startswith("\\") and - regexp.count("\\") == 1 and - "-" not in regexp) or - len(regexp) == 1 - ): + regexp.startswith("\\") + and regexp.count("\\") == 1 + and "-" not in regexp + ) or len(regexp) == 1: return regexp else: return "[{0}]".format(regexp)