/**************************************************************************** ** ** Copyright (C) 2015 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Calendar module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL3$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 3 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPLv3 included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 3 requirements ** will be met: https://www.gnu.org/licenses/lgpl.html. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 2.0 or later as published by the Free ** Software Foundation and appearing in the file LICENSE.GPL included in ** the packaging of this file. Please review the following information to ** ensure the GNU General Public License version 2.0 requirements will be ** met: http://www.gnu.org/licenses/gpl-2.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qquickcalendarmodel_p.h" #include QT_BEGIN_NAMESPACE class QQuickCalendarModelPrivate : public QAbstractItemModelPrivate { Q_DECLARE_PUBLIC(QQuickCalendarModel) public: QQuickCalendarModelPrivate() : complete(false), from(1,1,1), to(275759, 9, 25), count(0) { } static int getCount(const QDate& from, const QDate &to); void populate(const QDate &from, const QDate &to, bool force = false); bool complete; QDate from; QDate to; int count; }; int QQuickCalendarModelPrivate::getCount(const QDate& from, const QDate &to) { QDate f(from.year(), from.month(), 1); QDate t(to.year(), to.month(), to.daysInMonth()); QDate r = QDate(1, 1, 1).addDays(f.daysTo(t)); int years = r.year() - 1; int months = r.month() - 1; return 12 * years + months + (r.day() / t.day()); } void QQuickCalendarModelPrivate::populate(const QDate &f, const QDate &t, bool force) { Q_Q(QQuickCalendarModel); if (!force && f == from && t == to) return; int c = getCount(from, to); if (c != count) { q->beginResetModel(); count = c; q->endResetModel(); emit q->countChanged(); } else { emit q->dataChanged(q->index(0, 0), q->index(c - 1, 0)); } } QQuickCalendarModel::QQuickCalendarModel(QObject *parent) : QAbstractListModel(*(new QQuickCalendarModelPrivate), parent) { } QDate QQuickCalendarModel::from() const { Q_D(const QQuickCalendarModel); return d->from; } void QQuickCalendarModel::setFrom(const QDate &from) { Q_D(QQuickCalendarModel); if (d->from != from) { if (d->complete) d->populate(from, d->to); d->from = from; emit fromChanged(); } } QDate QQuickCalendarModel::to() const { Q_D(const QQuickCalendarModel); return d->to; } void QQuickCalendarModel::setTo(const QDate &to) { Q_D(QQuickCalendarModel); if (d->to != to) { if (d->complete) d->populate(d->from, to); d->to = to; emit fromChanged(); } } int QQuickCalendarModel::monthAt(int index) const { Q_D(const QQuickCalendarModel); return d->from.addMonths(index).month(); } int QQuickCalendarModel::yearAt(int index) const { Q_D(const QQuickCalendarModel); return d->from.addMonths(index).year(); } int QQuickCalendarModel::indexOf(const QDate &date) const { return indexOf(date.year(), date.month()); } int QQuickCalendarModel::indexOf(int year, int month) const { Q_D(const QQuickCalendarModel); return d->getCount(d->from, QDate(year, month, 1)) - 1; } QVariant QQuickCalendarModel::data(const QModelIndex &index, int role) const { Q_D(const QQuickCalendarModel); if (index.isValid() && index.row() < d->count) { switch (role) { case MonthRole: return monthAt(index.row()); case YearRole: return yearAt(index.row()); default: break; } } return QVariant(); } int QQuickCalendarModel::rowCount(const QModelIndex &parent) const { Q_D(const QQuickCalendarModel); if (!parent.isValid()) return d->count; return 0; } QHash QQuickCalendarModel::roleNames() const { QHash roles; roles[MonthRole] = QByteArrayLiteral("month"); roles[YearRole] = QByteArrayLiteral("year"); return roles; } void QQuickCalendarModel::classBegin() { } void QQuickCalendarModel::componentComplete() { Q_D(QQuickCalendarModel); d->complete = true; d->populate(d->from, d->to, true); } QT_END_NAMESPACE