-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpattern_edit_box.cpp
59 lines (49 loc) · 1.35 KB
/
pattern_edit_box.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
#include "pattern_edit_box.h"
#include <QDebug>
#include <sstream>
QString PatternEditBox::allowed_chars = "0123456789ABCDEF ";
PatternEditBox::PatternEditBox(QWidget* parent)
: QTextEdit(parent)
{
QObject::connect(this, &PatternEditBox::textChanged, this, &PatternEditBox::handleTextChanged);
}
void PatternEditBox::handleTextChanged()
{
QString text = toPlainText();
// filter to only hexes
QString modded_text = "";
for (auto& c : text)
{
QChar c2 = c.toUpper();
if (PatternEditBox::allowed_chars.indexOf(c2.toLatin1()) >= 0) {
modded_text += c2;
}
}
if (text.compare(modded_text) != 0) {
// Keep cursor position after modifying text
int pos = textCursor().position();
setText(modded_text);
QTextCursor cursor = textCursor();
cursor.setPosition(pos);
setTextCursor(cursor);
}
}
std::vector<uint8_t> PatternEditBox::getBytes() {
std::string text = toPlainText().toUpper().replace(" ", "").toStdString();
std::vector<uint8_t> out;
for (int i = 1; i < text.size(); i += 2) {
std::stringstream ss;
ss << text.at(i-1) << text.at(i);
std::string one_hex = ss.str();
ss.str("");
ss << std::hex << one_hex;
uint16_t hex = 0;
ss >> hex;
out.push_back(hex);
}
return out;
}
void PatternEditBox::addPattern(std::vector<std::vector<uint8_t> >& patterns) {
std::vector<uint8_t> data = getBytes();
patterns.push_back(data);
}