on 3165eb4 help sign in
unity8/plugins/Wizard/timezonemodel.cpp
164 lines 4.7 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 */
17#include <QTimeZone>
18#include <QDebug>
20#include "LocalePlugin.h"
21#include "timezonemodel.h"
24TimeZoneModel::TimeZoneModel(QObject *parent)
25 : QAbstractListModel(parent)
27 m_roleNames = {{IdRole, "id"},
28 {Abbreviation, "abbreviation"},
29 {Country, "country"},
30 {CountryCode, "countryCode"},
31 {City, "city"},
32 {Comment, "comment"},
33 {Time, "time"}};
34 init();
37QString TimeZoneModel::selectedZoneId() const
39 return m_selectedZoneId;
42void TimeZoneModel::setSelectedZoneId(const QString &selectedZoneId)
44 if (m_selectedZoneId == selectedZoneId)
45 return;
47 m_selectedZoneId = selectedZoneId;
48 Q_EMIT selectedZoneIdChanged(selectedZoneId);
51int TimeZoneModel::rowCount(const QModelIndex &parent) const
53 Q_UNUSED(parent)
54 return m_zoneIds.count();
57QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
59 if (index.isValid()) {
60 const QByteArray tzid = m_zoneIds.at(index.row());
61 QTimeZone tz(tzid);
63 if (!tz.isValid()) {
64 qWarning() << Q_FUNC_INFO << "Invalid timezone" << tzid;
65 return QVariant();
66 }
68 switch (role) {
69 case IdRole:
70 return QString(tz.id()); // to let QML compare it effortlessly with a QString
71 case Abbreviation:
72 return tz.abbreviation(QDateTime::currentDateTime());
73 case Country:
74 return LocaleAttached::countryToString(tz.country());
75 case CountryCode: {
76 return LocaleAttached::qlocToCountryCode(tz.country());
77 }
78 case City: {
79 const QString cityName = QString::fromUtf8(tzid.split('/').last().replace('_', ' ')); // take the last part, replace _ by a space
80 return cityName;
81 }
82 case Comment:
83 return tz.comment();
84 case Time:
85 return QDateTime::currentDateTime().toTimeZone(tz).toString("h:mm");
86 default:
87 qWarning() << Q_FUNC_INFO << "Unsupported data role" << role;
88 break;
89 }
90 }
92 return QVariant();
95QHash<int, QByteArray> TimeZoneModel::roleNames() const
97 return m_roleNames;
100void TimeZoneModel::init()
102 beginResetModel();
103 m_zoneIds = QTimeZone::availableTimeZoneIds();
104 endResetModel();
108TimeZoneFilterModel::TimeZoneFilterModel(QObject *parent)
109 : QSortFilterProxyModel(parent)
111 setDynamicSortFilter(false);
112 setSortLocaleAware(true);
113 setSortRole(TimeZoneModel::City);
114 m_stringMatcher.setCaseSensitivity(Qt::CaseInsensitive);
115 sort(0);
118bool TimeZoneFilterModel::filterAcceptsRow(int row, const QModelIndex &parentIndex) const
120 if (!sourceModel()) {
121 return true;
122 }
124 if (!m_filter.isEmpty()) { // filtering by freeform text input, cf setFilter(QString)
125 const QString city = sourceModel()->index(row, 0, parentIndex).data(TimeZoneModel::City).toString();
126 const QString country = sourceModel()->index(row, 0, parentIndex).data(TimeZoneModel::Country).toString();
128 if (m_stringMatcher.indexIn(city) == 0 || m_stringMatcher.indexIn(country) == 0) { // match at the beginning of the city/country name
129 return true;
130 }
131 } else if (!m_country.isEmpty()) { // filter by country code
132 const QString countryCode = sourceModel()->index(row, 0, parentIndex).data(TimeZoneModel::CountryCode).toString();
133 return m_country.compare(countryCode, Qt::CaseInsensitive) == 0;
134 }
136 return false;
139QString TimeZoneFilterModel::filter() const
141 return m_filter;
144void TimeZoneFilterModel::setFilter(const QString &filter)
146 m_filter = filter;
147 m_stringMatcher.setPattern(m_filter);
148 Q_EMIT filterChanged();
149 invalidate();
152QString TimeZoneFilterModel::country() const
154 return m_country;
157void TimeZoneFilterModel::setCountry(const QString &country)
159 if (m_country == country)
160 return;
162 m_country = country;
163 Q_EMIT countryChanged(country);