Skip to content

Commit

Permalink
Regular Expression example: Brush up and add a way to preview replace…
Browse files Browse the repository at this point in the history
…ments

- Set a fixed font on text edits so that parentheses stand out
- Rearrange the layout to have the text at the top be prevent
  long texts from being wrapped
- Add a replacement field where one can exercise replacement
  with the \1, \2... placeholders.

Pick-to: 6.1
Change-Id: I140a62e1fb2cd1c6bfe02a2f01b7f06a6f3b5eb2
Reviewed-by: Paul Wicking <[email protected]>
  • Loading branch information
FriedemannKleint committed Jun 1, 2021
1 parent c59fbf5 commit 6ac77e8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 27 deletions.
Binary file modified examples/widgets/doc/images/regularexpression-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 88 additions & 26 deletions examples/widgets/tools/regularexpression/regularexpressiondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@
#include <QAction>
#include <QClipboard>
#include <QContextMenuEvent>
#include <QFont>
#include <QFontDatabase>

#include <QHBoxLayout>
#include <QGridLayout>
#include <QFormLayout>

#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QRegularExpressionMatchIterator>

Expand Down Expand Up @@ -106,6 +107,20 @@ static QString codeToPattern(QString code)
return code;
}

static QFrame *createHorizontalSeparator()
{
auto *result = new QFrame;
result->setFrameStyle(QFrame::HLine | QFrame::Sunken);
return result;
}

static QFrame *createVerticalSeparator()
{
auto *result = new QFrame;
result->setFrameStyle(QFrame::VLine | QFrame::Sunken);
return result;
}

