-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindReplaceDialog.cpp
More file actions
278 lines (232 loc) · 9.27 KB
/
Copy pathFindReplaceDialog.cpp
File metadata and controls
278 lines (232 loc) · 9.27 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "FindReplaceDialog.h"
#include <QApplication>
#include <QFrame>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QKeyEvent>
#include <QRegularExpression>
#include <QSpacerItem>
#include <QStyle>
#include <QTextDocument>
#include <QVBoxLayout>
#include "CodeEditor.h"
FindReplaceDialog::FindReplaceDialog(QWidget *parent)
: QDialog(parent, Qt::Dialog | Qt::WindowCloseButtonHint) {
setWindowTitle("Find and Replace");
setModal(false);
setupUi();
resize(520, 340);
}
void FindReplaceDialog::setupUi() {
auto *root = new QVBoxLayout(this);
root->setSpacing(0);
root->setContentsMargins(0, 0, 0, 0);
auto *content = new QWidget(this);
auto *contentLayout = new QVBoxLayout(content);
contentLayout->setSpacing(16);
contentLayout->setContentsMargins(20, 16, 20, 16);
auto *findRow = new QHBoxLayout;
findRow->setSpacing(10);
auto *findLabel = new QLabel("Find", content);
findLabel->setFixedWidth(80);
findLabel->setObjectName("frLabel");
m_findEdit = new QLineEdit(content);
m_findEdit->setObjectName("frInput");
m_findEdit->setPlaceholderText("Search text…");
findRow->addWidget(findLabel);
findRow->addWidget(m_findEdit, 1);
contentLayout->addLayout(findRow);
auto *replaceRow = new QHBoxLayout;
replaceRow->setSpacing(10);
auto *replaceLabel = new QLabel("Replace", content);
replaceLabel->setFixedWidth(80);
replaceLabel->setObjectName("frLabel");
m_replaceEdit = new QLineEdit(content);
m_replaceEdit->setObjectName("frInput");
m_replaceEdit->setPlaceholderText("Replacement text…");
replaceRow->addWidget(replaceLabel);
replaceRow->addWidget(m_replaceEdit, 1);
contentLayout->addLayout(replaceRow);
auto *optGroup = new QWidget(content);
optGroup->setObjectName("frOptions");
auto *optLayout = new QGridLayout(optGroup);
optLayout->setContentsMargins(12, 12, 12, 12);
optLayout->setSpacing(10);
optLayout->setColumnStretch(1, 1);
m_matchCase = new QCheckBox("Match case", optGroup);
m_wholeWord = new QCheckBox("Whole word", optGroup);
m_regex = new QCheckBox("Regular expression", optGroup);
m_backward = new QCheckBox("Search backwards", optGroup);
m_wrap = new QCheckBox("Wrap around", optGroup);
m_wrap->setChecked(true);
optLayout->addWidget(m_matchCase, 0, 0);
optLayout->addWidget(m_wholeWord, 0, 1);
optLayout->addWidget(m_regex, 1, 0);
optLayout->addWidget(m_backward, 1, 1);
optLayout->addWidget(m_wrap, 2, 0);
contentLayout->addWidget(optGroup);
m_statusLabel = new QLabel(content);
m_statusLabel->setObjectName("frStatus");
m_statusLabel->setMinimumHeight(20);
contentLayout->addWidget(m_statusLabel);
auto *btnRow = new QHBoxLayout;
btnRow->setSpacing(8);
btnRow->addStretch();
m_findPrevBtn = new QPushButton("◀ Find Previous", content);
m_findPrevBtn->setObjectName("frBtnSecondary");
m_findPrevBtn->setCursor(Qt::PointingHandCursor);
m_findBtn = new QPushButton("Find Next ▶", content);
m_findBtn->setObjectName("frBtnPrimary");
m_findBtn->setCursor(Qt::PointingHandCursor);
m_findBtn->setDefault(true);
m_replaceBtn = new QPushButton("Replace", content);
m_replaceBtn->setObjectName("frBtnSecondary");
m_replaceBtn->setCursor(Qt::PointingHandCursor);
m_replaceAllBtn = new QPushButton("Replace All", content);
m_replaceAllBtn->setObjectName("frBtnSecondary");
m_replaceAllBtn->setCursor(Qt::PointingHandCursor);
btnRow->addWidget(m_findPrevBtn);
btnRow->addWidget(m_findBtn);
btnRow->addSpacing(12);
btnRow->addWidget(m_replaceBtn);
btnRow->addWidget(m_replaceAllBtn);
contentLayout->addLayout(btnRow);
root->addWidget(content, 1);
connect(m_findBtn, &QPushButton::clicked, this, &FindReplaceDialog::findNext);
connect(m_findPrevBtn, &QPushButton::clicked, this, &FindReplaceDialog::findPrev);
connect(m_replaceBtn, &QPushButton::clicked, this, &FindReplaceDialog::replace);
connect(m_replaceAllBtn, &QPushButton::clicked, this, &FindReplaceDialog::replaceAll);
connect(m_findEdit, &QLineEdit::returnPressed, this, &FindReplaceDialog::findNext);
connect(m_replaceEdit, &QLineEdit::returnPressed, this, &FindReplaceDialog::replace);
connect(m_findEdit, &QLineEdit::textChanged, this, &FindReplaceDialog::updateButtonStates);
connect(m_replaceEdit, &QLineEdit::textChanged, this, &FindReplaceDialog::updateButtonStates);
}
void FindReplaceDialog::updateButtonStates() {
const bool hasFindText = !m_findEdit->text().isEmpty();
m_findBtn->setEnabled(hasFindText);
m_findPrevBtn->setEnabled(hasFindText);
m_replaceBtn->setEnabled(hasFindText);
m_replaceAllBtn->setEnabled(hasFindText);
}
void FindReplaceDialog::setEditor(CodeEditor *editor) {
m_editor = editor;
updateButtonStates();
}
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("No matches 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);
}
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; padding-left: 4px;");
m_statusLabel->setText(count
? QString("%1 replacement(s) made.").arg(count)
: "No matches found.");
}
void FindReplaceDialog::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Escape) {
hide();
return;
}
QDialog::keyPressEvent(event);
}
void FindReplaceDialog::showEvent(QShowEvent *event) {
QDialog::showEvent(event);
if (m_findEdit) {
m_findEdit->setFocus();
m_findEdit->selectAll();
}
updateButtonStates();
}