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
qquickcalendarmodel.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
10
11/*!
12 \qmltype CalendarModel
13//! \inherits AbstractListModel
14//! \nativetype QQuickCalendarModel
15 \inqmlmodule QtQuick.Controls
16 \brief A calendar model.
17
18 CalendarModel provides a way of creating a range of MonthGrid
19 instances. It is typically used as a model for a ListView that uses
20 MonthGrid as a delegate.
21
22 \snippet qtquickcontrols-calendarmodel.qml 1
23
24 In addition to the \c index property, a list of model data roles
25 are available in the context of each delegate:
26 \table
27 \row \li \b model.month : int \li The number of the month
28 \row \li \b model.year : int \li The number of the year
29 \endtable
30
31 \include zero-based-months.qdocinc
32
33 \sa MonthGrid, Calendar
34*/
35
37{
38 Q_DECLARE_PUBLIC(QQuickCalendarModel)
39
40public:
42 from(1,1,1), to(275759, 9, 25), count(0)
43 {
44 }
45
46 static int getCount(QDate from, QDate to);
47
48 void populate(QDate from, QDate to, bool force = false);
49
53 int count;
54};
55
56// Returns the number of months we need to display for both from and to to be shown,
57// or zero if from is in a later month than to, or either is invalid.
58int QQuickCalendarModelPrivate::getCount(QDate from, QDate to)
59{
60 if (!from.isValid() || !to.isValid())
61 return 0;
62
63 const QCalendar gregorian;
64 Q_ASSERT(gregorian.isGregorian());
65 const QCalendar::YearMonthDay &f = gregorian.partsFromDate(from);
66 const QCalendar::YearMonthDay &t = gregorian.partsFromDate(to);
67 Q_ASSERT(f.isValid() && t.isValid()); // ... because from and to are valid.
68 if (f.year > t.year || (f.year == t.year && f.month > t.month))
69 return 0;
70
71 // Count from's month and every subsequent month until to's:
72 return 1 + t.month + 12 * (t.year - f.year) - f.month;
73}
74
75void QQuickCalendarModelPrivate::populate(QDate f, QDate t, bool force)
76{
77 Q_Q(QQuickCalendarModel);
78 if (!force && f == from && t == to)
79 return;
80
81 int c = getCount(from, to);
82 if (c != count) {
83 q->beginResetModel();
84 count = c;
85 q->endResetModel();
86 emit q->countChanged();
87 } else {
88 emit q->dataChanged(q->index(0, 0), q->index(c - 1, 0));
89 }
90}
91
92QQuickCalendarModel::QQuickCalendarModel(QObject *parent) :
93 QAbstractListModel(*(new QQuickCalendarModelPrivate), parent)
94{
95}
96
97/*!
98 \qmlproperty date QtQuick.Controls::CalendarModel::from
99
100 This property holds the start date.
101*/
103{
104 Q_D(const QQuickCalendarModel);
105 return d->from;
106}
107
109{
110 Q_D(QQuickCalendarModel);
111 if (d->from != from) {
112 if (d->complete)
113 d->populate(from, d->to);
114 d->from = from;
115 emit fromChanged();
116 }
117}
118
119/*!
120 \qmlproperty date QtQuick.Controls::CalendarModel::to
121
122 This property holds the end date.
123*/
125{
126 Q_D(const QQuickCalendarModel);
127 return d->to;
128}
129
131{
132 Q_D(QQuickCalendarModel);
133 if (d->to != to) {
134 if (d->complete)
135 d->populate(d->from, to);
136 d->to = to;
137 emit toChanged();
138 }
139}
140
141/*!
142 \qmlmethod int QtQuick.Controls::CalendarModel::monthAt(int index)
143
144 Returns the month number at the specified model \a index.
145*/
146int QQuickCalendarModel::monthAt(int index) const
147{
148 Q_D(const QQuickCalendarModel);
149 return d->from.addMonths(index).month() - 1;
150}
151
152/*!
153 \qmlmethod int QtQuick.Controls::CalendarModel::yearAt(int index)
154
155 Returns the year number at the specified model \a index.
156*/
157int QQuickCalendarModel::yearAt(int index) const
158{
159 Q_D(const QQuickCalendarModel);
160 return d->from.addMonths(index).year();
161}
162
163/*!
164 \qmlmethod int QtQuick.Controls::CalendarModel::indexOf(Date date)
165
166 Returns the model index of the specified \a date.
167*/
168int QQuickCalendarModel::indexOf(QDate date) const
169{
170 Q_D(const QQuickCalendarModel);
171 return d->getCount(d->from, date) - 1;
172}
173
174/*!
175 \qmlmethod int QtQuick.Controls::CalendarModel::indexOf(int year, int month)
176
177 Returns the model index of the specified \a year and \a month.
178*/
179int QQuickCalendarModel::indexOf(int year, int month) const
180{
181 return indexOf(QDate(year, month + 1, 1));
182}
183
184QVariant QQuickCalendarModel::data(const QModelIndex &index, int role) const
185{
186 Q_D(const QQuickCalendarModel);
187 if (index.isValid() && index.row() < d->count) {
188 switch (role) {
189 case MonthRole:
190 return monthAt(index.row());
191 case YearRole:
192 return yearAt(index.row());
193 default:
194 break;
195 }
196 }
197 return QVariant();
198}
199
200int QQuickCalendarModel::rowCount(const QModelIndex &parent) const
201{
202 Q_D(const QQuickCalendarModel);
203 if (!parent.isValid())
204 return d->count;
205 return 0;
206}
207
209{
210 QHash<int, QByteArray> roles;
211 roles[MonthRole] = QByteArrayLiteral("month");
212 roles[YearRole] = QByteArrayLiteral("year");
213 return roles;
214}
215
219
221{
222 Q_D(QQuickCalendarModel);
223 d->complete = true;
224 d->populate(d->from, d->to, true);
225}
226
227QT_END_NAMESPACE
228
229#include "moc_qquickcalendarmodel_p.cpp"
void populate(QDate from, QDate to, bool force=false)
Q_INVOKABLE int yearAt(int index) const
\qmlmethod int QtQuick.Controls::CalendarModel::yearAt(int index)
QDate to() const
\qmlproperty date QtQuick.Controls::CalendarModel::to
void classBegin() override
Invoked after class creation, but before any properties have been set.
Q_INVOKABLE int indexOf(int year, int month) const
\qmlmethod int QtQuick.Controls::CalendarModel::indexOf(int year, int month)
QDate from() const
\qmlproperty date QtQuick.Controls::CalendarModel::from
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
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(QDate date) const
\qmlmethod int QtQuick.Controls::CalendarModel::indexOf(Date date)
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 monthAt(int index) const
\qmlmethod int QtQuick.Controls::CalendarModel::monthAt(int index)