From: Matthias Clasen Date: Tue, 23 May 2023 10:30:40 +0000 (-0400) Subject: sortlistmodel: Emit sections-changed X-Git-Tag: archive/raspbian/4.12.3+ds-1+rpi1~1^2^2^2~22^2~1^2~210^2~6 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=aeba1e08e8fd78a4dd0994da774ca67337bcfb64;p=gtk4.git sortlistmodel: Emit sections-changed When a new section sorter is set, potentially all sections have changed. So emit sections-changed for all items. Tests included. --- diff --git a/gtk/gtksortlistmodel.c b/gtk/gtksortlistmodel.c index becfec012e..8d05d30407 100644 --- a/gtk/gtksortlistmodel.c +++ b/gtk/gtksortlistmodel.c @@ -1246,6 +1246,9 @@ gtk_sort_list_model_set_section_sorter (GtkSortListModel *self, g_set_object (&self->section_sorter, sorter); gtk_sort_list_model_ensure_real_sorter (self); + if (self->n_items > 0) + gtk_section_model_sections_changed (GTK_SECTION_MODEL (self), 0, self->n_items); + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SECTION_SORTER]); } diff --git a/testsuite/gtk/sortlistmodel.c b/testsuite/gtk/sortlistmodel.c index a9a4dc97f4..d3e6e558db 100644 --- a/testsuite/gtk/sortlistmodel.c +++ b/testsuite/gtk/sortlistmodel.c @@ -52,6 +52,41 @@ model_to_string (GListModel *model) return g_string_free (string, FALSE); } +static char * +section_model_to_string (GListModel *model) +{ + GString *string = g_string_new (NULL); + guint i, s, e; + + if (!GTK_IS_SECTION_MODEL (model)) + return model_to_string (model); + + i = 0; + while (i < g_list_model_get_n_items (model)) + { + gtk_section_model_get_section (GTK_SECTION_MODEL (model), i, &s, &e); + g_assert (s == i); + + if (i > 0) + g_string_append (string, " "); + + g_string_append (string, "["); + + for (; i < e; i++) + { + if (i > s) + g_string_append (string, " "); + + g_string_append_printf (string, "%u", get (model, i)); + } + + g_string_append (string, "]"); + i = e; + } + + return g_string_free (string, FALSE); +} + static void splice (GListStore *store, guint pos, @@ -115,6 +150,14 @@ insert (GListStore *store, g_free (s); \ }G_STMT_END +#define assert_section_model(model, expected) G_STMT_START{ \ + char *s = section_model_to_string (G_LIST_MODEL (model)); \ + if (!g_str_equal (s, expected)) \ + g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ + #model " == " #expected, s, "==", expected); \ + g_free (s); \ +}G_STMT_END + #define assert_changes(model, expected) G_STMT_START{ \ GString *changes = g_object_get_qdata (G_OBJECT (model), changes_quark); \ if (!g_str_equal (changes->str, expected)) \ @@ -176,6 +219,20 @@ items_changed (GListModel *model, } } +static void +sections_changed (GListModel *model, + guint position, + guint n_items, + GString *changes) +{ + g_assert_true (n_items != 0); + + if (changes->len) + g_string_append (changes, ", "); + + g_string_append_printf (changes, "s%u:%u", position, n_items); +} + static void notify_n_items (GObject *object, GParamSpec *pspec, @@ -236,6 +293,7 @@ new_model (gpointer model) changes = g_string_new (""); g_object_set_qdata_full (G_OBJECT(result), changes_quark, changes, free_changes); g_signal_connect (result, "items-changed", G_CALLBACK (items_changed), changes); + g_signal_connect (result, "sections-changed", G_CALLBACK (sections_changed), changes); g_signal_connect (result, "notify::n-items", G_CALLBACK (notify_n_items), changes); return result; @@ -571,55 +629,59 @@ test_add_remove_item (void) } static int -sort_func (gconstpointer p1, - gconstpointer p2, - gpointer data) +by_n (gconstpointer p1, + gconstpointer p2, + gpointer data) { - const char *s1 = gtk_string_object_get_string ((GtkStringObject *)p1); - const char *s2 = gtk_string_object_get_string ((GtkStringObject *)p2); + guint n1 = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (p1), number_quark)); + guint n2 = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (p2), number_quark)); + unsigned int n = GPOINTER_TO_UINT (data); - /* compare just the first byte */ - return (int)(s1[0]) - (int)(s2[0]); + n1 = n1 / n; + n2 = n2 / n; + + if (n1 < n2) + return -1; + else if (n1 > n2) + return 1; + else + return 0; } static void test_sections (void) { - GtkStringList *list; - const char *strings[] = { - "aaa", - "aab", - "abc", - "bbb", - "bq1", - "bq2", - "cc", - "cx", - NULL - }; + GListStore *store; + GtkSortListModel *model; GtkSorter *sorter; - GtkSortListModel *sorted; - GtkSorter *section_sorter; - guint s, e; - - list = gtk_string_list_new (strings); - sorter = GTK_SORTER (gtk_string_sorter_new (gtk_property_expression_new (GTK_TYPE_STRING_OBJECT, NULL, "string"))); - sorted = gtk_sort_list_model_new (G_LIST_MODEL (list), sorter); - section_sorter = GTK_SORTER (gtk_custom_sorter_new (sort_func, NULL, NULL)); - gtk_sort_list_model_set_section_sorter (GTK_SORT_LIST_MODEL (sorted), section_sorter); - g_object_unref (section_sorter); - - gtk_section_model_get_section (GTK_SECTION_MODEL (sorted), 0, &s, &e); - g_assert_cmpint (s, ==, 0); - g_assert_cmpint (e, ==, 3); - gtk_section_model_get_section (GTK_SECTION_MODEL (sorted), 3, &s, &e); - g_assert_cmpint (s, ==, 3); - g_assert_cmpint (e, ==, 6); - gtk_section_model_get_section (GTK_SECTION_MODEL (sorted), 6, &s, &e); - g_assert_cmpint (s, ==, 6); - g_assert_cmpint (e, ==, 8); - - g_object_unref (sorted); + + store = new_store ((guint[]) { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }); + model = new_model (store); + assert_model (model, "1 2 3 4 5 6 7 8 9 10"); + assert_section_model (model, "[1 2 3 4 5 6 7 8 9 10]"); + assert_changes (model, ""); + + g_assert_true (gtk_sort_list_model_get_section_sorter (model) == NULL); + + sorter = GTK_SORTER (gtk_custom_sorter_new (by_n, GUINT_TO_POINTER (3), NULL)); + gtk_sort_list_model_set_section_sorter (model, sorter); + g_assert_true (gtk_sort_list_model_get_section_sorter (model) == sorter); + g_object_unref (sorter); + + assert_changes (model, "s0:10"); + assert_section_model (model, "[1 2] [3 4 5] [6 7 8] [9 10]"); + + sorter = GTK_SORTER (gtk_custom_sorter_new (by_n, GUINT_TO_POINTER (5), NULL)); + g_assert_false (gtk_sort_list_model_get_section_sorter (model) == sorter); + gtk_sort_list_model_set_section_sorter (model, sorter); + g_assert_true (gtk_sort_list_model_get_section_sorter (model) == sorter); + g_object_unref (sorter); + + assert_changes (model, "s0:10"); + assert_section_model (model, "[1 2 3 4] [5 6 7 8 9] [10]"); + + g_object_unref (store); + g_object_unref (model); } int