From f54327c3c3956c045f4bfe4023bf4ce92fceee3b Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 15 May 2017 12:44:30 +0200 Subject: Regular Expression Example: Add context menu Add context menu providing: "Escape Selection" Apply QRegularExpression::escape() to selection "Paste from Code" Paste from C++ "Copy to Code" (for completeness) Task-number: QTBUG-60635 Change-Id: Iee15c2243c7d0435b623dde4a1b26249aecd0553 Reviewed-by: Giuseppe D'Angelo --- .../regularexpression/regularexpressiondialog.cpp | 101 +++++++++++++++++++-- 1 file changed, 94 insertions(+), 7 deletions(-) diff --git a/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp b/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp index 371e2bda4b..7fdce3b674 100644 --- a/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp +++ b/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp @@ -58,12 +58,14 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include @@ -75,6 +77,94 @@ Q_DECLARE_METATYPE(QRegularExpression::MatchType) +static QString patternToCode(QString pattern) +{ + pattern.replace(QLatin1String("\\"), QLatin1String("\\\\")); + pattern.replace(QLatin1String("\""), QLatin1String("\\\"")); + pattern.prepend(QLatin1Char('"')); + pattern.append(QLatin1Char('"')); + return pattern; +} + +static QString codeToPattern(QString code) +{ + for (int i = 0; i < code.size(); ++i) { + if (code.at(i) == QLatin1Char('\\')) + code.remove(i, 1); + } + if (code.startsWith(QLatin1Char('"')) && code.endsWith(QLatin1Char('"'))) { + code.chop(1); + code.remove(0, 1); + } + return code; +} + +class PatternLineEdit : public QLineEdit +{ + Q_OBJECT +public: + explicit PatternLineEdit(QWidget *parent = nullptr); + +private slots: + void copyToCode(); + void pasteFromCode(); + void escapeSelection(); + +protected: + void contextMenuEvent(QContextMenuEvent *event) override; + +private: + QAction *escapeSelectionAction; + QAction *copyToCodeAction; + QAction *pasteFromCodeAction; +}; + +PatternLineEdit::PatternLineEdit(QWidget *parent) : + QLineEdit(parent), + escapeSelectionAction(new QAction(tr("Escape Selection"), this)), + copyToCodeAction(new QAction(tr("Copy to Code"), this)), + pasteFromCodeAction(new QAction(tr("Paste from Code"), this)) +{ + setClearButtonEnabled(true); + connect(escapeSelectionAction, &QAction::triggered, this, &PatternLineEdit::escapeSelection); + connect(copyToCodeAction, &QAction::triggered, this, &PatternLineEdit::copyToCode); + connect(pasteFromCodeAction, &QAction::triggered, this, &PatternLineEdit::pasteFromCode); +} + +void PatternLineEdit::escapeSelection() +{ + const QString selection = selectedText(); + const QString escapedSelection = QRegularExpression::escape(selection); + if (escapedSelection != selection) { + QString t = text(); + t.replace(selectionStart(), selection.size(), escapedSelection); + setText(t); + } +} + +void PatternLineEdit::copyToCode() +{ + QGuiApplication::clipboard()->setText(patternToCode(text())); +} + +void PatternLineEdit::pasteFromCode() +{ + setText(codeToPattern(QGuiApplication::clipboard()->text())); +} + +void PatternLineEdit::contextMenuEvent(QContextMenuEvent *event) +{ + QMenu *menu = createStandardContextMenu(); + menu->setAttribute(Qt::WA_DeleteOnClose); + menu->addSeparator(); + escapeSelectionAction->setEnabled(hasSelectedText()); + menu->addAction(escapeSelectionAction); + menu->addSeparator(); + menu->addAction(copyToCodeAction); + menu->addAction(pasteFromCodeAction); + menu->popup(event->globalPos()); +} + RegularExpressionDialog::RegularExpressionDialog(QWidget *parent) : QDialog(parent) { @@ -131,12 +221,7 @@ void RegularExpressionDialog::refresh() offsetSpinBox->setMaximum(qMax(0, text.length() - 1)); - QString escaped = pattern; - escaped.replace(QLatin1String("\\"), QLatin1String("\\\\")); - escaped.replace(QLatin1String("\""), QLatin1String("\\\"")); - escaped.prepend(QLatin1Char('"')); - escaped.append(QLatin1Char('"')); - escapedPatternLineEdit->setText(escaped); + escapedPatternLineEdit->setText(patternToCode(pattern)); setTextColor(patternLineEdit, subjectTextEdit->palette().color(QPalette::Text)); matchDetailsTreeWidget->clear(); @@ -266,7 +351,7 @@ QWidget *RegularExpressionDialog::setupLeftUi() QLabel *regexpAndSubjectLabel = new QLabel(tr("

Regular expression and text input

")); layout->addRow(regexpAndSubjectLabel); - patternLineEdit = new QLineEdit; + patternLineEdit = new PatternLineEdit; patternLineEdit->setClearButtonEnabled(true); layout->addRow(tr("&Pattern:"), patternLineEdit); @@ -373,3 +458,5 @@ QWidget *RegularExpressionDialog::setupRightUi() return container; } + +#include "regularexpressiondialog.moc" -- cgit v1.2.3