-
Notifications
You must be signed in to change notification settings - Fork 2
/
MinutePicker.qml
133 lines (120 loc) · 3.18 KB
/
MinutePicker.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
import QtQuick 2.7
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3
import QtQuick.Controls.Material 2.0
Rectangle {
id: minutePicker
height: 42
width: parent.width
property int indicatorHeight: 2
property string indicatorColor: "#e91e63"
property int minute
property int divisions: 5
property string textEntered
signal submit()
signal abort()
activeFocusOnTab: true
onActiveFocusChanged: {
textEntered = ""
if (minutePicker.activeFocus) {
Qt.inputMethod.show()
}
}
MouseArea {
id: clickarea
z: 1
propagateComposedEvents: true
anchors.fill: parent
onPressed: {
if (! minutePicker.activeFocus) {
minutePicker.forceActiveFocus()
}
mouse.accepted = false
}
onClicked: mouse.accepted = false;
onReleased: mouse.accepted = false;
onDoubleClicked: mouse.accepted = false;
onPositionChanged: mouse.accepted = false;
onPressAndHold: mouse.accepted = false;
}
Tumbler {
id: minutePickerTumbler
height: parent.width
width: parent.height - parent.indicatorHeight
activeFocusOnTab: false
z: 0
transform: Rotation {
angle: -90
origin.x: minutePickerTumbler.width / 2
origin.y: minutePickerTumbler.width / 2
}
model: 60 / minutePicker.divisions
delegate: Text {
transform: Rotation {
angle: 90
origin.x: width / 2
origin.y: width / 2
}
anchors.right: parent.horizontalCenter
text: ("00" + (modelData * minutePicker.divisions)).slice(-2)
opacity: 0.4 + Math.max(0, 1 - Math.abs(Tumbler.displacement)) * 0.6
font.pixelSize: 15 + Math.max(0, 1 - Math.abs(Tumbler.displacement)) * 3
}
onCurrentIndexChanged: {
var m = minutePickerTumbler.currentIndex * minutePicker.divisions
if (minutePicker.minute != m) {
minutePicker.minute = m
}
}
}
Rectangle {
id: minutePickerIndicator
width: parent.width
height: minutePicker.indicatorHeight
color: parent.activeFocus ? parent.indicatorColor : parent.color
anchors.bottom: parent.bottom
activeFocusOnTab: false
}
onMinuteChanged: {
var m = minutePicker.minute / minutePicker.divisions
if (minutePickerTumbler.currentIndex != m) {
minutePickerTumbler.currentIndex = m
}
}
Keys.onPressed: {
if (event.key == Qt.Key_Return || event.key == Qt.Key_Enter) {
event.accepted = true
minutePicker.submit()
}
else if (event.key == Qt.Key_Escape) {
event.accepted = true
minutePicker.abort()
}
else if (event.text.replace(/[^A-Z0-9]/gi, "").length > 0) {
var foundIndex = searchIndex(event.text)
if (foundIndex > -1) {
minutePickerTumbler.currentIndex = foundIndex
}
}
}
// Search for Index. If nothing is found, starts over with the last entered key
function searchIndex(t) {
minutePicker.textEntered += t
var foundIndex = findIndex(minutePicker.textEntered)
if (foundIndex > -1) {
return foundIndex
}
// If more than one key has been entered, take just the last one and try again
else if (minutePicker.textEntered.length > t.length) {
minutePicker.textEntered = ""
return searchIndex(t)
}
}
// return Index of the first item that contains the given string.
function findIndex(t) {
if (parseInt(t) < 60) {
return Math.round(parseInt(t) / minutePicker.divisions)
}
return -1
}
}