Debian Codegraph @

src/qml/CustomPickers/1.0/HoursModel.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 property int from
22 circular: count >= 24
23
24 function getHourLocalized(hour) {
25 var date = new Date()
26 date.setHours(hour)
27 var localizedTime = Qt.formatTime(date);
28 return localizedTime.split(/\D/)[0];
29 }
30
31 function reset() {
32 resetting = true;
33
34 clear();
35 from = minimum.getHours();
36 var distance = (!Date.prototype.isValid.call(maximum) || (minimum.daysTo(maximum) > 1)) ? 24 : minimum.hoursTo(maximum);
37 for (var i = 0; i < distance; i++) {
38 append({"hour": getHourLocalized((from + i) % 24)});
39 }
40
41 resetting = false;
42 }
43
44 function resetLimits(label, margin) {
45 label.text = "99";
46 narrowFormatLimit = shortFormatLimit = longFormatLimit = label.paintedWidth + 2 * margin;
47 }
48
49 function indexOf() {
50 var index = date.getHours() - from;
51 if (index >= count) {
52 index = -1;
53 }
54
55 return index;
56 }
57
58 function dateFromIndex(index) {
59 if (index < 0 || index >= count) {
60 return date;
61 }
62 var newDate = new Date(date);
63 newDate.setHours(index + from);
64 return newDate;
65 }
66
67 function text(value) {
68 return (value !== undefined) ? ("00" + value).slice(-2) : "";
69 }
70 }