-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerLogic.qml
262 lines (220 loc) · 6.85 KB
/
TimerLogic.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
import QtQuick 2.0
TimerElement {
property double timeLeft: 10 // Time left in seconds
property bool paused: false
property bool overtime: false
property int timeStep: 20 // How frequently to update the timer
property int scaleFactor: 3
property bool hideSequence: false
signal hideTimer
// ----- JavaScript Functions ----- \\
Component.onCompleted: {
changeTimerHeight()
}
onVisibleChanged: {
changeTimerHeight()
}
function changeTimerHeight(){
// Changes timer heights so that when visibility is turned on, they "jump" onto the screen
if (visible){
timerMargins = 0
}
else{
timerMargins = 400
}
}
function scaleGraphic(factor){
switch (factor){
case 1:
return 2
case 2:
return 2
case 3:
return 2
case 4:
return 2
case 5:
return 3
case 6:
return 3
}
}
function updateText()
{
// Converts timeLeft variable to hh:mm:ss and displays it on the label
var date = new Date(null) // Date object for conversion
// Set displayed time. Add a negative sign if we're overtime
if (overtime){
date.setSeconds(timeLeft); // Show 1 second more than current time so that the timer appears to stop on zero
txtTime.text = "-" + date.toISOString().substr(11, 8);
radTime.minValue = 0
radTime.maxValue = 100
radTime.value = 20
radTime.progressColor = "red"
finishChaser.running = true
}
else{
date.setSeconds(timeLeft + 1); // Show 1 second more than current time so that the timer appears to stop on zero
txtTime.text = date.toISOString().substr(11, 8);
// Also update the graphicContainerHeight
radTime.value = timeLeft
}
}
function startTimer()
{
// Gets the time values from the tumblers and starts the timer
timeLeft = tmbSecond.currentIndex + tmbMinute.currentIndex*60 + tmbHour.currentIndex*60*60;
radTime.maxValue = timeLeft
overtime = false
// Show the new countdown time on the label
updateText();
paused = false
// Change the pause button's icon to pause symbol
pauseIconPath = "assets/timer/pause.svg"
// Hide start button and show text and pause button
btnStart.visible = false
btnPause.visible = true
txtTime.visible = true
// Adjust the graphic's height
graphicContainerHeight = parent.height/scaleGraphic(scaleFactor)
// Reset colours and animation
resetAnimations()
// Start the countdown timer
countdown.running = true
}
function resetAnimations(){
finishChaser.running = false
radTime.startAngle = 180
radTime.progressColor = "#00ffc1"
radTime.value = radTime.maxValue // Show bar as full
}
function stopTimer()
{
// If we were finished then reset everything and display the start button
timeLeft = 0
btnStart.visible = true
btnPause.visible = false
txtTime.visible = false
overtime = false
countdown.running = false
graphicContainerHeight = parent.height/5
resetAnimations()
}
// ----- QML Objects -----\\
Timer{
id: countdown
interval: timeStep
running: false
repeat: true
onTriggered:{
if (!overtime){
// Decerement the time if we haven't gone overtime yet
timeLeft -= timeStep/1000;
}
if (timeLeft < 0){
// We're now overtime. The original timer is finished
overtime = true
// Change icon to display a tick to finish the timer
pauseIconPath = "assets/timer/tick.svg"
// Increment the timer to set it back to zero
timeLeft += timeStep/1000
}
if (overtime){
// If we're overtime, then start incrementing the timer to show how long overtime we are
timeLeft += timeStep/1000
}
// Update the displayed text showing the time left (or overtime)
updateText();
}
}
// ----- QML Animators -----\\
// When the circular graphic size changes, animate that change
Behavior on graphicContainerHeight {
NumberAnimation{
duration: 500
easing {type: Easing.OutBack; overshoot: 5}
}
}
Behavior on timerMargins {
NumberAnimation{
duration: 500
easing {type: Easing.OutBack; overshoot: 5}
}
}
// Behavior on width {
// NumberAnimation{
// duration: 500
// easing {type: Easing.OutBack; overshoot: 5}
// }
// }
PropertyAnimation{
id: finishChaser
target: radTime
property: "startAngle"
from: 0
to: 360
duration: 500
running: false
loops: Animation.Infinite
}
// ----- QML Signal Handlers -----\\
onTimerMarginsChanged: {
if (hideSequence && timerMargins == 1000){
visible = false
hideTimer()
}
}
onScaleFactorChanged: {
if (countdown.running){
graphicContainerHeight = parent.height/scaleGraphic(scaleFactor)
}
}
btnHide.onClicked: {
if (countdown.running)
{
// If the timer is running, then stop
stopTimer()
}
else
{
// Hide this timer when the X button is pressed if the timer isn't running
hideSequence = true
timerMargins = 1000
// visible = false
// hideTimer()
}
}
btnStart.onClicked: {
// Get the required time and start timer
startTimer()
}
btnPause.onClicked: {
// Toggle between paused and unpaused states
paused = !paused;
countdown.running = !paused
if (overtime){
stopTimer()
}
else if (paused){
// If we're currently paused then show the play/resume symbol on the button
pauseIconPath = "assets/timer/play.svg"
}
else{
// If we're currently running then show the pause symbol on the button
pauseIconPath = "assets/timer/pause.svg"
}
}
btnRestart.onClicked: {
overtime = false
startTimer()
}
btnStop.onClicked: {
stopTimer()
}
// txtName.Keys.onReturnPressed: backgroundMouse.forceActiveFocus()
}
/*##^##
Designer {
D{i:0;autoSize:true;height:480;width:640}
}
##^##*/