on xenial help sign in
make the timezone model filterable by both predefined country and user filter string
commit63783c6b6711138eed4761b24fab5b1f172729d7 authorLukáš Tinkl <lukas.tinkl@canonical.com> dateThu 29 Oct 2015, 21:58 (11 years ago) parent400057b
8 files changed+201−106
debian/control+2−1 plugins/Wizard/CMakeLists.txt+6−1 plugins/Wizard/plugin.cpp+1−1 plugins/Wizard/timezonemodel.cpp+110−63 plugins/Wizard/timezonemodel.h+64−37 qml/Wizard/Pages/50-timezone.qml+12−1 tests/mocks/Wizard/CMakeLists.txt+5−1 tests/mocks/Wizard/mockplugin.cpp+1−1
debian/control+2−1
@@ -8,7 +8,7 @@ Build-Depends: cmake,
88 doxyqml,
99# To allow cross-compiling to work, we also must append :native
1010# to g++ so we don't try to run armhf g++
11−# on an x86 CPU for eaxmple, when cross-compiling.
11+# on an x86 CPU for example, when cross-compiling.
1212 g++:native,
1313 libandroid-properties-dev,
1414 graphviz,
@@ -28,6 +28,7 @@ Build-Depends: cmake,
2828 libqt5svg5-dev,
2929 libqt5xmlpatterns5-dev,
3030 libsystemsettings-dev,
31+ libtimezonemap1-dev,
3132 libudev-dev,
3233 libunity-api-dev (>= 7.101),
3334 libusermetricsoutput1-dev,
plugins/Wizard/CMakeLists.txt+6−1
@@ -1,3 +1,7 @@
1+pkg_search_module(TIMEZONEMAP REQUIRED timezonemap)
2+
3+include_directories(${GLIB_INCLUDE_DIRS} ${TIMEZONEMAP_INCLUDE_DIRS})
4+
15add_library(Wizard-qml MODULE
26 plugin.cpp
37 PageList.cpp
@@ -7,7 +11,8 @@ add_library(Wizard-qml MODULE
711 Status.cpp
812)
913
10−qt5_use_modules(Wizard-qml DBus Qml)
14+qt5_use_modules(Wizard-qml DBus Qml Concurrent)
15+target_link_libraries(Wizard-qml ${GLIB_LDFLAGS} ${TIMEZONEMAP_LDFLAGS})
1116add_unity8_plugin(Wizard 0.1 Wizard TARGETS Wizard-qml)
1217
1318set(POLKIT_LIB_DIR "${CMAKE_INSTALL_LOCALSTATEDIR}/lib/polkit-1")
plugins/Wizard/plugin.cpp+1−1
@@ -29,7 +29,7 @@ void WizardPlugin::registerTypes(const char *uri)
2929 qmlRegisterType<PageList>(uri, 0, 1, "PageList");
3030 qmlRegisterSingletonType<System>(uri, 0, 1, "System", [](QQmlEngine*, QJSEngine*) -> QObject* { return new System; });
3131 qmlRegisterSingletonType<Status>(uri, 0, 1, "Status", [](QQmlEngine*, QJSEngine*) -> QObject* { return new Status; });
32− qmlRegisterType<TimeZoneModel>(uri, 0, 1, "TimeZoneModel");
32+ qmlRegisterType<TimeZoneLocationModel>(uri, 0, 1, "TimeZoneModel");
3333 qmlRegisterType<TimeZoneFilterModel>(uri, 0, 1, "TimeZoneFilterModel");
3434 qmlRegisterType<LocalePlugin>(uri, 0, 1, "LocalePlugin");
3535}
plugins/Wizard/timezonemodel.cpp+110−63
@@ -14,94 +14,141 @@
1414 * with this program. If not, see <http://www.gnu.org/licenses/>.
1515 */
1616
17−#include <QTimeZone>
1817#include <QDebug>
1918
19+#include <glib.h>
20+#include <glib-object.h>
21+#include <timezonemap/tz.h>
22+
2023#include "LocalePlugin.h"
2124#include "timezonemodel.h"
2225
2326
24−TimeZoneModel::TimeZoneModel(QObject *parent)
25− : QAbstractListModel(parent)
27+TimeZoneLocationModel::TimeZoneLocationModel(QObject *parent):
28+ QAbstractListModel(parent),
29+ m_workerThread(new TimeZonePopulateWorker())
2630{
27− m_roleNames = {{IdRole, "id"},
28− {Abbreviation, "abbreviation"},
29− {Country, "country"},
30− {CountryCode, "countryCode"},
31− {City, "city"},
32− {Comment, "comment"},
33− {Time, "time"}};
31+ qRegisterMetaType<TzLocation>();
32+
33+ m_roleNames[Qt::DisplayRole] = "displayName";
34+ m_roleNames[TimeZoneRole] = "timeZone";
35+ m_roleNames[CityRole] = "city";
36+ m_roleNames[CountryRole] = "country";
37+
38+ QObject::connect(m_workerThread,
39+ &TimeZonePopulateWorker::resultReady,
40+ this,
41+ &TimeZoneLocationModel::processModelResult);
42+ QObject::connect(m_workerThread,
43+ &TimeZonePopulateWorker::finished,
44+ this,
45+ &TimeZoneLocationModel::store);
46+ QObject::connect(m_workerThread,
47+ &TimeZonePopulateWorker::finished,
48+ m_workerThread,
49+ &QObject::deleteLater);
50+
3451 init();
3552}
3653
37−QString TimeZoneModel::selectedZoneId() const
54+void TimeZoneLocationModel::init()
3855{
39− return m_selectedZoneId;
56+ beginResetModel();
57+ m_workerThread->start();
4058}
4159
42−void TimeZoneModel::setSelectedZoneId(const QString &selectedZoneId)
60+void TimeZoneLocationModel::store()
4361{
44− if (m_selectedZoneId == selectedZoneId)
45− return;
46−
47− m_selectedZoneId = selectedZoneId;
48− Q_EMIT selectedZoneIdChanged(selectedZoneId);
62+ m_workerThread = nullptr;
63+ endResetModel();
4964}
5065
51−int TimeZoneModel::rowCount(const QModelIndex &parent) const
66+void TimeZoneLocationModel::processModelResult(const TzLocation &location)
5267{
53− Q_UNUSED(parent)
54− return m_zoneIds.count();
68+ m_locations.append(location);
5569}
5670
57−QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
71+int TimeZoneLocationModel::rowCount(const QModelIndex &parent) const
5872{
59− if (index.isValid()) {
60− const QByteArray tzid = m_zoneIds.at(index.row());
61− QTimeZone tz(tzid);
62−
63− if (!tz.isValid()) {
64− qWarning() << Q_FUNC_INFO << "Invalid timezone" << tzid;
65− return QVariant();
66− }
73+ if (parent.isValid())
74+ return 0;
75+ return m_locations.count();
76+}
6777
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− }
78+QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
79+{
80+ if (index.row() >= m_locations.count() || index.row() < 0)
81+ return QVariant();
82+
83+ const TzLocation tz = m_locations.at(index.row());
84+
85+ const QString country(tz.full_country.isEmpty() ? tz.country : tz.full_country);
86+
87+ switch (role) {
88+ case Qt::DisplayRole:
89+ if (!tz.state.isEmpty())
90+ return QStringLiteral("%1, %2, %3").arg(tz.city).arg(tz.state).arg(country);
91+ else
92+ return QStringLiteral("%1, %2").arg(tz.city).arg(country);
93+ case SimpleRole:
94+ return QStringLiteral("%1, %2").arg(tz.city).arg(country);
95+ case TimeZoneRole:
96+ return tz.timezone;
97+ case CountryRole:
98+ return tz.country;
99+ case CityRole:
100+ return tz.city;
101+ default:
102+ qWarning() << Q_FUNC_INFO << "Unknown role";
103+ return QVariant();
90104 }
91−
92− return QVariant();
93105}
94106
95−QHash<int, QByteArray> TimeZoneModel::roleNames() const
107+QHash<int, QByteArray> TimeZoneLocationModel::roleNames() const
96108{
97109 return m_roleNames;
98110}
99111
100−void TimeZoneModel::init()
112+void TimeZonePopulateWorker::run()
101113{
102− beginResetModel();
103− m_zoneIds = QTimeZone::availableTimeZoneIds();
104− endResetModel();
114+ buildCityMap();
115+}
116+
117+void TimeZonePopulateWorker::buildCityMap()
118+{
119+ TzDB *tzdb = tz_load_db();
120+ GPtrArray *tz_locations = tz_get_locations(tzdb);
121+
122+ TimeZoneLocationModel::TzLocation tmpTz;
123+
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,
128+ "country", &country,
129+ "zone", &zone,
130+ "state", &state,
131+ "full_country", &full_country,
132+ nullptr);
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;
138+ tmpTz.state = state;
139+ tmpTz.full_country = full_country;
140+ }
141+ g_free (en_name);
142+ g_free (country);
143+ g_free (zone);
144+ g_free (state);
145+ g_free (full_country);
146+
147+ Q_EMIT (resultReady(tmpTz));
148+ }
149+
150+ g_ptr_array_free (tz_locations, TRUE);
151+ tz_db_free(tzdb);
105152}
106153
107154
@@ -110,7 +157,7 @@ TimeZoneFilterModel::TimeZoneFilterModel(QObject *parent)
110157{
111158 setDynamicSortFilter(false);
112159 setSortLocaleAware(true);
113− setSortRole(TimeZoneModel::City);
160+ setSortRole(TimeZoneLocationModel::CityRole);
114161 m_stringMatcher.setCaseSensitivity(Qt::CaseInsensitive);
115162 sort(0);
116163}
@@ -122,14 +169,14 @@ bool TimeZoneFilterModel::filterAcceptsRow(int row, const QModelIndex &parentInd
122169 }
123170
124171 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();
172+ const QString city = sourceModel()->index(row, 0, parentIndex).data(TimeZoneLocationModel::CityRole).toString();
173+ //const QString country = sourceModel()->index(row, 0, parentIndex).data(TimeZoneLocationModel::CountryRole).toString();
127174
128175 if (m_stringMatcher.indexIn(city) == 0 /*|| m_stringMatcher.indexIn(country) == 0*/) { // match at the beginning of the city name
129176 return true;
130177 }
131178 } else if (!m_country.isEmpty()) { // filter by country code
132− const QString countryCode = sourceModel()->index(row, 0, parentIndex).data(TimeZoneModel::CountryCode).toString();
179+ const QString countryCode = sourceModel()->index(row, 0, parentIndex).data(TimeZoneLocationModel::CountryRole).toString();
133180 return m_country.compare(countryCode, Qt::CaseInsensitive) == 0;
134181 }
135182
plugins/Wizard/timezonemodel.h+64−37
@@ -19,6 +19,70 @@
1919
2020#include <QAbstractListModel>
2121#include <QSortFilterProxyModel>
22+#include <QThread>
23+#include <QtConcurrent>
24+
25+class TimeZonePopulateWorker;
26+
27+class TimeZoneLocationModel: public QAbstractListModel
28+{
29+ Q_OBJECT
30+ Q_ENUMS(Roles)
31+public:
32+ explicit TimeZoneLocationModel(QObject *parent = 0);
33+ ~TimeZoneLocationModel() = default;
34+
35+ enum Roles {
36+ TimeZoneRole = Qt::UserRole + 1,
37+ CityRole,
38+ CountryRole,
39+ SimpleRole
40+ };
41+
42+ struct TzLocation {
43+ bool operator<(const TzLocation &other) const
44+ {
45+ QString pattern("%1, %2");
46+ return pattern.arg(city).arg(country) <
47+ pattern.arg(other.city).arg(other.country);
48+ }
49+
50+ QString city;
51+ QString country;
52+ QString timezone;
53+ QString state;
54+ QString full_country;
55+ };
56+
57+ int rowCount(const QModelIndex &parent = QModelIndex()) const override;
58+ QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
59+ QHash<int, QByteArray> roleNames() const override;
60+
61+private Q_SLOTS:
62+ void processModelResult(const TzLocation &location);
63+ void store();
64+
65+private:
66+ void init();
67+ QHash<int, QByteArray> m_roleNames;
68+ QList<TzLocation> m_locations;
69+ TimeZonePopulateWorker *m_workerThread;
70+};
71+
72+Q_DECLARE_METATYPE (TimeZoneLocationModel::TzLocation)
73+
74+class TimeZonePopulateWorker: public QThread
75+{
76+ Q_OBJECT
77+public:
78+ void run() override;
79+
80+Q_SIGNALS:
81+ void resultReady(const TimeZoneLocationModel::TzLocation &tz);
82+
83+private:
84+ void buildCityMap();
85+};
2286
2387class TimeZoneFilterModel: public QSortFilterProxyModel
2488{
@@ -47,41 +111,4 @@ private:
47111 QString m_country;
48112};
49113
50−class TimeZoneModel: public QAbstractListModel
51−{
52− Q_OBJECT
53− Q_PROPERTY(QString selectedZoneId READ selectedZoneId WRITE setSelectedZoneId NOTIFY selectedZoneIdChanged)
54− Q_ENUMS(Roles)
55−public:
56− enum Roles {
57− IdRole = Qt::UserRole + 1,
58− Abbreviation,
59− Country,
60− CountryCode,
61− City,
62− Comment,
63− Time
64− };
65−
66− explicit TimeZoneModel(QObject *parent = nullptr);
67− ~TimeZoneModel() = default;
68−
69− QString selectedZoneId() const;
70− void setSelectedZoneId(const QString &selectedZoneId);
71−
72−protected:
73− int rowCount(const QModelIndex &parent) const override;
74− QVariant data(const QModelIndex &index, int role) const override;
75− QHash<int, QByteArray> roleNames() const override;
76−
77−Q_SIGNALS:
78− void selectedZoneIdChanged(const QString &selectedZoneId);
79−
80−private:
81− void init();
82− QByteArrayList m_zoneIds;
83− QString m_selectedZoneId;
84− QHash<int, QByteArray> m_roleNames;
85−};
86−
87114#endif
qml/Wizard/Pages/50-timezone.qml+12−1
@@ -32,7 +32,17 @@ LocalComponents.Page {
3232
3333 UbuntuTimeDatePanel {
3434 id: timeDatePanel
35+ }
36+
37+ TimeZoneModel {
38+ id: tzModel
39+ }
40+
41+ TimeZoneFilterModel {
42+ id: tzFilterModel
43+ sourceModel: tzModel
3544 filter: searchField.text
45+ country: root.countryCode
3646 }
3747
3848 Component.onCompleted: {
@@ -99,6 +109,7 @@ LocalComponents.Page {
99109 print("Clicked index", index)
100110 ListView.view.currentIndex = index
101111 selectedTimeZone = timeZone
112+ print("Clicked city", city)
102113 print("Clicked country", country)
103114 }
104115 }
@@ -153,7 +164,7 @@ LocalComponents.Page {
153164 }
154165
155166 height: column.height - searchField.height - customMargin - topMargin - divider.height
156− model: timeDatePanel.timeZoneModel
167+ model: tzFilterModel
157168 delegate: tzComponent
158169 }
159170 }
tests/mocks/Wizard/CMakeLists.txt+5−1
@@ -1,4 +1,7 @@
1+pkg_search_module(TIMEZONEMAP REQUIRED timezonemap)
2+
13include_directories(
4+ ${GLIB_INCLUDE_DIRS} ${TIMEZONEMAP_INCLUDE_DIRS}
25 ${CMAKE_SOURCE_DIR}/plugins/Wizard
36)
47
@@ -11,5 +14,6 @@ add_library(MockWizard-qml MODULE
1114 ${CMAKE_SOURCE_DIR}/plugins/Wizard/Status.cpp
1215)
1316
14−qt5_use_modules(MockWizard-qml DBus Qml)
17+qt5_use_modules(MockWizard-qml DBus Qml Concurrent)
18+target_link_libraries(MockWizard-qml ${GLIB_LDFLAGS} ${TIMEZONEMAP_LDFLAGS})
1519add_unity8_mock(Wizard 0.1 Wizard TARGETS MockWizard-qml)
tests/mocks/Wizard/mockplugin.cpp+1−1
@@ -29,7 +29,7 @@ void MockWizardPlugin::registerTypes(const char *uri)
2929 qmlRegisterType<PageList>(uri, 0, 1, "PageList");
3030 qmlRegisterSingletonType<MockSystem>(uri, 0, 1, "System", [](QQmlEngine*, QJSEngine*) -> QObject* { return new MockSystem; });
3131 qmlRegisterSingletonType<Status>(uri, 0, 1, "Status", [](QQmlEngine*, QJSEngine*) -> QObject* { return new Status; });
32− qmlRegisterType<TimeZoneModel>(uri, 0, 1, "TimeZoneModel");
32+ qmlRegisterType<TimeZoneLocationModel>(uri, 0, 1, "TimeZoneModel");
3333 qmlRegisterType<TimeZoneFilterModel>(uri, 0, 1, "TimeZoneFilterModel");
3434 qmlRegisterType<LocalePlugin>(uri, 0, 1, "LocalePlugin");
3535}