forked from kambala-decapitator/QTblEditor
/
findreplacedialog.cpp
204 lines (173 loc) · 6.61 KB
/
findreplacedialog.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
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
#include "findreplacedialog.h"
#include "tablepanelwidget.h"
#include <QMessageBox>
#include <QDialog>
#include <QTableWidgetItem>
#include <QCloseEvent>
#include <QSettings>
FindReplaceDialog::FindReplaceDialog(QWidget *parent) : QDialog(parent)
{
ui.setupUi(this);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
ui.buttonFindNext->setToolTip(ui.buttonFindNext->shortcut().toString(QKeySequence::NativeText));
ui.buttonFindPrevious->setToolTip(ui.buttonFindPrevious->shortcut().toString(QKeySequence::NativeText));
ui.buttonReplaceAll->setToolTip(ui.buttonReplaceAll->shortcut().toString(QKeySequence::NativeText));
ui.buttonReplaceNext->setToolTip(ui.buttonReplaceNext->shortcut().toString(QKeySequence::NativeText));
connect(ui.lineEditReplace, SIGNAL(textChanged(QString)), SLOT(enableButtons()));
connect(ui.lineEditFind, SIGNAL(textChanged(QString)), SLOT(enableButtons()));
connect(ui.lineEditFind, SIGNAL(textChanged(QString)), SLOT(needsRefind()));
connect(ui.checkBoxCaseSensitive, SIGNAL(toggled(bool)), SLOT(needsRefind()));
connect(ui.checkBoxExactMatch, SIGNAL(toggled(bool)), SLOT(needsRefind()));
connect(ui.checkBoxOnlyActiveTable, SIGNAL(toggled(bool)), SLOT(needsRefind()));
connect(ui.buttonFindNext, SIGNAL(clicked()), SLOT(findNext()));
connect(ui.buttonFindPrevious, SIGNAL(clicked()), SLOT(findPrevious()));
connect(ui.buttonReplaceNext, SIGNAL(clicked()), SLOT(replaceNext()));
connect(ui.buttonReplaceAll, SIGNAL(clicked()), SLOT(replaceAll()));
resize(sizeHint());
readSettings();
}
void FindReplaceDialog::readSettings()
{
QSettings s;
s.beginGroup("findReplaceDialog");
ui.lineEditFind->setText(s.value("lastFindString").toString());
ui.lineEditReplace->setText(s.value("lastReplaceString").toString());
ui.checkBoxCaseSensitive->setChecked(s.value("isCaseSensitive").toBool());
ui.checkBoxExactMatch->setChecked(s.value("isExactMatch").toBool());
ui.checkBoxOnlyActiveTable->setChecked(s.value("isSearchOnlyActiveTable", true).toBool());
s.endGroup();
}
void FindReplaceDialog::writeSettings()
{
QSettings s;
s.beginGroup("findReplaceDialog");
if (!ui.lineEditFind->text().isEmpty())
s.setValue("lastFindString", ui.lineEditFind->text());
if (!ui.lineEditReplace->text().isEmpty())
s.setValue("lastReplaceString", ui.lineEditReplace->text());
s.setValue("isCaseSensitive", ui.checkBoxCaseSensitive->isChecked());
s.setValue("isExactMatch", ui.checkBoxExactMatch->isChecked());
s.setValue("isSearchOnlyActiveTable", ui.checkBoxOnlyActiveTable->isChecked());
s.endGroup();
}
void FindReplaceDialog::closeEvent(QCloseEvent *e)
{
writeSettings();
e->accept();
}
void FindReplaceDialog::show(bool isTwoTablesOpened)
{
ui.lineEditFind->setFocus();
ui.lineEditFind->selectAll();
ui.checkBoxOnlyActiveTable->setEnabled(isTwoTablesOpened);
QDialog::show();
}
void FindReplaceDialog::enableButtons()
{
bool isFindEqualToReplace = ui.lineEditFind->text() != ui.lineEditReplace->text(), isTextNotEmpty = !ui.lineEditFind->text().isEmpty();
ui.buttonFindNext->setEnabled(isTextNotEmpty);
ui.buttonFindPrevious->setEnabled(isTextNotEmpty);
ui.buttonReplaceAll->setEnabled(isTextNotEmpty && isFindEqualToReplace);
ui.buttonReplaceNext->setEnabled(isTextNotEmpty && isFindEqualToReplace);
}
bool FindReplaceDialog::areResultsObsolete()
{
if (_query.isEmpty() || _query != ui.lineEditFind->text() || _findConditionChanged)
{
_query = ui.lineEditFind->text();
emit getStrings(_query, ui.checkBoxCaseSensitive->isChecked(), ui.checkBoxExactMatch->isChecked(),
ui.checkBoxOnlyActiveTable->isEnabled() && !ui.checkBoxOnlyActiveTable->isChecked());
return true;
}
return false;
}
void FindReplaceDialog::getNextString(bool isPrevious)
{
if (areResultsObsolete())
return;
if (!_searchFailed)
{
if (isPrevious) // get previous
{
if (_currentStringIterator == _foundTableItems.begin())
{
qApp->beep();
_currentStringIterator = _foundTableItems.end() - 1;
}
else
_currentStringIterator--;
}
else // get next
{
if (_currentStringIterator + 1 == _foundTableItems.end())
{
qApp->beep();
_currentStringIterator = _foundTableItems.begin();
}
else
_currentStringIterator++;
}
emit currentItemChanged(*_currentStringIterator);
}
else
{
qApp->beep();
QMessageBox::information(this, tr("Lrn2read!"), tr("You've already been told that there's no string \"%1\"").arg(_query));
}
}
void FindReplaceDialog::findNext()
{
getNextString(false);
}
void FindReplaceDialog::findPrevious()
{
getNextString(true);
}
void FindReplaceDialog::getFoundStrings(const QList<QTableWidgetItem *> &foundItems)
{
_findConditionChanged = false;
if (!foundItems.size())
{
qApp->beep();
QMessageBox::information(this, qApp->applicationName(), tr("String \"%1\" not found").arg(_query));
_searchFailed = true;
return;
}
_searchFailed = false;
_foundTableItems = foundItems;
_currentStringIterator = _foundTableItems.begin();
emit currentItemChanged(*_currentStringIterator);
}
void FindReplaceDialog::replaceNext()
{
bool notSearched = !_currentStringIterator.i;
if (notSearched)
findNext();
if (!_searchFailed)
replaceInCurrentString();
if (!(notSearched && _searchFailed))
findNext();
}
void FindReplaceDialog::replaceAll()
{
areResultsObsolete();
if (_searchFailed)
return;
int n = 0;
for (_currentStringIterator = _foundTableItems.begin(); _currentStringIterator != _foundTableItems.end(); _currentStringIterator++, n++)
replaceInCurrentString();
QMessageBox::information(this, tr("Replace"), tr("%n occurrence(s) replaced", 0, n));
_query.clear();
}
void FindReplaceDialog::replaceInCurrentString()
{
QTableWidgetItem *itemToReplaceIn = *_currentStringIterator;
QString replaceIn = itemToReplaceIn->text(), replaceWith = ui.lineEditReplace->text();
Qt::CaseSensitivity cs = (Qt::CaseSensitivity)ui.checkBoxCaseSensitive->isChecked();
int occurencesCount = replaceIn.count(_query, cs), position = 0;
for (int i = 0; i < occurencesCount; i++)
{
position = replaceIn.indexOf(_query, position, cs);
itemToReplaceIn->setText(replaceIn.replace(position, _query.length(), replaceWith));
}
}