summaryrefslogtreecommitdiffstats
path: root/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/tools/regularexpression/regularexpressiondialog.cpp')
-rw-r--r--examples/widgets/tools/regularexpression/regularexpressiondialog.cpp169
1 files changed, 92 insertions, 77 deletions
diff --git a/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp b/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp
index 51025b7100..6ba781731f 100644
--- a/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp
+++ b/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp
@@ -1,54 +1,7 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
-** Copyright (C) 2016 Samuel Gaist <samuel.gaist@edeltech.ch>
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the examples of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
+// Copyright (C) 2016 Samuel Gaist <samuel.gaist@edeltech.ch>
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "regularexpressiondialog.h"
@@ -66,12 +19,13 @@
#include <QAction>
#include <QClipboard>
#include <QContextMenuEvent>
+#include <QFont>
+#include <QFontDatabase>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QFormLayout>
-#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QRegularExpressionMatchIterator>
@@ -106,6 +60,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
@@ -239,6 +207,7 @@ void RegularExpressionDialog::setResultUiEnabled(bool enabled)
{
matchDetailsTreeWidget->setEnabled(enabled);
namedGroupsTreeWidget->setEnabled(enabled);
+ replacementTextEdit->setEnabled(enabled);
}
static void setTextColor(QWidget *widget, const QColor &color)
@@ -264,6 +233,7 @@ void RegularExpressionDialog::refresh()
matchDetailsTreeWidget->clear();
namedGroupsTreeWidget->clear();
regexpStatusLabel->setText(QString());
+ replacementTextEdit->clear();
if (pattern.isEmpty()) {
setResultUiEnabled(false);
@@ -271,12 +241,12 @@ void RegularExpressionDialog::refresh()
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;
@@ -308,11 +278,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()) {
@@ -334,7 +306,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);
@@ -343,28 +315,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;
@@ -387,6 +375,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)"));
@@ -431,7 +448,7 @@ QWidget *RegularExpressionDialog::setupLeftUi()
return container;
}
-QWidget *RegularExpressionDialog::setupRightUi()
+QWidget *RegularExpressionDialog::setupInfoUi()
{
QWidget *container = new QWidget;
@@ -447,9 +464,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);