Debian Codegraph @

src/qml/CustomPickers/1.0/MonthModel.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: (count >= 11)
22
23 // local properties
24 property int from
25
26 function reset() {
27 resetting = true;
28 clear();
29 var modelDate = new Date(date);
30 modelDate.setDate(1);
31
32 // if maximum is invalid, we have full model (12 months to show)
33 var distance, to;
34 distance = to = maximum.isValid() ? minimum.monthsTo(maximum) : 11;
35 if (to < 0 || to > 11) to = 11;
36 from = (to < 11) ? minimum.getMonth() : 0;
37 // fill the model
38 for (var i = from; i <= from + to; i++) {
39 modelDate.setMonth(i);
40 append({"month": modelDate.getMonth()});
41 }
42 }
43
44 function resetLimits(label, margin) {
45 label.text = '9999';
46 narrowFormatLimit = label.paintedWidth + 2 * margin;
47 shortFormatLimit = longFormatLimit = 0.0;
48 for (var month = 0; month < 12; month++) {
49 label.text = mainComponent.locale.monthName(month, Locale.LongFormat);
50 shortFormatLimit = Math.max(label.paintedWidth + 2 * margin, shortFormatLimit);
51 label.text = mainComponent.locale.monthName(month, Locale.LongFormat);
52 longFormatLimit = Math.max(label.paintedWidth + 2 * margin, longFormatLimit);
53 }
54 }
55
56 function indexOf() {
57 var index = date.getMonth() - from;
58 if (index >= count) {
59 index = -1;
60 }
61 return index;
62 }
63
64 function dateFromIndex(index) {
65 if (index < 0 || index >= count) {
66 return date;
67 }
68 var newDate = new Date(date);
69 // check if the days are in the diff zone (29-31)
70 var fromDay = newDate.getDate();
71 // move the day to the 1st of the month so we don't overflow when setting the month
72 newDate.setDate(1);
73 newDate.setMonth(get(index).month);
74 var maxDays = newDate.daysInMonth();
75 // check whether the original day would overflow
76 // and trim to the mont's maximum date
77 newDate.setDate((fromDay > maxDays) ? maxDays : fromDay);
78 return newDate;
79 }
80
81 function text(value) {
82 if (!mainComponent || value === undefined) {
83 return "";
84 }
85 if (pickerWidth >= longFormatLimit) {
86 return mainComponent.locale.monthName(value, Locale.LongFormat);
87 }
88
89 if (pickerWidth >= shortFormatLimit) {
90 return mainComponent.locale.monthName(value, Locale.ShortFormat);
91 }
92
93 var thisDate = new Date(date);
94 thisDate.setDate(1);
95 thisDate.setMonth(value);
96 return Qt.formatDate(thisDate, "MM");
97 }
98 }