-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercisesPage.qml
executable file
·305 lines (273 loc) · 10.1 KB
/
ExercisesPage.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import QtQuick
import QtQuick.Controls
import com.chattymango.exercises_connector 1.0
Page {
id: exercisesPage
title: qsTr("Exercises")
height: window.height
width: window.width
property bool listViewDone: false
Column {
anchors.fill: parent
Rectangle {
width: parent.width
height: 60
color: "transparent"
Button {
id: newButton
font.pixelSize: 22
icon.source: "qrc:/icons/plus.svg"
display: AbstractButton.IconOnly
anchors.right: parent.right
anchors.rightMargin: 20
anchors.top: parent.top
anchors.topMargin: 10
background: Rectangle {
radius: 5
color: parent.down ? "#bbbbbb" :
(parent.hovered ? "#d6d6d6" : "transparent")
}
onClicked: exercises_connector.createExercise()
}
}
ListView {
id: reportList
spacing: 5
width: parent.width - 40
height: exercisesPage.height - 60
anchors.left: parent.left
anchors.leftMargin: 20
model: ListModel {
id: reportModel
}
delegate: Rectangle {
id: reportDelegate
height: 50
radius: 5
color: "#eee"
width: reportList.width
Row {
spacing: 5
anchors.fill: parent
anchors.leftMargin: 5
anchors.bottomMargin: 5
anchors.topMargin: 5
anchors.rightMargin: 5
height: 50
property int id: 0
SpinBox {
id: positionField
font.pointSize: 14
height: 50
width: parent.width * 0.1
value: position
from: 1
to: maxPosition
onValueModified: exercises_connector.updateExercise(id, "position", positionField.value)
}
Switch {
id: activeSwitch
text: ""
height: 50
width: parent.width * 0.05
checked: active
onToggled: exercises_connector.updateExercise(id, "active", activeSwitch.checked?1:0)
}
TextField {
id: nameField
text: name
verticalAlignment: Text.AlignVCenter
font.pointSize: 16
font.bold: true
height: 50
width: parent.width * 0.20
placeholderText: qsTr("Name")
onEditingFinished: exercises_connector.updateExercise(id, "name", nameField.text)
onTextEdited: descriptionField.color="darkslateblue"
}
TextField {
id: descriptionField
text: description
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
height: 50
width: parent.width * 0.25
placeholderText: qsTr("Description")
onEditingFinished: exercises_connector.updateExercise(id, "description", descriptionField.text)
onTextEdited: descriptionField.color="darkslateblue"
}
SpinBox {
id: amountField
height: 50
width: parent.width * 0.15
font.pointSize: 14
value: amount
editable: true
from: 1
to: 500
onValueModified: exercises_connector.updateExercise(id, "amount", amountField.value)
}
ComboBox {
id: typeComboBox
height: 50
width: parent.width * 0.15
font.pointSize: 14
textRole: "text"
model: ListModel {
id: typeComboBoxModel
ListElement { key: "repeat"; text: qsTr("times") }
ListElement { key: "sustain"; text: qsTr("seconds") }
}
onCurrentTextChanged: {
if (listViewDone) {
exercises_connector.updateExercise(id, "type", typeComboBox.currentIndex)
}
}
Component.onCompleted: {
if (type === "repeat") {
currentIndex = 0;
} else {
currentIndex = 1;
}
}
}
DelayButton {
delay: 3000
height: 50
width: parent.width * 0.05
opacity: 0.8
contentItem:Image{
source: "qrc:/icons/trash.svg"
height: 15
width: 15
fillMode: Image.PreserveAspectFit
}
onActivated: exercises_connector.deleteExercise(id)
}
}
}
}
}
Component.onCompleted: {
exercises_connector.getExercises();
}
ExercisesConnector {
id: exercises_connector
function sortObject(obj) {
return Object.keys(obj).sort().reduce(function (result, key) {
result[key] = obj[key];
return result;
}, {});
}
function updateExercise(id, name, value) {
if (!id) {return;}
let position;
if (name === "position") {
for(let i = 0; i < reportModel.count; i++) {
const obj1 = reportModel.get(i);
if (obj1.id === id) {
position = obj1[name];
obj1[name] = value;
reportModel.set(i, obj1);
}
}
for(let j = 0; j < reportModel.count; j++) {
const obj2 = reportModel.get(j);
if (obj2.id !== id && obj2[name] === value) {
obj2[name] = position;
reportModel.set(j, obj2);
break;
}
}
} else {
let found = false;
for(let k = 0; k < reportModel.count; k++) {
const obj3 = reportModel.get(k);
if (obj3.id === id) {
found = true;
if (name === "type") {
if (value === 1) {
obj3.type = "sustain";
} else {
obj3.type = "repeat";
}
} else {
obj3[name] = value;
}
reportModel.set(k, obj3);
break;
}
}
if (!found) {
return;
}
}
let returnArray = [];
for(let l = 0; l < reportModel.count; l++) {
returnArray.push(reportModel.get(l));
}
updateExercises(JSON.stringify(returnArray));
}
onDataSent:
data => {
listViewDone = false;
const dataObj = sortObject(JSON.parse(data));
const maxPosition = Object.keys(dataObj).length;
reportModel.clear();
for (var key in dataObj) {
let type;
reportModel.append({
"id": dataObj[key].id,
"position": dataObj[key].position,
"active": dataObj[key].active,
"name": dataObj[key].name,
"description": dataObj[key].description,
"amount": dataObj[key].amount,
"type": dataObj[key].type,
"maxPosition": maxPosition,
});
}
comboTimer.start();
}
onDataSaved: {
savedPopup.open();
popupTimer.start();
}
}
Popup {
id: savedPopup
x: 100
y: 100
width: parent.width - 200
height: parent.height - 200
anchors.centerIn: parent
modal: true
focus: true
closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
Column {
spacing: 10
anchors.centerIn: parent
Label {
text: qsTr("The data was saved.")
font.pixelSize: 36
font.bold: true
}
}
}
Timer {
id: popupTimer
interval: 2000
running: false
repeat: false
onTriggered: savedPopup.close()
triggeredOnStart: false
}
Timer {
id: comboTimer
interval: 1000
running: false
repeat: false
onTriggered: ()=>{listViewDone = true}
triggeredOnStart: false
}
}