on xenial help sign in
add a sample timezonemodel
commit1332e4cf448c9d4ccb9f0899b57f3d742e0a611e authorLukáš Tinkl <lukas.tinkl@canonical.com> dateMon 22 Jun 2015, 16:18 (11 years ago) parentc4c7e43
4 files changed+152
plugins/Wizard/CMakeLists.txt+1 plugins/Wizard/plugin.cpp+2 plugins/Wizard/timezonemodel.cpp+94 plugins/Wizard/timezonemodel.h+55
plugins/Wizard/CMakeLists.txt+1
@@ -2,6 +2,7 @@ add_library(Wizard-qml MODULE
22 plugin.cpp
33 PageList.cpp
44 System.cpp
5+ timezonemodel.cpp
56)
67
78qt5_use_modules(Wizard-qml DBus Qml)
plugins/Wizard/plugin.cpp+2
@@ -17,6 +17,7 @@
1717#include "plugin.h"
1818#include "PageList.h"
1919#include "System.h"
20+#include "timezonemodel.h"
2021
2122#include <QtQml/qqml.h>
2223
@@ -32,4 +33,5 @@ void WizardPlugin::registerTypes(const char *uri)
3233 Q_ASSERT(uri == QLatin1String("Wizard"));
3334 qmlRegisterType<PageList>(uri, 0, 1, "PageList");
3435 qmlRegisterSingletonType<System>(uri, 0, 1, "System", system_provider);
36+ qmlRegisterType<TimeZoneModel>(uri, 0, 1, "TimeZoneModel");
3537}
plugins/Wizard/timezonemodel.cppnew file+94
@@ -0,0 +1,94 @@
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+ */
18+
19+#include <QTimeZone>
20+#include <QDebug>
21+
22+#include "timezonemodel.h"
23+
24+
25+TimeZoneModel::TimeZoneModel(QObject *parent)
26+ : QAbstractListModel(parent)
27+{
28+ QMetaObject::invokeMethod(this, "init");
29+}
30+
31+QString TimeZoneModel::selectedZoneId() const
32+{
33+ return m_selectedZoneId;
34+}
35+
36+void TimeZoneModel::setSelectedZoneId(const QString &selectedZoneId)
37+{
38+ if (m_selectedZoneId == selectedZoneId)
39+ return;
40+
41+ m_selectedZoneId = selectedZoneId;
42+ Q_EMIT selectedZoneIdChanged(selectedZoneId);
43+}
44+
45+int TimeZoneModel::rowCount(const QModelIndex &parent) const
46+{
47+ Q_UNUSED(parent)
48+ return m_zoneIds.count();
49+}
50+
51+QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
52+{
53+ if (index.isValid()) {
54+ const QByteArray tzid = m_zoneIds.at(index.row());
55+ QTimeZone tz(tzid);
56+
57+ if (!tz.isValid()) {
58+ qWarning() << Q_FUNC_INFO << "Invalid timezone" << tzid;
59+ return QVariant();
60+ }
61+
62+ switch (role) {
63+ case IdRole:
64+ return tz.id();
65+ case Abbreviation:
66+ return tz.abbreviation(QDateTime::currentDateTime());
67+ case Country:
68+ return QLocale::countryToString(tz.country());
69+ case City: {
70+ const QString cityName = QString::fromUtf8(tzid.split('/').last());
71+ return cityName;
72+ }
73+ case Comment:
74+ return tz.comment();
75+ default:
76+ qWarning() << Q_FUNC_INFO << "Unsupported data role";
77+ break;
78+ }
79+ }
80+
81+ return QVariant();
82+}
83+
84+QHash<int, QByteArray> TimeZoneModel::roleNames() const
85+{
86+ return {{IdRole, "id"}, {Abbreviation, "abbreviation"}, {Country, "country"}, {City, "city"}, {Comment, "comment"}};
87+}
88+
89+void TimeZoneModel::init()
90+{
91+ beginResetModel();
92+ m_zoneIds = QTimeZone::availableTimeZoneIds();
93+ endResetModel();
94+}
plugins/Wizard/timezonemodel.hnew file+55
@@ -0,0 +1,55 @@
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+
17+#ifndef TIMEZONEMODEL_H
18+#define TIMEZONEMODEL_H
19+
20+#include <QAbstractListModel>
21+
22+class TimeZoneModel: public QAbstractListModel
23+{
24+ Q_OBJECT
25+ Q_PROPERTY(QString selectedZoneId READ selectedZoneId WRITE setSelectedZoneId NOTIFY selectedZoneIdChanged)
26+public:
27+ enum Roles {
28+ IdRole = Qt::UserRole + 1,
29+ Abbreviation,
30+ Country,
31+ City,
32+ Comment
33+ };
34+
35+ explicit TimeZoneModel(QObject *parent = nullptr);
36+ ~TimeZoneModel() = default;
37+
38+ QString selectedZoneId() const;
39+ void setSelectedZoneId(const QString &selectedZoneId);
40+
41+protected:
42+ int rowCount(const QModelIndex &parent) const override;
43+ QVariant data(const QModelIndex &index, int role) const override;
44+ QHash<int, QByteArray> roleNames() const override;
45+
46+Q_SIGNALS:
47+ void selectedZoneIdChanged(const QString &selectedZoneId);
48+
49+private:
50+ void init();
51+ QByteArrayList m_zoneIds;
52+ QString m_selectedZoneId;
53+};
54+
55+#endif