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 "LocalePlugin.h"
21#include "timezonemodel.h"
24TimeZoneModel::TimeZoneModel(QObject *parent)
25 : QAbstractListModel(parent)
27 m_roleNames = {{IdRole, "id"},
28 {Abbreviation, "abbreviation"},
30 {CountryCode, "countryCode"},
37QString TimeZoneModel::selectedZoneId() const
39 return m_selectedZoneId;
42void TimeZoneModel::setSelectedZoneId(const QString &selectedZoneId)
44 if (m_selectedZoneId == selectedZoneId)
47 m_selectedZoneId = selectedZoneId;
48 Q_EMIT selectedZoneIdChanged(selectedZoneId);
51int TimeZoneModel::rowCount(const QModelIndex &parent) const
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());
64 qWarning() << Q_FUNC_INFO << "Invalid timezone" << tzid;
70 return QString(tz.id()); // to let QML compare it effortlessly with a QString
72 return tz.abbreviation(QDateTime::currentDateTime());
74 return LocaleAttached::countryToString(tz.country());
76 return LocaleAttached::qlocToCountryCode(tz.country());
79 const QString cityName = QString::fromUtf8(tzid.split('/').last().replace('_', ' ')); // take the last part, replace _ by a space
85 return QDateTime::currentDateTime().toTimeZone(tz).toString("h:mm");
87 qWarning() << Q_FUNC_INFO << "Unsupported data role" << role;
95QHash<int, QByteArray> TimeZoneModel::roleNames() const
100void TimeZoneModel::init()
103 m_zoneIds = QTimeZone::availableTimeZoneIds();
108TimeZoneFilterModel::TimeZoneFilterModel(QObject *parent)
109 : QSortFilterProxyModel(parent)
111 setDynamicSortFilter(false);
112 setSortLocaleAware(true);
113 setSortRole(TimeZoneModel::City);
114 m_stringMatcher.setCaseSensitivity(Qt::CaseInsensitive);
118bool TimeZoneFilterModel::filterAcceptsRow(int row, const QModelIndex &parentIndex) const
120 if (!sourceModel()) {
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
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;
139QString TimeZoneFilterModel::filter() const
144void TimeZoneFilterModel::setFilter(const QString &filter)
147 m_stringMatcher.setPattern(m_filter);
148 Q_EMIT filterChanged();
152QString TimeZoneFilterModel::country() const
157void TimeZoneFilterModel::setCountry(const QString &country)
159 if (m_country == country)
163 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