-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindandreplacedialog.cpp
118 lines (95 loc) · 3.22 KB
/
findandreplacedialog.cpp
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
#include "findandreplacedialog.h"
#include "ui_findandreplacedialog.h"
FindAndReplaceDialog::FindAndReplaceDialog(CodeEditor *codeEditor, QWidget *parent) :
QDialog(parent),
ui(new Ui::FindAndReplaceDialog)
{
ui->setupUi(this);
this->codeEditor = codeEditor;
setWindowFlags(Qt::Window);
connect(ui->textToFind, SIGNAL(textChanged(QString)), this, SLOT(setFindEnabled()));
connect(codeEditor, SIGNAL(selectionChanged()), this, SLOT(setReplaceEnabled()));
connect(ui->findButton, SIGNAL(clicked()), this, SLOT(actionFind()));
connect(ui->replaceButton, SIGNAL(clicked()), this, SLOT(actionReplace()));
connect(ui->replaceAllButton, SIGNAL(clicked()), this, SLOT(actionReplaceAll()));
connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}
void FindAndReplaceDialog::showMessage(QString message, bool error)
{
if (error) {
ui->infoLabel->setStyleSheet("color:red");
} else {
ui->infoLabel->setStyleSheet("color:green");
}
ui->infoLabel->setText(message);
}
void FindAndReplaceDialog::setFindEnabled()
{
ui->findButton->setEnabled(ui->textToFind->text().count() > 0);
}
void FindAndReplaceDialog::setReplaceEnabled()
{
ui->replaceButton->setEnabled(codeEditor->textCursor().selectedText() == ui->textToFind->text());
}
void FindAndReplaceDialog::actionFind()
{
bool back = ui->upRadioButton->isChecked();
QString toSearch = ui->textToFind->text();
bool result = false;
QTextDocument::FindFlags flags;
if (back) {
flags |= QTextDocument::FindBackward;
}
if (ui->caseCheckBox->isChecked()) {
flags |= QTextDocument::FindCaseSensitively;
}
if (ui->wholeCheckBox->isChecked()) {
flags |= QTextDocument::FindWholeWords;
}
result = codeEditor->find(toSearch, flags);
/* if found, we are done */
if (result) {
showMessage("");
return;
}
/* if the string was not found, try to wrap around begin/end of file */
QTextCursor originalPosition = codeEditor->textCursor();
codeEditor->moveCursor(back ? QTextCursor::End : QTextCursor::Start);
result = codeEditor->find(toSearch, flags);
/* if found, we are done and tell the user that we have wrapped around */
if(result)
{
if (back) {
showMessage("Found the last occurrence");
} else {
showMessage("Found the first occurence");
}
return;
}
/* if the string was still not found, it is not in the text */
showMessage("No match found", true);
codeEditor->setTextCursor(originalPosition);
}
void FindAndReplaceDialog::actionReplace()
{
codeEditor->textCursor().insertText(ui->textToReplace->text());
actionFind();
}
void FindAndReplaceDialog::actionReplaceAll()
{
QTextCursor originalPosition = codeEditor->textCursor();
codeEditor->moveCursor(QTextCursor::Start);
actionFind();
int i = 0;
while (codeEditor->textCursor().hasSelection()) {
codeEditor->textCursor().insertText(ui->textToReplace->text());
actionFind();
i++;
}
showMessage(tr("Replaced %1 occurrence(s)").arg(i));
codeEditor->setTextCursor(originalPosition);
}
FindAndReplaceDialog::~FindAndReplaceDialog()
{
delete ui;
}