-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathYearMonthDialog.qml
257 lines (225 loc) · 7.31 KB
/
YearMonthDialog.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
import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Controls.Material 2.14
import Qt.labs.calendar 1.0 as LAB
import QtQuick.Layouts 1.12
import yacalendar 1.0
import "util.js" as UTIL
Dialog
{
id: control
width: {
const width_size = parent.width * 0.6;
if (width_size <= 300)
return 300;
else if (width_size >= 400)
return 400;
return width_size;
}
height: {
const height_size = parent.height * 0.5;
if (height_size <= 350)
return 350;
else if (height_size >= 450)
return 450;
return height_size;
}
x: (parent.width - width) / 2
y: (parent.height - height) / 2
dim: true
modal: true
clip: true
topPadding: 0
leftPadding: 5
rightPadding: 5
bottomPadding: 5
closePolicy: Dialog.CloseOnEscape
standardButtons: Dialog.Ok | Dialog.Cancel
property CalendarSystem system: CalendarSystem {
type: CalendarSystem.Gregorian
locale: control.locale
}
// FIXME: Qt 5.15 Deprecation warnings
Connections
{
target: control.system
onLocaleChanged:
{
generate_month_range((selected_year < 0) ? today.year : selected_year);
}
onTypeChanged:
{
generate_month_range((selected_year < 0) ? today.year : selected_year);
}
}
property var today: control.system.today();
property ListModel month_range: ListModel {}
property int from: 1980
property int to: 2030
property int selected_month: -1
property int selected_year: -1
property int month_name_format: Locale.LongFormat
signal finished(int result, var selected_date)
Component.onCompleted:
{
generate_month_range(today.year);
}
function generate_month_range(year)
{
control.month_range.clear();
const numbers_of_month = control.system.months_in_year(year);
for (let counter = 1; counter <= numbers_of_month; counter++)
{
let month_name = control.system.month_name(counter, year, control.month_name_format);
control.month_range.append({
"id": counter,
"name": month_name
});
}
const index_month = UTIL.find_in_model(month_gridview.model, (item) => {
let key = (selected_month < 0) ? today.month : selected_month
return item.id === key;
});
month_gridview.currentIndex = index_month;
}
header: Rectangle {
id: header
height: parent.height * 0.25
color: Material.accent
Label
{
id: title
anchors.top: parent.top
anchors.left: parent.left
anchors.leftMargin: 16
anchors.topMargin: 8
color: "white"
text: qsTr("SELECT A DATE")
font.pointSize: 9
}
Label
{
id: date_lbl
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.bottomMargin: 8
anchors.leftMargin: 16
font.pointSize: 15
font.bold: true
color: "white"
text:
{
if (!control.opened)
return "";
const year = year_combo.currentText;
const month_name = month_gridview.model.get(month_gridview.currentIndex).name;
return `${year}, ${month_name}`;
}
}
}
ColumnLayout
{
id: container
anchors.fill: parent
spacing: 0
GridCombobox
{
id: year_combo
flat: true
width: 170
padding: 0
popup.width: container.width
popup.height: container.height
cell_width: ((container.width - 10) / 3)
cell_height: 35
textRole: "number"
indicator.rotation: (down) ? 180 : 0
model: RangeModel {
from: control.from
to: control.to
}
delegate: ItemDelegate {
width: year_combo.cell_width
height: year_combo.cell_height
background: Rectangle {
color: "transparent"
}
Label
{
anchors.centerIn: parent
text: model.number
color: (year_combo.highlightedIndex === index) ? "white" : Material.foreground
}
}
highlight: Rectangle {
color: Material.accent
radius: (width / 4)
}
onCurrentIndexChanged:
{
const year = year_combo.model.get(currentIndex).number;
generate_month_range(year);
year_combo.displayText = year;
}
}
GridView
{
id: month_gridview
model: control.month_range
Layout.fillWidth: true
Layout.fillHeight: true
cellWidth: ((container.width - 10) / 3)
cellHeight: 35
clip: true
delegate: ItemDelegate
{
width: month_gridview.cellWidth
height: month_gridview.cellHeight
Label
{
anchors.centerIn: parent
text: model.name
color: (month_gridview.currentIndex === index) ? "white" : Material.foreground
}
MouseArea
{
anchors.fill: parent
onClicked: {
month_gridview.currentIndex = index;
control.selected_month = index + 1;
}
}
}
highlight: Rectangle {
color: Material.accent
radius: (width / 4)
}
}
}
onOpened:
{
if (control.opened)
{
const index_year = year_combo.find((selected_year < 0 ) ? today.year : selected_year);
year_combo.currentIndex = index_year;
year_combo.displayText = year_combo.model.get(year_combo.currentIndex).number;
const index_month = UTIL.find_in_model(month_gridview.model, (item) => {
let key = (selected_month < 0) ? today.month : selected_month
return item.id === key;
});
month_gridview.currentIndex = index_month;
}
}
onAccepted:
{
control.selected_year = year_combo.model.get(year_combo.currentIndex).number;
control.selected_month = month_gridview.model.get(month_gridview.currentIndex).id;
const month_name = month_gridview.model.get(month_gridview.currentIndex).name
const selected_date = {
year: control.selected_year,
month: control.selected_month,
month_name: month_name
}
finished(control.result, selected_date);
}
}