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