on xenial help sign in
Add default city list for the current country
9a5d0c7Michael Terry
plugins/Wizard/timezonemodel.cpp+37−5
@@ -38,18 +38,26 @@ TimeZoneLocationModel::TimeZoneLocationModel(QObject *parent):
38
38
39int TimeZoneLocationModel::rowCount(const QModelIndex &parent) const
39int TimeZoneLocationModel::rowCount(const QModelIndex &parent) const
40{
40{
41− if (parent.isValid())
41+ if (parent.isValid()) {
42 return 0;
42 return 0;
43+ } else if (m_filter.isEmpty()) {
44+ return m_countryLocations.count();
45+ } else {
43− return m_locations.count();
46+ return m_locations.count();
47+ }
44}
48}
45
49
46QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
50QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
47{
51{
52+ GeonamesCity *city;
53+ if (m_filter.isEmpty()) {
54+ city = m_countryLocations.value(index.row());
55+ } else {
48− if (index.row() >= m_locations.count() || index.row() < 0)
56+ city = m_locations.value(index.row());
57+ }
58+ if (!city)
49 return QVariant();
59 return QVariant();
50
60
51− GeonamesCity *city = m_locations[index.row()];
52−
53 switch (role) {
61 switch (role) {
54 case Qt::DisplayRole:
62 case Qt::DisplayRole:
55 return QVariant(QString("%1, %2, %3").arg(geonames_city_get_name(city))
63 return QVariant(QString("%1, %2, %3").arg(geonames_city_get_name(city))
@@ -185,12 +193,32 @@ QString TimeZoneLocationModel::country() const
185 return m_country;
193 return m_country;
186}
194}
187
195
196+static bool citycmp(GeonamesCity *a, GeonamesCity *b)
197+{
198+ return geonames_city_get_population(b) < geonames_city_get_population(a);
199+}
200+
188void TimeZoneLocationModel::setCountry(const QString &country)
201void TimeZoneLocationModel::setCountry(const QString &country)
189{
202{
190 if (m_country == country)
203 if (m_country == country)
191 return;
204 return;
192
205
193 m_country = country;
206 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+
194 Q_EMIT countryChanged(country);
222 Q_EMIT countryChanged(country);
195}
223}
196
224
@@ -201,6 +229,10 @@ TimeZoneLocationModel::~TimeZoneLocationModel()
201 g_clear_object(&m_cancellable);
229 g_clear_object(&m_cancellable);
202 }
230 }
203
231
232+ Q_FOREACH(GeonamesCity *city, m_countryLocations) {
233+ geonames_city_free(city);
234+ }
235+
204 Q_FOREACH(GeonamesCity *city, m_locations) {
236 Q_FOREACH(GeonamesCity *city, m_locations) {
205 geonames_city_free(city);
237 geonames_city_free(city);
206 }
238 }
plugins/Wizard/timezonemodel.h+1
@@ -74,6 +74,7 @@ private:
74 GCancellable *m_cancellable;
74 GCancellable *m_cancellable;
75 QHash<int, QByteArray> m_roleNames;
75 QHash<int, QByteArray> m_roleNames;
76 QList<GeonamesCity *> m_locations;
76 QList<GeonamesCity *> m_locations;
77+ QList<GeonamesCity *> m_countryLocations;
77};
78};
78
79
79#endif
80#endif