File indexing completed on 2024-05-12 05:46:44

0001 /*
0002   This file is part of the kholidays library.
0003 
0004   Copyright (c) 2015 Martin Klapetek <mklapetek@kde.org>
0005 
0006   This library is free software; you can redistribute it and/or
0007   modify it under the terms of the GNU Library General Public
0008   License as published by the Free Software Foundation; either
0009   version 2 of the License, or (at your option) any later version.
0010 
0011   This library is distributed in the hope that it will be useful,
0012   but WITHOUT ANY WARRANTY; without even the implied warranty of
0013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014   GNU Library General Public License for more details.
0015 
0016   You should have received a copy of the GNU Library General Public License
0017   along with this library; see the file COPYING.LIB.  If not, write to the
0018   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019   Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include "holidayregionsmodel.h"
0023 
0024 #include "../holidayregion.h"
0025 
0026 class HolidayRegionsDeclarativeModel::Private
0027 {
0028 public:
0029     QStringList regionCodes;
0030 };
0031 
0032 HolidayRegionsDeclarativeModel::HolidayRegionsDeclarativeModel(QObject *parent)
0033     : QAbstractListModel(parent),
0034       d(new Private())
0035 {
0036     d->regionCodes = KHolidays::HolidayRegion::regionCodes();
0037     // Make sure we don't add the same regions twice
0038     // This can happen if two copies of the data exists
0039     // in the system and both are read by KHolidays
0040     d->regionCodes.removeDuplicates();
0041 }
0042 
0043 HolidayRegionsDeclarativeModel::~HolidayRegionsDeclarativeModel()
0044 {
0045     delete d;
0046 }
0047 
0048 int HolidayRegionsDeclarativeModel::rowCount(const QModelIndex &parent) const
0049 {
0050     Q_UNUSED(parent)
0051 
0052     return d->regionCodes.size();
0053 }
0054 
0055 QVariant HolidayRegionsDeclarativeModel::data(const QModelIndex &index, int role) const
0056 {
0057     if (!index.isValid()) {
0058         return QVariant();
0059     }
0060 
0061     const QString regionCode = d->regionCodes.at(index.row());
0062 
0063     switch (role) {
0064     case HolidayRegionsDeclarativeModel::RegionRole:
0065         return regionCode;
0066     case HolidayRegionsDeclarativeModel::NameRole:
0067         return KHolidays::HolidayRegion::name(regionCode);
0068     case HolidayRegionsDeclarativeModel::DescriptionRole:
0069         return KHolidays::HolidayRegion::description(regionCode);
0070     }
0071 
0072     return QVariant();
0073 }
0074 
0075 QHash<int, QByteArray> HolidayRegionsDeclarativeModel::roleNames() const
0076 {
0077     QHash<int, QByteArray> roles;
0078     roles.insert(HolidayRegionsDeclarativeModel::RegionRole, QByteArrayLiteral("region"));
0079     roles.insert(HolidayRegionsDeclarativeModel::NameRole, QByteArrayLiteral("name"));
0080     roles.insert(HolidayRegionsDeclarativeModel::DescriptionRole, QByteArrayLiteral("description"));
0081 
0082     return roles;
0083 }