forked from torrent-file-editor/torrent-file-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchdlg.cpp
232 lines (196 loc) · 6.94 KB
/
searchdlg.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
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
/*
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*
* Copyright (C) 2016-2017 Ivan Romanov <drizt72@zoho.eu>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "searchdlg.h"
#include "ui_searchdlg.h"
#include "bencodemodel.h"
#ifdef Q_OS_MAC
# include <QSizeGrip>
#endif
SearchDlg::SearchDlg(BencodeModel *model, QWidget *parent)
#ifdef Q_OS_WIN
: QDialog(parent, Qt::Tool | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint)
#else
: QDialog(parent, Qt::Tool | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint)
#endif
, ui(new Ui::SearchDlg)
, _searchList()
, _searchIndex(-1)
, _model(model)
#ifdef Q_OS_MAC
, _sizeGrip(new QSizeGrip(this))
#endif
{
ui->setupUi(this);
connect(_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(resetSearchList()));
connect(_model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), SLOT(resetSearchList()));
#ifdef Q_OS_MAC
// Workaround. Qt has no size grip on Mac OS X. Bug?
setSizeGripEnabled(false);
_sizeGrip->setFixedSize(20, 20);
updateSizeGripPos();
#endif
}
SearchDlg::~SearchDlg()
{
delete ui;
}
void SearchDlg::setReplaceModeEnabled(bool b)
{
setWindowTitle(b ? tr("Replace") : tr("Find"));
ui->wdgReplace->setVisible(b);
ui->btnReplace->setVisible(b);
ui->btnReplaceAll->setVisible(b);
setMinimumHeight(0);
setMaximumHeight(16777215);
adjustSize();
setFixedHeight(height());
updateSearchNext();
}
void SearchDlg::searchNext()
{
_searchIndex += ui->rdDown->isChecked() ? +1 : -1;
if ((ui->rdDown->isChecked() && _searchIndex == _searchList.size())
|| (ui->rdUp->isChecked() && _searchIndex == -1)) {
resetSearchList();
}
if (_searchList.isEmpty()) {
QModelIndexList keys;
QModelIndexList values;
if (ui->grpKey->isChecked()) {
QString key = ui->lneKey->text();
if (key.isEmpty())
return;
Qt::MatchFlags matchFlags(Qt::MatchFlag::MatchRecursive);
if (ui->chkKeyCase->isChecked())
matchFlags |= Qt::MatchCaseSensitive;
if (ui->rdKeyExactMatch->isChecked())
matchFlags |= Qt::MatchFlag::MatchFixedString;
else if (ui->rdKeyWildcards->isChecked())
matchFlags |= Qt::MatchFlag::MatchWildcard;
else if (ui->rdKeyRegexp->isChecked())
matchFlags |= Qt::MatchFlag::MatchRegExp;
keys = _model->match(_model->index(0, 0), Qt::DisplayRole, key, -1, matchFlags);
}
if (ui->grpValue->isChecked()) {
QString value = ui->lneValue->text();
if (value.isEmpty())
return;
Qt::ItemDataRole role = Qt::UserRole;
Qt::MatchFlags matchFlags(Qt::MatchFlag::MatchRecursive);
if (ui->chkValueCase->isChecked())
matchFlags |= Qt::MatchCaseSensitive;
if (ui->rdValueExactMatch->isChecked()) {
matchFlags |= Qt::MatchFlag::MatchFixedString;
}
else if (ui->rdValueWildcards->isChecked()) {
matchFlags |= Qt::MatchFlag::MatchWildcard;
}
else if (ui->rdValueRegexp->isChecked()) {
matchFlags |= Qt::MatchFlag::MatchRegExp;
}
else if (ui->rdValueHex->isChecked()) {
matchFlags |= Qt::MatchFlag::MatchContains;
matchFlags &= ~Qt::MatchFlag::MatchCaseSensitive;
role = static_cast<Qt::ItemDataRole>(Qt::UserRole + 1);
}
values = _model->match(_model->index(0, 0), role, value, -1, matchFlags); // -V2006 PVS-Studio
}
if (ui->grpKey->isChecked() && ui->grpValue->isChecked()) {
for (const auto &key: keys) {
for (const auto &value: values) {
if (value == key) {
_searchList << key;
}
}
}
}
else if (ui->grpKey->isChecked()) {
_searchList = keys;
}
else if (ui->grpValue->isChecked()) {
_searchList = values;
}
_searchIndex = ui->rdDown->isChecked() ? 0 : _searchList.size() - 1;
}
if (_searchList.isEmpty()) {
ui->lblItemsFound->setText(tr("No matches found"));
_searchIndex = -1;
return;
}
ui->lblItemsFound->setText(tr("%1 of %n match(es)", 0, _searchList.size()).arg(_searchIndex + 1));
emit foundItem(_searchList.at(_searchIndex));
}
void SearchDlg::updateSearchNext()
{
bool enabled = false;
if (ui->grpKey->isChecked() && !ui->lneKey->text().isEmpty())
enabled = true;
else if (ui->grpValue->isChecked() && !ui->lneValue->text().isEmpty())
enabled = true;
if (ui->grpKey->isChecked() && ui->lneKey->text().isEmpty())
enabled = false;
else if (ui->grpValue->isChecked() && ui->lneValue->text().isEmpty())
enabled = false;
ui->btnSearchNext->setEnabled(enabled);
ui->btnReplace->setEnabled(enabled);
ui->btnReplaceAll->setEnabled(enabled);
}
void SearchDlg::resetSearchList()
{
_searchList.clear();
_searchIndex = -1;
ui->lblItemsFound->setText(QString());
}
void SearchDlg::replace()
{
if (_searchIndex >= 0) {
QString replaceStr = ui->lneReplace->text();
_model->setData(_searchList.at(_searchIndex), replaceStr, Qt::UserRole + (ui->chkHex->isChecked() ? 1 : 0));
}
searchNext();
}
void SearchDlg::replaceAll()
{
if (_searchList.isEmpty())
searchNext();
if (_searchList.isEmpty())
return;
QString replaceStr = ui->lneReplace->text();
for (const auto &item: _searchList) {
_model->setData(item, replaceStr, Qt::UserRole + (ui->chkHex->isChecked() ? 1 : 0));
}
QString resStr = tr("%n value(s) was(were) replaced", 0, _searchList.size());
resetSearchList();
ui->lblItemsFound->setText(resStr);
}
#ifdef Q_OS_MAC
void SearchDlg::resizeEvent(QResizeEvent *event)
{
QDialog::resizeEvent(event);
updateSizeGripPos();
}
void SearchDlg::updateSizeGripPos()
{
int x = width() - _sizeGrip->width();
int y = height() - _sizeGrip->height();
_sizeGrip->move(x, y);
}
#endif