Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
5
#
include
"qquickcalendarmodel_p.h"
6
7
#
include
<
QtCore
/
private
/
qabstractitemmodel_p
.
h
>
8
9
QT_BEGIN_NAMESPACE
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
36
class
QQuickCalendarModelPrivate
:
public
QAbstractItemModelPrivate
37
{
38
Q_DECLARE_PUBLIC(QQuickCalendarModel)
39
40
public
:
41
QQuickCalendarModelPrivate
() :
complete
(
false
),
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
50
bool
complete
;
51
QDate
from
;
52
QDate
to
;
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.
58
int
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
75
void
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(f, t);
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
92
QQuickCalendarModel
::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
*/
102
QDate
QQuickCalendarModel
::
from
()
const
103
{
104
Q_D(
const
QQuickCalendarModel);
105
return
d->from;
106
}
107
108
void
QQuickCalendarModel
::
setFrom
(QDate from)
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
*/
124
QDate
QQuickCalendarModel
::
to
()
const
125
{
126
Q_D(
const
QQuickCalendarModel);
127
return
d->to;
128
}
129
130
void
QQuickCalendarModel
::
setTo
(QDate to)
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
*/
146
int
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
*/
157
int
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
*/
168
int
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
*/
179
int
QQuickCalendarModel
::
indexOf
(
int
year,
int
month)
const
180
{
181
return
indexOf(QDate(year, month + 1, 1));
182
}
183
184
QVariant
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
200
int
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
208
QHash
<
int
,
QByteArray
>
QQuickCalendarModel
::
roleNames
()
const
209
{
210
QHash<
int
, QByteArray> roles;
211
roles[
MonthRole
] = QByteArrayLiteral(
"month"
);
212
roles[
YearRole
] = QByteArrayLiteral(
"year"
);
213
return
roles;
214
}
215
216
void
QQuickCalendarModel
::
classBegin
()
217
{
218
}
219
220
void
QQuickCalendarModel
::
componentComplete
()
221
{
222
Q_D(QQuickCalendarModel);
223
d->complete =
true
;
224
d->populate(d->from, d->to,
true
);
225
}
226
227
QT_END_NAMESPACE
228
229
#
include
"moc_qquickcalendarmodel_p.cpp"
QQuickCalendarModelPrivate
A calendar model.
Definition
qquickcalendarmodel.cpp:37
QQuickCalendarModelPrivate::from
QDate from
Definition
qquickcalendarmodel.cpp:51
QQuickCalendarModelPrivate::to
QDate to
Definition
qquickcalendarmodel.cpp:52
QQuickCalendarModelPrivate::populate
void populate(QDate from, QDate to, bool force=false)
Definition
qquickcalendarmodel.cpp:75
QQuickCalendarModelPrivate::complete
bool complete
Definition
qquickcalendarmodel.cpp:50
QQuickCalendarModelPrivate::count
int count
Definition
qquickcalendarmodel.cpp:53
QQuickCalendarModel
Definition
qquickcalendarmodel_p.h:30
QQuickCalendarModel::yearAt
Q_INVOKABLE int yearAt(int index) const
\qmlmethod int QtQuick.Controls::CalendarModel::yearAt(int index)
Definition
qquickcalendarmodel.cpp:157
QQuickCalendarModel::to
QDate to() const
\qmlproperty date QtQuick.Controls::CalendarModel::to
Definition
qquickcalendarmodel.cpp:124
QQuickCalendarModel::classBegin
void classBegin() override
Invoked after class creation, but before any properties have been set.
Definition
qquickcalendarmodel.cpp:216
QQuickCalendarModel::indexOf
Q_INVOKABLE int indexOf(int year, int month) const
\qmlmethod int QtQuick.Controls::CalendarModel::indexOf(int year, int month)
Definition
qquickcalendarmodel.cpp:179
QQuickCalendarModel::from
QDate from() const
\qmlproperty date QtQuick.Controls::CalendarModel::from
Definition
qquickcalendarmodel.cpp:102
QQuickCalendarModel::componentComplete
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
Definition
qquickcalendarmodel.cpp:220
QQuickCalendarModel::data
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.
Definition
qquickcalendarmodel.cpp:184
QQuickCalendarModel::MonthRole
@ MonthRole
Definition
qquickcalendarmodel_p.h:54
QQuickCalendarModel::YearRole
@ YearRole
Definition
qquickcalendarmodel_p.h:55
QQuickCalendarModel::indexOf
Q_INVOKABLE int indexOf(QDate date) const
\qmlmethod int QtQuick.Controls::CalendarModel::indexOf(Date date)
Definition
qquickcalendarmodel.cpp:168
QQuickCalendarModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
Definition
qquickcalendarmodel.cpp:200
QQuickCalendarModel::setFrom
void setFrom(QDate from)
Definition
qquickcalendarmodel.cpp:108
QQuickCalendarModel::roleNames
QHash< int, QByteArray > roleNames() const override
Definition
qquickcalendarmodel.cpp:208
QQuickCalendarModel::monthAt
Q_INVOKABLE int monthAt(int index) const
\qmlmethod int QtQuick.Controls::CalendarModel::monthAt(int index)
Definition
qquickcalendarmodel.cpp:146
QQuickCalendarModel::setTo
void setTo(QDate to)
Definition
qquickcalendarmodel.cpp:130
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtdeclarative
src
quicktemplates
qquickcalendarmodel.cpp
Generated on
for Qt by
1.18.0