-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMonthGrid.qml
193 lines (169 loc) · 5.59 KB
/
MonthGrid.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
import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Controls.Material 2.14
import "util.js" as UTIL
Control
{
id: control
width: 200
height: 200
property var system: null
property int year
property int month: 1
property Component delegate: Item {}
property Component highlight: Item {}
property double cell_width: 48
property double cell_height: 48
property int selected_days: 0
property var start_date: ({})
property var end_date: ({})
function index_of_day(day)
{
return UTIL.find_in_model(day_model, (item) => {
return item.day === day;
});
}
function select_day(day, single_select=false)
{
const index = index_of_day(day);
if (index > -1)
{
if (single_select)
deselect();
day_model.setProperty(index, "selected", true);
control.selected_days += 1;
}
}
function select_range(start, end)
{
const start_index = index_of_day(start);
const end_index = index_of_day(end);
for (let counter = start_index; counter <= end_index; counter++)
{
day_model.setProperty(counter, "selected", true);
}
control.selected_days += (end_index - start_index);
}
function deselect()
{
for (let counter = 0; counter < day_model.count; counter++)
day_model.setProperty(counter, "selected", false);
control.selected_days = 0;
}
function calculate_select()
{
const days_in_month = system.days_in_month(month, year);
let temp = system.to_gregorian(control.start_date.year, control.start_date.month, control.start_date.day);
const qdate_start_date = new Date(temp.year, (temp.month - 1), temp.day);
temp = system.to_gregorian(control.end_date.year, control.end_date.month, control.end_date.day);
const qdate_end_date = new Date(temp.year, (temp.month - 1), temp.day);
temp = system.to_gregorian(control.year, control.month, 1);
const qdate_source_date = new Date(temp.year, (temp.month - 1), temp.day);
if (control.year === control.start_date.year &&
control.year === control.end_date.year &&
control.month === control.start_date.month &&
control.month === control.end_date.month)
{
select_range(control.start_date.day, control.end_date.day);
return;
}
if (control.year === control.start_date.year &&
control.month === control.start_date.month)
{
select_range(control.start_date.day, days_in_month);
return;
}
if (control.year === control.end_date.year &&
control.month === control.end_date.month)
{
select_range(1, control.end_date.day);
return;
}
if (system.is_between(qdate_source_date, qdate_start_date, qdate_end_date))
{
select_range(1, days_in_month);
return;
}
}
function get_offset(first_day_of_week, first_day_of_month)
{
let offset = 0
while (first_day_of_week !== first_day_of_month)
{
first_day_of_week = (first_day_of_week === 6) ? 0 : first_day_of_week += 1;
offset++;
}
return offset;
}
function get_model()
{
if (!system)
return;
day_model.clear();
const first_day_of_month = control.system.first_day_of_month(month, year).getDay();
const first_day_of_week = control.locale.firstDayOfWeek;
const days_in_month = system.days_in_month(month, year);
const offset = get_offset(first_day_of_week, first_day_of_month);
const max_days_in_month = (days_in_month + (offset - 1));
let counter = 0;
for (let row_count = 1; row_count <= 6; row_count++)
{
for (let day_count = 1; day_count <= 7; day_count++)
{
day_model.append({
in_month: (counter >= offset && counter <= max_days_in_month),
year: control.year,
month: control.month,
start_day: (counter === offset),
day: (counter - offset) + 1,
id: counter,
selected: false
});
counter++;
}
}
if (Object.keys(start_date).length && Object.keys(end_date).length)
calculate_select();
}
Component.onCompleted:
{
if (day_model.count)
deselect();
get_model();
}
onStart_dateChanged:
{
if (day_model.count)
deselect();
}
onEnd_dateChanged:
{
if (day_model.count && Object.keys(control.end_date).length)
calculate_select();
}
ListModel
{
id: day_model
}
GridView
{
id: grid
cellWidth: control.cell_width
cellHeight: control.cell_height
anchors.fill: parent
model: day_model
delegate: Item {
Loader
{
sourceComponent: control.highlight
active: model.selected
property var dataModel: model
}
Loader
{
sourceComponent: control.delegate
property var dataModel: model
}
}
}
}