Warning, file /frameworks/kholidays/src/declarative/holidayregionsmodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     This file is part of the kholidays library.
0003 
0004     SPDX-FileCopyrightText: 2015 Martin Klapetek <mklapetek@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "holidayregionsmodel.h"
0010 
0011 #include "../holidayregion.h"
0012 
0013 class HolidayRegionsDeclarativeModel::Private
0014 {
0015 public:
0016     QStringList regionCodes;
0017 };
0018 
0019 HolidayRegionsDeclarativeModel::HolidayRegionsDeclarativeModel(QObject *parent)
0020     : QAbstractTableModel(parent)
0021     , d(new Private())
0022 {
0023     d->regionCodes = KHolidays::HolidayRegion::regionCodes();
0024     // Make sure we don't add the same regions twice
0025     // This can happen if two copies of the data exists
0026     // in the system and both are read by KHolidays
0027     d->regionCodes.removeDuplicates();
0028 }
0029 
0030 HolidayRegionsDeclarativeModel::~HolidayRegionsDeclarativeModel()
0031 {
0032     delete d;
0033 }
0034 
0035 int HolidayRegionsDeclarativeModel::columnCount(const QModelIndex &parent) const
0036 {
0037     Q_UNUSED(parent);
0038     return 3;
0039 }
0040 
0041 int HolidayRegionsDeclarativeModel::rowCount(const QModelIndex &parent) const
0042 {
0043     if (parent.isValid()) {
0044         return 0;
0045     }
0046     return d->regionCodes.size();
0047 }
0048 
0049 QVariant HolidayRegionsDeclarativeModel::data(const QModelIndex &index, int role) const
0050 {
0051     if (!index.isValid()) {
0052         return QVariant();
0053     }
0054 
0055     const QString regionCode = d->regionCodes.at(index.row());
0056 
0057     switch (index.column()) {
0058     case RegionColumn:
0059         switch (role) {
0060         case Qt::DisplayRole:
0061         case HolidayRegionsDeclarativeModel::RegionRole:
0062             return regionCode;
0063         case HolidayRegionsDeclarativeModel::NameRole:
0064             return KHolidays::HolidayRegion::name(regionCode);
0065         case HolidayRegionsDeclarativeModel::DescriptionRole:
0066             return KHolidays::HolidayRegion::description(regionCode);
0067         }
0068         break;
0069     case NameColumn:
0070         if (role == Qt::DisplayRole) {
0071             return KHolidays::HolidayRegion::name(regionCode);
0072         }
0073         break;
0074     case DescriptionColumn:
0075         if (role == Qt::DisplayRole) {
0076             return KHolidays::HolidayRegion::description(regionCode);
0077         }
0078         break;
0079     }
0080 
0081     return QVariant();
0082 }
0083 
0084 QVariant HolidayRegionsDeclarativeModel::headerData(int section, Qt::Orientation orientation, int role) const
0085 {
0086     if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
0087         switch (section) {
0088         case RegionColumn:
0089             return tr("Region");
0090         case NameColumn:
0091             return tr("Name");
0092         case DescriptionColumn:
0093             return tr("Description");
0094         }
0095     }
0096     return QAbstractTableModel::headerData(section, orientation, role);
0097 }
0098 
0099 QHash<int, QByteArray> HolidayRegionsDeclarativeModel::roleNames() const
0100 {
0101     QHash<int, QByteArray> roles = QAbstractTableModel::roleNames();
0102     roles.insert(HolidayRegionsDeclarativeModel::RegionRole, QByteArrayLiteral("region"));
0103     roles.insert(HolidayRegionsDeclarativeModel::NameRole, QByteArrayLiteral("name"));
0104     roles.insert(HolidayRegionsDeclarativeModel::DescriptionRole, QByteArrayLiteral("description"));
0105 
0106     return roles;
0107 }