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
qquickmonthmodel.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
8namespace {
9 static const int daysInAWeek = 7;
10 static const int weeksOnACalendarMonth = 6;
11 static const int daysOnACalendarMonth = daysInAWeek * weeksOnACalendarMonth;
12}
13
15
17{
18 Q_DECLARE_PUBLIC(QQuickMonthModel)
19
20public:
21 QQuickMonthModelPrivate() : dates(daysOnACalendarMonth)
22 {
24 month = today.month();
25 year = today.year();
26 }
27
28 bool populate(int month, int year, const QLocale &locale, bool force = false);
29
30 int month;
31 int year;
34 QVector<QDate> dates;
36};
37
38bool QQuickMonthModelPrivate::populate(int m, int y, const QLocale &l, bool force)
39{
41 if (!force && m == month && y == year && l.firstDayOfWeek() == locale.firstDayOfWeek())
42 return false;
43
44 // The actual first (1st) day of the month.
45 QDate firstDayOfMonthDate(y, m, 1);
46 int difference = ((firstDayOfMonthDate.dayOfWeek() - l.firstDayOfWeek()) + 7) % 7;
47 // The first day to display should never be the 1st of the month, as we want some days from
48 // the previous month to be visible.
49 if (difference == 0)
50 difference += 7;
51 QDate firstDateToDisplay = firstDayOfMonthDate.addDays(-difference);
52
54 for (int i = 0; i < daysOnACalendarMonth; ++i)
55 dates[i] = firstDateToDisplay.addDays(i);
56
57 q->setTitle(l.standaloneMonthName(m) + QStringLiteral(" ") + QString::number(y));
58
59 return true;
60}
61
64{
66 d->populate(d->month, d->year, d->locale, true);
67}
68
70{
71 Q_D(const QQuickMonthModel);
72 return d->month;
73}
74
76{
78 if (d->month != month) {
79 if (d->populate(month, d->year, d->locale))
80 emit dataChanged(index(0, 0), index(daysOnACalendarMonth - 1, 0));
81 d->month = month;
83 }
84}
85
87{
88 Q_D(const QQuickMonthModel);
89 return d->year;
90}
91
93{
95 if (d->year != year) {
96 if (d->populate(d->month, year, d->locale))
97 emit dataChanged(index(0, 0), index(daysOnACalendarMonth - 1, 0));
98 d->year = year;
100 }
101}
102
104{
105 Q_D(const QQuickMonthModel);
106 return d->locale;
107}
108
110{
111 Q_D(QQuickMonthModel);
112 if (d->locale != locale) {
113 if (d->populate(d->month, d->year, locale))
114 emit dataChanged(index(0, 0), index(daysOnACalendarMonth - 1, 0));
115 d->locale = locale;
117 }
118}
119
121{
122 Q_D(const QQuickMonthModel);
123 return d->title;
124}
125
127{
128 Q_D(QQuickMonthModel);
129 if (d->title != title) {
130 d->title = title;
132 }
133}
134
136{
137 Q_D(const QQuickMonthModel);
138 return d->dates.value(index);
139}
140
142{
143 Q_D(const QQuickMonthModel);
144 if (date < d->dates.first() || date > d->dates.last())
145 return -1;
146 return qMax(qint64(0), d->dates.first().daysTo(date));
147}
148
150{
151 Q_D(const QQuickMonthModel);
152 if (index.isValid() && index.row() < daysOnACalendarMonth) {
153 const QDate date = d->dates.at(index.row());
154 switch (role) {
155 case DateRole:
156 return date;
157 case DayRole:
158 return date.day();
159 case TodayRole:
160 return date == d->today;
161 case WeekNumberRole:
162 return date.weekNumber();
163 case MonthRole:
164 return date.month() - 1;
165 case YearRole:
166 return date.year();
167 default:
168 break;
169 }
170 }
171 return QVariant();
172}
173
175{
176 if (parent.isValid())
177 return 0;
178 return daysOnACalendarMonth;
179}
180
181QHash<int, QByteArray> QQuickMonthModel::roleNames() const
182{
183 QHash<int, QByteArray> roles;
184 roles[DateRole] = QByteArrayLiteral("date");
185 roles[DayRole] = QByteArrayLiteral("day");
186 roles[TodayRole] = QByteArrayLiteral("today");
187 roles[WeekNumberRole] = QByteArrayLiteral("weekNumber");
188 roles[MonthRole] = QByteArrayLiteral("month");
189 roles[YearRole] = QByteArrayLiteral("year");
190 return roles;
191}
192
194
195#include "moc_qquickmonthmodel_p.cpp"
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles=QList< int >())
This signal is emitted whenever the data in an existing item changes.
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
\inmodule QtCore \reentrant
Definition qdatetime.h:29
int weekNumber(int *yearNum=nullptr) const
Returns the ISO 8601 week number (1 to 53).
int month() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
int day() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
QDate addDays(qint64 days) const
Returns a QDate object containing a date ndays later than the date of this object (or earlier if nday...
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
QString standaloneMonthName(int, FormatType format=LongFormat) const
Definition qlocale.cpp:2979
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:103
bool populate(int month, int year, const QLocale &locale, bool force=false)
Q_INVOKABLE QDate dateAt(int index) const
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.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
QHash< int, QByteArray > roleNames() const override
Q_INVOKABLE int indexOf(QDate date) const
void setMonth(int month)
void setYear(int year)
void setLocale(const QLocale &locale)
QQuickMonthModel(QObject *parent=nullptr)
void setTitle(const QString &title)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
\inmodule QtCore
Definition qvariant.h:65
QDate date
[1]
Combined button and popup list for selecting options.
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
const GLfloat * m
GLuint index
[2]
GLint y
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define QStringLiteral(str)
#define emit
long long qint64
Definition qtypes.h:60
QString title
[35]