File indexing completed on 2024-04-28 04:40:44

0001 // SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
0002 // SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
0003 // SPDX-License-Identifier: LGPL-2.0-or-later
0004 
0005 #include "yearmodel.h"
0006 
0007 #include <QDate>
0008 #include <QCalendar>
0009 #include <QLocale>
0010 
0011 YearModel::YearModel(QObject *parent)
0012     : QAbstractListModel(parent)
0013 {
0014     setYear(QDate::currentDate().year());
0015 }
0016 
0017 YearModel::~YearModel()
0018 {}
0019 
0020 int YearModel::year() const
0021 {
0022     return m_year;
0023 }
0024 
0025 void YearModel::setYear(int year)
0026 {
0027     if (m_year == year) {
0028         return;
0029     }
0030     if (QCalendar().monthsInYear(m_year) != QCalendar().monthsInYear(year)) {
0031         beginResetModel();
0032         m_year = year;
0033         endResetModel();
0034         Q_EMIT yearChanged();
0035         return;
0036     }
0037     m_year = year;
0038     Q_EMIT yearChanged();
0039 }
0040 
0041 int YearModel::rowCount(const QModelIndex &parent) const
0042 {
0043     if (!parent.isValid()) {
0044         return QCalendar().monthsInYear(m_year);
0045     }
0046     return 0;
0047 }
0048 
0049 QVariant YearModel::data(const QModelIndex &index, int role) const
0050 {
0051     if (!checkIndex(index, CheckIndexOption::IndexIsValid)) {
0052         return QVariant();
0053     }
0054     if (role == Qt::DisplayRole) {
0055         // model indexes 0-11, months are 1-12
0056         return QLocale().monthName(index.row()+1, QLocale::ShortFormat);
0057     }
0058     return QVariant();
0059 }
0060 
0061 
0062 #include "moc_yearmodel.cpp"