FreeCAD

Форк
0
/
regexpdialog.cpp 
173 строки · 5.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2005 Werner Mayer <werner.wm.mayer@gmx.de>              *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23

24
#include "regexpdialog.h"
25
#include "ui_regexpdialog.h"
26

27
#include <qcheckbox.h>
28
#include <qlabel.h>
29
#include <qlineedit.h>
30
#include <qmessagebox.h>
31
#include <qvalidator.h>
32

33
RegExpDialog::RegExpDialog(QWidget* parent)
34
    : QDialog(parent)
35
    , ui(new Ui_RegExpDialog())
36
{
37
    ui->setupUi(this);
38
    rxhilighter = new RegExpSyntaxHighlighter(ui->textEdit1);
39

40
    validator = new QRegularExpressionValidator(this);
41
    ui->lineEdit->setValidator(validator);
42

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,
47
            &QCheckBox::toggled,
48
            this,
49
            &RegExpDialog::performRegExp);
50
    connect(ui->multilineOption, &QCheckBox::toggled, this, &RegExpDialog::performRegExp);
51
    connect(ui->extendedPatternSyntaxOption,
52
            &QCheckBox::toggled,
53
            this,
54
            &RegExpDialog::performRegExp);
55
    connect(ui->dontCaptureOption, &QCheckBox::toggled, this, &RegExpDialog::performRegExp);
56
    connect(ui->useUnicodePropertiesOption,
57
            &QCheckBox::toggled,
58
            this,
59
            &RegExpDialog::performRegExp);
60
}
61

62
RegExpDialog::~RegExpDialog()
63
{
64
    delete ui;
65
}
66

67
void RegExpDialog::performRegExp()
68
{
69
    QString txt = ui->lineEditRegExp->text();
70
    if (txt.isEmpty()) {
71
        rxhilighter->resethighlight();
72
        return;
73
    }
74

75
    QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption;
76
    if (ui->caseInsensitiveOption->isChecked()) {
77
        options |= QRegularExpression::CaseInsensitiveOption;
78
    }
79

80
    if (ui->invertedGreedinessOption->isChecked()) {
81
        options |= QRegularExpression::InvertedGreedinessOption;
82
    }
83

84
    if (ui->dotMatchesEverythingOption->isChecked()) {
85
        options |= QRegularExpression::DotMatchesEverythingOption;
86
    }
87

88
    if (ui->multilineOption->isChecked()) {
89
        options |= QRegularExpression::MultilineOption;
90
    }
91

92
    if (ui->extendedPatternSyntaxOption->isChecked()) {
93
        options |= QRegularExpression::ExtendedPatternSyntaxOption;
94
    }
95

96
    if (ui->dontCaptureOption->isChecked()) {
97
        options |= QRegularExpression::DontCaptureOption;
98
    }
99

100
    if (ui->useUnicodePropertiesOption->isChecked()) {
101
        options |= QRegularExpression::UseUnicodePropertiesOption;
102
    }
103

104
    QRegularExpression rx(txt, options);
105

106
    // evaluate regular expression
107
    ui->textLabel4->setText(rx.errorString());
108
    if (!rx.isValid()) {
109
        rxhilighter->resethighlight();
110
        return;  // invalid expression
111
    }
112

113
    rxhilighter->highlightMatchedText(rx);
114
    validator->setRegularExpression(rx);
115
}
116

117
void RegExpDialog::about()
118
{
119
    QString msg = "This is a tool for playing around with regular expressions.";
120
    QMessageBox::information(this, "RegExp Explorer", msg);
121
}
122

123
// -------------------------------------------------------------
124

125
RegExpSyntaxHighlighter::RegExpSyntaxHighlighter(QTextEdit* textEdit)
126
    : QSyntaxHighlighter(textEdit)
127
{}
128

129
RegExpSyntaxHighlighter::~RegExpSyntaxHighlighter()
130
{}
131

132
void RegExpSyntaxHighlighter::highlightBlock(const QString& text)
133
{
134
    QTextCharFormat regFormat;
135
    regFormat.setForeground(Qt::black);
136
    regFormat.setFontWeight(QFont::Normal);
137
    setFormat(0, text.length(), regFormat);
138

139
    if (regexp.pattern().isEmpty()) {
140
        return;  // empty regular expression
141
    }
142

143
    int pos = 0;
144
    int last = -1;
145
    regFormat.setFontWeight(QFont::Bold);
146
    regFormat.setForeground(Qt::blue);
147

148
    QRegularExpressionMatch match;
149
    while ((pos = text.indexOf(regexp, pos, &match)) != -1) {
150
        if (last == pos) {
151
            break;
152
        }
153
        QString sub = text.mid(pos, match.capturedLength());
154
        if (!sub.isEmpty()) {
155
            setFormat(pos, sub.length(), regFormat);
156
        }
157

158
        pos += match.capturedLength();
159
        last = pos;
160
    }
161
}
162

163
void RegExpSyntaxHighlighter::highlightMatchedText(const QRegularExpression& rx)
164
{
165
    regexp = rx;
166
    rehighlight();
167
}
168

169
void RegExpSyntaxHighlighter::resethighlight()
170
{
171
    regexp.setPattern("");
172
    rehighlight();
173
}
174

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.