From 71e841fbe51ecb07747b3996d3db5e913f9d4a82 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 8 Oct 2022 11:07:42 +0200 Subject: [PATCH] Tools: extend RegExp utility --- src/Tools/RegExp/regexpdialog.cpp | 79 +++++++++------- src/Tools/RegExp/regexpdialog.h | 3 +- src/Tools/RegExp/regexpdialog.ui | 145 +++++++++++++++++++----------- 3 files changed, 142 insertions(+), 85 deletions(-) diff --git a/src/Tools/RegExp/regexpdialog.cpp b/src/Tools/RegExp/regexpdialog.cpp index 39d984ea4c..3ccbb5c22f 100644 --- a/src/Tools/RegExp/regexpdialog.cpp +++ b/src/Tools/RegExp/regexpdialog.cpp @@ -28,6 +28,7 @@ #include #include #include +#include RegExpDialog::RegExpDialog(QWidget* parent) : QDialog(parent), ui(new Ui_RegExpDialog()) @@ -35,8 +36,25 @@ RegExpDialog::RegExpDialog(QWidget* parent) ui->setupUi(this); rxhilighter = new RegExpSyntaxHighlighter(ui->textEdit1); - connect(ui->lineEdit1, SIGNAL(textChanged(const QString &)), - this, SLOT(performRegExp())); + validator = new QRegularExpressionValidator(this); + ui->lineEdit->setValidator(validator); + + connect(ui->lineEditRegExp, &QLineEdit::textChanged, + this, &RegExpDialog::performRegExp); + connect(ui->caseInsensitiveOption, &QCheckBox::toggled, + this, &RegExpDialog::performRegExp); + connect(ui->invertedGreedinessOption, &QCheckBox::toggled, + this, &RegExpDialog::performRegExp); + connect(ui->dotMatchesEverythingOption, &QCheckBox::toggled, + this, &RegExpDialog::performRegExp); + connect(ui->multilineOption, &QCheckBox::toggled, + this, &RegExpDialog::performRegExp); + connect(ui->extendedPatternSyntaxOption, &QCheckBox::toggled, + this, &RegExpDialog::performRegExp); + connect(ui->dontCaptureOption, &QCheckBox::toggled, + this, &RegExpDialog::performRegExp); + connect(ui->useUnicodePropertiesOption, &QCheckBox::toggled, + this, &RegExpDialog::performRegExp); } RegExpDialog::~RegExpDialog() @@ -46,20 +64,41 @@ RegExpDialog::~RegExpDialog() void RegExpDialog::performRegExp() { - QString txt = ui->lineEdit1->text(); + QString txt = ui->lineEditRegExp->text(); if (txt.isEmpty()) { rxhilighter->resethighlight(); return; } QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption; - if (ui->checkBox1->isChecked()) { + if (ui->caseInsensitiveOption->isChecked()) { options |= QRegularExpression::CaseInsensitiveOption; } - if (ui->checkBox3->isChecked()) { + + if (ui->invertedGreedinessOption->isChecked()) { options |= QRegularExpression::InvertedGreedinessOption; } + if (ui->dotMatchesEverythingOption->isChecked()) { + options |= QRegularExpression::DotMatchesEverythingOption; + } + + if (ui->multilineOption->isChecked()) { + options |= QRegularExpression::MultilineOption; + } + + if (ui->extendedPatternSyntaxOption->isChecked()) { + options |= QRegularExpression::ExtendedPatternSyntaxOption; + } + + if (ui->dontCaptureOption->isChecked()) { + options |= QRegularExpression::DontCaptureOption; + } + + if (ui->useUnicodePropertiesOption->isChecked()) { + options |= QRegularExpression::UseUnicodePropertiesOption; + } + QRegularExpression rx(txt, options); // evaluate regular expression @@ -70,6 +109,7 @@ void RegExpDialog::performRegExp() } rxhilighter->highlightMatchedText(rx); + validator->setRegularExpression(rx); } void RegExpDialog::about() @@ -117,36 +157,7 @@ void RegExpSyntaxHighlighter::highlightBlock (const QString & text) last = pos; } } -#if 0 -int RegExpSyntaxHighlighter::highlightParagraph ( const QString & text, int /*endStateOfLastPara*/ ) -{ - // reset format - QFont font = textEdit()->font(); - font.setBold( false ); - setFormat(0, text.length(), font, Qt::black ); - if (regexp.isEmpty()) - return 0; // empty regular expression - - int pos = 0; - int last = -1; - font.setBold(true); - - while ( (pos = regexp.indexIn(text, pos)) != -1 ) { - if (last == pos) - break; - QString sub = text.mid( pos, regexp.matchedLength()); - if (!sub.isEmpty()) { - setFormat(pos, sub.length(), font, Qt::blue ); - } - - pos += regexp.matchedLength(); - last=pos; - } - - return 0; -} -#endif void RegExpSyntaxHighlighter::highlightMatchedText(const QRegularExpression& rx) { regexp = rx; diff --git a/src/Tools/RegExp/regexpdialog.h b/src/Tools/RegExp/regexpdialog.h index 7a6650a0bf..6ba5fe8e57 100644 --- a/src/Tools/RegExp/regexpdialog.h +++ b/src/Tools/RegExp/regexpdialog.h @@ -29,6 +29,7 @@ #include #include +class QRegularExpressionValidator; class RegExpSyntaxHighlighter; class Ui_RegExpDialog; class RegExpDialog : public QDialog @@ -45,6 +46,7 @@ protected Q_SLOTS: private: RegExpSyntaxHighlighter* rxhilighter; + QRegularExpressionValidator* validator; Ui_RegExpDialog* ui; }; @@ -57,7 +59,6 @@ public: ~RegExpSyntaxHighlighter(); void highlightBlock (const QString & text); - //int highlightParagraph ( const QString & text, int endStateOfLastPara ); void highlightMatchedText( const QRegularExpression& ); void resethighlight(); diff --git a/src/Tools/RegExp/regexpdialog.ui b/src/Tools/RegExp/regexpdialog.ui index d2832af82b..6b9a12248f 100644 --- a/src/Tools/RegExp/regexpdialog.ui +++ b/src/Tools/RegExp/regexpdialog.ui @@ -6,8 +6,8 @@ 0 0 - 519 - 285 + 693 + 563 @@ -16,63 +16,104 @@ true - - - 10 - - - 6 - - - - - Text: - - - + - - - 6 - - - 0 - + - + - Case insensitive + Validator - - - Minimal - - + - + Regular Expression: - - + + + + Pattern options + + + + + + Case insensitive + + + + + + + Multiline + + + + + + + Inverted greediness + + + + + + + Dot matches everything + + + + + + + Extended pattern syntax + + + + + + + Don't capture + + + + + + + Unicode properties + + + + + - + 6 - + + 0 + + + 0 + + + 0 + + 0 @@ -121,29 +162,33 @@ - - - - &Help - - - true - - - + + + + + + + Text: + + + textEdit1 - checkBox1 - checkBox2 - checkBox3 - lineEdit1 + lineEdit + caseInsensitiveOption + invertedGreedinessOption + dotMatchesEverythingOption + multilineOption + extendedPatternSyntaxOption + dontCaptureOption + useUnicodePropertiesOption + lineEditRegExp buttonClose - buttonHelp