-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlTable.qml
270 lines (244 loc) · 8.77 KB
/
ControlTable.qml
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import QtQuick 2.13
import QtQuick.Controls 2.13
import QtQuick.Layouts 1.13
Item {
property string title
property var fan
property var pctChanges
property var tempChanges
Layout.fillWidth: true
implicitHeight: titleCmp.height + listViewHolder.height
Connections {
target: window
// make sure changes are flushed to config, before writing to device
onBeforeSave: function() {
save()
}
// make sure changes are flushed to config, when back button is pressed
onBeforeClose: function() {
save()
}
}
function load() {
let lastObj = undefined,
revTbl = fan.tbl.slice().reverse();
// loop values backwards, skipping rows that match the previous row
revTbl.forEach(function(obj) {
if (!lastObj || lastObj.pct !== obj.pct || lastObj.temp !== obj.temp) {
tblModel.insert(0, {
temp: obj.temp,
pct: obj.pct
});
}
lastObj = obj;
});
if (!revTbl.length) {
tblModel.append({
temp: 25,
pct: 0
});
}
}
function save() {
// first validate table data, incase change made and save clicked without lossing field focus
validateTable();
let rec, i,
newTbl = [];
// if < 10 rows displayed, populate initial rows with first displayed row's values
for (i = 0; i < (10 - Math.min(Math.max(tblModel.count, 0), 10)); i++) {
if (!rec)
rec = tblModel.get(0);
if (!rec)
break;
newTbl.push({
temp: rec.temp,
pct: rec.pct
});
}
// populate remaining rows
for (i = 0; i < Math.min(Math.max(tblModel.count, 0), 10); i++) {
rec = tblModel.get(i);
newTbl.push({
temp: rec.temp,
pct: rec.pct
});
}
fan.tbl = newTbl;
}
function trackPercentChange(index) {
if (!Array.isArray(pctChanges))
pctChanges = [];
pctChanges.push(index);
}
function trackTemperatureChange(index) {
if (!Array.isArray(tempChanges))
tempChanges = [];
tempChanges.push(index);
}
function validateTable() {
// percents must be in ascending order
if (pctChanges && pctChanges.length) {
// filter unique indexes
pctChanges = pctChanges.filter(function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
});
pctChanges.forEach(function(index) {
if (!tblModel.get(index))
return;
let pct = tblModel.get(index).pct,
rec, i;
// if previous row % > pct, set previous row %s = pct
for (i = 0; i < Math.min(Math.max(index, 0), 10); i++) {
rec = tblModel.get(i);
if (rec.pct > pct) {
tblModel.setProperty(i, "pct", pct);
}
}
// if later row % < pct, set later row % = pct
for (i = index + 1; i < Math.min(Math.max(tblModel.count, 0), 10); i++) {
rec = tblModel.get(i);
if (rec.pct < pct) {
tblModel.setProperty(i, "pct", pct);
}
}
});
pctChanges = undefined;
}
// temperatures must be in ascending order
if (tempChanges && tempChanges.length) {
// filter unique indexes
tempChanges = tempChanges.filter(function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
});
tempChanges.forEach(function(index) {
if (!tblModel.get(index))
return;
let temp = tblModel.get(index).temp,
rec, i;
// if previous row temperature > temp, set previous row temperature = temp
for (i = 0; i < Math.min(Math.max(index, 0), 10); i++) {
rec = tblModel.get(i);
if (rec.temp > temp) {
tblModel.setProperty(i, "temp", temp);
}
}
// if later row temperature < temp, set later row temperature = temp
for (i = index + 1; i < Math.min(Math.max(tblModel.count, 0), 10); i++) {
rec = tblModel.get(i);
if (rec.temp < temp) {
tblModel.setProperty(i, "temp", temp);
}
}
});
tempChanges = undefined;
}
}
Label {
id: titleCmp
text: title
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.bold: true
}
ListModel {
id: tblModel
}
Component {
id: tblDelegate
RowLayout {
Layout.fillWidth: true
anchors.left: parent.left
anchors.right: parent.right
spacing: 10
TextFieldExt {
placeholderText: qsTr("°C")
label: qsTr("°C")
text: temp
onTextChanged: {
if (typeof text !== 'string')
return;
let temp = Number(text),
rec;
if (text.length && (temp !== 0 || text === '0') && !isNaN(temp)) {
if (temp > 131)
temp = 131; // max temp = 131 °C (2^16/500)
else if (temp < 0)
temp = 0; // min temp = 0 °C
tblModel.setProperty(index, "temp", temp);
trackTemperatureChange(index); // track change for validation
}
}
onFocusChanged: {
if (!focus) {
save(); // trigger table validation/update config when focus lost
}
}
}
TextFieldExt {
placeholderText: "%"
label: "%"
text: pct
inputMask: "000;"
inputMethodHints: Qt.ImhDigitsOnly
onTextChanged: {
if (typeof text !== 'string')
return;
let pct = Number(text),
rec;
if (text.length && (pct !== 0 || text === '0') && !isNaN(pct)) {
if (pct > 100)
pct = 100; // max % = 100
else if (pct < 0)
pct = 0; // min % = 0
tblModel.setProperty(index, "pct", pct);
trackPercentChange(index); // track change for validation
}
}
onFocusChanged: {
if (!focus) {
save(); // trigger table validation/update config when focus lost
}
}
}
}
}
Item {
id: listViewHolder
anchors.top: titleCmp.bottom
anchors.left: parent.left
anchors.right: parent.right
anchors.topMargin: 12
height: listView.contentHeight + addEntryBtn.height + 25
ListView {
id: listView
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
implicitHeight: contentHeight
Layout.fillWidth: true
model: tblModel
delegate: tblDelegate
}
Button {
id: addEntryBtn
text: qsTr("Add")
anchors.top: listView.bottom
anchors.right: listView.right
visible: tblModel.count < 10
onClicked: {
if (tblModel.count >= 10) {
console.log('No more rows supported')
return;
}
let last = tblModel.count ? tblModel.get(tblModel.count - 1) : undefined;
tblModel.append({
temp: last ? last.temp : 25,
pct: last ? last.pct : 0
});
}
}
}
}