on 4752bd8 help sign in
unity8/plugins/Wizard/timezonemodel.cpp
137 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 "LocalePlugin.h"
23#include "timezonemodel.h"
26TimeZoneModel::TimeZoneModel(QObject *parent)
27 : QAbstractListModel(parent)
29 init();
32QString TimeZoneModel::selectedZoneId() const
34 return m_selectedZoneId;
37void TimeZoneModel::setSelectedZoneId(const QString &selectedZoneId)
39 if (m_selectedZoneId == selectedZoneId)
40 return;
42 m_selectedZoneId = selectedZoneId;
43 Q_EMIT selectedZoneIdChanged(selectedZoneId);
46int TimeZoneModel::rowCount(const QModelIndex &parent) const
48 Q_UNUSED(parent)
49 return m_zoneIds.count();
52QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
54 if (index.isValid()) {
55 const QByteArray tzid = m_zoneIds.at(index.row());
56 QTimeZone tz(tzid);
58 if (!tz.isValid()) {
59 qWarning() << Q_FUNC_INFO << "Invalid timezone" << tzid;
60 return QVariant();
61 }
63 switch (role) {
64 case IdRole:
65 return QString(tz.id()); // to let QML compare it effortlessly with a QString
66 case Abbreviation:
67 return tz.abbreviation(QDateTime::currentDateTime());
68 case Country:
69 return LocaleAttached::countryToString(tz.country());
70 case City: {
71 const QString cityName = QString::fromUtf8(tzid.split('/').last().replace("_", " ")); // take the last part, replace _ by a space
72 return cityName;
73 }
74 case Comment:
75 return tz.comment();
76 case Time:
77 return QDateTime::currentDateTime().toTimeZone(tz).toString("h:mm");
78 default:
79 qWarning() << Q_FUNC_INFO << "Unsupported data role" << role;
80 break;
81 }
82 }
84 return QVariant();
87QHash<int, QByteArray> TimeZoneModel::roleNames() const
89 return {{IdRole, "id"}, {Abbreviation, "abbreviation"}, {Country, "country"}, {City, "city"}, {Comment, "comment"}, {Time, "time"}};
92void TimeZoneModel::init()
94 beginResetModel();
95 m_zoneIds = QTimeZone::availableTimeZoneIds();
96 endResetModel();
100TimeZoneFilterModel::TimeZoneFilterModel(QObject *parent)
101 : QSortFilterProxyModel(parent)
103 setDynamicSortFilter(false);
104 setSortLocaleAware(true);
105 setSortRole(TimeZoneModel::City);
106 m_stringMatcher.setCaseSensitivity(Qt::CaseInsensitive);
107 sort(0);
110bool TimeZoneFilterModel::filterAcceptsRow(int row, const QModelIndex &parentIndex) const
112 if (!sourceModel() || m_filter.isEmpty()) {
113 return true;
114 }
116 const QString city = sourceModel()->index(row, 0, parentIndex).data(TimeZoneModel::City).toString();
117 const QString country = sourceModel()->index(row, 0, parentIndex).data(TimeZoneModel::Country).toString();
119 if (m_stringMatcher.indexIn(city) != -1 || m_stringMatcher.indexIn(country) != -1) {
120 return true;
121 }
123 return false;
126QString TimeZoneFilterModel::filter() const
128 return m_filter;
131void TimeZoneFilterModel::setFilter(const QString &filter)
133 m_filter = filter;
134 m_stringMatcher.setPattern(m_filter);
135 Q_EMIT filterChanged();
136 invalidate();