forked from papyros/qml-material
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimePicker.qml
More file actions
579 lines (486 loc) · 18.7 KB
/
Copy pathTimePicker.qml
File metadata and controls
579 lines (486 loc) · 18.7 KB
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
/*
* QML Material - An application framework implementing Material Design.
*
* Copyright (C) 2015 Steve Coffey <scoffey@barracuda.com>
* 2015 Jordan Neidlinger <JNeidlinger@gmail.com>
* 2016 Michael Spencer <sonrisesoftware@gmail.com>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import QtQuick 2.4
import Material 0.3
import Material.Extras 0.1
import QtQuick.Controls 1.3 as QuickControls
import QtQuick.Controls.Styles 1.3
FocusScope {
id: timePicker
width: 300 * Units.dp
height: content.height
/*!
Set to \c true if selection is 24 hour based. Defaults to false
*/
property bool prefer24Hour: false
/*!
The visual padding around the clock element
*/
property real clockPadding: 24 * Units.dp
/*!
Set to \c true if the time picker should first show the hours. Defaults to true
*/
property bool isHours: true
/*!
Sets the bottom margin for the time picker
*/
property int bottomMargin: 0
Keys.onUpPressed: {
var date = internal.timePicked
if(isHours)
date.setHours(internal.timePicked.getHours() + 1)
else
date.setMinutes(internal.timePicked.getMinutes() + 1)
internal.timePicked = date
}
Keys.onDownPressed: {
var date = internal.timePicked
if(isHours)
date.setHours(internal.timePicked.getHours() - 1)
else
date.setMinutes(internal.timePicked.getMinutes() - 1)
internal.timePicked = date
}
Keys.onLeftPressed: {
if(!isHours)
setIsHours(true)
}
Keys.onRightPressed: {
if(isHours)
setIsHours(false)
}
property string __digitsPressed
Keys.onDigit0Pressed: setDigitsPressed(0)
Keys.onDigit1Pressed: setDigitsPressed(1)
Keys.onDigit2Pressed: setDigitsPressed(2)
Keys.onDigit3Pressed: setDigitsPressed(3)
Keys.onDigit4Pressed: setDigitsPressed(4)
Keys.onDigit5Pressed: setDigitsPressed(5)
Keys.onDigit6Pressed: setDigitsPressed(6)
Keys.onDigit7Pressed: setDigitsPressed(7)
Keys.onDigit8Pressed: setDigitsPressed(8)
Keys.onDigit9Pressed: setDigitsPressed(9)
QtObject {
id: internal
property bool resetFlag: false
property date timePicked
property bool completed: false
onTimePickedChanged: {
if(completed) {
var hours = timePicked.getHours()
if(hours > 11 && !prefer24Hour){
hours -= 12
amPmPicker.isAm = false
} else {
amPmPicker.isAm = true
}
hoursPathView.currentIndex = hours
var minutes = internal.timePicked.getMinutes()
minutesPathView.currentIndex = minutes
}
}
}
Component.onCompleted: {
internal.completed = true
internal.timePicked = new Date(Date.now())
forceActiveFocus()
}
Column {
id:content
height: childrenRect.height + bottomMargin
width: parent.width
Rectangle {
id: headerView
width: parent.width
height: 88 * Units.dp
color: Theme.accentColor
Row {
id: timeContainer
anchors.centerIn: parent
height: 48 * Units.dp
Label {
id:hoursLabel
style: "display3"
color: isHours ? "white" : "#99ffffff"
text: internal.timePicked.getHours()
anchors.verticalCenter: parent.verticalCenter
MouseArea {
anchors.fill: parent
onClicked: {
if(!isHours){
setIsHours(true)
}
}
}
}
Label {
style: "display3"
color: "white"
text:":"
anchors.verticalCenter: parent.verticalCenter
}
Label {
id: minutesLabel
style: "display3"
color: !isHours ? "white" : "#99ffffff"
text: internal.timePicked.getMinutes() < 10 ? "0" + internal.timePicked.getMinutes() : internal.timePicked.getMinutes()
anchors.verticalCenter: parent.verticalCenter
MouseArea {
anchors.fill: parent
onClicked: {
if(isHours){
setIsHours(false)
}
}
}
}
}
Column {
id: amPmPicker
visible: !prefer24Hour
property bool isAm: true
anchors {
bottom: timeContainer.bottom
left: timeContainer.right
leftMargin: 12 * Units.dp
}
spacing: 4 * Units.dp
Label {
style: "subheading"
font.weight: Font.DemiBold
color: amPmPicker.isAm ? "white" : "#99ffffff"
text: "AM"
MouseArea {
anchors.fill: parent
onClicked: amPmPicker.isAm = true
}
}
Label {
style: "subheading"
font.weight: Font.DemiBold
color: !amPmPicker.isAm ? "white" : "#99ffffff"
text: "PM"
MouseArea {
anchors.fill: parent
onClicked: amPmPicker.isAm = false
}
}
}
}
Rectangle {
id: picker
width: parent.width
height: width
color: "white"
Rectangle {
id: circle
color: "#eee"
anchors.centerIn: parent
width: parent.width * 0.9
height: width
radius: width / 2
Rectangle {
id: centerPoint
anchors.centerIn: parent
color: Theme.accentColor
width: 8 * Units.dp
height: 8 * Units.dp
radius: width / 2
}
Rectangle {
id: pointer
color: Theme.accentColor
width: 2 * Units.dp
height: circle.height / 2 - clockPadding
y: clockPadding
anchors.horizontalCenter: parent.horizontalCenter
antialiasing: true
transformOrigin: Item.Bottom
Connections {
target: hoursPathView
onCurrentIndexChanged: {
if(isHours)
pointer.setAngle()
}
}
Connections {
target: minutesPathView
onCurrentIndexChanged: {
if(!isHours)
pointer.setAngle()
}
}
Connections {
target: timePicker
onIsHoursChanged: pointer.setAngle()
}
function setAngle()
{
var idx = isHours ? hoursPathView.currentIndex : minutesPathView.currentIndex
var angle
if(isHours)
angle = (360 / ((prefer24Hour) ? 24 : 12)) * idx
else
angle = 360 / 60 * idx
if(Math.abs(pointer.rotation - angle) == 180)
pointerRotation.direction = RotationAnimation.Clockwise
else
pointerRotation.direction = RotationAnimation.Shortest
pointer.rotation = angle
}
Behavior on rotation {
RotationAnimation {
id: pointerRotation
duration: 200
direction: RotationAnimation.Shortest
}
}
}
Component {
id: pathViewHighlight
Rectangle {
id: highlight
width: 40 * Units.dp
height: 40 * Units.dp
color: Theme.accentColor
radius: width / 2
}
}
Component {
id: pathViewItem
Rectangle {
id: rectangle
width: !isHours && modelData % 5 == 0 ? 12 * Units.dp : isHours ? 30 * Units.dp : 8 * Units.dp
height: !isHours && modelData % 5 == 0 ? 12 * Units.dp : isHours ? 30 * Units.dp : 8 * Units.dp
color: "transparent"
property bool isSelected: false
Label {
anchors.centerIn: parent
text:{
var model = isHours ? hoursPathView.model : minutesPathView.model
return model.data < 10 && !isHours ? "0" + modelData : modelData
}
visible: modelData >= 0 && (isHours ? true : modelData % 5 == 0)
style: "subheading"
}
Connections {
target: parentMouseArea
onClicked: {
checkClick(false)
}
onPositionChanged: {
checkClick(true)
}
function checkClick(isPress)
{
if((isPress ? parentMouseArea.leftButtonPressed : true) && rectangle.visible) {
var thisPosition = rectangle.mapToItem(null, 0, 0, width, height)
if(parentMouseArea.globalX > thisPosition.x &&
parentMouseArea.globalY > thisPosition.y &&
parentMouseArea.globalX < (thisPosition.x + width) &&
parentMouseArea.globalY < (thisPosition.y + height)) {
if(!rectangle.isSelected) {
rectangle.isSelected = true
var newDate = new Date(internal.timePicked) // Grab a new date from existing
var time = parseInt(modelData)
if(isHours) {
if(!prefer24Hour && !amPmPicker.isAm && time < 12) {
time += 12
}
else if(!prefer24Hour && amPmPicker.isAm && time === 12) {
time = 0
}
newDate.setHours(time)
} else {
newDate.setMinutes(time)
}
internal.timePicked = newDate
}
}
else {
rectangle.isSelected = false
}
}
}
}
}
}
MouseArea {
property bool leftButtonPressed
property int globalX
property int globalY
id: parentMouseArea
anchors.fill: circle
hoverEnabled: true
onClicked: {
globalX = parentMouseArea.mapToItem(null, mouse.x, mouse.y).x
globalY = parentMouseArea.mapToItem(null, mouse.x, mouse.y).y
}
onPositionChanged: {
if(containsPress)
{
leftButtonPressed = true
globalX = parentMouseArea.mapToItem(null, mouse.x, mouse.y).x
globalY = parentMouseArea.mapToItem(null, mouse.x, mouse.y).y
}
else
{
leftButtonPressed = false
}
}
}
PathView {
id: hoursPathView
anchors.fill: parent
visible: isHours
model: {
var limit = prefer24Hour ? 24 : 12
var zeroBased = prefer24Hour
return getTimeList(limit, zeroBased)
}
highlightRangeMode: PathView.NoHighlightRange
highlightMoveDuration: 200
onCurrentIndexChanged: {
var newText = currentIndex
if(currentIndex == 0 && !prefer24Hour)
newText = 12
hoursLabel.text = newText
}
delegate: pathViewItem
interactive: false
highlight: pathViewHighlight
path: Path {
startX: circle.width / 2
startY: clockPadding
PathArc {
x: circle.width / 2
y: circle.height - clockPadding
radiusX: circle.width / 2 - clockPadding
radiusY: circle.width / 2 - clockPadding
useLargeArc: false
}
PathArc {
x: circle.width / 2
y: clockPadding
radiusX: circle.width / 2 - clockPadding
radiusY: circle.width / 2 - clockPadding
useLargeArc: false
}
}
}
PathView {
id: minutesPathView
anchors.fill: parent
visible: !isHours
model: {
return getTimeList(60, true)
}
highlightRangeMode: PathView.NoHighlightRange
highlightMoveDuration: 200
delegate: pathViewItem
highlight: pathViewHighlight
interactive: false
path: Path {
startX: circle.width / 2
startY: clockPadding
PathArc {
x: circle.width / 2
y: circle.height - clockPadding
radiusX: circle.width / 2 - clockPadding
radiusY: circle.width / 2 - clockPadding
useLargeArc: false
}
PathArc {
x: circle.width / 2
y: clockPadding
radiusX: circle.width / 2 - clockPadding
radiusY: circle.width / 2 - clockPadding
useLargeArc: false
}
}
}
}
}
}
/**
Switches contexts
If previously we hadn't set hours, and we're now switching contexts, disable the auto switch to minutes
*/
function setIsHours(_isHours) {
if(_isHours == isHours)
return
if(!internal.resetFlag)
internal.resetFlag = true
var prevRotation = pointerRotation.duration
pointerRotation.duration = 0
isHours = _isHours
pointerRotation.duration = prevRotation
}
function getCurrentTime() {
var date = new Date(internal.timePicked)
if(amPmPicker.isAm && date.getHours() > 11)
date.setHours(date.getHours() - 12)
else if(!amPmPicker.isAm && date.getHours() < 11)
date.setHours(date.getHours() + 12)
return date
}
/*!
\internal
Resets the view after closing
*/
function reset() {
isHours = true
internal.resetFlag = false
amPmPicker.isAm = true
internal.timePicked = new Date(Date.now())
}
/*!
\internal
Provides list of ints for pathview based on the context and user prefs
*/
function getTimeList(limit, isZeroBased) {
var items = []
if(!isZeroBased) {
items[0] = limit
}
var start = isZeroBased ? 0 : 1
var jump = limit > 24 ? 1 : 1
for(var i = start; i < limit; i += jump) {
items[i / jump] = i
}
return items
}
/*!
\internal
*/
function setDigitsPressed(minute)
{
if(__digitsPressed.length > 1) {
__digitsPressed = ""
__digitsPressed = minute
}
else {
__digitsPressed += minute
}
var date = internal.timePicked
var value = parseInt(__digitsPressed)
if((isHours && value > 23) || (!isHours && value > 59)) {
__digitsPressed = ""
return
}
if(isHours)
date.setHours(value)
else
date.setMinutes(value)
internal.timePicked = date
}
}