-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.qml
798 lines (734 loc) · 29 KB
/
main.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Dialogs
import QtCore
import Pohles
ApplicationWindow {
id: root
width: 640
height: 480
visible: true
title: qsTr("PohLes administration v3")
minimumWidth: 640
minimumHeight: 480
property string apiUrl: "https://api.pohles.rudickamladez.cz/"
property string keycloak_url: "https://auth.lukasmatuska.cz/"
property string keycloak_client_id: "admin-v3"
property string keycloak_client_secret: ""
property string username: ""
property string password: ""
property var session
property bool validSession: root.session !== undefined && root.session.access_token !== undefined && root.session.refresh_token !== undefined
property int paid: 0
property int free: 0
property int reserved: 0
property int total: 0
property int cancelled: 0
footer: Label {
text: "DEBUG"
visible: root.debug
}
Component.onCompleted: {
loginPopup.open()
}
property bool debug: false
Settings {
id: settings
property alias username: root.username
property alias password: root.password
property alias keycloak_client_secret: root.keycloak_client_secret
}
TicketModel {
id: ticketModel
}
TicketFilterModel {
id: ticketFilterModel
sourceModel: ticketModel
query: searchField.text
}
Shortcut {
sequence: "Ctrl+D"
onActivated: root.debug = !root.debug
}
onSessionChanged: {
if (root.session !== undefined && root.session.access_token !== undefined && root.session.refresh_token !== undefined) {
loginPopup.close()
root.getTime()
root.getTicket()
} else {
console.log("cannot retrieve authentication tokens")
loginPopup.open()
}
}
function getAuthTokens() {
let request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (root.debug) {
console.log(request.responseText)
}
root.session = JSON.parse(request.responseText)
}
}
request.open("POST", root.keycloak_url+"realms/pohles/protocol/openid-connect/token", true)
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
request.setRequestHeader("Access-Control-Allow-Origin", true)
request.send("client_id="+root.keycloak_client_id+"&client_secret="+root.keycloak_client_secret+"&grant_type=password&username="+root.username+"&password="+root.password)
}
function getTime() {
let request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState === 4) {
var text = request.responseText;
if (root.debug) {
console.log(text);
}
const timeArray = JSON.parse(text)
timeModel.clear()
for (let i = 0; i < timeArray.length; ++i) {
timeArray[i]["occupiedPositions"] = 0
timeArray[i]["freePositions"] = 0
timeModel.append(timeArray[i])
root.getTimeAvailableId(timeArray[i]["id"])
}
}
}
request.open("GET", root.apiUrl+"time", true);
request.setRequestHeader("Access-Control-Allow-Origin", true)
request.send();
}
function getTimeAvailableId(id) {
let request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState === 4) {
var text = request.responseText;
if (root.debug) {
console.log(text);
}
let obj = JSON.parse(text)
for (let i = 0; i < timeModel.rowCount(); ++i) {
if (timeModel.get(i)["id"] === obj["_id"]) {
timeModel.setProperty(i, "occupiedPositions", obj["occupiedPositions"])
timeModel.setProperty(i, "freePositions", obj["freePositions"])
break
}
}
}
}
request.open("GET", root.apiUrl+"time/available/"+id, true);
request.setRequestHeader("Access-Control-Allow-Origin", true)
request.send();
}
function getTicket() {
let request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState === 4) {
var text = request.responseText;
if (root.debug) {
console.log(text);
}
ticketModel.loadFromJson(text)
}
}
request.open("GET", root.apiUrl+"ticket", true);
request.setRequestHeader("Content-Type", "application/json")
request.setRequestHeader("Authorization", "Bearer "+root.session.access_token)
request.setRequestHeader("Access-Control-Allow-Origin", true)
request.send();
}
function patchTicketId(ticketObject) {
let request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (root.debug) {
console.log(request.responseText)
}
root.getTicket()
}
}
request.open("PATCH", root.apiUrl+"ticket/"+ticketObject.id, true);
request.setRequestHeader("accept", "application/json")
request.setRequestHeader("Content-Type", "application/json")
request.setRequestHeader("Authorization", "Bearer "+root.session.access_token)
request.setRequestHeader("Access-Control-Allow-Origin", true)
request.send(JSON.stringify(ticketObject));
}
function deleteTicketId(ticketID) {
let request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (root.debug) {
console.log(request.responseText)
}
root.getTicket()
}
}
request.open("DELETE", root.apiUrl+"ticket/"+ticketID, true)
request.setRequestHeader("accept", "application/json")
request.setRequestHeader("Authorization", "Bearer "+root.session.access_token)
request.setRequestHeader("Access-Control-Allow-Origin", true)
request.send()
}
function postTicketEasy(newTicket) {
let request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (root.debug) {
console.log(request.responseText)
}
root.getTicket()
}
}
request.open("POST", root.apiUrl+"ticket/easy", true)
request.setRequestHeader("accept", "application/json")
request.setRequestHeader("Content-Type", "application/json")
request.setRequestHeader("Authorization", "Bearer "+root.session.access_token)
request.setRequestHeader("Access-Control-Allow-Origin", true)
request.send(JSON.stringify(newTicket))
}
function getTimeActiveSum() {
let request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (root.debug) {
console.log(request.responseText)
}
let stats = JSON.parse(request.responseText)
root.paid = stats.paid
root.free = stats.free
root.reserved = stats.reserved
root.total = stats.total
statsPopup.open()
}
}
request.open("GET", root.apiUrl+"time/active/sum", true)
request.setRequestHeader("accept", "application/json")
request.setRequestHeader("Access-Control-Allow-Origin", true)
request.send()
}
ListModel {
id: timeModel
}
ItemSelectionModel {
id: timeSelectionModel
model: timeModel
}
ItemSelectionModel {
id: ticketSelectionModel
model: ticketFilterModel
}
Component {
id: deleteDialogComponent
Dialog {
x: (parent.width-width)/2
y: (parent.height-height)/2
parent: Overlay.overlay
width: deleteLabel.width+horizontalPadding*2
height: deleteLabel.height+verticalPadding*2+50
standardButtons: Dialog.Yes | Dialog.No
property string time: ""
property string first: ""
property string last: ""
property string email: ""
property string ticketID: ""
Label {
id: deleteLabel
text: "Opravdu smazat rezervaci?\n"+time+"\n"+first+" "+last+"\n"+email
}
onAccepted: {
root.deleteTicketId(ticketID)
close()
}
onRejected: {
close()
}
onClosed: {
time = ""
first = ""
last = ""
email = ""
ticketID = ""
deleteDialogLoader.active = false
}
}
}
Loader {
id: deleteDialogLoader
active: false
sourceComponent: deleteDialogComponent
}
Popup {
id: statsPopup
modal: true
x: (parent.width-width)/2
y: (parent.height-height)/2
Label {
id: statsLabel
text: "Volno:\t"+root.free+"\nRezervace:\t"+root.reserved+"\nKapacita:\t"+root.total
}
}
Popup {
id: loginPopup
width: parent.width*0.8
height: parent.height*0.8
x: (parent.width-width)/2
y: (parent.height-height)/2
closePolicy: Popup.NoAutoClose
modal: true
GridLayout {
anchors.fill: parent
columns: 2
Label {
text: "Přihlášení"
horizontalAlignment: Text.AlignHCenter
Layout.columnSpan: 2
Layout.fillWidth: true
}
Label {
text: "Login"
}
TextField {
id: loginField
text: root.username
Layout.fillWidth: true
}
Label {
text: "Heslo"
}
TextField {
id: passwordField
text: root.password
echoMode: TextInput.Password
Layout.fillWidth: true
}
Label {
text: "Token"
}
TextField {
id: tokenField
text: root.keycloak_client_secret
echoMode: TextInput.Password
Layout.fillWidth: true
}
RowLayout {
Layout.columnSpan: 2
Layout.fillWidth: true
Item { Layout.fillWidth: true }
Button {
text: "Přihlásit"
enabled: loginField.text != "" && passwordField.text != "" && tokenField.text != ""
onClicked: {
root.username = loginField.text
root.password = passwordField.text
root.keycloak_client_secret = tokenField.text
root.getAuthTokens()
}
}
Item { Layout.fillWidth: true }
}
}
}
Component {
id: addTicketComponent
Popup {
parent: Overlay.overlay
modal: true
width: parent.width*0.8
height: parent.height*0.8
x: (Overlay.overlay.width-width)/2
y: (Overlay.overlay.height-height)/2
GridLayout {
id: addTicketGrid
columns: 2
anchors.fill: parent
Label {
text: "Vytvořit rezervaci"
horizontalAlignment: Text.AlignHCenter
Layout.columnSpan: 2
Layout.fillWidth: true
}
Label {
text: "Jméno"
}
TextField {
id: addFirstName
Layout.fillWidth: true
}
Label {
text: "Příjmení"
}
TextField {
id: addlastName
Layout.fillWidth: true
}
Label {
text: "Email"
}
TextField {
id: addEmail
Layout.fillWidth: true
}
Label {
text: "Čas"
}
ComboBox {
id: addTime
model: timeModel
valueRole: "id"
textRole: "name"
}
RowLayout {
Layout.columnSpan: 2
Layout.fillWidth: true
Item {
Layout.fillWidth: true
}
Button {
text: "Přidat"
enabled: addFirstName.text !== "" &&
addlastName.text !== "" &&
addEmail.text !== "" &&
addTime.currentValue !== ""
onClicked: {
let object = {
name: {
first: addFirstName.text,
last: addlastName.text
},
email: addEmail.text,
time: addTime.currentValue
}
root.postTicketEasy(object)
addTicketLoader.item.close()
}
}
Item {
Layout.fillWidth: true
}
}
}
onClosed: {
addTicketLoader.active = false
}
}
}
Loader {
id: addTicketLoader
active: false
sourceComponent: addTicketComponent
}
RowLayout {
anchors.fill: parent
ColumnLayout {
Layout.fillWidth: false
Layout.fillHeight: true
Button {
text: "Rezervace"
onClicked: {
stack.currentIndex = 0
root.getTicket()
}
}
Button {
text: "Časy"
onClicked: {
stack.currentIndex = 1
root.getTime()
}
}
Item {
Layout.fillHeight: true
}
}
StackLayout {
id: stack
ColumnLayout {
Item {
Layout.fillHeight: true
Layout.maximumHeight: 10
}
RowLayout {
Button {
text: "Přidat..."
icon.source: "qrc:/icons/add.svg"
onClicked: {
addTicketLoader.active = true
addTicketLoader.item.open()
}
}
RoundButton {
icon.source: "qrc:/icons/info.svg"
onClicked: root.getTimeActiveSum()
}
TextField {
id: searchField
placeholderText: "vyhledávání..."
Layout.fillWidth: true
Layout.fillHeight: false
}
}
ListView {
id: ticketView
clip: true
model: ticketFilterModel
Layout.fillWidth: true
Layout.fillHeight: true
delegate: ItemDelegate {
width: ListView.view.width
text: timeRole+" "+firstNameRole+" "+lastNameRole+" "+emailRole
onClicked: {
ticketSelectionModel.select(ticketFilterModel.index(index,0), ItemSelectionModel.ClearAndSelect)
}
onDoubleClicked: {
ticketEditLoader.active = true
ticketEditLoader.item.open()
}
Component.onCompleted: {
highlighted = ticketSelectionModel.isSelected(ticketFilterModel.index(index,0))
}
Connections {
target: ticketSelectionModel
function onSelectionChanged(selected, deselected) {
highlighted = ticketSelectionModel.isSelected(ticketFilterModel.index(index,0))
}
}
TapHandler {
acceptedButtons: Qt.RightButton
onTapped: (eventPoint) => {
ticketSelectionModel.select(ticketFilterModel.index(index,0), ItemSelectionModel.ClearAndSelect)
let p = mapToItem(Overlay.overlay, eventPoint.position)
ticketContextMenuLoader.active = true
ticketContextMenuLoader.item.x = p.x
ticketContextMenuLoader.item.y = p.y
ticketContextMenuLoader.item.open()
}
}
Component {
id: ticketDialog
Dialog {
parent: Overlay.overlay
width: parent.width*0.8
height: ticketGrid.preferredHeight
x: (Overlay.overlay.width-width)/2
y: (Overlay.overlay.height-height)/2
standardButtons: Dialog.Cancel | Dialog.Ok
GridLayout {
id: ticketGrid
columns: 2
anchors.fill: parent
RowLayout {
Layout.columnSpan: 2
Layout.fillWidth: true
Item {Layout.fillWidth: true}
Switch {
id: editSwitch
text: "Upravit"
checked: false
}
Item {Layout.fillWidth: true}
}
Label {
text: "Jméno"
}
TextField {
id: firstNameField
text: firstNameRole
enabled: editSwitch.checked
Layout.fillWidth: true
}
Label {
text: "Příjmení"
}
TextField {
id: lastNameField
text: lastNameRole
enabled: editSwitch.checked
Layout.fillWidth: true
}
Label {
text: "Email"
}
TextField {
id: emailField
text: emailRole
enabled: editSwitch.checked
Layout.fillWidth: true
}
Label {
text: "Čas"
}
ComboBox {
id: timeComboBox
enabled: editSwitch.checked
model: timeModel
valueRole: "id"
textRole: "name"
Component.onCompleted: {
timeComboBox.currentIndex = timeComboBox.indexOfValue(timeIdRole)
}
}
}
onClosed: {
ticketEditLoader.active = false
}
onAccepted: {
let object = {
id: idRole,
name: {
first: firstNameField.text,
last: lastNameField.text
},
email: emailField.text,
time: timeComboBox.currentValue
}
root.patchTicketId(object)
}
}
}
Loader {
id: ticketEditLoader
active: false
sourceComponent: ticketDialog
}
Component {
id: ticketContextMenu
Menu {
parent: Overlay.overlay
MenuItem {
text: "Upravit"
icon.source: "qrc:/icons/edit.svg"
onClicked: {
ticketEditLoader.active = true
ticketEditLoader.item.open()
}
}
MenuItem {
text: "Smazat"
icon.source: "qrc:/icons/delete.svg"
onClicked: {
deleteDialogLoader.active = true
deleteDialogLoader.item.time = timeRole
deleteDialogLoader.item.first = firstNameRole
deleteDialogLoader.item.last = lastNameRole
deleteDialogLoader.item.email = emailRole
deleteDialogLoader.item.ticketID = idRole
deleteDialogLoader.item.open()
}
}
onClosed: {
ticketContextMenuLoader.active = false
}
}
}
Loader {
id: ticketContextMenuLoader
active: false
sourceComponent: ticketContextMenu
}
}
add: Transition {
NumberAnimation {
property: "opacity"
from: 0.0
to: 1.0
duration: 200
}
NumberAnimation {
property: "opacity"
to: 1.0
duration: 0
}
}
}
}
ListView {
id: timeView
model: timeModel
clip: true
delegate: ItemDelegate {
width: ListView.view.width
text: name+" "+occupiedPositions+"/"+maxCountOfTickets
onClicked: {
root.getTimeAvailableId(id)
timeSelectionModel.select(timeModel.index(index,0), ItemSelectionModel.ClearAndSelect)
}
Component.onCompleted: {
highlighted = timeSelectionModel.isSelected(timeModel.index(index,0))
}
TapHandler {
acceptedButtons: Qt.RightButton
onTapped: (eventPoint) => {
root.getTimeAvailableId(id)
timeSelectionModel.select(timeModel.index(index,0), ItemSelectionModel.ClearAndSelect)
let p = mapToItem(Overlay.overlay, eventPoint.position)
timeContextMenuLoader.active = true
timeContextMenuLoader.item.x = p.x
timeContextMenuLoader.item.y = p.y
timeContextMenuLoader.item.open()
}
}
Connections {
target: timeSelectionModel
function onSelectionChanged(selected, deselected) {
highlighted = timeSelectionModel.isSelected(timeModel.index(index,0))
}
}
Rectangle {
anchors.left: parent.left
anchors.top: parent.top
anchors.bottom: parent.bottom
color: pickColor(occupiedPositions, maxCountOfTickets)
width: occupiedPositions > 0 ? (occupiedPositions/maxCountOfTickets)*parent.width : 0
function pickColor(occupiedPositions, maxCountOfTickets) {
if (maxCountOfTickets === 0) {
return "transparent"
} else if (occupiedPositions/maxCountOfTickets < 0.5) {
return "green"
} else if (occupiedPositions === maxCountOfTickets) {
return "crimson"
} else {
return "goldenrod"
}
}
}
Component {
id: timeContextMenu
Menu {
parent: Overlay.overlay
MenuItem {
text: "Upravit"
icon.source: "qrc:/icons/edit.svg"
}
MenuItem {
text: "Smazat"
icon.source: "qrc:/icons/delete.svg"
}
}
}
Loader {
id: timeContextMenuLoader
active: false
sourceComponent: timeContextMenu
}
}
add: Transition {
NumberAnimation {
property: "opacity"
from: 0.0
to: 1.0
duration: 200
}
}
Label {
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
wrapMode: Text.WordWrap
visible: parent.count === 0
text: "Žádné časy k zobrazení"
}
}
}
}
}