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 "qquickdayofweekrow_p.h" |
5 | #include "qquickdayofweekmodel_p.h" |
6 | |
7 | #include <QtQuickTemplates2/private/qquickcontrol_p_p.h> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | /*! |
12 | \qmltype DayOfWeekRow |
13 | \inherits Control |
14 | //! \instantiates QQuickDayOfWeekRow |
15 | \inqmlmodule QtQuick.Controls |
16 | \brief A row of names for the days in a week. |
17 | |
18 | DayOfWeekRow presents day of week names in a row. The names of the days |
19 | are ordered and formatted using the specified \l {Control::locale}{locale}. |
20 | |
21 | \image qtquickcontrols-dayofweekrow.png |
22 | \snippet qtquickcontrols-dayofweekrow.qml 1 |
23 | |
24 | DayOfWeekRow can be used as a standalone control, but it is most |
25 | often used in conjunction with MonthGrid. Regardless of the use case, |
26 | positioning of the row is left to the user. |
27 | |
28 | \image qtquickcontrols-dayofweekrow-layout.png |
29 | \snippet qtquickcontrols-dayofweekrow-layout.qml 1 |
30 | |
31 | The visual appearance of DayOfWeekRow can be changed by |
32 | implementing a \l {delegate}{custom delegate}. |
33 | |
34 | \sa MonthGrid, WeekNumberColumn |
35 | */ |
36 | |
37 | class QQuickDayOfWeekRowPrivate : public QQuickControlPrivate |
38 | { |
39 | public: |
40 | QQuickDayOfWeekRowPrivate() : delegate(nullptr), model(nullptr) { } |
41 | |
42 | void resizeItems(); |
43 | |
44 | QVariant source; |
45 | QQmlComponent *delegate; |
46 | QQuickDayOfWeekModel *model; |
47 | }; |
48 | |
49 | void QQuickDayOfWeekRowPrivate::resizeItems() |
50 | { |
51 | if (!contentItem) |
52 | return; |
53 | |
54 | QSizeF itemSize; |
55 | itemSize.setWidth((contentItem->width() - 6 * spacing) / 7); |
56 | itemSize.setHeight(contentItem->height()); |
57 | |
58 | const auto childItems = contentItem->childItems(); |
59 | for (QQuickItem *item : childItems) |
60 | item->setSize(itemSize); |
61 | } |
62 | |
63 | QQuickDayOfWeekRow::QQuickDayOfWeekRow(QQuickItem *parent) : |
64 | QQuickControl(*(new QQuickDayOfWeekRowPrivate), parent) |
65 | { |
66 | Q_D(QQuickDayOfWeekRow); |
67 | d->model = new QQuickDayOfWeekModel(this); |
68 | d->source = QVariant::fromValue(value: d->model); |
69 | } |
70 | |
71 | /*! |
72 | \internal |
73 | \qmlproperty model QtQuick.Controls::DayOfWeekRow::source |
74 | |
75 | This property holds the source model that is used as a data model |
76 | for the internal content row. |
77 | */ |
78 | QVariant QQuickDayOfWeekRow::source() const |
79 | { |
80 | Q_D(const QQuickDayOfWeekRow); |
81 | return d->source; |
82 | } |
83 | |
84 | void QQuickDayOfWeekRow::setSource(const QVariant &source) |
85 | { |
86 | Q_D(QQuickDayOfWeekRow); |
87 | if (d->source != source) { |
88 | d->source = source; |
89 | emit sourceChanged(); |
90 | } |
91 | } |
92 | |
93 | /*! |
94 | \qmlproperty Component QtQuick.Controls::DayOfWeekRow::delegate |
95 | |
96 | This property holds the item delegate that visualizes each day of the week. |
97 | |
98 | In addition to the \c index property, a list of model data roles |
99 | are available in the context of each delegate: |
100 | \table |
101 | \row |
102 | \li \b model.day : int |
103 | \li The day of week (\l Qt::DayOfWeek) |
104 | \row |
105 | \li \b model.longName : string |
106 | \li The long version of the day name; for example, |
107 | "Monday" (\l QLocale::LongFormat) |
108 | \row |
109 | \li \b model.shortName : string |
110 | \li The short version of the day name; for example, "Mon" |
111 | (\l QLocale::ShortFormat) |
112 | \row |
113 | \li \b model.narrowName : string |
114 | \li A special version of the day name for use when space is limited. |
115 | For example, "M" (\l QLocale::NarrowFormat) |
116 | \endtable |
117 | |
118 | The following snippet presents the default implementation of the item |
119 | delegate. It can be used as a starting point for implementing custom |
120 | delegates. |
121 | |
122 | \snippet basic/DayOfWeekRow.qml delegate |
123 | */ |
124 | QQmlComponent *QQuickDayOfWeekRow::delegate() const |
125 | { |
126 | Q_D(const QQuickDayOfWeekRow); |
127 | return d->delegate; |
128 | } |
129 | |
130 | void QQuickDayOfWeekRow::setDelegate(QQmlComponent *delegate) |
131 | { |
132 | Q_D(QQuickDayOfWeekRow); |
133 | if (d->delegate != delegate) { |
134 | d->delegate = delegate; |
135 | emit delegateChanged(); |
136 | } |
137 | } |
138 | |
139 | void QQuickDayOfWeekRow::componentComplete() |
140 | { |
141 | Q_D(QQuickDayOfWeekRow); |
142 | QQuickControl::componentComplete(); |
143 | d->resizeItems(); |
144 | } |
145 | |
146 | void QQuickDayOfWeekRow::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) |
147 | { |
148 | Q_D(QQuickDayOfWeekRow); |
149 | QQuickControl::geometryChange(newGeometry, oldGeometry); |
150 | if (isComponentComplete()) |
151 | d->resizeItems(); |
152 | } |
153 | |
154 | void QQuickDayOfWeekRow::localeChange(const QLocale &newLocale, const QLocale &oldLocale) |
155 | { |
156 | Q_D(QQuickDayOfWeekRow); |
157 | QQuickControl::localeChange(newLocale, oldLocale); |
158 | d->model->setLocale(newLocale); |
159 | } |
160 | |
161 | void QQuickDayOfWeekRow::paddingChange(const QMarginsF &newPadding, const QMarginsF &oldPadding) |
162 | { |
163 | Q_D(QQuickDayOfWeekRow); |
164 | QQuickControl::paddingChange(newPadding, oldPadding); |
165 | if (isComponentComplete()) |
166 | d->resizeItems(); |
167 | } |
168 | |
169 | QT_END_NAMESPACE |
170 | |
171 | #include "moc_qquickdayofweekrow_p.cpp" |
172 | |