summaryrefslogtreecommitdiffstats
path: root/examples/widgets/tools
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2017-05-15 12:44:30 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2017-07-19 20:58:19 +0000
commitf54327c3c3956c045f4bfe4023bf4ce92fceee3b (patch)
treeac6f2b3d440c3d73fef1dc05cd922f47ba6f676f /examples/widgets/tools
parent29f6b507325862fe3dd728913b5911f8edde9d35 (diff)
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 <giuseppe.dangelo@kdab.com>
Diffstat (limited to 'examples/widgets/tools')
-rw-r--r--examples/widgets/tools/regularexpression/regularexpressiondialog.cpp101
1 files 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 <QComboBox>
#include <QLabel>
#include <QLineEdit>
+#include <QMenu>
#include <QSpinBox>
#include <QPlainTextEdit>
#include <QTreeWidget>
#include <QAction>
#include <QClipboard>
+#include <QContextMenuEvent>
#include <QHBoxLayout>
#include <QGridLayout>
@@ -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("<h3>Regular expression and text input</h3>"));
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"