on xenial help sign in
Add default city list for the current country
commit9a5d0c741bd24eb909292b41ecfa7051190786b5 authorMichael Terry <michael.terry@canonical.com> dateMon 29 Feb 2016, 14:43 (11 years ago) parent6297dc3
2 files changed+38−5
plugins/Wizard/timezonemodel.cpp+37−5 plugins/Wizard/timezonemodel.h+1
plugins/Wizard/timezonemodel.cpp+37−5
@@ -38,18 +38,26 @@ TimeZoneLocationModel::TimeZoneLocationModel(QObject *parent):
3838
3939int TimeZoneLocationModel::rowCount(const QModelIndex &parent) const
4040{
41− if (parent.isValid())
41+ if (parent.isValid()) {
4242 return 0;
43− return m_locations.count();
43+ } else if (m_filter.isEmpty()) {
44+ return m_countryLocations.count();
45+ } else {
46+ return m_locations.count();
47+ }
4448}
4549
4650QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
4751{
48− if (index.row() >= m_locations.count() || index.row() < 0)
52+ GeonamesCity *city;
53+ if (m_filter.isEmpty()) {
54+ city = m_countryLocations.value(index.row());
55+ } else {
56+ city = m_locations.value(index.row());
57+ }
58+ if (!city)
4959 return QVariant();
5060
51− GeonamesCity *city = m_locations[index.row()];
52−
5361 switch (role) {
5462 case Qt::DisplayRole:
5563 return QVariant(QString("%1, %2, %3").arg(geonames_city_get_name(city))
@@ -185,12 +193,32 @@ QString TimeZoneLocationModel::country() const
185193 return m_country;
186194}
187195
196+static bool citycmp(GeonamesCity *a, GeonamesCity *b)
197+{
198+ return geonames_city_get_population(b) < geonames_city_get_population(a);
199+}
200+
188201void TimeZoneLocationModel::setCountry(const QString &country)
189202{
190203 if (m_country == country)
191204 return;
192205
193206 m_country = country;
207+
208+ Q_FOREACH(GeonamesCity *city, m_countryLocations) {
209+ geonames_city_free(city);
210+ }
211+
212+ gint num_cities = geonames_get_n_cities();
213+ for (gint i = 0; i < num_cities; i++) {
214+ GeonamesCity *city = geonames_get_city(i);
215+ if (city && m_country == geonames_city_get_country_code(city)) {
216+ m_countryLocations.append(city);
217+ }
218+ }
219+
220+ std::sort(m_countryLocations.begin(), m_countryLocations.end(), citycmp);
221+
194222 Q_EMIT countryChanged(country);
195223}
196224
@@ -201,6 +229,10 @@ TimeZoneLocationModel::~TimeZoneLocationModel()
201229 g_clear_object(&m_cancellable);
202230 }
203231
232+ Q_FOREACH(GeonamesCity *city, m_countryLocations) {
233+ geonames_city_free(city);
234+ }
235+
204236 Q_FOREACH(GeonamesCity *city, m_locations) {
205237 geonames_city_free(city);
206238 }
plugins/Wizard/timezonemodel.h+1
@@ -74,6 +74,7 @@ private:
7474 GCancellable *m_cancellable;
7575 QHash<int, QByteArray> m_roleNames;
7676 QList<GeonamesCity *> m_locations;
77+ QList<GeonamesCity *> m_countryLocations;
7778};
7879
7980#endif