-
Notifications
You must be signed in to change notification settings - Fork 2
/
DateSelector.qml
124 lines (95 loc) · 3.15 KB
/
DateSelector.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
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Controls.Private 1.0
import QtQuick.Layouts 1.1
Control {
id: dateSelector
focus: true
implicitHeight: _rowLayout.height
implicitWidth: _rowLayout.width
readonly property date date: _rowLayout.date
property int startYear: 1970
property int endYear: 2100
Rectangle{
anchors.fill: parent
color: "transparent"
border.width: 1
border.color: "#ccc"
}
RowLayout{
id: _rowLayout
focus: true
anchors.centerIn: parent
// I don't why the month value sub one will be success
property date date: new Date(Number(year.currentText), Number(month.currentText)-1, Number(day.currentText), 1,1,1)
readonly property var leapYearMonthDaysCountList: [31,29,31,30,31,30,31,31,30,31,30,31]
readonly property var yearMonthDaysCountList: [31,28,31,30,31,30,31,31,30,31,30,31]
function __isLeapYear(year){ return ((year%4==0&&year%100!=0) ||year%400==0); }
function __getDaysCount(year, month){
var __year = Number(year);
var __month = Number(month);
var __isLeap = __isLeapYear(year);
return __isLeap ? __get(1, leapYearMonthDaysCountList[__month-1]):
__get(1, yearMonthDaysCountList[__month-1]);
}
// 1970 ~ 2100
function __get(from, to){
var result = [];
while(from <= to) {
result.push(from++);
}
return result;
}
ComboBox {
id: year
focus: true
Layout.fillWidth: true
Layout.minimumWidth: 100
Layout.preferredWidth: 120
KeyNavigation.left: day
KeyNavigation.right: month
model: _rowLayout.__get(startYear, endYear);
onCurrentTextChanged: {
_rowLayout.date.setFullYear(Number(year.currentText));
dateSelector.dateChanged();
}
}
Text{
text: qsTr("年")
}
ComboBox {
id: month
focus: true
Layout.fillWidth: true
Layout.minimumWidth: 80
Layout.preferredWidth: 100
KeyNavigation.left: year
KeyNavigation.right: day
model:_rowLayout.__get(1, 12);
onCurrentTextChanged: {
_rowLayout.date.setMonth(Number(month.currentText));
dateSelector.dateChanged();
}
}
Text{
text: qsTr("月")
}
ComboBox {
id: day
focus: true
Layout.fillWidth: true
Layout.minimumWidth: 80
Layout.preferredWidth: 100
KeyNavigation.left: month
KeyNavigation.right: year
model: _rowLayout.__getDaysCount(year.currentText, month.currentText);
onCurrentTextChanged: {
_rowLayout.date.setDate(Number(day.currentText));
dateSelector.dateChanged();
}
}
Text{
text: qsTr("日")
}
}
}