File indexing completed on 2024-11-24 04:50:38

0001 // SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "timezonelistmodel.h"
0005 #include "merkuro_calendar_debug.h"
0006 #include <KLocalizedString>
0007 #include <QByteArray>
0008 #include <QMetaEnum>
0009 #include <QTimeZone>
0010 
0011 TimeZoneListModel::TimeZoneListModel(QObject *parent)
0012     : QAbstractListModel(parent)
0013 {
0014     // Reimplementation of IncidenceEditorPage's KTimeZoneComboBox
0015     // Read all system time zones
0016     const QList<QByteArray> lstTimeZoneIds = QTimeZone::availableTimeZoneIds();
0017     m_timeZones.reserve(lstTimeZoneIds.count());
0018 
0019     std::copy(lstTimeZoneIds.begin(), lstTimeZoneIds.end(), std::back_inserter(m_timeZones));
0020     std::sort(m_timeZones.begin(), m_timeZones.end()); // clazy:exclude=detaching-member
0021 
0022     // Prepend Local, UTC and Floating, for convenience
0023     m_timeZones.prepend("UTC"); // do not use i18n here  index=2
0024     m_timeZones.prepend("Floating"); // do not use i18n here  index=1
0025     m_timeZones.prepend(QTimeZone::systemTimeZoneId()); // index=0
0026 }
0027 
0028 QVariant TimeZoneListModel::data(const QModelIndex &idx, int role) const
0029 {
0030     if (!hasIndex(idx.row(), idx.column())) {
0031         return {};
0032     }
0033     auto timeZone = m_timeZones[idx.row()];
0034     switch (role) {
0035     case Qt::DisplayRole:
0036         return i18n(timeZone.replace('_', ' ').constData());
0037     case IdRole:
0038         return timeZone;
0039     default:
0040         qCWarning(MERKURO_CALENDAR_LOG) << "Unknown role for timezone:" << QMetaEnum::fromType<Roles>().valueToKey(role);
0041         return {};
0042     }
0043 }
0044 
0045 QHash<int, QByteArray> TimeZoneListModel::roleNames() const
0046 {
0047     return {
0048         {Qt::DisplayRole, QByteArrayLiteral("display")},
0049         {IdRole, QByteArrayLiteral("id")},
0050     };
0051 }
0052 
0053 int TimeZoneListModel::rowCount(const QModelIndex &) const
0054 {
0055     return m_timeZones.length();
0056 }
0057 
0058 int TimeZoneListModel::getTimeZoneRow(const QByteArray &timeZone)
0059 {
0060     for (int i = 0; i < rowCount(); i++) {
0061         QModelIndex idx = index(i, 0);
0062         QVariant data = idx.data(IdRole).toByteArray();
0063 
0064         if (data == timeZone)
0065             return i;
0066     }
0067 
0068     return 0;
0069 }
0070 
0071 #include "moc_timezonelistmodel.cpp"