1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QtWidgets>
52
53#include "regexpdialog.h"
54
55RegExpDialog::RegExpDialog(QWidget *parent)
56 : QDialog(parent)
57{
58 patternComboBox = new QComboBox;
59 patternComboBox->setEditable(true);
60 patternComboBox->setSizePolicy(hor: QSizePolicy::Expanding,
61 ver: QSizePolicy::Preferred);
62
63 patternLabel = new QLabel(tr(s: "&Pattern:"));
64 patternLabel->setBuddy(patternComboBox);
65
66 escapedPatternLineEdit = new QLineEdit;
67 escapedPatternLineEdit->setReadOnly(true);
68 QPalette palette = escapedPatternLineEdit->palette();
69 palette.setBrush(acr: QPalette::Base,
70 abrush: palette.brush(cg: QPalette::Disabled, cr: QPalette::Base));
71 escapedPatternLineEdit->setPalette(palette);
72
73 escapedPatternLabel = new QLabel(tr(s: "&Escaped Pattern:"));
74 escapedPatternLabel->setBuddy(escapedPatternLineEdit);
75
76 syntaxComboBox = new QComboBox;
77 syntaxComboBox->addItem(atext: tr(s: "Regular expression v1"), auserData: QRegExp::RegExp);
78 syntaxComboBox->addItem(atext: tr(s: "Regular expression v2"), auserData: QRegExp::RegExp2);
79 syntaxComboBox->addItem(atext: tr(s: "Wildcard"), auserData: QRegExp::Wildcard);
80 syntaxComboBox->addItem(atext: tr(s: "Fixed string"), auserData: QRegExp::FixedString);
81 syntaxComboBox->addItem(atext: tr(s: "W3C Xml Schema 1.1"), auserData: QRegExp::W3CXmlSchema11);
82
83 syntaxLabel = new QLabel(tr(s: "&Pattern Syntax:"));
84 syntaxLabel->setBuddy(syntaxComboBox);
85
86 textComboBox = new QComboBox;
87 textComboBox->setEditable(true);
88 textComboBox->setSizePolicy(hor: QSizePolicy::Expanding, ver: QSizePolicy::Preferred);
89
90 textLabel = new QLabel(tr(s: "&Text:"));
91 textLabel->setBuddy(textComboBox);
92
93 caseSensitiveCheckBox = new QCheckBox(tr(s: "Case &Sensitive"));
94 caseSensitiveCheckBox->setChecked(true);
95 minimalCheckBox = new QCheckBox(tr(s: "&Minimal"));
96
97 indexLabel = new QLabel(tr(s: "Index of Match:"));
98 indexEdit = new QLineEdit;
99 indexEdit->setReadOnly(true);
100
101 matchedLengthLabel = new QLabel(tr(s: "Matched Length:"));
102 matchedLengthEdit = new QLineEdit;
103 matchedLengthEdit->setReadOnly(true);
104
105 for (int i = 0; i < MaxCaptures; ++i) {
106 captureLabels[i] = new QLabel(tr(s: "Capture %1:").arg(a: i));
107 captureEdits[i] = new QLineEdit;
108 captureEdits[i]->setReadOnly(true);
109 }
110 captureLabels[0]->setText(tr(s: "Match:"));
111
112 QHBoxLayout *checkBoxLayout = new QHBoxLayout;
113 checkBoxLayout->addWidget(caseSensitiveCheckBox);
114 checkBoxLayout->addWidget(minimalCheckBox);
115 checkBoxLayout->addStretch(stretch: 1);
116
117 QGridLayout *mainLayout = new QGridLayout;
118 mainLayout->addWidget(patternLabel, row: 0, column: 0);
119 mainLayout->addWidget(patternComboBox, row: 0, column: 1);
120 mainLayout->addWidget(escapedPatternLabel, row: 1, column: 0);
121 mainLayout->addWidget(escapedPatternLineEdit, row: 1, column: 1);
122 mainLayout->addWidget(syntaxLabel, row: 2, column: 0);
123 mainLayout->addWidget(syntaxComboBox, row: 2, column: 1);
124 mainLayout->addLayout(checkBoxLayout, row: 3, column: 0, rowSpan: 1, columnSpan: 2);
125 mainLayout->addWidget(textLabel, row: 4, column: 0);
126 mainLayout->addWidget(textComboBox, row: 4, column: 1);
127 mainLayout->addWidget(indexLabel, row: 5, column: 0);
128 mainLayout->addWidget(indexEdit, row: 5, column: 1);
129 mainLayout->addWidget(matchedLengthLabel, row: 6, column: 0);
130 mainLayout->addWidget(matchedLengthEdit, row: 6, column: 1);
131
132 for (int j = 0; j < MaxCaptures; ++j) {
133 mainLayout->addWidget(captureLabels[j], row: 7 + j, column: 0);
134 mainLayout->addWidget(captureEdits[j], row: 7 + j, column: 1);
135 }
136 setLayout(mainLayout);
137
138 connect(sender: patternComboBox, signal: &QComboBox::editTextChanged,
139 receiver: this, slot: &RegExpDialog::refresh);
140 connect(sender: textComboBox, signal: &QComboBox::editTextChanged,
141 receiver: this, slot: &RegExpDialog::refresh);
142 connect(sender: caseSensitiveCheckBox, signal: &QAbstractButton::toggled,
143 receiver: this, slot: &RegExpDialog::refresh);
144 connect(sender: minimalCheckBox, signal: &QAbstractButton::toggled, receiver: this, slot: &RegExpDialog::refresh);
145 connect(sender: syntaxComboBox, signal: QOverload<int>::of(ptr: &QComboBox::currentIndexChanged),
146 receiver: this, slot: &RegExpDialog::refresh);
147
148 patternComboBox->addItem(atext: tr(s: "[A-Za-z_]+([A-Za-z_0-9]*)"));
149 textComboBox->addItem(atext: tr(s: "(10 + delta4) * 32"));
150
151 setWindowTitle(tr(s: "RegExp"));
152 setFixedHeight(sizeHint().height());
153 refresh();
154}
155
156void RegExpDialog::refresh()
157{
158 setUpdatesEnabled(false);
159
160 QString pattern = patternComboBox->currentText();
161 QString text = textComboBox->currentText();
162
163 QString escaped = pattern;
164 escaped.replace(before: "\\", after: "\\\\");
165 escaped.replace(before: "\"", after: "\\\"");
166 escaped.prepend(c: '"');
167 escaped.append(c: '"');
168 escapedPatternLineEdit->setText(escaped);
169
170 QRegExp rx(pattern);
171 Qt::CaseSensitivity cs = Qt::CaseInsensitive;
172 if (caseSensitiveCheckBox->isChecked())
173 cs = Qt::CaseSensitive;
174 rx.setCaseSensitivity(cs);
175 rx.setMinimal(minimalCheckBox->isChecked());
176 QRegExp::PatternSyntax syntax = QRegExp::PatternSyntax(
177 syntaxComboBox->itemData(index: syntaxComboBox->currentIndex()).toInt());
178 rx.setPatternSyntax(syntax);
179
180 QPalette palette = patternComboBox->palette();
181 if (rx.isValid()) {
182 palette.setColor(acr: QPalette::Text,
183 acolor: textComboBox->palette().color(cr: QPalette::Text));
184 } else {
185 palette.setColor(acr: QPalette::Text, acolor: Qt::red);
186 }
187 patternComboBox->setPalette(palette);
188
189 indexEdit->setText(QString::number(rx.indexIn(str: text)));
190 matchedLengthEdit->setText(QString::number(rx.matchedLength()));
191 for (int i = 0; i < MaxCaptures; ++i) {
192 captureLabels[i]->setEnabled(i <= rx.captureCount());
193 captureEdits[i]->setEnabled(i <= rx.captureCount());
194 captureEdits[i]->setText(rx.cap(nth: i));
195 }
196
197 setUpdatesEnabled(true);
198}
199

source code of qtbase/examples/widgets/tools/regexp/regexpdialog.cpp