MauiKit Calendar

timezonelistmodel.cpp
1// SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#include "timezonelistmodel.h"
5#include <KLocalizedString>
6#include <QByteArray>
7#include <QDebug>
8#include <QMetaEnum>
9#include <QTimeZone>
10
11TimeZoneListModel::TimeZoneListModel(QObject *parent)
12 : QAbstractListModel(parent)
13{
14 // Reimplementation of IncidenceEditorPage's KTimeZoneComboBox
15 // Read all system time zones
17 m_timeZones.reserve(lstTimeZoneIds.count());
18
19 std::copy(lstTimeZoneIds.begin(), lstTimeZoneIds.end(), std::back_inserter(m_timeZones));
20 std::sort(m_timeZones.begin(), m_timeZones.end()); // clazy:exclude=detaching-member
21
22 // Prepend Local, UTC and Floating, for convenience
23 m_timeZones.prepend("UTC"); // do not use i18n here index=2
24 m_timeZones.prepend("Floating"); // do not use i18n here index=1
25 m_timeZones.prepend(QTimeZone::systemTimeZoneId()); // index=0
26}
27
28QVariant TimeZoneListModel::data(const QModelIndex &idx, int role) const
29{
30 if (!hasIndex(idx.row(), idx.column())) {
31 return {};
32 }
33 auto timeZone = m_timeZones[idx.row()];
34 switch (role) {
35 case Qt::DisplayRole:
36 return i18n(timeZone.replace('_', ' ').constData());
37 case IdRole:
38 return timeZone;
39 default:
40 qWarning() << "Unknown role for timezone:" << QMetaEnum::fromType<Roles>().valueToKey(role);
41 return {};
42 }
43}
44
45QHash<int, QByteArray> TimeZoneListModel::roleNames() const
46{
47 return {
48 {Qt::DisplayRole, QByteArrayLiteral("display")},
49 {IdRole, QByteArrayLiteral("id")},
50 };
51}
52
53int TimeZoneListModel::rowCount(const QModelIndex &) const
54{
55 return m_timeZones.length();
56}
57
58int TimeZoneListModel::getTimeZoneRow(const QByteArray &timeZone)
59{
60 for (int i = 0; i < rowCount(); i++) {
61 QModelIndex idx = index(i, 0);
62 QVariant data = idx.data(IdRole).toByteArray();
63
64 if (data == timeZone)
65 return i;
66 }
67
68 return 0;
69}
QString i18n(const char *text, const TYPE &arg...)
bool hasIndex(int row, int column, const QModelIndex &parent) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
iterator begin()
qsizetype count() const const
iterator end()
T qobject_cast(QObject *object)
DisplayRole
QList< QByteArray > availableTimeZoneIds()
QByteArray systemTimeZoneId()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:50:32 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.