| 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 "qquickweeknumbercolumn_p.h" |
| 5 | #include "qquickweeknumbermodel_p.h" |
| 6 | |
| 7 | #include <QtQuickTemplates2/private/qquickcontrol_p_p.h> |
| 8 | #include <QtQml/qqmlinfo.h> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | /*! |
| 13 | \qmltype WeekNumberColumn |
| 14 | \inherits Control |
| 15 | //! \nativetype QQuickWeekNumberColumn |
| 16 | \inqmlmodule QtQuick.Controls |
| 17 | \brief A column of week numbers. |
| 18 | |
| 19 | WeekNumberColumn presents week numbers in a column. The week numbers |
| 20 | are calculated for a given \l month and \l year, using the specified |
| 21 | \l {Control::locale}{locale}. |
| 22 | |
| 23 | \image qtquickcontrols-weeknumbercolumn.png |
| 24 | \snippet qtquickcontrols-weeknumbercolumn.qml 1 |
| 25 | |
| 26 | WeekNumberColumn can be used as a standalone control, but it is most |
| 27 | often used in conjunction with MonthGrid. Regardless of the use case, |
| 28 | positioning of the column is left to the user. |
| 29 | |
| 30 | \image qtquickcontrols-weeknumbercolumn-layout.png |
| 31 | \snippet qtquickcontrols-weeknumbercolumn-layout.qml 1 |
| 32 | |
| 33 | The visual appearance of WeekNumberColumn can be changed by |
| 34 | implementing a \l {delegate}{custom delegate}. |
| 35 | |
| 36 | \sa MonthGrid, DayOfWeekRow |
| 37 | */ |
| 38 | |
| 39 | class QQuickWeekNumberColumnPrivate : public QQuickControlPrivate |
| 40 | { |
| 41 | public: |
| 42 | QQuickWeekNumberColumnPrivate() : delegate(nullptr), model(nullptr) { } |
| 43 | |
| 44 | void resizeItems(); |
| 45 | |
| 46 | QVariant source; |
| 47 | QQmlComponent *delegate; |
| 48 | QQuickWeekNumberModel *model; |
| 49 | }; |
| 50 | |
| 51 | void QQuickWeekNumberColumnPrivate::resizeItems() |
| 52 | { |
| 53 | if (!contentItem) |
| 54 | return; |
| 55 | |
| 56 | QSizeF itemSize; |
| 57 | itemSize.setWidth(contentItem->width()); |
| 58 | itemSize.setHeight((contentItem->height() - 5 * spacing) / 6); |
| 59 | |
| 60 | const auto childItems = contentItem->childItems(); |
| 61 | for (QQuickItem *item : childItems) |
| 62 | item->setSize(itemSize); |
| 63 | } |
| 64 | |
| 65 | QQuickWeekNumberColumn::QQuickWeekNumberColumn(QQuickItem *parent) : |
| 66 | QQuickControl(*(new QQuickWeekNumberColumnPrivate), parent) |
| 67 | { |
| 68 | Q_D(QQuickWeekNumberColumn); |
| 69 | d->model = new QQuickWeekNumberModel(this); |
| 70 | d->source = QVariant::fromValue(value: d->model); |
| 71 | connect(sender: d->model, signal: &QQuickWeekNumberModel::monthChanged, context: this, slot: &QQuickWeekNumberColumn::monthChanged); |
| 72 | connect(sender: d->model, signal: &QQuickWeekNumberModel::yearChanged, context: this, slot: &QQuickWeekNumberColumn::yearChanged); |
| 73 | } |
| 74 | |
| 75 | /*! |
| 76 | \qmlproperty int QtQuick.Controls::WeekNumberColumn::month |
| 77 | |
| 78 | This property holds the number of the month that the week numbers are |
| 79 | calculated for. The default value is the current month. |
| 80 | |
| 81 | \include zero-based-months.qdocinc |
| 82 | |
| 83 | \sa Calendar |
| 84 | */ |
| 85 | int QQuickWeekNumberColumn::month() const |
| 86 | { |
| 87 | Q_D(const QQuickWeekNumberColumn); |
| 88 | return d->model->month() - 1; |
| 89 | } |
| 90 | |
| 91 | void QQuickWeekNumberColumn::setMonth(int month) |
| 92 | { |
| 93 | Q_D(QQuickWeekNumberColumn); |
| 94 | if (month < 0 || month > 11) { |
| 95 | qmlWarning(me: this) << "month " << month << " is out of range [0...11]" ; |
| 96 | return; |
| 97 | } |
| 98 | d->model->setMonth(month + 1); |
| 99 | } |
| 100 | |
| 101 | /*! |
| 102 | \qmlproperty int QtQuick.Controls::WeekNumberColumn::year |
| 103 | |
| 104 | This property holds the number of the year that the week numbers are calculated for. |
| 105 | |
| 106 | The value must be in the range from \c -271820 to \c 275759. The default |
| 107 | value is the current year. |
| 108 | */ |
| 109 | int QQuickWeekNumberColumn::year() const |
| 110 | { |
| 111 | Q_D(const QQuickWeekNumberColumn); |
| 112 | return d->model->year(); |
| 113 | } |
| 114 | |
| 115 | void QQuickWeekNumberColumn::setYear(int year) |
| 116 | { |
| 117 | Q_D(QQuickWeekNumberColumn); |
| 118 | if (year < -271820 || year > 275759) { |
| 119 | qmlWarning(me: this) << "year " << year << " is out of range [-271820...275759]" ; |
| 120 | return; |
| 121 | } |
| 122 | d->model->setYear(year); |
| 123 | } |
| 124 | |
| 125 | /*! |
| 126 | \internal |
| 127 | \qmlproperty model QtQuick.Controls::WeekNumberColumn::source |
| 128 | |
| 129 | This property holds the source model that is used as a data model |
| 130 | for the internal content column. |
| 131 | */ |
| 132 | QVariant QQuickWeekNumberColumn::source() const |
| 133 | { |
| 134 | Q_D(const QQuickWeekNumberColumn); |
| 135 | return d->source; |
| 136 | } |
| 137 | |
| 138 | void QQuickWeekNumberColumn::setSource(const QVariant &source) |
| 139 | { |
| 140 | Q_D(QQuickWeekNumberColumn); |
| 141 | if (d->source != source) { |
| 142 | d->source = source; |
| 143 | emit sourceChanged(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /*! |
| 148 | \qmlproperty Component QtQuick.Controls::WeekNumberColumn::delegate |
| 149 | |
| 150 | This property holds the item delegate that visualizes each week number. |
| 151 | |
| 152 | In addition to the \c index property, a list of model data roles |
| 153 | are available in the context of each delegate: |
| 154 | \table |
| 155 | \row \li \b model.weekNumber : int \li The week number |
| 156 | \endtable |
| 157 | |
| 158 | The following snippet presents the default implementation of the item |
| 159 | delegate. It can be used as a starting point for implementing custom |
| 160 | delegates. |
| 161 | |
| 162 | \snippet basic/WeekNumberColumn.qml delegate |
| 163 | */ |
| 164 | QQmlComponent *QQuickWeekNumberColumn::delegate() const |
| 165 | { |
| 166 | Q_D(const QQuickWeekNumberColumn); |
| 167 | return d->delegate; |
| 168 | } |
| 169 | |
| 170 | void QQuickWeekNumberColumn::setDelegate(QQmlComponent *delegate) |
| 171 | { |
| 172 | Q_D(QQuickWeekNumberColumn); |
| 173 | if (d->delegate != delegate) { |
| 174 | d->delegate = delegate; |
| 175 | emit delegateChanged(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void QQuickWeekNumberColumn::componentComplete() |
| 180 | { |
| 181 | Q_D(QQuickWeekNumberColumn); |
| 182 | QQuickControl::componentComplete(); |
| 183 | d->resizeItems(); |
| 184 | } |
| 185 | |
| 186 | void QQuickWeekNumberColumn::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) |
| 187 | { |
| 188 | Q_D(QQuickWeekNumberColumn); |
| 189 | QQuickControl::geometryChange(newGeometry, oldGeometry); |
| 190 | if (isComponentComplete()) |
| 191 | d->resizeItems(); |
| 192 | } |
| 193 | |
| 194 | void QQuickWeekNumberColumn::localeChange(const QLocale &newLocale, const QLocale &oldLocale) |
| 195 | { |
| 196 | Q_D(QQuickWeekNumberColumn); |
| 197 | QQuickControl::localeChange(newLocale, oldLocale); |
| 198 | d->model->setLocale(newLocale); |
| 199 | } |
| 200 | |
| 201 | void QQuickWeekNumberColumn::paddingChange(const QMarginsF &newPadding, const QMarginsF &oldPadding) |
| 202 | { |
| 203 | Q_D(QQuickWeekNumberColumn); |
| 204 | QQuickControl::paddingChange(newPadding, oldPadding); |
| 205 | if (isComponentComplete()) |
| 206 | d->resizeItems(); |
| 207 | } |
| 208 | |
| 209 | QT_END_NAMESPACE |
| 210 | |
| 211 | #include "moc_qquickweeknumbercolumn_p.cpp" |
| 212 | |