on 3dedc68 help sign in
unity8/plugins/Wizard/timezonemodel.cpp
136 lines 3.8 kB history raw
1/*
2 * Copyright (C) 2015 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Lukáš Tinkl <lukas.tinkl@canonical.com>
17 */
19#include <QTimeZone>
20#include <QDebug>
22#include "timezonemodel.h"
25TimeZoneModel::TimeZoneModel(QObject *parent)
26 : QAbstractListModel(parent)
28 init();
31QString TimeZoneModel::selectedZoneId() const
33 return m_selectedZoneId;
36void TimeZoneModel::setSelectedZoneId(const QString &selectedZoneId)
38 if (m_selectedZoneId == selectedZoneId)
39 return;
41 m_selectedZoneId = selectedZoneId;
42 Q_EMIT selectedZoneIdChanged(selectedZoneId);
45int TimeZoneModel::rowCount(const QModelIndex &parent) const
47 Q_UNUSED(parent)
48 return m_zoneIds.count();
51QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
53 if (index.isValid()) {
54 const QByteArray tzid = m_zoneIds.at(index.row());
55 QTimeZone tz(tzid);
57 if (!tz.isValid()) {
58 qWarning() << Q_FUNC_INFO << "Invalid timezone" << tzid;
59 return QVariant();
60 }
62 switch (role) {
63 case IdRole:
64 return QString(tz.id()); // to let QML compare it effortlessly with a QString
65 case Abbreviation:
66 return tz.abbreviation(QDateTime::currentDateTime());
67 case Country:
68 return QLocale::countryToString(tz.country());
69 case City: {
70 const QString cityName = QString::fromUtf8(tzid.split('/').last().replace("_", " "));
71 return cityName;
72 }
73 case Comment:
74 return tz.comment();
75 case Time:
76 return QDateTime::currentDateTime().toTimeZone(tz).toString("h:mm");
77 default:
78 qWarning() << Q_FUNC_INFO << "Unsupported data role" << role;
79 break;
80 }
81 }
83 return QVariant();
86QHash<int, QByteArray> TimeZoneModel::roleNames() const
88 return {{IdRole, "id"}, {Abbreviation, "abbreviation"}, {Country, "country"}, {City, "city"}, {Comment, "comment"}, {Time, "time"}};
91void TimeZoneModel::init()
93 beginResetModel();
94 m_zoneIds = QTimeZone::availableTimeZoneIds();
95 endResetModel();
99TimeZoneFilterModel::TimeZoneFilterModel(QObject *parent)
100 : QSortFilterProxyModel(parent)
102 setDynamicSortFilter(false);
103 setSortLocaleAware(true);
104 setSortRole(TimeZoneModel::City);
105 m_stringMatcher.setCaseSensitivity(Qt::CaseInsensitive);
106 sort(0);
109bool TimeZoneFilterModel::filterAcceptsRow(int row, const QModelIndex &parentIndex) const
111 if (!sourceModel() || m_filter.isEmpty()) {
112 return true;
113 }
115 const QString city = sourceModel()->index(row, 0, parentIndex).data(TimeZoneModel::City).toString();
116 const QString country = sourceModel()->index(row, 0, parentIndex).data(TimeZoneModel::Country).toString();
118 if (m_stringMatcher.indexIn(city) != -1 || m_stringMatcher.indexIn(country) != -1) {
119 return true;
120 }
122 return false;
125QString TimeZoneFilterModel::filter() const
127 return m_filter;
130void TimeZoneFilterModel::setFilter(const QString &filter)
132 m_filter = filter;
133 m_stringMatcher.setPattern(m_filter);
134 Q_EMIT filterChanged();
135 invalidate();