on xenial help sign in
extend the timezone model with country code

to be able to filter the list by it

commita3651e85132a8fd137f66366ea386fc940a08206 authorLukáš Tinkl <lukas.tinkl@canonical.com> dateMon 20 Jul 2015, 19:43 (11 years ago) parent5f8fcb7
7 files changed+33−11
plugins/Wizard/LocalePlugin.cpp+10 plugins/Wizard/LocalePlugin.h+1 plugins/Wizard/timezonemodel.cpp+12−4 plugins/Wizard/timezonemodel.h+2 qml/Wizard/Pages.qml+2−1 qml/Wizard/Pages/15-country.qml+5−6 qml/Wizard/Pages/50-timezone.qml+1
plugins/Wizard/LocalePlugin.cpp+10
@@ -337,6 +337,16 @@ QString LocaleAttached::countryToString(QLocale::Country c)
337337 return d->countryToString(c);
338338}
339339
340+QString LocaleAttached::qlocToCountryCode(QLocale::Country c)
341+{
342+ const QList<QLocale> locales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, c);
343+ if (!locales.isEmpty()) {
344+ return locales.first().name().split('_').at(1);
345+ }
346+
347+ return QString();
348+}
349+
340350
341351LocalePlugin::LocalePlugin(QObject* parent)
342352 : QObject(parent)
plugins/Wizard/LocalePlugin.h+1
@@ -33,6 +33,7 @@ public:
3333 Q_INVOKABLE QJsonObject countriesForLanguage(const QString &code) const;
3434 Q_INVOKABLE QJsonObject countries() const;
3535 Q_INVOKABLE static QString countryToString(QLocale::Country c);
36+ Q_INVOKABLE static QString qlocToCountryCode(QLocale::Country c);
3637
3738 friend class LocalePlugin;
3839};
plugins/Wizard/timezonemodel.cpp+12−4
@@ -12,8 +12,6 @@
1212 *
1313 * You should have received a copy of the GNU General Public License along
1414 * with this program. If not, see <http://www.gnu.org/licenses/>.
15− *
16− * Author: Lukáš Tinkl <lukas.tinkl@canonical.com>
1715 */
1816
1917#include <QTimeZone>
@@ -26,6 +24,13 @@
2624TimeZoneModel::TimeZoneModel(QObject *parent)
2725 : QAbstractListModel(parent)
2826{
27+ m_roleNames = {{IdRole, "id"},
28+ {Abbreviation, "abbreviation"},
29+ {Country, "country"},
30+ {CountryCode, "countryCode"},
31+ {City, "city"},
32+ {Comment, "comment"},
33+ {Time, "time"}};
2934 init();
3035}
3136
@@ -67,8 +72,11 @@ QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
6772 return tz.abbreviation(QDateTime::currentDateTime());
6873 case Country:
6974 return LocaleAttached::countryToString(tz.country());
75+ case CountryCode: {
76+ return LocaleAttached::qlocToCountryCode(tz.country());
77+ }
7078 case City: {
71− const QString cityName = QString::fromUtf8(tzid.split('/').last().replace("_", " ")); // take the last part, replace _ by a space
79+ const QString cityName = QString::fromUtf8(tzid.split('/').last().replace('_', ' ')); // take the last part, replace _ by a space
7280 return cityName;
7381 }
7482 case Comment:
@@ -86,7 +94,7 @@ QVariant TimeZoneModel::data(const QModelIndex &index, int role) const
8694
8795QHash<int, QByteArray> TimeZoneModel::roleNames() const
8896{
89− return {{IdRole, "id"}, {Abbreviation, "abbreviation"}, {Country, "country"}, {City, "city"}, {Comment, "comment"}, {Time, "time"}};
97+ return m_roleNames;
9098}
9199
92100void TimeZoneModel::init()
plugins/Wizard/timezonemodel.h+2
@@ -51,6 +51,7 @@ public:
5151 IdRole = Qt::UserRole + 1,
5252 Abbreviation,
5353 Country,
54+ CountryCode,
5455 City,
5556 Comment,
5657 Time
@@ -74,6 +75,7 @@ private:
7475 void init();
7576 QByteArrayList m_zoneIds;
7677 QString m_selectedZoneId;
78+ QHash<int, QByteArray> m_roleNames;
7779};
7880
7981#endif
qml/Wizard/Pages.qml+2−1
@@ -31,7 +31,8 @@ Item {
3131 // the user exits the wizard.
3232 property int passwordMethod: UbuntuSecurityPrivacyPanel.Passphrase
3333 property string password: ""
34− property string language: ""
34+ property string language: "" // language code
35+ property string country: "" // country code, uppercase
3536
3637 UbuntuSecurityPrivacyPanel {
3738 id: securityPrivacy
qml/Wizard/Pages/15-country.qml+5−6
@@ -26,7 +26,6 @@ LocalComponents.Page {
2626 title: i18n.tr("Country")
2727 forwardButtonSourceComponent: forwardButton
2828
29− property string selectedCountry: ""
3029 readonly property var preferedCountries: LocalePlugin.countriesForLanguage(root.language)
3130
3231 Component.onCompleted: {
@@ -63,8 +62,10 @@ LocalComponents.Page {
6362
6463 function selectCountry(code, index)
6564 {
66− selectedCountry = code
65+ root.country = code
6766 regionsListView.currentIndex = index
67+ print("Selected country: " + root.country)
68+ print("Current index: " + regionsListView.currentIndex)
6869 }
6970
7071 UbuntuLanguagePlugin {
@@ -143,8 +144,6 @@ LocalComponents.Page {
143144
144145 onClicked: {
145146 selectCountry(code, index)
146− print("Selected country: " + selectedCountry)
147− print("Current index: " + ListView.view.currentIndex)
148147 }
149148 }
150149 }
@@ -154,11 +153,11 @@ LocalComponents.Page {
154153 id: forwardButton
155154 LocalComponents.StackButton {
156155 text: i18n.tr("Next")
157− enabled: selectedCountry !== ""
156+ enabled: root.country !== ""
158157 onClicked: {
159158 if (root.language !== plugin.languageCodes[plugin.currentLanguage]) {
160159 plugin.currentLanguage = plugin.languageCodes.indexOf(root.language)
161− System.updateSessionLanguage(root.language + "_" + selectedCountry); // also updates the locale
160+ System.updateSessionLanguage(root.language + "_" + root.country); // also updates the locale
162161 }
163162 pageStack.next()
164163 }
qml/Wizard/Pages/50-timezone.qml+1
@@ -82,6 +82,7 @@ LocalComponents.Page {
8282 onClicked: {
8383 tzModel.selectedZoneId = id
8484 print("Selected tz: " + selectedTimeZone)
85+ print("Clicked country", countryCode)
8586 }
8687 }
8788 }