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
|
/*
* Copyright (C) 2009 Lorenzo Bettini <http://www.lorenzobettini.it>
* See COPYING file that comes with this distribution
*/
#include <QtGui>
#include <QTextEdit>
#include <QRegularExpression>
#include <QSettings>
#include "varianteditor.h"
#include "findreplaceform.h"
#include "ui_findreplaceform.h"
#define TEXT_TO_FIND "textToFind"
#define TEXT_TO_REPLACE "textToReplace"
#define DOWN_RADIO "downRadio"
#define UP_RADIO "upRadio"
#define CASE_CHECK "caseCheck"
#define WHOLE_CHECK "wholeCheck"
#define REGEXP_CHECK "regexpCheck"
FindReplaceForm::FindReplaceForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::FindReplaceForm), textEdit(0)
{
ui->setupUi(this);
ui->errorLabel->setText("");
connect(ui->textToFind, SIGNAL(textChanged(QString)), this, SLOT(textToFindChanged()));
connect(ui->textToFind, SIGNAL(textChanged(QString)), this, SLOT(validateRegExp(QString)));
connect(ui->regexCheckBox, SIGNAL(toggled(bool)), this, SLOT(regexpSelected(bool)));
connect(ui->findButton, SIGNAL(clicked()), this, SLOT(find()));
connect(ui->closeButton, SIGNAL(clicked()), parent, SLOT(close()));
connect(ui->replaceButton, SIGNAL(clicked()), this, SLOT(replace()));
connect(ui->replaceAllButton, SIGNAL(clicked()), this, SLOT(replaceAll()));
}
FindReplaceForm::~FindReplaceForm()
{
delete ui;
if(textEdit)
delete textEdit;
}
void FindReplaceForm::hideReplaceWidgets() {
ui->replaceLabel->setVisible(false);
ui->textToReplace->setVisible(false);
ui->replaceButton->setVisible(false);
ui->replaceAllButton->setVisible(false);
}
void FindReplaceForm::setTextEdit(QTextEdit *textEdit_) {
textEdit = new VariantEditor(textEdit_);
textEdit->connectToSetEnabled(ui->replaceButton);
textEdit->connectToSetEnabled(ui->replaceAllButton);
}
void FindReplaceForm::setTextEdit(QPlainTextEdit *textEdit_)
{
textEdit = new VariantEditor(textEdit_);
textEdit->connectToSetEnabled(ui->replaceButton);
textEdit->connectToSetEnabled(ui->replaceAllButton);
}
void FindReplaceForm::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void FindReplaceForm::textToFindChanged() {
ui->findButton->setEnabled(ui->textToFind->text().size() > 0);
}
void FindReplaceForm::regexpSelected(bool sel) {
if (sel)
validateRegExp(ui->textToFind->text());
else
validateRegExp("");
}
void FindReplaceForm::validateRegExp(const QString &text) {
if (!ui->regexCheckBox->isChecked() || text.size() == 0) {
ui->errorLabel->setText("");
return; // nothing to validate
}
QRegularExpression reg(text,
(ui->caseCheckBox->isChecked() ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption));
if (reg.isValid()) {
showError("");
} else {
showError(reg.errorString());
}
}
void FindReplaceForm::showError(const QString &error) {
if (error == "") {
ui->errorLabel->setText("");
} else {
ui->errorLabel->setText("<span style=\" font-weight:600; color:#ff0000;\">" +
error +
"</span>");
}
}
void FindReplaceForm::showMessage(const QString &message) {
if (message == "") {
ui->errorLabel->setText("");
} else {
ui->errorLabel->setText("<span style=\" font-weight:600; color:green;\">" +
message +
"</span>");
}
}
void FindReplaceForm::find() {
find(ui->downRadioButton->isChecked());
}
void FindReplaceForm::find(bool next) {
if (!textEdit)
return; // TODO: show some warning?
// backward search
bool back = !next;
const 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;
if (ui->regexCheckBox->isChecked()) {
QRegularExpression reg(toSearch,
(ui->caseCheckBox->isChecked() ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption));
qDebug() << "searching for regexp: " << reg.pattern();
textCursor = textEdit->document()->find(reg, textCursor, flags);
textEdit->setTextCursor(textCursor);
result = (!textCursor.isNull());
} else {
qDebug() << "searching for: " << toSearch;
result = textEdit->find(toSearch, flags);
}
if (result) {
showError("");
} else {
showError(tr("no match found"));
// move to the beginning of the document for the next find
textEdit->setTextCursor(QTextCursor(textEdit->document()->begin()));
}
}
void FindReplaceForm::replace() {
if (!textEdit->textCursor().hasSelection()) {
find();
} else {
textEdit->textCursor().insertText(ui->textToReplace->text());
find();
}
}
void FindReplaceForm::replaceAll() {
int i=0;
while (textEdit->textCursor().hasSelection()){
textEdit->textCursor().insertText(ui->textToReplace->text());
find();
i++;
}
showMessage(tr("Replaced %1 occurrence(s)").arg(i));
}
void FindReplaceForm::writeSettings(QSettings &settings, const QString &prefix) {
settings.beginGroup(prefix);
settings.setValue(TEXT_TO_FIND, ui->textToFind->text());
settings.setValue(TEXT_TO_REPLACE, ui->textToReplace->text());
settings.setValue(DOWN_RADIO, ui->downRadioButton->isChecked());
settings.setValue(UP_RADIO, ui->upRadioButton->isChecked());
settings.setValue(CASE_CHECK, ui->caseCheckBox->isChecked());
settings.setValue(WHOLE_CHECK, ui->wholeCheckBox->isChecked());
settings.setValue(REGEXP_CHECK, ui->regexCheckBox->isChecked());
settings.endGroup();
}
void FindReplaceForm::readSettings(QSettings &settings, const QString &prefix) {
settings.beginGroup(prefix);
ui->textToFind->setText(settings.value(TEXT_TO_FIND, "").toString());
ui->textToReplace->setText(settings.value(TEXT_TO_REPLACE, "").toString());
ui->downRadioButton->setChecked(settings.value(DOWN_RADIO, true).toBool());
ui->upRadioButton->setChecked(settings.value(UP_RADIO, false).toBool());
ui->caseCheckBox->setChecked(settings.value(CASE_CHECK, false).toBool());
ui->wholeCheckBox->setChecked(settings.value(WHOLE_CHECK, false).toBool());
ui->regexCheckBox->setChecked(settings.value(REGEXP_CHECK, false).toBool());
settings.endGroup();
}
|