-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathintarraydataref.cpp
More file actions
94 lines (82 loc) · 2.6 KB
/
Copy pathintarraydataref.cpp
File metadata and controls
94 lines (82 loc) · 2.6 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
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
#include "intarraydataref.h"
#include "../../util/console.h"
#include <algorithm>
#include <QStringList>
IntArrayDataRef::IntArrayDataRef(QObject *parent, QString name, void *ref) : DataRef(parent, name, ref) {
_typeString = "ia";
_type = extplaneRefTypeIntArray;
_length = 0;
_valueArray = nullptr;
}
IntArrayDataRef::~IntArrayDataRef() {
if(_valueArray) delete [] _valueArray;
}
std::vector<int> & IntArrayDataRef::value() {
return _values;
}
// Copies values from valuearray to value list and emits changed as needed
void IntArrayDataRef::updateValue() {
Q_ASSERT(_length > 0);
bool valuesChanged = false;
for(int i=0;i<_length;i++){
if(_values.at(i) != _valueArray[i]) {
_values[i] = _valueArray[i];
valuesChanged = true;
}
}
if (valuesChanged || !isValid()) {
if(!_valueValid) setValueValid();
emit changed(this);
}
}
QString IntArrayDataRef::valueString() {
QString ret="[";
for(int i=0;i<_length;i++){
ret += QString::number(_values[i]) + (i < _length -1 ? QString(",") : "");
}
ret += "]";
return ret;
}
void IntArrayDataRef::setValue(QString &newValue) {
// Check that value starts with [ and ends with ]
if(!newValue.startsWith('[') || !newValue.endsWith(']')) {
INFO << "Invalid array value" << newValue;
return;
}
// Remove [] and split values
QString arrayString = newValue.mid(1, newValue.length() - 2);
QStringList values = arrayString.split(',');
// Limit number of values to write to ref length or number of given values
int numberOfValuesToWrite = qMin(_length, values.size());
// Convert values to int and copy to _valueArray
for(int i=0;i<numberOfValuesToWrite;i++) {
bool ok = true;
int value = values[i].toInt(&ok);
if(ok) {
_valueArray[i] = value;
if(changedIndices.empty() || changedIndices.back().second != (i-1))
{
changedIndices.push_back(std::pair<int, int>(i, i));
} else {
changedIndices.back().second = i;
}
} else if(!values.at(i).isEmpty()) {
INFO << "Invalid value " << values.at(i) << "in array";
}
}
updateValue();
}
void IntArrayDataRef::setLength(int newLength)
{
Q_ASSERT(newLength >= 0);
_values.resize(newLength);
std::fill(_values.begin(), _values.end(), -9999);
if(_valueArray) delete[] _valueArray;
_valueArray = new int[newLength];
_length = newLength;
}
int *IntArrayDataRef::valueArray()
{
Q_ASSERT(_valueArray);
return _valueArray;
}