2 * Copyright (C) 2015 Canonical Ltd.
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.
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.
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/>.
20#include <glib-object.h>
21#include <timezonemap/tz.h>
23#include "LocalePlugin.h"
24#include "timezonemodel.h"
27TimeZoneLocationModel::TimeZoneLocationModel(QObject *parent):
28 QAbstractListModel(parent),
29 m_workerThread(new TimeZonePopulateWorker())
31 qRegisterMetaType<TzLocation>();
33 m_roleNames[Qt::DisplayRole] = "displayName";
34 m_roleNames[TimeZoneRole] = "timeZone";
35 m_roleNames[CityRole] = "city";
36 m_roleNames[CountryRole] = "country";
38 QObject::connect(m_workerThread,
39 &TimeZonePopulateWorker::resultReady,
41 &TimeZoneLocationModel::processModelResult);
42 QObject::connect(m_workerThread,
43 &TimeZonePopulateWorker::finished,
45 &TimeZoneLocationModel::store);
46 QObject::connect(m_workerThread,
47 &TimeZonePopulateWorker::finished,
49 &QObject::deleteLater);
54void TimeZoneLocationModel::init()
57 m_workerThread->start();
60void TimeZoneLocationModel::store()
62 m_workerThread = nullptr;
66void TimeZoneLocationModel::processModelResult(const TzLocation &location)
68 m_locations.append(location);
71int TimeZoneLocationModel::rowCount(const QModelIndex &parent) const
75 return m_locations.count();
78QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
80 if (index.row() >= m_locations.count() || index.row() < 0)
83 const TzLocation tz = m_locations.at(index.row());
85 const QString country(tz.full_country.isEmpty() ? tz.country : tz.full_country);
89 if (!tz.state.isEmpty())
90 return QStringLiteral("%1, %2, %3").arg(tz.city).arg(tz.state).arg(country);
92 return QStringLiteral("%1, %2").arg(tz.city).arg(country);
94 return QStringLiteral("%1, %2").arg(tz.city).arg(country);
102 qWarning() << Q_FUNC_INFO << "Unknown role";
107QHash<int, QByteArray> TimeZoneLocationModel::roleNames() const
112void TimeZonePopulateWorker::run()
117void TimeZonePopulateWorker::buildCityMap()
119 TzDB *tzdb = tz_load_db();
120 GPtrArray *tz_locations = tz_get_locations(tzdb);
122 TimeZoneLocationModel::TzLocation tmpTz;
124 for (guint i = 0; i < tz_locations->len; ++i) {
125 auto tmp = static_cast<CcTimezoneLocation *>(g_ptr_array_index(tz_locations, i));
126 gchar *en_name, *country, *zone, *state, *full_country;
127 g_object_get (tmp, "en_name", &en_name,
131 "full_country", &full_country,
133 // There are empty entries in the DB
134 if (g_strcmp0(en_name, "") != 0) {
135 tmpTz.city = en_name;
136 tmpTz.country = country;
137 tmpTz.timezone = zone;
139 tmpTz.full_country = full_country;
141 Q_EMIT (resultReady(tmpTz));
147 g_free (full_country);
150 g_ptr_array_free (tz_locations, TRUE);
155TimeZoneFilterModel::TimeZoneFilterModel(QObject *parent)
156 : QSortFilterProxyModel(parent)
158 setDynamicSortFilter(false);
159 setSortLocaleAware(true);
160 setSortRole(TimeZoneLocationModel::CityRole);
161 m_stringMatcher.setCaseSensitivity(Qt::CaseInsensitive);
165bool TimeZoneFilterModel::filterAcceptsRow(int row, const QModelIndex &parentIndex) const
167 if (!sourceModel()) {
171 if (!m_filter.isEmpty()) { // filtering by freeform text input, cf setFilter(QString)
172 const QString city = sourceModel()->index(row, 0, parentIndex).data(TimeZoneLocationModel::CityRole).toString();
174 if (m_stringMatcher.indexIn(city) == 0) { // match at the beginning of the city name
177 } else if (!m_country.isEmpty()) { // filter by country code
178 const QString countryCode = sourceModel()->index(row, 0, parentIndex).data(TimeZoneLocationModel::CountryRole).toString();
179 return m_country.compare(countryCode, Qt::CaseInsensitive) == 0;
185QString TimeZoneFilterModel::filter() const
190void TimeZoneFilterModel::setFilter(const QString &filter)
192 if (filter != m_filter) {
194 m_stringMatcher.setPattern(m_filter);
195 Q_EMIT filterChanged();
200QString TimeZoneFilterModel::country() const
205void TimeZoneFilterModel::setCountry(const QString &country)
207 if (m_country == country)
211 Q_EMIT countryChanged(country);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list