1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Labs Calendar module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickdayofweekrow_p.h"
38#include "qquickdayofweekmodel_p.h"
39
40#include <QtQuickTemplates2/private/qquickcontrol_p_p.h>
41
42QT_BEGIN_NAMESPACE
43
44/*!
45 \qmltype DayOfWeekRow
46 \inherits Control
47 \instantiates QQuickDayOfWeekRow
48 \inqmlmodule Qt.labs.calendar
49 \brief A row of names for the days in a week.
50
51 DayOfWeekRow presents day of week names in a row. The names of the days
52 are ordered and formatted using the specified \l {Control::locale}{locale}.
53
54 \image qtlabscalendar-dayofweekrow.png
55 \snippet qtlabscalendar-dayofweekrow.qml 1
56
57 DayOfWeekRow can be used as a standalone control, but it is most
58 often used in conjunction with MonthGrid. Regardless of the use case,
59 positioning of the row is left to the user.
60
61 \image qtlabscalendar-dayofweekrow-layout.png
62 \snippet qtlabscalendar-dayofweekrow-layout.qml 1
63
64 The visual appearance of DayOfWeekRow can be changed by
65 implementing a \l {delegate}{custom delegate}.
66
67 \labs
68
69 \sa MonthGrid, WeekNumberColumn
70*/
71
72class QQuickDayOfWeekRowPrivate : public QQuickControlPrivate
73{
74public:
75 QQuickDayOfWeekRowPrivate() : delegate(nullptr), model(nullptr) { }
76
77 void resizeItems();
78
79 QVariant source;
80 QQmlComponent *delegate;
81 QQuickDayOfWeekModel *model;
82};
83
84void QQuickDayOfWeekRowPrivate::resizeItems()
85{
86 if (!contentItem)
87 return;
88
89 QSizeF itemSize;
90 itemSize.setWidth((contentItem->width() - 6 * spacing) / 7);
91 itemSize.setHeight(contentItem->height());
92
93 const auto childItems = contentItem->childItems();
94 for (QQuickItem *item : childItems)
95 item->setSize(itemSize);
96}
97
98QQuickDayOfWeekRow::QQuickDayOfWeekRow(QQuickItem *parent) :
99 QQuickControl(*(new QQuickDayOfWeekRowPrivate), parent)
100{
101 Q_D(QQuickDayOfWeekRow);
102 d->model = new QQuickDayOfWeekModel(this);
103 d->source = QVariant::fromValue(d->model);
104}
105
106/*!
107 \internal
108 \qmlproperty model Qt.labs.calendar::DayOfWeekRow::source
109
110 This property holds the source model that is used as a data model
111 for the internal content row.
112*/
113QVariant QQuickDayOfWeekRow::source() const
114{
115 Q_D(const QQuickDayOfWeekRow);
116 return d->source;
117}
118
119void QQuickDayOfWeekRow::setSource(const QVariant &source)
120{
121 Q_D(QQuickDayOfWeekRow);
122 if (d->source != source) {
123 d->source = source;
124 emit sourceChanged();
125 }
126}
127
128/*!
129 \qmlproperty Component Qt.labs.calendar::DayOfWeekRow::delegate
130
131 This property holds the item delegate that visualizes each day of the week.
132
133 In addition to the \c index property, a list of model data roles
134 are available in the context of each delegate:
135 \table
136 \row \li \b model.day : int \li The day of week (\l Qt::DayOfWeek)
137 \row \li \b model.longName : string \li The long version of the day name; for example, "Monday" (\l QLocale::LongFormat)
138 \row \li \b model.shortName : string \li The short version of the day name; for example, "Mon" (\l QLocale::ShortFormat)
139 \row \li \b model.narrowName : string \li A special version of the day name for use when space is limited; for example, "M" (\l QLocale::NarrowFormat)
140 \endtable
141
142 The following snippet presents the default implementation of the item
143 delegate. It can be used as a starting point for implementing custom
144 delegates.
145
146 \snippet DayOfWeekRow.qml delegate
147*/
148QQmlComponent *QQuickDayOfWeekRow::delegate() const
149{
150 Q_D(const QQuickDayOfWeekRow);
151 return d->delegate;
152}
153
154void QQuickDayOfWeekRow::setDelegate(QQmlComponent *delegate)
155{
156 Q_D(QQuickDayOfWeekRow);
157 if (d->delegate != delegate) {
158 d->delegate = delegate;
159 emit delegateChanged();
160 }
161}
162
163void QQuickDayOfWeekRow::componentComplete()
164{
165 Q_D(QQuickDayOfWeekRow);
166 QQuickControl::componentComplete();
167 d->resizeItems();
168}
169
170void QQuickDayOfWeekRow::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
171{
172 Q_D(QQuickDayOfWeekRow);
173 QQuickControl::geometryChanged(newGeometry, oldGeometry);
174 if (isComponentComplete())
175 d->resizeItems();
176}
177
178void QQuickDayOfWeekRow::localeChange(const QLocale &newLocale, const QLocale &oldLocale)
179{
180 Q_D(QQuickDayOfWeekRow);
181 QQuickControl::localeChange(newLocale, oldLocale);
182 d->model->setLocale(newLocale);
183}
184
185void QQuickDayOfWeekRow::paddingChange(const QMarginsF &newPadding, const QMarginsF &oldPadding)
186{
187 Q_D(QQuickDayOfWeekRow);
188 QQuickControl::paddingChange(newPadding, oldPadding);
189 if (isComponentComplete())
190 d->resizeItems();
191}
192
193QT_END_NAMESPACE
194