-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindreplacedialog.cpp.autosave
executable file
·130 lines (109 loc) · 4.47 KB
/
findreplacedialog.cpp.autosave
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
#include "findreplacedialog.h"
#include "ui_findreplacedialog.h"
FindReplaceDialog::FindReplaceDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::FindReplaceDialog)
{
ui->setupUi(this);
//manually ask for ui components
dataLabel = ui->dataLabel;
findTextEdit = ui->findTextEdit;
replaceTextEdit = ui->replaceWithTextEdit;
findBtn = ui->findNextBtn;
replaceBtn = ui->replaceBtn;
replaceAllBtn = ui->replaceAllBtn;
cancelBtn = ui->cancelBtn;
connect(findBtn, &QPushButton::clicked, this, &FindReplaceDialog::findNext);
connect(replaceBtn, &QPushButton::clicked, this, &FindReplaceDialog::replace);
connect(replaceAllBtn, &QPushButton::clicked, this, &FindReplaceDialog::replaceAll);
connect(cancelBtn, &QPushButton::clicked, this, &FindReplaceDialog::close);
dataLabel->setVisible(false);
}
void FindReplaceDialog::setEditor(CodeEditor* editor) {
this->editor = editor;
}
FindReplaceDialog::~FindReplaceDialog()
{
delete ui;
}
void FindReplaceDialog::findNext() {
//resets the format of any previously found words
if (oldFormatPosition != -1) {
QTextCharFormat regularFormat;
regularFormat.setBackground(Qt::white);
QTextCursor removalCursor = editor->textCursor();
removalCursor.setPosition(oldFormatPosition, QTextCursor::MoveAnchor);
removalCursor.setPosition(oldFormatEndPosition, QTextCursor::KeepAnchor);
removalCursor.setCharFormat(regularFormat);
oldFormatPosition = -1;
oldFormatEndPosition = -1;
}
replacedOnce = false;
QString textToFind = findTextEdit->text();
if (!textToFind.isEmpty()) {
if (textToFind == previousFind) {
cursor = editor->document()->find(QRegularExpression(textToFind), cursor);
} else {
wordFoundBefore = false;
cursor = editor->document()->find(QRegularExpression(textToFind));
}
}
//no matches found update the label
if (cursor.position() == -1) {
dataLabel->setText("Error. No matches found");
dataLabel->setStyleSheet("QLabel {color:red}");
dataLabel->setVisible(true);
} else if (cursor.position() != -1) {
//found a word update the formatting
QTextCharFormat highlightFormat;
highlightFormat.setBackground(Qt::yellow);
cursor.setCharFormat(highlightFormat);
oldFormatPosition = cursor.selectionStart();
oldFormatEndPosition = cursor.selectionEnd();
//update the data label
dataLabel->setStyleSheet("QLabel {color:black}");
dataLabel->setText("\"" + textToFind + "\" found at cursor position: " + QString::number(cursor.position()));
dataLabel->setVisible(true);
wordFoundBefore = true;
//focus in on word
int currentBlock = cursor.block().blockNumber();
editor->verticalScrollBar()->setSliderPosition(currentBlock);
}
//so we can tell if we found a different word before
previousFind = textToFind;
}
void FindReplaceDialog::replace() {
QString replaceWith = replaceTextEdit->text();
if (cursor.position() != -1 && replacedOnce == false) {
cursor.removeSelectedText();
cursor.insertText(replaceWith);
replacedOnce = true;
}
}
//doesnt use the replace function at all. So no need to find a match first
void FindReplaceDialog::replaceAll() {
QString wordToReplace = findTextEdit->text();
QString replaceWith = replaceTextEdit->text();
cursor = editor->document()->find(QRegularExpression(wordToReplace));
do {
cursor = editor->document()->find(QRegularExpression(wordToReplace));
if (cursor.position() != -1) {
cursor.removeSelectedText();
cursor.insertText(replaceWith);
}
} while (cursor.position() != -1);
}
//sets all formatting back to default when the dialog closes
void FindReplaceDialog::closeEvent(QCloseEvent *e) {
if (oldFormatPosition != -1) {
QTextCharFormat regularFormat;
regularFormat.setBackground(Qt::white);
QTextCursor removalCursor = editor->textCursor();
removalCursor.setPosition(oldFormatPosition, QTextCursor::MoveAnchor);
removalCursor.setPosition(oldFormatEndPosition, QTextCursor::KeepAnchor);
removalCursor.setCharFormat(regularFormat);
oldFormatPosition = -1;
oldFormatEndPosition = -1;
}
QDialog::closeEvent(e);
}