Debian Codegraph @

src/qml/CustomPickers/1.0/DayModel.qml

raw
1 /*
2 * Copyright 2013 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components 1.3
19
20 PickerModelBase {
21 circular: true
22
23 function reset() {
24 resetting = true;
25 clear();
26 for (var i = 0; i < date.daysInMonth(); i++) {
27 append({"day": i});
28 }
29 }
30
31 function resetLimits(label, margin) {
32 label.text = '9999';
33 narrowFormatLimit = label.paintedWidth + 2 * margin;
34 shortFormatLimit = longFormatLimit = 0.0;
35 for (var day = 1; day <= 7; day++) {
36 label.text = '99 ' + mainComponent.locale.dayName(day, Locale.ShortFormat)
37 shortFormatLimit = Math.max(label.paintedWidth + 2 * margin, shortFormatLimit);
38 label.text = '99 ' + mainComponent.locale.dayName(day, Locale.LongFormat)
39 longFormatLimit = Math.max(label.paintedWidth + 2 * margin, longFormatLimit);
40 }
41 }
42
43 function syncModels() {
44 var newDaysCount = mainComponent.date.daysInMonth(mainComponent.year, mainComponent.month);
45 var modelCount = count;
46 var daysDiff = newDaysCount - modelCount;
47 if (daysDiff < 0) {
48 remove(modelCount + daysDiff, -daysDiff);
49 } else if (daysDiff > 0) {
50 for (var d = modelCount; d < modelCount + daysDiff; d++) {
51 append({"day": d});
52 }
53 }
54 }
55
56 function indexOf() {
57 return date.getDate() - 1;
58 }
59
60 function dateFromIndex(index) {
61 if (index < 0 || index >= count) {
62 return date;
63 }
64 var newDate = new Date(date);
65 newDate.setDate(index + 1);
66 return newDate;
67 }
68
69 function text(value) {
70 if (value === undefined) {
71 return "";
72 }
73
74 var thisDate = new Date(date);
75 thisDate.setDate(value + 1);
76
77 if (pickerWidth >= longFormatLimit) {
78 return Qt.formatDate(thisDate, "dd ") + mainComponent.locale.dayName(thisDate.getDay(), Locale.LongFormat);
79 }
80
81 if (pickerWidth >= shortFormatLimit) {
82 return Qt.formatDate(thisDate, "dd ") + mainComponent.locale.dayName(thisDate.getDay(), Locale.ShortFormat);
83 }
84 return Qt.formatDate(thisDate, "dd");
85 }
86 }