Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickweeknumbermodel.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
6
7#include <QtCore/private/qabstractitemmodel_p.h>
8#include <QtCore/qdatetime.h>
9
11
13{
14 Q_DECLARE_PUBLIC(QQuickWeekNumberModel)
15
16public:
24
25 void init(int month, int year, const QLocale &locale = QLocale());
26 static QDate calculateFirst(int month, int year, const QLocale &locale);
27
28 int month;
29 int year;
32};
33
34void QQuickWeekNumberModelPrivate::init(int m, int y, const QLocale &l)
35{
36 Q_Q(QQuickWeekNumberModel);
37 if (m == month && y == year && l.firstDayOfWeek() == locale.firstDayOfWeek())
38 return;
39
40 // The actual first (1st) day of the month.
41 QDate firstDayOfMonthDate(y, m, 1);
42 int difference = ((firstDayOfMonthDate.dayOfWeek() - l.firstDayOfWeek()) + 7) % 7;
43 // The first day to display should never be the 1st of the month, as we want some days from
44 // the previous month to be visible.
45 if (difference == 0)
46 difference += 7;
47
48 for (int i = 0; i < 6; ++i)
49 weekNumbers[i] = firstDayOfMonthDate.addDays(i * 7 - difference).weekNumber();
50
51 if (q) // null at construction
52 emit q->dataChanged(q->index(0, 0), q->index(5, 0));
53}
54
55QQuickWeekNumberModel::QQuickWeekNumberModel(QObject *parent) :
56 QAbstractListModel(*(new QQuickWeekNumberModelPrivate), parent)
57{
58}
59
61{
62 Q_D(const QQuickWeekNumberModel);
63 return d->month;
64}
65
67{
68 Q_D(QQuickWeekNumberModel);
69 if (d->month != month) {
70 d->init(month, d->year, d->locale);
71 d->month = month;
72 emit monthChanged();
73 }
74}
75
77{
78 Q_D(const QQuickWeekNumberModel);
79 return d->year;
80}
81
83{
84 Q_D(QQuickWeekNumberModel);
85 if (d->year != year) {
86 d->init(d->month, year, d->locale);
87 d->year = year;
88 emit yearChanged();
89 }
90}
91
93{
94 Q_D(const QQuickWeekNumberModel);
95 return d->locale;
96}
97
98void QQuickWeekNumberModel::setLocale(const QLocale &locale)
99{
100 Q_D(QQuickWeekNumberModel);
101 if (d->locale != locale) {
102 d->init(d->month, d->year, locale);
103 d->locale = locale;
104 emit localeChanged();
105 }
106}
107
109{
110 Q_D(const QQuickWeekNumberModel);
111 if (index < 0 || index > 5)
112 return -1;
113 return d->weekNumbers[index];
114}
115
116int QQuickWeekNumberModel::indexOf(int weekNumber) const
117{
118 Q_D(const QQuickWeekNumberModel);
119 if (weekNumber < d->weekNumbers[0] || weekNumber > d->weekNumbers[5])
120 return -1;
121 return weekNumber - d->weekNumbers[0];
122}
123
124QVariant QQuickWeekNumberModel::data(const QModelIndex &index, int role) const
125{
126 if (role == WeekNumberRole) {
127 int weekNumber = weekNumberAt(index.row());
128 if (weekNumber != -1)
129 return weekNumber;
130 }
131 return QVariant();
132}
133
134int QQuickWeekNumberModel::rowCount(const QModelIndex &parent) const
135{
136 if (parent.isValid())
137 return 0;
138 return 6;
139}
140
142{
143 QHash<int, QByteArray> roles;
144 roles[WeekNumberRole] = QByteArrayLiteral("weekNumber");
145 return roles;
146}
147
148QT_END_NAMESPACE
149
150#include "moc_qquickweeknumbermodel_p.cpp"
static QDate calculateFirst(int month, int year, const QLocale &locale)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
QHash< int, QByteArray > roleNames() const override
void setLocale(const QLocale &locale)
QVariant data(const QModelIndex &index, int role) const override
Returns the data stored under the given role for the item referred to by the index.
Q_INVOKABLE int indexOf(int weekNumber) const
Q_INVOKABLE int weekNumberAt(int index) const