1
/***************************************************************************
2
* Copyright (c) 2005 Werner Mayer <werner.wm.mayer@gmx.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "regexpdialog.h"
25
#include "ui_regexpdialog.h"
30
#include <qmessagebox.h>
31
#include <qvalidator.h>
33
RegExpDialog::RegExpDialog(QWidget* parent)
35
, ui(new Ui_RegExpDialog())
38
rxhilighter = new RegExpSyntaxHighlighter(ui->textEdit1);
40
validator = new QRegularExpressionValidator(this);
41
ui->lineEdit->setValidator(validator);
43
connect(ui->lineEditRegExp, &QLineEdit::textChanged, this, &RegExpDialog::performRegExp);
44
connect(ui->caseInsensitiveOption, &QCheckBox::toggled, this, &RegExpDialog::performRegExp);
45
connect(ui->invertedGreedinessOption, &QCheckBox::toggled, this, &RegExpDialog::performRegExp);
46
connect(ui->dotMatchesEverythingOption,
49
&RegExpDialog::performRegExp);
50
connect(ui->multilineOption, &QCheckBox::toggled, this, &RegExpDialog::performRegExp);
51
connect(ui->extendedPatternSyntaxOption,
54
&RegExpDialog::performRegExp);
55
connect(ui->dontCaptureOption, &QCheckBox::toggled, this, &RegExpDialog::performRegExp);
56
connect(ui->useUnicodePropertiesOption,
59
&RegExpDialog::performRegExp);
62
RegExpDialog::~RegExpDialog()
67
void RegExpDialog::performRegExp()
69
QString txt = ui->lineEditRegExp->text();
71
rxhilighter->resethighlight();
75
QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption;
76
if (ui->caseInsensitiveOption->isChecked()) {
77
options |= QRegularExpression::CaseInsensitiveOption;
80
if (ui->invertedGreedinessOption->isChecked()) {
81
options |= QRegularExpression::InvertedGreedinessOption;
84
if (ui->dotMatchesEverythingOption->isChecked()) {
85
options |= QRegularExpression::DotMatchesEverythingOption;
88
if (ui->multilineOption->isChecked()) {
89
options |= QRegularExpression::MultilineOption;
92
if (ui->extendedPatternSyntaxOption->isChecked()) {
93
options |= QRegularExpression::ExtendedPatternSyntaxOption;
96
if (ui->dontCaptureOption->isChecked()) {
97
options |= QRegularExpression::DontCaptureOption;
100
if (ui->useUnicodePropertiesOption->isChecked()) {
101
options |= QRegularExpression::UseUnicodePropertiesOption;
104
QRegularExpression rx(txt, options);
106
// evaluate regular expression
107
ui->textLabel4->setText(rx.errorString());
109
rxhilighter->resethighlight();
110
return; // invalid expression
113
rxhilighter->highlightMatchedText(rx);
114
validator->setRegularExpression(rx);
117
void RegExpDialog::about()
119
QString msg = "This is a tool for playing around with regular expressions.";
120
QMessageBox::information(this, "RegExp Explorer", msg);
123
// -------------------------------------------------------------
125
RegExpSyntaxHighlighter::RegExpSyntaxHighlighter(QTextEdit* textEdit)
126
: QSyntaxHighlighter(textEdit)
129
RegExpSyntaxHighlighter::~RegExpSyntaxHighlighter()
132
void RegExpSyntaxHighlighter::highlightBlock(const QString& text)
134
QTextCharFormat regFormat;
135
regFormat.setForeground(Qt::black);
136
regFormat.setFontWeight(QFont::Normal);
137
setFormat(0, text.length(), regFormat);
139
if (regexp.pattern().isEmpty()) {
140
return; // empty regular expression
145
regFormat.setFontWeight(QFont::Bold);
146
regFormat.setForeground(Qt::blue);
148
QRegularExpressionMatch match;
149
while ((pos = text.indexOf(regexp, pos, &match)) != -1) {
153
QString sub = text.mid(pos, match.capturedLength());
154
if (!sub.isEmpty()) {
155
setFormat(pos, sub.length(), regFormat);
158
pos += match.capturedLength();
163
void RegExpSyntaxHighlighter::highlightMatchedText(const QRegularExpression& rx)
169
void RegExpSyntaxHighlighter::resethighlight()
171
regexp.setPattern("");