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 "qquickweeknumbercolumn_p.h"
38#include "qquickweeknumbermodel_p.h"
39
40#include <QtQuickTemplates2/private/qquickcontrol_p_p.h>
41#include <QtQml/qqmlinfo.h>
42
43QT_BEGIN_NAMESPACE
44
45/*!
46 \qmltype WeekNumberColumn
47 \inherits Control
48//! \instantiates QQuickWeekNumberColumn
49 \inqmlmodule Qt.labs.calendar
50 \brief A column of week numbers.
51
52 WeekNumberColumn presents week numbers in a column. The week numbers
53 are calculated for a given \l month and \l year, using the specified
54 \l {Control::locale}{locale}.
55
56 \image qtlabscalendar-weeknumbercolumn.png
57 \snippet qtlabscalendar-weeknumbercolumn.qml 1
58
59 WeekNumberColumn can be used as a standalone control, but it is most
60 often used in conjunction with MonthGrid. Regardless of the use case,
61 positioning of the column is left to the user.
62
63 \image qtlabscalendar-weeknumbercolumn-layout.png
64 \snippet qtlabscalendar-weeknumbercolumn-layout.qml 1
65
66 The visual appearance of WeekNumberColumn can be changed by
67 implementing a \l {delegate}{custom delegate}.
68
69 \labs
70
71 \sa MonthGrid, DayOfWeekRow
72*/
73
74class QQuickWeekNumberColumnPrivate : public QQuickControlPrivate
75{
76public:
77 QQuickWeekNumberColumnPrivate() : delegate(nullptr), model(nullptr) { }
78
79 void resizeItems();
80
81 QVariant source;
82 QQmlComponent *delegate;
83 QQuickWeekNumberModel *model;
84};
85
86void QQuickWeekNumberColumnPrivate::resizeItems()
87{
88 if (!contentItem)
89 return;
90
91 QSizeF itemSize;
92 itemSize.setWidth(contentItem->width());
93 itemSize.setHeight((contentItem->height() - 5 * spacing) / 6);
94
95 const auto childItems = contentItem->childItems();
96 for (QQuickItem *item : childItems)
97 item->setSize(itemSize);
98}
99
100QQuickWeekNumberColumn::QQuickWeekNumberColumn(QQuickItem *parent) :
101 QQuickControl(*(new QQuickWeekNumberColumnPrivate), parent)
102{
103 Q_D(QQuickWeekNumberColumn);
104 d->model = new QQuickWeekNumberModel(this);
105 d->source = QVariant::fromValue(value: d->model);
106 connect(sender: d->model, signal: &QQuickWeekNumberModel::monthChanged, receiver: this, slot: &QQuickWeekNumberColumn::monthChanged);
107 connect(sender: d->model, signal: &QQuickWeekNumberModel::yearChanged, receiver: this, slot: &QQuickWeekNumberColumn::yearChanged);
108}
109
110/*!
111 \qmlproperty int Qt.labs.calendar::WeekNumberColumn::month
112
113 This property holds the number of the month that the week numbers are
114 calculated for. The default value is the current month.
115
116 The Qt Labs Calendar module uses 0-based month numbers to be consistent
117 with the JavaScript Date type, that is used by the QML language. This
118 means that \c Date::getMonth() can be assigned to this property as is.
119 When dealing with dealing with month numbers directly, it is highly
120 recommended to use the following enumeration values to avoid confusion.
121
122 \value Calendar.January January (0)
123 \value Calendar.February February (1)
124 \value Calendar.March March (2)
125 \value Calendar.April April (3)
126 \value Calendar.May May (4)
127 \value Calendar.June June (5)
128 \value Calendar.July July (6)
129 \value Calendar.August August (7)
130 \value Calendar.September September (8)
131 \value Calendar.October October (9)
132 \value Calendar.November November (10)
133 \value Calendar.December December (11)
134
135 \sa Calendar
136*/
137int QQuickWeekNumberColumn::month() const
138{
139 Q_D(const QQuickWeekNumberColumn);
140 return d->model->month() - 1;
141}
142
143void QQuickWeekNumberColumn::setMonth(int month)
144{
145 Q_D(QQuickWeekNumberColumn);
146 if (month < 0 || month > 11) {
147 qmlWarning(me: this) << "month " << month << " is out of range [0...11]";
148 return;
149 }
150 d->model->setMonth(month + 1);
151}
152
153/*!
154 \qmlproperty int Qt.labs.calendar::WeekNumberColumn::year
155
156 This property holds the number of the year that the week numbers are calculated for.
157
158 The value must be in the range from \c -271820 to \c 275759. The default
159 value is the current year.
160*/
161int QQuickWeekNumberColumn::year() const
162{
163 Q_D(const QQuickWeekNumberColumn);
164 return d->model->year();
165}
166
167void QQuickWeekNumberColumn::setYear(int year)
168{
169 Q_D(QQuickWeekNumberColumn);
170 if (year < -271820 || year > 275759) {
171 qmlWarning(me: this) << "year " << year << " is out of range [-271820...275759]";
172 return;
173 }
174 d->model->setYear(year);
175}
176
177/*!
178 \internal
179 \qmlproperty model Qt.labs.calendar::WeekNumberColumn::source
180
181 This property holds the source model that is used as a data model
182 for the internal content column.
183*/
184QVariant QQuickWeekNumberColumn::source() const
185{
186 Q_D(const QQuickWeekNumberColumn);
187 return d->source;
188}
189
190void QQuickWeekNumberColumn::setSource(const QVariant &source)
191{
192 Q_D(QQuickWeekNumberColumn);
193 if (d->source != source) {
194 d->source = source;
195 emit sourceChanged();
196 }
197}
198
199/*!
200 \qmlproperty Component Qt.labs.calendar::WeekNumberColumn::delegate
201
202 This property holds the item delegate that visualizes each week number.
203
204 In addition to the \c index property, a list of model data roles
205 are available in the context of each delegate:
206 \table
207 \row \li \b model.weekNumber : int \li The week number
208 \endtable
209
210 The following snippet presents the default implementation of the item
211 delegate. It can be used as a starting point for implementing custom
212 delegates.
213
214 \snippet WeekNumberColumn.qml delegate
215*/
216QQmlComponent *QQuickWeekNumberColumn::delegate() const
217{
218 Q_D(const QQuickWeekNumberColumn);
219 return d->delegate;
220}
221
222void QQuickWeekNumberColumn::setDelegate(QQmlComponent *delegate)
223{
224 Q_D(QQuickWeekNumberColumn);
225 if (d->delegate != delegate) {
226 d->delegate = delegate;
227 emit delegateChanged();
228 }
229}
230
231void QQuickWeekNumberColumn::componentComplete()
232{
233 Q_D(QQuickWeekNumberColumn);
234 QQuickControl::componentComplete();
235 d->resizeItems();
236}
237
238void QQuickWeekNumberColumn::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
239{
240 Q_D(QQuickWeekNumberColumn);
241 QQuickControl::geometryChanged(newGeometry, oldGeometry);
242 if (isComponentComplete())
243 d->resizeItems();
244}
245
246void QQuickWeekNumberColumn::localeChange(const QLocale &newLocale, const QLocale &oldLocale)
247{
248 Q_D(QQuickWeekNumberColumn);
249 QQuickControl::localeChange(newLocale, oldLocale);
250 d->model->setLocale(newLocale);
251}
252
253void QQuickWeekNumberColumn::paddingChange(const QMarginsF &newPadding, const QMarginsF &oldPadding)
254{
255 Q_D(QQuickWeekNumberColumn);
256 QQuickControl::paddingChange(newPadding, oldPadding);
257 if (isComponentComplete())
258 d->resizeItems();
259}
260
261QT_END_NAMESPACE
262

source code of qtquickcontrols2/src/imports/calendar/qquickweeknumbercolumn.cpp