-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparameter.cpp
209 lines (175 loc) · 4.58 KB
/
parameter.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
#include "parameter.h"
#include <QDateTime>
#include <QLocale>
Parameter::Parameter(const QString& name, const QString& unit, const QString &description, int ID, int parentID, const parameter_min_max_val_serial_entry& entry)
{
this->name = name;
this->unit = unit;
this->description = description;
this->ID = ID;
this->parentID = parentID;
type = (parameter_type)entry.type;
min = entry.min;
max = entry.max;
value = entry.value;
if(this->type == parametertype_ptime)
{
Parameter::clipTimeValue(min);
Parameter::clipTimeValue(max);
Parameter::clipTimeValue(value);
}
}
bool Parameter::isValidValue(const QString &valueVar)
{
bool valid = false;
switch(type)
{
case parametertype_double:
{
valueVar.toDouble(&valid);
} break;
case parametertype_uint:
{
valueVar.toUInt(&valid);
} break;
case parametertype_bool:
{
valid = true; //NOTE: This is handled differently, so we don't do anything here
} break;
case parametertype_ptime:
{
valid = true; //NOTE: This is handled differently, so we don't do anything here
} break;
default:
{
//Do nothing
} break;
}
return valid;
}
bool Parameter::setValue(const QString &valueVar)
{
bool changed = false;
switch(type)
{
case parametertype_double:
{
double newVal = valueVar.toDouble();
if(value.val_double != newVal)
{
value.val_double = newVal;
changed = true;
}
} break;
case parametertype_uint:
{
uint64_t newVal = valueVar.toUInt();
if(value.val_uint != newVal)
{
value.val_uint = newVal;
changed = true;
}
} break;
case parametertype_bool:
{
//NOTE! This is handled differently, so we don't set it here.
} break;
case parametertype_ptime:
{
//NOTE! This is handled differently, so we don't set it here.
} break;
default:
{
//Do nothing
} break;
}
return changed;
}
QString Parameter::getValueDisplayString(parameter_value value, parameter_type type, int precision)
{
//This is for displaying the value in the parameter view
switch(type)
{
case parametertype_double:
{
return QString::number(value.val_double, 'g', precision);
} break;
case parametertype_uint:
{
return QString::number(value.val_uint);
} break;
case parametertype_bool:
{
return value.val_bool ? "true" : "false";
} break;
case parametertype_ptime:
{
QDate date = Parameter::valueAsQDate(value);
return QLocale().toString(date, "d. MMMM yyyy");
} break;
default:
{
return "Unsupported type";
} break;
}
return "";
}
QDate Parameter::valueAsQDate(parameter_value value)
{
return QDateTime::fromSecsSinceEpoch(value.val_ptime, Qt::OffsetFromUTC).date();
}
void Parameter::clipTimeValue(parameter_value &value)
{
//NOTE: The range of valid seconds for a QDateTime may vary on different architectures, so if we are unlucky, this clipping may not be good enough.
if(value.val_ptime > std::numeric_limits<qint64>::max() / 10000)
{
value.val_ptime = std::numeric_limits<qint64>::max() / 10000;
}
else if(value.val_ptime < std::numeric_limits<qint64>::min() / 10000)
{
value.val_ptime = std::numeric_limits<qint64>::min() / 10000;
}
}
int Parameter::isNotInRange(const parameter_value& newval)
{
switch(type)
{
case parametertype_double:
{
int result = 0;
if(newval.val_double < min.val_double) result = -1;
if(newval.val_double > max.val_double) result = 1;
return result;
} break;
case parametertype_uint:
{
int result = 0;
if(newval.val_uint < min.val_uint) result = -1;
if(newval.val_uint > max.val_uint) result = 1;
return result;
} break;
case parametertype_bool:
{
return 0;
} break;
case parametertype_ptime:
{
int result = 0;
if(newval.val_ptime < min.val_ptime) result = -1;
if(newval.val_ptime > max.val_ptime) result = 1;
return result;
} break;
default:
{
return -1;
} break;
}
return false;
}
parameter_type Parameter::parseType(QString &string)
{
if(string == "DOUBLE") return parametertype_double;
if(string == "UINT") return parametertype_uint;
if(string == "BOOL") return parametertype_bool;
if(string == "PTIME") return parametertype_ptime;
}