File indexing completed on 2024-05-05 05:36:41

0001 /*
0002     SPDX-FileCopyrightText: 2014 Kai Uwe Broulik <kde@privat.broulik.de>
0003     SPDX-FileCopyrightText: 2014 Martin Klapetek <mklapetek@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include <QAbstractListModel>
0011 #include <QSortFilterProxyModel>
0012 #include <QTimeZone>
0013 
0014 #include "timezonedata.h"
0015 
0016 class TimezonesI18n;
0017 
0018 class TimeZoneFilterProxy : public QSortFilterProxyModel
0019 {
0020     Q_OBJECT
0021     Q_PROPERTY(QString filterString WRITE setFilterString MEMBER m_filterString NOTIFY filterStringChanged)
0022 
0023 public:
0024     explicit TimeZoneFilterProxy(QObject *parent = nullptr);
0025     bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
0026 
0027     void setFilterString(const QString &filterString);
0028 
0029 Q_SIGNALS:
0030     void filterStringChanged();
0031 
0032 private:
0033     QString m_filterString;
0034     QStringMatcher m_stringMatcher;
0035 };
0036 
0037 //=============================================================================
0038 
0039 class TimeZoneModel : public QAbstractListModel
0040 {
0041     Q_OBJECT
0042     Q_PROPERTY(QStringList selectedTimeZones WRITE setSelectedTimeZones MEMBER m_selectedTimeZones NOTIFY selectedTimeZonesChanged)
0043 
0044 public:
0045     explicit TimeZoneModel(QObject *parent = nullptr);
0046     ~TimeZoneModel() override;
0047 
0048     enum Roles {
0049         TimeZoneIdRole = Qt::UserRole + 1,
0050         RegionRole,
0051         CityRole,
0052         CommentRole,
0053         CheckedRole,
0054     };
0055 
0056     int rowCount(const QModelIndex &parent) const override;
0057     QVariant data(const QModelIndex &index, int role) const override;
0058     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0059 
0060     void update();
0061     void setSelectedTimeZones(const QStringList &selectedTimeZones);
0062 
0063     Q_INVOKABLE void selectLocalTimeZone();
0064 
0065 Q_SIGNALS:
0066     void selectedTimeZonesChanged();
0067 
0068 protected:
0069     QHash<int, QByteArray> roleNames() const override;
0070 
0071 private:
0072     void sortTimeZones();
0073 
0074     QList<TimeZoneData> m_data;
0075     QHash<QString, int> m_offsetData; // used for sorting
0076     QStringList m_selectedTimeZones;
0077     TimezonesI18n *m_timezonesI18n;
0078 };