1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquickweeknumbermodel_p.h"
5
6#include <QtCore/private/qabstractitemmodel_p.h>
7#include <QtCore/qdatetime.h>
8
9QT_BEGIN_NAMESPACE
10
11class QQuickWeekNumberModelPrivate : public QAbstractItemModelPrivate
12{
13 Q_DECLARE_PUBLIC(QQuickWeekNumberModel)
14
15public:
16 QQuickWeekNumberModelPrivate() : month(-1), year(-1), weekNumbers{}
17 {
18 QDate date = QDate::currentDate();
19 init(month: date.month(), year: date.year(), locale);
20 month = date.month();
21 year = date.year();
22 }
23
24 void init(int month, int year, const QLocale &locale = QLocale());
25 static QDate calculateFirst(int month, int year, const QLocale &locale);
26
27 int month;
28 int year;
29 QLocale locale;
30 int weekNumbers[6];
31};
32
33void QQuickWeekNumberModelPrivate::init(int m, int y, const QLocale &l)
34{
35 Q_Q(QQuickWeekNumberModel);
36 if (m == month && y == year && l.firstDayOfWeek() == locale.firstDayOfWeek())
37 return;
38
39 // The actual first (1st) day of the month.
40 QDate firstDayOfMonthDate(y, m, 1);
41 int difference = ((firstDayOfMonthDate.dayOfWeek() - l.firstDayOfWeek()) + 7) % 7;
42 // The first day to display should never be the 1st of the month, as we want some days from
43 // the previous month to be visible.
44 if (difference == 0)
45 difference += 7;
46
47 for (int i = 0; i < 6; ++i)
48 weekNumbers[i] = firstDayOfMonthDate.addDays(days: i * 7 - difference).weekNumber();
49
50 if (q) // null at construction
51 emit q->dataChanged(topLeft: q->index(row: 0, column: 0), bottomRight: q->index(row: 5, column: 0));
52}
53
54QQuickWeekNumberModel::QQuickWeekNumberModel(QObject *parent) :
55 QAbstractListModel(*(new QQuickWeekNumberModelPrivate), parent)
56{
57}
58
59int QQuickWeekNumberModel::month() const
60{
61 Q_D(const QQuickWeekNumberModel);
62 return d->month;
63}
64
65void QQuickWeekNumberModel::setMonth(int month)
66{
67 Q_D(QQuickWeekNumberModel);
68 if (d->month != month) {
69 d->init(m: month, y: d->year, l: d->locale);
70 d->month = month;
71 emit monthChanged();
72 }
73}
74
75int QQuickWeekNumberModel::year() const
76{
77 Q_D(const QQuickWeekNumberModel);
78 return d->year;
79}
80
81void QQuickWeekNumberModel::setYear(int year)
82{
83 Q_D(QQuickWeekNumberModel);
84 if (d->year != year) {
85 d->init(m: d->month, y: year, l: d->locale);
86 d->year = year;
87 emit yearChanged();
88 }
89}
90
91QLocale QQuickWeekNumberModel::locale() const
92{
93 Q_D(const QQuickWeekNumberModel);
94 return d->locale;
95}
96
97void QQuickWeekNumberModel::setLocale(const QLocale &locale)
98{
99 Q_D(QQuickWeekNumberModel);
100 if (d->locale != locale) {
101 d->init(m: d->month, y: d->year, l: locale);
102 d->locale = locale;
103 emit localeChanged();
104 }
105}
106
107int QQuickWeekNumberModel::weekNumberAt(int index) const
108{
109 Q_D(const QQuickWeekNumberModel);
110 if (index < 0 || index > 5)
111 return -1;
112 return d->weekNumbers[index];
113}
114
115int QQuickWeekNumberModel::indexOf(int weekNumber) const
116{
117 Q_D(const QQuickWeekNumberModel);
118 if (weekNumber < d->weekNumbers[0] || weekNumber > d->weekNumbers[5])
119 return -1;
120 return weekNumber - d->weekNumbers[0];
121}
122
123QVariant QQuickWeekNumberModel::data(const QModelIndex &index, int role) const
124{
125 if (role == WeekNumberRole) {
126 int weekNumber = weekNumberAt(index: index.row());
127 if (weekNumber != -1)
128 return weekNumber;
129 }
130 return QVariant();
131}
132
133int QQuickWeekNumberModel::rowCount(const QModelIndex &parent) const
134{
135 if (parent.isValid())
136 return 0;
137 return 6;
138}
139
140QHash<int, QByteArray> QQuickWeekNumberModel::roleNames() const
141{
142 QHash<int, QByteArray> roles;
143 roles[WeekNumberRole] = QByteArrayLiteral("weekNumber");
144 return roles;
145}
146
147QT_END_NAMESPACE
148
149#include "moc_qquickweeknumbermodel_p.cpp"
150

source code of qtdeclarative/src/quicktemplates/qquickweeknumbermodel.cpp