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
qquickdayofweekmodel.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
12{
13 Q_DECLARE_PUBLIC(QQuickDayOfWeekModel)
14
15public:
17};
18
19QQuickDayOfWeekModel::QQuickDayOfWeekModel(QObject *parent) :
20 QAbstractListModel(*(new QQuickDayOfWeekModelPrivate), parent)
21{
22}
23
25{
26 Q_D(const QQuickDayOfWeekModel);
27 return d->locale;
28}
29
30void QQuickDayOfWeekModel::setLocale(const QLocale &locale)
31{
32 Q_D(QQuickDayOfWeekModel);
33 if (d->locale != locale) {
34 d->locale = locale;
35 emit localeChanged();
36 emit dataChanged(index(0, 0), index(6, 0));
37 }
38}
39
40int QQuickDayOfWeekModel::dayAt(int index) const
41{
42 Q_D(const QQuickDayOfWeekModel);
43 int day = d->locale.firstDayOfWeek() + index;
44 if (day > 7)
45 day -= 7;
46 if (day == 7)
47 day = 0; // Qt::Sunday = 7, but Sunday is 0 in JS Date
48 return day;
49}
50
51QVariant QQuickDayOfWeekModel::data(const QModelIndex &index, int role) const
52{
53 Q_D(const QQuickDayOfWeekModel);
54 if (index.isValid() && index.row() < 7) {
55 int day = dayAt(index.row());
56 switch (role) {
57 case DayRole:
58 return day;
59 case LongNameRole:
60 return d->locale.standaloneDayName(day == 0 ? Qt::Sunday : day, QLocale::LongFormat);
61 case ShortNameRole:
62 return d->locale.standaloneDayName(day == 0 ? Qt::Sunday : day, QLocale::ShortFormat);
63 case NarrowNameRole:
64 return d->locale.standaloneDayName(day == 0 ? Qt::Sunday : day, QLocale::NarrowFormat);
65 default:
66 break;
67 }
68 }
69 return QVariant();
70}
71
72int QQuickDayOfWeekModel::rowCount(const QModelIndex &parent) const
73{
74 if (parent.isValid())
75 return 0;
76 return 7;
77}
78
80{
81 QHash<int, QByteArray> roles;
82 roles[DayRole] = QByteArrayLiteral("day");
83 roles[LongNameRole] = QByteArrayLiteral("longName");
84 roles[ShortNameRole] = QByteArrayLiteral("shortName");
85 roles[NarrowNameRole] = QByteArrayLiteral("narrowName");
86 return roles;
87}
88
89QT_END_NAMESPACE
90
91#include "moc_qquickdayofweekmodel_p.cpp"
Q_INVOKABLE int dayAt(int index) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
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.