-
Notifications
You must be signed in to change notification settings - Fork 2
/
MonthPicker.qml
187 lines (174 loc) · 3.82 KB
/
MonthPicker.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
import QtQuick 2.7
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3
import QtQuick.Controls.Material 2.0
Rectangle {
id: monthPicker
height: 42
width: parent.width
property int indicatorHeight: 2
property string indicatorColor: "#e91e63"
property int month
property string textEntered
signal submit()
signal abort()
activeFocusOnTab: true
onActiveFocusChanged: {
textEntered = ""
if (monthPicker.activeFocus) {
Qt.inputMethod.show()
}
}
ListModel {
id: monthModel
ListElement {
month: "Jan"
days: 31
}
ListElement {
month: "Feb"
days: 28
}
ListElement {
month: "Mar"
days: 31
}
ListElement {
month: "Apr"
days: 30
}
ListElement {
month: "May"
days: 31
}
ListElement {
month: "Jun"
days: 30
}
ListElement {
month: "Jul"
days: 31
}
ListElement {
month: "Aug"
days: 31
}
ListElement {
month: "Sep"
days: 30
}
ListElement {
month: "Oct"
days: 31
}
ListElement {
month: "Nov"
days: 30
}
ListElement {
month: "Dec"
days: 31
}
}
MouseArea {
id: clickarea
z: 1
propagateComposedEvents: true
anchors.fill: parent
onPressed: {
if (! monthPicker.activeFocus) {
monthPicker.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: monthPickerTumbler
height: parent.width
width: parent.height - parent.indicatorHeight
activeFocusOnTab: false
z: 0
transform: Rotation {
angle: -90
origin.x: monthPickerTumbler.width / 2
origin.y: monthPickerTumbler.width / 2
}
model: monthModel
delegate: Text {
transform: Rotation {
angle: 90
origin.x: width / 2
origin.y: width / 2
}
anchors.right: parent.horizontalCenter
text: month
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 = monthPickerTumbler.currentIndex
if (monthPicker.month != m) {
monthPicker.month = m
}
}
}
Rectangle {
id: monthPickerIndicator
width: parent.width
height: parent.indicatorHeight
color: parent.activeFocus ? parent.indicatorColor : parent.color
anchors.bottom: parent.bottom
activeFocusOnTab: false
}
onMonthChanged: {
if (monthPickerTumbler.currentIndex != monthPicker.month) {
monthPickerTumbler.currentIndex = monthPicker.month
}
}
Keys.onPressed: {
if (event.key == Qt.Key_Return || event.key == Qt.Key_Enter) {
event.accepted = true
monthPicker.submit()
}
else if (event.key == Qt.Key_Escape) {
event.accepted = true
monthPicker.abort()
}
else if (event.text.replace(/[^A-Z0-9]/gi, "").length > 0) {
var foundIndex = searchIndex(event.text)
if (foundIndex > -1) {
monthPickerTumbler.currentIndex = foundIndex
}
}
}
// Search for Index. If nothing is found, starts over with the last entered key
function searchIndex(t) {
monthPicker.textEntered += t
var foundIndex = findIndex(monthPicker.textEntered)
if (foundIndex > -1) {
return foundIndex
}
// If more than one key has been entered, take just the last one and try again
else if (monthPicker.textEntered.length > t.length) {
monthPicker.textEntered = ""
return searchIndex(t)
}
}
// return Index of the first item that contains the given string.
function findIndex(t) {
for (var i = 0; i < monthModel.count; i++) {
if (monthModel.get(i).month.toLowerCase().startsWith(monthPicker.textEntered.toLowerCase())) {
return i
}
else if (parseInt(monthPicker.textEntered) > 0 && parseInt(monthPicker.textEntered) < 13) {
return parseInt(monthPicker.textEntered) - 1
}
}
return -1
}
}