Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
5
6#include <QtCore/private/qabstractitemmodel_p.h>
7
9
14
36{
37 Q_DECLARE_PUBLIC(QQuickCalendarModel)
38
39public:
41 from(1,1,1), to(275759, 9, 25), count(0)
42 {
43 }
44
45 static int getCount(QDate from, QDate to);
46
47 void populate(QDate from, QDate to, bool force = false);
48
52 int count;
53};
54
55// Returns the number of months we need to display for both from and to to be shown,
56// or zero if from is in a later month than to, or either is invalid.
58{
59 if (!from.isValid() || !to.isValid())
60 return 0;
61
62 const QCalendar gregorian;
63 Q_ASSERT(gregorian.isGregorian());
64 const QCalendar::YearMonthDay &f = gregorian.partsFromDate(from);
65 const QCalendar::YearMonthDay &t = gregorian.partsFromDate(to);
66 Q_ASSERT(f.isValid() && t.isValid()); // ... because from and to are valid.
67 if (f.year > t.year || (f.year == t.year && f.month > t.month))
68 return 0;
69
70 // Count from's month and every subsequent month until to's:
71 return 1 + t.month + 12 * (t.year - f.year) - f.month;
72}
73
75{
77 if (!force && f == from && t == to)
78 return;
79
80 int c = getCount(from, to);
81 if (c != count) {
82 q->beginResetModel();
83 count = c;
84 q->endResetModel();
85 emit q->countChanged();
86 } else {
87 emit q->dataChanged(q->index(0, 0), q->index(c - 1, 0));
88 }
89}
90
93{
94}
95
102{
103 Q_D(const QQuickCalendarModel);
104 return d->from;
105}
106
108{
110 if (d->from != from) {
111 if (d->complete)
112 d->populate(from, d->to);
113 d->from = from;
115 }
116}
117
124{
125 Q_D(const QQuickCalendarModel);
126 return d->to;
127}
128
130{
132 if (d->to != to) {
133 if (d->complete)
134 d->populate(d->from, to);
135 d->to = to;
136 emit toChanged();
137 }
138}
139
146{
147 Q_D(const QQuickCalendarModel);
148 return d->from.addMonths(index).month() - 1;
149}
150
157{
158 Q_D(const QQuickCalendarModel);
159 return d->from.addMonths(index).year();
160}
161
168{
169 Q_D(const QQuickCalendarModel);
170 return d->getCount(d->from, date) - 1;
171}
172
178int QQuickCalendarModel::indexOf(int year, int month) const
179{
180 return indexOf(QDate(year, month + 1, 1));
181}
182
184{
185 Q_D(const QQuickCalendarModel);
186 if (index.isValid() && index.row() < d->count) {
187 switch (role) {
188 case MonthRole:
189 return monthAt(index.row());
190 case YearRole:
191 return yearAt(index.row());
192 default:
193 break;
194 }
195 }
196 return QVariant();
197}
198
200{
201 Q_D(const QQuickCalendarModel);
202 if (!parent.isValid())
203 return d->count;
204 return 0;
205}
206
208{
210 roles[MonthRole] = QByteArrayLiteral("month");
211 roles[YearRole] = QByteArrayLiteral("year");
212 return roles;
213}
214
216{
217}
218
220{
222 d->complete = true;
223 d->populate(d->from, d->to, true);
224}
225
227
228#include "moc_qquickcalendarmodel_p.cpp"
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
The QCalendar class describes calendar systems.
Definition qcalendar.h:53
bool isGregorian() const
Returns true if this calendar object is the Gregorian calendar object used as default calendar by oth...
YearMonthDay partsFromDate(QDate date) const
Converts a QDate to a year, month, and day of the month.
\inmodule QtCore \reentrant
Definition qdatetime.h:27
constexpr bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition qdatetime.h:86
\inmodule QtCore
Definition qhash.h:818
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:90
void populate(QDate from, QDate to, bool force=false)
static int getCount(QDate from, QDate to)
Q_INVOKABLE int yearAt(int index) const
\qmlmethod int QtQuick.Controls::CalendarModel::yearAt(int index)
void classBegin() override
Invoked after class creation, but before any properties have been set.
void setFrom(const QDate &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.
QQuickCalendarModel(QObject *parent=nullptr)
void setTo(const QDate &to)
Q_INVOKABLE int indexOf(const 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)
\inmodule QtCore
Definition qvariant.h:64
QDate date
[1]
Combined button and popup list for selecting options.
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
GLuint index
[2]
GLenum GLenum GLsizei count
GLfloat GLfloat f
const GLubyte * c
GLdouble GLdouble t
Definition qopenglext.h:243
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define emit
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent