-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchdialog.cpp
115 lines (99 loc) · 2.76 KB
/
searchdialog.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
#include "searchdialog.h"
#include "ui_searchdialog.h"
#include <QMessageBox>
SearchDialog::SearchDialog(QHexEdit *hexEdit, QWidget *parent) :
QDialog(parent),
ui(new Ui::SearchDialog)
{
ui->setupUi(this);
_hexEdit = hexEdit;
}
SearchDialog::~SearchDialog()
{
delete ui;
}
qint64 SearchDialog::findNext()
{
qint64 from = _hexEdit->cursorPosition() / 2;
_findBa = getContent(ui->cbFindFormat->currentIndex(), ui->cbFind->currentText());
qint64 idx = -1;
if (_findBa.length() > 0)
{
if (ui->cbBackwards->isChecked())
idx = _hexEdit->lastIndexOf(_findBa, from);
else
idx = _hexEdit->indexOf(_findBa, from);
}
return idx;
}
void SearchDialog::on_pbFind_clicked()
{
findNext();
}
void SearchDialog::on_pbReplace_clicked()
{
int idx = findNext();
if (idx >= 0)
{
QByteArray replaceBa = getContent(ui->cbReplaceFormat->currentIndex(), ui->cbReplace->currentText());
replaceOccurrence(idx, replaceBa);
}
}
void SearchDialog::on_pbReplaceAll_clicked()
{
int replaceCounter = 0;
int idx = 0;
int goOn = QMessageBox::Yes;
while ((idx >= 0) && (goOn == QMessageBox::Yes))
{
idx = findNext();
if (idx >= 0)
{
QByteArray replaceBa = getContent(ui->cbReplaceFormat->currentIndex(), ui->cbReplace->currentText());
int result = replaceOccurrence(idx, replaceBa);
if (result == QMessageBox::Yes)
replaceCounter += 1;
if (result == QMessageBox::Cancel)
goOn = result;
}
}
if (replaceCounter > 0)
QMessageBox::information(this, tr("QHexEdit"), QString(tr("%1 occurrences replaced.")).arg(replaceCounter));
}
QByteArray SearchDialog::getContent(int comboIndex, const QString &input)
{
QByteArray findBa;
switch (comboIndex)
{
case 0: // hex
findBa = QByteArray::fromHex(input.toLatin1());
break;
case 1: // text
findBa = input.toUtf8();
break;
}
return findBa;
}
qint64 SearchDialog::replaceOccurrence(qint64 idx, const QByteArray &replaceBa)
{
int result = QMessageBox::Yes;
if (replaceBa.length() >= 0)
{
if (ui->cbPrompt->isChecked())
{
result = QMessageBox::question(this, tr("QHexEdit"),
tr("Replace occurrence?"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
if (result == QMessageBox::Yes)
{
_hexEdit->replace(idx, replaceBa.length(), replaceBa);
_hexEdit->update();
}
}
else
{
_hexEdit->replace(idx, _findBa.length(), replaceBa);
}
}
return result;
}