-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreset.cpp
More file actions
69 lines (52 loc) · 1.72 KB
/
preset.cpp
File metadata and controls
69 lines (52 loc) · 1.72 KB
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
#include "preset.h"
/* =========================== */
/* Class Constructor */
/* =========================== */
// Default constructor
Preset::Preset(QString hex, QString description, int index) :
description_(description),
index_(static_cast<short>(index))
{
data_ = QByteArray::fromHex(hex.toLatin1());
std::reverse(data_.begin(), data_.end()); // Reverse the array for endianess
}
Preset::Preset(QByteArray &data, QString description, int index) :
description_(description),
data_(data),
index_(static_cast<short>(index))
{
}
/* ================== */
/* Get Data */
/* ================== */
// Get data as a hex string
QString Preset::getData()
{
QString tmpStr;
// Read the bytes from left to right so as to account for the endianess
for(int i = data_.size() - 1; i >= 0; --i) {
QString hex = QString("%1").arg(static_cast<unsigned char>(data_[i]), 2, 16, QChar('0')).toUpper();
if(i > 0)
hex.append(" ");
tmpStr.append(hex);
}
return tmpStr;
}
// Get description and hex as a string
QString Preset::getDescAndData()
{
return (getData() + ": " + getDesc());
}
// Find the index of the preset vector matching the source array (and add the source to the vector if no match is found)
short Preset::findIndex(QVector<Preset> &vect, const QByteArray &source, QString desc)
{
// Attempt to match the source with an existing preset in the vector
for(auto itr = vect.begin(); itr < vect.end(); ++itr)
{
if(itr->data_ == source)
return itr->getIndex();
}
// If no match is found, add the source data to the preset vector
vect.push_back(Preset(source, desc, vect.size()));
return vect.size() - 1;
}