1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: http://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Labs Calendar module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at http://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or later as published by the Free |
28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
29 | ** the packaging of this file. Please review the following information to |
30 | ** ensure the GNU General Public License version 2.0 requirements will be |
31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
32 | ** |
33 | ** $QT_END_LICENSE$ |
34 | ** |
35 | ****************************************************************************/ |
36 | |
37 | #include "qquickweeknumbermodel_p.h" |
38 | |
39 | #include <QtCore/private/qabstractitemmodel_p.h> |
40 | #include <QtCore/qdatetime.h> |
41 | |
42 | QT_BEGIN_NAMESPACE |
43 | |
44 | class QQuickWeekNumberModelPrivate : public QAbstractItemModelPrivate |
45 | { |
46 | Q_DECLARE_PUBLIC(QQuickWeekNumberModel) |
47 | |
48 | public: |
49 | QQuickWeekNumberModelPrivate() : month(-1), year(-1), weekNumbers{} |
50 | { |
51 | QDate date = QDate::currentDate(); |
52 | init(month: date.month(), year: date.year(), locale); |
53 | month = date.month(); |
54 | year = date.year(); |
55 | } |
56 | |
57 | void init(int month, int year, const QLocale &locale = QLocale()); |
58 | static QDate calculateFirst(int month, int year, const QLocale &locale); |
59 | |
60 | int month; |
61 | int year; |
62 | QLocale locale; |
63 | int weekNumbers[6]; |
64 | }; |
65 | |
66 | void QQuickWeekNumberModelPrivate::init(int m, int y, const QLocale &l) |
67 | { |
68 | Q_Q(QQuickWeekNumberModel); |
69 | if (m == month && y == year && l.firstDayOfWeek() == locale.firstDayOfWeek()) |
70 | return; |
71 | |
72 | // The actual first (1st) day of the month. |
73 | QDate firstDayOfMonthDate(y, m, 1); |
74 | int difference = ((firstDayOfMonthDate.dayOfWeek() - l.firstDayOfWeek()) + 7) % 7; |
75 | // The first day to display should never be the 1st of the month, as we want some days from |
76 | // the previous month to be visible. |
77 | if (difference == 0) |
78 | difference += 7; |
79 | |
80 | for (int i = 0; i < 6; ++i) |
81 | weekNumbers[i] = firstDayOfMonthDate.addDays(days: i * 7 - difference).weekNumber(); |
82 | |
83 | if (q) // null at construction |
84 | emit q->dataChanged(topLeft: q->index(row: 0, column: 0), bottomRight: q->index(row: 5, column: 0)); |
85 | } |
86 | |
87 | QQuickWeekNumberModel::QQuickWeekNumberModel(QObject *parent) : |
88 | QAbstractListModel(*(new QQuickWeekNumberModelPrivate), parent) |
89 | { |
90 | } |
91 | |
92 | int QQuickWeekNumberModel::month() const |
93 | { |
94 | Q_D(const QQuickWeekNumberModel); |
95 | return d->month; |
96 | } |
97 | |
98 | void QQuickWeekNumberModel::setMonth(int month) |
99 | { |
100 | Q_D(QQuickWeekNumberModel); |
101 | if (d->month != month) { |
102 | d->init(m: month, y: d->year, l: d->locale); |
103 | d->month = month; |
104 | emit monthChanged(); |
105 | } |
106 | } |
107 | |
108 | int QQuickWeekNumberModel::year() const |
109 | { |
110 | Q_D(const QQuickWeekNumberModel); |
111 | return d->year; |
112 | } |
113 | |
114 | void QQuickWeekNumberModel::setYear(int year) |
115 | { |
116 | Q_D(QQuickWeekNumberModel); |
117 | if (d->year != year) { |
118 | d->init(m: d->month, y: year, l: d->locale); |
119 | d->year = year; |
120 | emit yearChanged(); |
121 | } |
122 | } |
123 | |
124 | QLocale QQuickWeekNumberModel::locale() const |
125 | { |
126 | Q_D(const QQuickWeekNumberModel); |
127 | return d->locale; |
128 | } |
129 | |
130 | void QQuickWeekNumberModel::setLocale(const QLocale &locale) |
131 | { |
132 | Q_D(QQuickWeekNumberModel); |
133 | if (d->locale != locale) { |
134 | d->init(m: d->month, y: d->year, l: locale); |
135 | d->locale = locale; |
136 | emit localeChanged(); |
137 | } |
138 | } |
139 | |
140 | int QQuickWeekNumberModel::weekNumberAt(int index) const |
141 | { |
142 | Q_D(const QQuickWeekNumberModel); |
143 | if (index < 0 || index > 5) |
144 | return -1; |
145 | return d->weekNumbers[index]; |
146 | } |
147 | |
148 | int QQuickWeekNumberModel::indexOf(int weekNumber) const |
149 | { |
150 | Q_D(const QQuickWeekNumberModel); |
151 | if (weekNumber < d->weekNumbers[0] || weekNumber > d->weekNumbers[5]) |
152 | return -1; |
153 | return weekNumber - d->weekNumbers[0]; |
154 | } |
155 | |
156 | QVariant QQuickWeekNumberModel::data(const QModelIndex &index, int role) const |
157 | { |
158 | if (role == WeekNumberRole) { |
159 | int weekNumber = weekNumberAt(index: index.row()); |
160 | if (weekNumber != -1) |
161 | return weekNumber; |
162 | } |
163 | return QVariant(); |
164 | } |
165 | |
166 | int QQuickWeekNumberModel::rowCount(const QModelIndex &parent) const |
167 | { |
168 | if (parent.isValid()) |
169 | return 0; |
170 | return 6; |
171 | } |
172 | |
173 | QHash<int, QByteArray> QQuickWeekNumberModel::roleNames() const |
174 | { |
175 | QHash<int, QByteArray> roles; |
176 | roles[WeekNumberRole] = QByteArrayLiteral("weekNumber" ); |
177 | return roles; |
178 | } |
179 | |
180 | QT_END_NAMESPACE |
181 | |