[libgweather/benzea/wip-variant-backend: 10/11] location-entry: Sort locations in drop-down
- From: Benjamin Berg <bberg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgweather/benzea/wip-variant-backend: 10/11] location-entry: Sort locations in drop-down
- Date: Mon, 11 Jan 2021 13:59:07 +0000 (UTC)
commit afcac89f50dda5e3bdae141ebb40e9df590fd32f
Author: Benjamin Berg <bberg redhat com>
Date: Thu Apr 30 12:06:52 2020 +0200
location-entry: Sort locations in drop-down
The order of locations as returned by GWeatherLocation used to be sorted
by the localised name. However, that requires quite a lot of resources
and is not needed in many usecases.
To ensure that the parent takes precedence, it is now changed to be at
the front for sorting purposes.
As such, sort the resulting tree model by location name. Note that this
actually changes the behaviour slightly as Countries were grouped by
region before and are now all sorted by name.
libgweather/gweather-location-entry.c | 31 +++++++++++++++++++++++++++----
1 file changed, 27 insertions(+), 4 deletions(-)
---
diff --git a/libgweather/gweather-location-entry.c b/libgweather/gweather-location-entry.c
index f8071aa3..04c51b6f 100644
--- a/libgweather/gweather-location-entry.c
+++ b/libgweather/gweather-location-entry.c
@@ -165,6 +165,25 @@ dispose (GObject *object)
G_OBJECT_CLASS (gweather_location_entry_parent_class)->dispose (object);
}
+static int
+tree_compare_local_name (GtkTreeModel *model,
+ GtkTreeIter *a,
+ GtkTreeIter *b,
+ gpointer user_data)
+{
+ g_autofree gchar *name_a = NULL, *name_b = NULL;
+
+ gtk_tree_model_get (model, a,
+ LOC_GWEATHER_LOCATION_ENTRY_COL_LOCAL_COMPARE_NAME, &name_a,
+ -1);
+ gtk_tree_model_get (model, b,
+ LOC_GWEATHER_LOCATION_ENTRY_COL_LOCAL_COMPARE_NAME, &name_b,
+ -1);
+
+ return g_utf8_collate (name_a, name_b);
+}
+
+
static void
constructed (GObject *object)
{
@@ -178,6 +197,8 @@ constructed (GObject *object)
entry->priv->top = gweather_location_get_world ();
store = gtk_list_store_new (4, G_TYPE_STRING, GWEATHER_TYPE_LOCATION, G_TYPE_STRING, G_TYPE_STRING);
+ gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
+ tree_compare_local_name, NULL, NULL);
fill_location_entry_model (store, entry->priv->top, NULL, NULL, NULL, entry->priv->show_named_timezones);
entry->priv->model = GTK_TREE_MODEL (store);
@@ -523,8 +544,8 @@ fill_location_entry_model (GtkListStore *store, GWeatherLocation *loc,
* You shouldn't need to translate this string unless the language has a different comma.
*/
display_name = g_strdup_printf (_("%s, %s"), gweather_location_get_name (loc), parent_display_name);
- local_compare_name = g_strdup_printf ("%s, %s", gweather_location_get_sort_name (loc),
parent_compare_local_name);
- english_compare_name = g_strdup_printf ("%s, %s", gweather_location_get_english_sort_name (loc),
parent_compare_english_name);
+ local_compare_name = g_strdup_printf ("%s, %s", parent_compare_local_name,
gweather_location_get_sort_name (loc));
+ english_compare_name = g_strdup_printf ("%s, %s", parent_compare_english_name,
gweather_location_get_english_sort_name (loc));
while ((child = gweather_location_next_child (loc, child)))
fill_location_entry_model (store, child,
@@ -552,9 +573,9 @@ fill_location_entry_model (GtkListStore *store, GWeatherLocation *loc,
display_name = g_strdup_printf (_("%s, %s"),
gweather_location_get_name (loc), parent_display_name);
local_compare_name = g_strdup_printf ("%s, %s",
- gweather_location_get_sort_name (loc),
parent_compare_local_name);
+ parent_compare_local_name, gweather_location_get_sort_name
(loc));
english_compare_name = g_strdup_printf ("%s, %s",
- gweather_location_get_english_sort_name (loc),
parent_compare_english_name);
+ parent_compare_english_name,
gweather_location_get_english_sort_name (loc));
gtk_list_store_insert_with_values (store, NULL, -1,
LOC_GWEATHER_LOCATION_ENTRY_COL_LOCATION, loc,
@@ -792,6 +813,8 @@ _got_places (GObject *source_object,
completion = gtk_entry_get_completion (user_data);
store = gtk_list_store_new (4, G_TYPE_STRING, GEOCODE_TYPE_PLACE, G_TYPE_STRING, G_TYPE_STRING);
+ gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
+ tree_compare_local_name, NULL, NULL);
g_list_foreach (places, fill_store, store);
g_list_free (places);
gtk_entry_completion_set_match_func (completion, new_matcher, NULL, NULL);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]