regularexpression example: Add raw string literal display
Factor out the line edit displaying the code to a class and add another instance for displaying raw string literals. Task-number: QTBUG-60635 Change-Id: I4614e4a56e355bad5158523c58edf784868dbf4d Reviewed-by: Venugopal Shivashankar <Venugopal.Shivashankar@qt.io> Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
parent
63c7a8e725
commit
f6f40014c4
@ -77,6 +77,13 @@
|
|||||||
|
|
||||||
Q_DECLARE_METATYPE(QRegularExpression::MatchType)
|
Q_DECLARE_METATYPE(QRegularExpression::MatchType)
|
||||||
|
|
||||||
|
static QString rawStringLiteral(QString pattern)
|
||||||
|
{
|
||||||
|
pattern.prepend(QLatin1String("R\"RX("));
|
||||||
|
pattern.append(QLatin1String(")RX\""));
|
||||||
|
return pattern;
|
||||||
|
}
|
||||||
|
|
||||||
static QString patternToCode(QString pattern)
|
static QString patternToCode(QString pattern)
|
||||||
{
|
{
|
||||||
pattern.replace(QLatin1String("\\"), QLatin1String("\\\\"));
|
pattern.replace(QLatin1String("\\"), QLatin1String("\\\\"));
|
||||||
@ -173,6 +180,29 @@ void PatternLineEdit::contextMenuEvent(QContextMenuEvent *event)
|
|||||||
menu->popup(event->globalPos());
|
menu->popup(event->globalPos());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DisplayLineEdit : public QLineEdit
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit DisplayLineEdit(QWidget *parent = nullptr);
|
||||||
|
};
|
||||||
|
|
||||||
|
DisplayLineEdit::DisplayLineEdit(QWidget *parent) : QLineEdit(parent)
|
||||||
|
{
|
||||||
|
setReadOnly(true);
|
||||||
|
QPalette disabledPalette = palette();
|
||||||
|
disabledPalette.setBrush(QPalette::Base, disabledPalette.brush(QPalette::Disabled, QPalette::Base));
|
||||||
|
setPalette(disabledPalette);
|
||||||
|
|
||||||
|
#if QT_CONFIG(clipboard)
|
||||||
|
QAction *copyAction = new QAction(this);
|
||||||
|
copyAction->setText(RegularExpressionDialog::tr("Copy to clipboard"));
|
||||||
|
copyAction->setIcon(QIcon(QStringLiteral(":/images/copy.png")));
|
||||||
|
connect(copyAction, &QAction::triggered, this,
|
||||||
|
[this] () { QGuiApplication::clipboard()->setText(text()); });
|
||||||
|
addAction(copyAction, QLineEdit::TrailingPosition);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
RegularExpressionDialog::RegularExpressionDialog(QWidget *parent)
|
RegularExpressionDialog::RegularExpressionDialog(QWidget *parent)
|
||||||
: QDialog(parent)
|
: QDialog(parent)
|
||||||
{
|
{
|
||||||
@ -230,6 +260,7 @@ void RegularExpressionDialog::refresh()
|
|||||||
offsetSpinBox->setMaximum(qMax(0, text.length() - 1));
|
offsetSpinBox->setMaximum(qMax(0, text.length() - 1));
|
||||||
|
|
||||||
escapedPatternLineEdit->setText(patternToCode(pattern));
|
escapedPatternLineEdit->setText(patternToCode(pattern));
|
||||||
|
rawStringLiteralLineEdit->setText(rawStringLiteral(pattern));
|
||||||
|
|
||||||
setTextColor(patternLineEdit, subjectTextEdit->palette().color(QPalette::Text));
|
setTextColor(patternLineEdit, subjectTextEdit->palette().color(QPalette::Text));
|
||||||
matchDetailsTreeWidget->clear();
|
matchDetailsTreeWidget->clear();
|
||||||
@ -322,15 +353,6 @@ void RegularExpressionDialog::refresh()
|
|||||||
setUpdatesEnabled(true);
|
setUpdatesEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RegularExpressionDialog::copyEscapedPatternToClipboard()
|
|
||||||
{
|
|
||||||
#if QT_CONFIG(clipboard)
|
|
||||||
QClipboard *clipboard = QGuiApplication::clipboard();
|
|
||||||
if (clipboard)
|
|
||||||
clipboard->setText(escapedPatternLineEdit->text());
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void RegularExpressionDialog::setupUi()
|
void RegularExpressionDialog::setupUi()
|
||||||
{
|
{
|
||||||
QWidget *leftHalfContainer = setupLeftUi();
|
QWidget *leftHalfContainer = setupLeftUi();
|
||||||
@ -363,20 +385,9 @@ QWidget *RegularExpressionDialog::setupLeftUi()
|
|||||||
patternLineEdit->setClearButtonEnabled(true);
|
patternLineEdit->setClearButtonEnabled(true);
|
||||||
layout->addRow(tr("&Pattern:"), patternLineEdit);
|
layout->addRow(tr("&Pattern:"), patternLineEdit);
|
||||||
|
|
||||||
escapedPatternLineEdit = new QLineEdit;
|
rawStringLiteralLineEdit = new DisplayLineEdit;
|
||||||
escapedPatternLineEdit->setReadOnly(true);
|
layout->addRow(tr("&Raw string literal:"), rawStringLiteralLineEdit);
|
||||||
QPalette palette = escapedPatternLineEdit->palette();
|
escapedPatternLineEdit = new DisplayLineEdit;
|
||||||
palette.setBrush(QPalette::Base, palette.brush(QPalette::Disabled, QPalette::Base));
|
|
||||||
escapedPatternLineEdit->setPalette(palette);
|
|
||||||
|
|
||||||
#if QT_CONFIG(clipboard)
|
|
||||||
QAction *copyEscapedPatternAction = new QAction(this);
|
|
||||||
copyEscapedPatternAction->setText(tr("Copy to clipboard"));
|
|
||||||
copyEscapedPatternAction->setIcon(QIcon(QStringLiteral(":/images/copy.png")));
|
|
||||||
connect(copyEscapedPatternAction, &QAction::triggered, this, &RegularExpressionDialog::copyEscapedPatternToClipboard);
|
|
||||||
escapedPatternLineEdit->addAction(copyEscapedPatternAction, QLineEdit::TrailingPosition);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
layout->addRow(tr("&Escaped pattern:"), escapedPatternLineEdit);
|
layout->addRow(tr("&Escaped pattern:"), escapedPatternLineEdit);
|
||||||
|
|
||||||
subjectTextEdit = new QPlainTextEdit;
|
subjectTextEdit = new QPlainTextEdit;
|
||||||
|
@ -74,13 +74,13 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void refresh();
|
void refresh();
|
||||||
void copyEscapedPatternToClipboard();
|
|
||||||
void setupUi();
|
void setupUi();
|
||||||
QWidget *setupLeftUi();
|
QWidget *setupLeftUi();
|
||||||
QWidget *setupRightUi();
|
QWidget *setupRightUi();
|
||||||
void setResultUiEnabled(bool enabled);
|
void setResultUiEnabled(bool enabled);
|
||||||
|
|
||||||
QLineEdit *patternLineEdit;
|
QLineEdit *patternLineEdit;
|
||||||
|
QLineEdit *rawStringLiteralLineEdit;
|
||||||
QLineEdit *escapedPatternLineEdit;
|
QLineEdit *escapedPatternLineEdit;
|
||||||
|
|
||||||
QPlainTextEdit *subjectTextEdit;
|
QPlainTextEdit *subjectTextEdit;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user