-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindReplaceDialog.cpp
More file actions
201 lines (171 loc) · 6.81 KB
/
FindReplaceDialog.cpp
File metadata and controls
201 lines (171 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "FindReplaceDialog.h"
#include <QFormLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QRegularExpression>
#include <QTextDocument>
#include <QVBoxLayout>
FindReplaceDialog::FindReplaceDialog(QWidget *parent)
: QDialog(parent, Qt::Dialog | Qt::WindowCloseButtonHint) {
setWindowTitle("Find and Replace");
setModal(false);
setupUi();
resize(480, 280);
}
void FindReplaceDialog::setupUi() {
auto *root = new QVBoxLayout(this);
root->setSpacing(10);
root->setContentsMargins(16, 16, 16, 12);
auto *formLayout = new QGridLayout;
formLayout->setColumnStretch(1, 1);
formLayout->setVerticalSpacing(8);
formLayout->addWidget(new QLabel("Find"), 0, 0, Qt::AlignRight);
m_findEdit = new QLineEdit(this);
m_findEdit->setPlaceholderText("Search text…");
formLayout->addWidget(m_findEdit, 0, 1);
formLayout->addWidget(new QLabel("Replace with"), 1, 0, Qt::AlignRight);
m_replaceEdit = new QLineEdit(this);
m_replaceEdit->setPlaceholderText("Nothing");
formLayout->addWidget(m_replaceEdit, 1, 1);
root->addLayout(formLayout);
auto *optGrid = new QGridLayout;
optGrid->setHorizontalSpacing(20);
optGrid->setVerticalSpacing(4);
m_matchCase = new QCheckBox("Match case", this);
m_wholeWord = new QCheckBox("Match entire word only", this);
m_regex = new QCheckBox("Regular expression", this);
m_backward = new QCheckBox("Search backwards", this);
m_wrap = new QCheckBox("Wrap around", this);
m_wrap->setChecked(true);
optGrid->addWidget(m_matchCase, 0, 0);
optGrid->addWidget(m_backward, 0, 1);
optGrid->addWidget(m_wholeWord, 1, 0);
optGrid->addWidget(m_wrap, 1, 1);
optGrid->addWidget(m_regex, 2, 0);
root->addLayout(optGrid);
m_statusLabel = new QLabel(this);
m_statusLabel->setStyleSheet("color: #e74c3c; font-size: 12px;");
root->addWidget(m_statusLabel);
auto *btnRow = new QHBoxLayout;
btnRow->addStretch();
m_replaceAllBtn = new QPushButton("Replace All", this);
m_replaceBtn = new QPushButton("Replace", this);
m_findBtn = new QPushButton("Find", this);
m_findBtn->setDefault(true);
btnRow->addWidget(m_replaceAllBtn);
btnRow->addWidget(m_replaceBtn);
btnRow->addWidget(m_findBtn);
root->addLayout(btnRow);
connect(m_findBtn, &QPushButton::clicked, this, &FindReplaceDialog::findNext);
connect(m_replaceBtn, &QPushButton::clicked, this,
&FindReplaceDialog::replace);
connect(m_replaceAllBtn, &QPushButton::clicked, this,
&FindReplaceDialog::replaceAll);
connect(m_findEdit, &QLineEdit::returnPressed, this,
&FindReplaceDialog::findNext);
}
void FindReplaceDialog::setEditor(CodeEditor *editor) { m_editor = editor; }
QTextDocument::FindFlags FindReplaceDialog::buildFlags(bool backward) const {
QTextDocument::FindFlags flags;
if (m_matchCase->isChecked()) flags |= QTextDocument::FindCaseSensitively;
if (m_wholeWord->isChecked()) flags |= QTextDocument::FindWholeWords;
if (backward || m_backward->isChecked()) flags |= QTextDocument::FindBackward;
return flags;
}
bool FindReplaceDialog::doFind(bool backward) {
if (!m_editor || m_findEdit->text().isEmpty()) return false;
m_statusLabel->clear();
const QTextDocument::FindFlags flags = buildFlags(backward);
const QString needle = m_findEdit->text();
QTextDocument *doc = m_editor->document();
QTextCursor cur = m_editor->textCursor();
QTextCursor found;
if (m_regex->isChecked()) {
QRegularExpression::PatternOptions opts;
if (!m_matchCase->isChecked())
opts |= QRegularExpression::CaseInsensitiveOption;
found = doc->find(QRegularExpression(needle, opts), cur, flags);
if (found.isNull() && m_wrap->isChecked()) {
QTextCursor start(doc);
if (backward) start.movePosition(QTextCursor::End);
found = doc->find(QRegularExpression(needle, opts), start, flags);
}
} else {
found = doc->find(needle, cur, flags);
if (found.isNull() && m_wrap->isChecked()) {
QTextCursor start(doc);
if (backward) start.movePosition(QTextCursor::End);
found = doc->find(needle, start, flags);
}
}
if (!found.isNull()) {
m_editor->setTextCursor(found);
return true;
}
m_statusLabel->setText("Not found.");
return false;
}
void FindReplaceDialog::findNext() { doFind(false); }
void FindReplaceDialog::findPrev() { doFind(true); }
void FindReplaceDialog::replace() {
if (!m_editor) return;
QTextCursor cur = m_editor->textCursor();
if (cur.hasSelection()) {
const QString sel = cur.selectedText();
const QString needle = m_findEdit->text();
bool matches = false;
if (m_regex->isChecked()) {
QRegularExpression::PatternOptions opts;
if (!m_matchCase->isChecked())
opts |= QRegularExpression::CaseInsensitiveOption;
matches = QRegularExpression(needle, opts).match(sel).hasMatch();
} else {
Qt::CaseSensitivity cs =
m_matchCase->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
matches = (sel.compare(needle, cs) == 0);
}
if (matches) {
cur.insertText(m_replaceEdit->text());
m_editor->setTextCursor(cur);
}
}
doFind(false); // Advance to next
}
void FindReplaceDialog::replaceAll() {
if (!m_editor || m_findEdit->text().isEmpty()) return;
m_statusLabel->clear();
const QString needle = m_findEdit->text();
const QString replacement = m_replaceEdit->text();
const QTextDocument::FindFlags flags =
(m_matchCase->isChecked()
? QTextDocument::FindCaseSensitively
: QTextDocument::FindFlags()) |
(m_wholeWord->isChecked()
? QTextDocument::FindWholeWords
: QTextDocument::FindFlags());
QTextDocument *doc = m_editor->document();
QTextCursor cur(doc);
cur.beginEditBlock();
int count = 0;
while (true) {
QTextCursor found;
if (m_regex->isChecked()) {
QRegularExpression::PatternOptions opts;
if (!m_matchCase->isChecked())
opts |= QRegularExpression::CaseInsensitiveOption;
found = doc->find(QRegularExpression(needle, opts), cur, flags);
} else {
found = doc->find(needle, cur, flags);
}
if (found.isNull()) break;
found.insertText(replacement);
cur = found;
++count;
}
cur.endEditBlock();
m_statusLabel->setStyleSheet("color: #2ecc71; font-size: 12px;");
m_statusLabel->setText(count
? QString("%1 replacement(s) made.").arg(count)
: "Not found.");
}