Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5
6#include <QtCore/private/qabstractitemmodel_p.h>
7#include <QtCore/qdatetime.h>
8
10
12{
13 Q_DECLARE_PUBLIC(QQuickWeekNumberModel)
14
15public:
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;
31};
32
34{
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(i * 7 - difference).weekNumber();
49
50 if (q) // null at construction
51 emit q->dataChanged(q->index(0, 0), q->index(5, 0));
52}
53
58
60{
61 Q_D(const QQuickWeekNumberModel);
62 return d->month;
63}
64
66{
68 if (d->month != month) {
69 d->init(month, d->year, d->locale);
70 d->month = month;
72 }
73}
74
76{
77 Q_D(const QQuickWeekNumberModel);
78 return d->year;
79}
80
82{
84 if (d->year != year) {
85 d->init(d->month, year, d->locale);
86 d->year = year;
88 }
89}
90
92{
93 Q_D(const QQuickWeekNumberModel);
94 return d->locale;
95}
96
98{
100 if (d->locale != locale) {
101 d->init(d->month, d->year, locale);
102 d->locale = locale;
104 }
105}
106
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
124{
125 if (role == WeekNumberRole) {
126 int weekNumber = weekNumberAt(index.row());
127 if (weekNumber != -1)
128 return weekNumber;
129 }
130 return QVariant();
131}
132
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
148
149#include "moc_qquickweeknumbermodel_p.cpp"
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
\inmodule QtCore \reentrant
Definition qdatetime.h:29
int month() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
int year() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
static QDate currentDate()
Returns the system clock's current date.
Qt::DayOfWeek firstDayOfWeek() const
Definition qlocale.cpp:3272
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:103
static QDate calculateFirst(int month, int year, const QLocale &locale)
void init(int month, int year, const QLocale &locale=QLocale())
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
QQuickWeekNumberModel(QObject *parent=nullptr)
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
\inmodule QtCore
Definition qvariant.h:65
QDate date
[1]
Combined button and popup list for selecting options.
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
const GLfloat * m
GLuint index
[2]
GLint y
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define emit