KHolidays

holidayregionsmodel.cpp
1/*
2 This file is part of the kholidays library.
3
4 SPDX-FileCopyrightText: 2015 Martin Klapetek <mklapetek@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "holidayregionsmodel.h"
10
11#include "../holidayregion.h"
12
13class HolidayRegionsDeclarativeModel::Private
14{
15public:
16 QStringList regionCodes;
17};
18
19HolidayRegionsDeclarativeModel::HolidayRegionsDeclarativeModel(QObject *parent)
20 : QAbstractTableModel(parent)
21 , d(new Private())
22{
24 // Make sure we don't add the same regions twice.
25 // This can happen if two copies of the data exist
26 // in the system and both are read by KHolidays.
27 d->regionCodes.removeDuplicates();
28}
29
30HolidayRegionsDeclarativeModel::~HolidayRegionsDeclarativeModel()
31{
32 delete d;
33}
34
35int HolidayRegionsDeclarativeModel::columnCount(const QModelIndex &parent) const
36{
37 Q_UNUSED(parent);
38 return 3;
39}
40
41int HolidayRegionsDeclarativeModel::rowCount(const QModelIndex &parent) const
42{
43 if (parent.isValid()) {
44 return 0;
45 }
46 return d->regionCodes.size();
47}
48
49QVariant HolidayRegionsDeclarativeModel::data(const QModelIndex &index, int role) const
50{
51 if (!index.isValid()) {
52 return QVariant();
53 }
54
55 const QString regionCode = d->regionCodes.at(index.row());
56
57 switch (index.column()) {
58 case RegionColumn:
59 switch (role) {
60 case Qt::DisplayRole:
61 case HolidayRegionsDeclarativeModel::RegionRole:
62 return regionCode;
63 case HolidayRegionsDeclarativeModel::NameRole:
64 return KHolidays::HolidayRegion::name(regionCode);
65 case HolidayRegionsDeclarativeModel::DescriptionRole:
67 }
68 break;
69 case NameColumn:
70 if (role == Qt::DisplayRole) {
71 return KHolidays::HolidayRegion::name(regionCode);
72 }
73 break;
74 case DescriptionColumn:
75 if (role == Qt::DisplayRole) {
77 }
78 break;
79 }
80
81 return QVariant();
82}
83
84QVariant HolidayRegionsDeclarativeModel::headerData(int section, Qt::Orientation orientation, int role) const
85{
86 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
87 switch (section) {
88 case RegionColumn:
89 return tr("Region");
90 case NameColumn:
91 return tr("Name");
92 case DescriptionColumn:
93 return tr("Description");
94 }
95 }
96 return QAbstractTableModel::headerData(section, orientation, role);
97}
98
99QHash<int, QByteArray> HolidayRegionsDeclarativeModel::roleNames() const
100{
102 roles.insert(HolidayRegionsDeclarativeModel::RegionRole, QByteArrayLiteral("region"));
103 roles.insert(HolidayRegionsDeclarativeModel::NameRole, QByteArrayLiteral("name"));
104 roles.insert(HolidayRegionsDeclarativeModel::DescriptionRole, QByteArrayLiteral("description"));
105
106 return roles;
107}
108
109#include "moc_holidayregionsmodel.cpp"
static QStringList regionCodes()
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const
virtual QHash< int, QByteArray > roleNames() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
iterator insert(const Key &key, const T &value)
const_reference at(qsizetype i) const const
qsizetype size() const const
int column() const const
bool isValid() const const
int row() const const
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
DisplayRole
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:11:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.