class PatternLineEdit : public QLineEdit
{
Q_OBJECT
Expand Down Expand Up @@ -239,6 +254,7 @@ void RegularExpressionDialog::setResultUiEnabled(bool enabled)
{
matchDetailsTreeWidget->setEnabled(enabled);
namedGroupsTreeWidget->setEnabled(enabled);
replacementTextEdit->setEnabled(enabled);
}

static void setTextColor(QWidget *widget, const QColor &color)
Expand All @@ -264,19 +280,20 @@ void RegularExpressionDialog::refresh()
matchDetailsTreeWidget->clear();
namedGroupsTreeWidget->clear();
regexpStatusLabel->setText(QString());
replacementTextEdit->clear();

if (pattern.isEmpty()) {
setResultUiEnabled(false);
setUpdatesEnabled(true);
return;
}

QRegularExpression rx(pattern);
if (!rx.isValid()) {
regularExpression.setPattern(pattern);
if (!regularExpression.isValid()) {
setTextColor(patternLineEdit, Qt::red);
regexpStatusLabel->setText(tr("Invalid: syntax error at position %1 (%2)")
.arg(rx.patternErrorOffset())
.arg(rx.errorString()));
.arg(regularExpression.patternErrorOffset())
.arg(regularExpression.errorString()));
setResultUiEnabled(false);
setUpdatesEnabled(true);
return;
Expand Down Expand Up @@ -308,11 +325,13 @@ void RegularExpressionDialog::refresh()
if (useUnicodePropertiesOptionCheckBox->isChecked())
patternOptions |= QRegularExpression::UseUnicodePropertiesOption;

rx.setPatternOptions(patternOptions);
regularExpression.setPatternOptions(patternOptions);

const int capturingGroupsCount = rx.captureCount() + 1;
const int capturingGroupsCount = regularExpression.captureCount() + 1;

QRegularExpressionMatchIterator iterator = rx.globalMatch(text, offsetSpinBox->value(), matchType, matchOptions);
const int offset = offsetSpinBox->value();
QRegularExpressionMatchIterator iterator =
regularExpression.globalMatch(text, offset, matchType, matchOptions);
int i = 0;

while (iterator.hasNext()) {
Expand All @@ -334,7 +353,7 @@ void RegularExpressionDialog::refresh()

regexpStatusLabel->setText(tr("Valid"));

const QStringList namedCaptureGroups = rx.namedCaptureGroups();
const QStringList namedCaptureGroups = regularExpression.namedCaptureGroups();
for (int i = 0; i < namedCaptureGroups.size(); ++i) {
const QString currentNamedCaptureGroup = namedCaptureGroups.at(i);

Expand All @@ -343,28 +362,44 @@ void RegularExpressionDialog::refresh()
namedGroupItem->setText(1, currentNamedCaptureGroup.isNull() ? tr("<no name>") : currentNamedCaptureGroup);
}

updateReplacement();

setUpdatesEnabled(true);
}

void RegularExpressionDialog::setupUi()
void RegularExpressionDialog::updateReplacement()
{
QWidget *leftHalfContainer = setupLeftUi();

QFrame *verticalSeparator = new QFrame;
verticalSeparator->setFrameStyle(QFrame::VLine | QFrame::Sunken);

QWidget *rightHalfContainer = setupRightUi();
replacementTextEdit->clear();
const QString &replacement = replacementLineEdit->text();
if (!regularExpression.isValid() || replacement.isEmpty())
return;

QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(leftHalfContainer);
mainLayout->addWidget(verticalSeparator);
mainLayout->addWidget(rightHalfContainer);
QString replaced = subjectTextEdit->toPlainText();
replaced.replace(regularExpression, replacement);
replacementTextEdit->setPlainText(replaced);
}

setLayout(mainLayout);
void RegularExpressionDialog::setupUi()
{
auto *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(setupTextUi());
mainLayout->addWidget(createHorizontalSeparator());
auto *horizontalLayout = new QHBoxLayout();
mainLayout->addLayout(horizontalLayout);
horizontalLayout->addWidget(setupOptionsUi());
horizontalLayout->addWidget(createVerticalSeparator());
horizontalLayout->addWidget(setupInfoUi());

auto font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
patternLineEdit->setFont(font);
rawStringLiteralLineEdit->setFont(font);
escapedPatternLineEdit->setFont(font);
replacementLineEdit->setFont(font);
subjectTextEdit->setFont(font);
replacementTextEdit->setFont(font);
}

QWidget *RegularExpressionDialog::setupLeftUi()
QWidget *RegularExpressionDialog::setupTextUi()
{
QWidget *container = new QWidget;

Expand All @@ -387,6 +422,35 @@ QWidget *RegularExpressionDialog::setupLeftUi()
subjectTextEdit = new QPlainTextEdit;
layout->addRow(tr("&Subject text:"), subjectTextEdit);

layout->addRow(createHorizontalSeparator());

QLabel *replaceLabel = new QLabel(tr("<h3>Replacement"));
layout->addRow(replaceLabel);

replacementLineEdit = new QLineEdit;
replacementLineEdit->setClearButtonEnabled(true);
connect(replacementLineEdit, &QLineEdit::textChanged, this,
&RegularExpressionDialog::updateReplacement);
layout->addRow(tr("&Replace by:"), replacementLineEdit);
replacementLineEdit->setToolTip(tr("Use \\1, \\2... as placeholders for the captured groups."));

replacementTextEdit = new QPlainTextEdit;
replacementTextEdit->setReadOnly(true);
layout->addRow(tr("Result:"), replacementTextEdit);

return container;
}

QWidget *RegularExpressionDialog::setupOptionsUi()
{
QWidget *container = new QWidget;

QFormLayout *layout = new QFormLayout(container);
layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
layout->setContentsMargins(QMargins());

layout->addRow(new QLabel(tr("<h3>Options</h3>")));

caseInsensitiveOptionCheckBox = new QCheckBox(tr("Case insensitive (/i)"));
dotMatchesEverythingOptionCheckBox = new QCheckBox(tr("Dot matches everything (/s)"));
multilineOptionCheckBox = new QCheckBox(tr("Multiline (/m)"));
Expand Down Expand Up @@ -431,7 +495,7 @@ QWidget *RegularExpressionDialog::setupLeftUi()
return container;
}

QWidget *RegularExpressionDialog::setupRightUi()
QWidget *RegularExpressionDialog::setupInfoUi()
{
QWidget *container = new QWidget;

Expand All @@ -447,9 +511,7 @@ QWidget *RegularExpressionDialog::setupRightUi()
matchDetailsTreeWidget->setSizeAdjustPolicy(QTreeWidget::AdjustToContents);
layout->addRow(tr("Match details:"), matchDetailsTreeWidget);

QFrame *horizontalSeparator = new QFrame;
horizontalSeparator->setFrameStyle(QFrame::HLine | QFrame::Sunken);
layout->addRow(horizontalSeparator);
layout->addRow(createHorizontalSeparator());

QLabel *regexpInfoLabel = new QLabel(tr("<h3>Regular expression information</h3>"));
layout->addRow(regexpInfoLabel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#define REGULAREXPRESSIONDIALOG_H

#include <QDialog>
#include <QRegularExpression>

QT_BEGIN_NAMESPACE
class QCheckBox;
Expand All @@ -72,18 +73,25 @@ class RegularExpressionDialog : public QDialog
public:
RegularExpressionDialog(QWidget *parent = nullptr);

private slots:
void updateReplacement();

private:
void refresh();
void setupUi();
QWidget *setupLeftUi();
QWidget *setupRightUi();
QWidget *setupTextUi();
QWidget *setupOptionsUi();
QWidget *setupInfoUi();
void setResultUiEnabled(bool enabled);

QLineEdit *patternLineEdit;
QLineEdit *rawStringLiteralLineEdit;
QLineEdit *escapedPatternLineEdit;
QLineEdit *replacementLineEdit;

QPlainTextEdit *subjectTextEdit;
QPlainTextEdit *replacementTextEdit;

QCheckBox *caseInsensitiveOptionCheckBox;
QCheckBox *dotMatchesEverythingOptionCheckBox;
Expand All @@ -104,6 +112,8 @@ class RegularExpressionDialog : public QDialog

QLabel *regexpStatusLabel;
QTreeWidget *namedGroupsTreeWidget;

QRegularExpression regularExpression;
};

#endif

0 comments on commit 6ac77e8

Please sign in to comment.