From 8825917140d5245c1885fe29b441c34e36f5f83b Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 23 May 2023 11:10:49 -0400 Subject: [PATCH] noselection: Pass through sections-changed If our underlying model emits sections-changed, we need to pass it on. Add a test for this too. --- gtk/gtknoselection.c | 21 +++++- testsuite/gtk/noselection.c | 137 +++++++++++++++++++++++++++++++++--- 2 files changed, 149 insertions(+), 9 deletions(-) diff --git a/gtk/gtknoselection.c b/gtk/gtknoselection.c index 20bff24df2..872ee2eaad 100644 --- a/gtk/gtknoselection.c +++ b/gtk/gtknoselection.c @@ -33,6 +33,8 @@ * * This model is meant to be used as a simple wrapper around a `GListModel` * when a `GtkSelectionModel` is required. + * + * `GtkNoSelection` passes through sections from the underlying model. */ struct _GtkNoSelection { @@ -152,15 +154,29 @@ gtk_no_selection_items_changed_cb (GListModel *model, g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]); } +static void +gtk_no_selection_sections_changed_cb (GtkSectionModel *model, + unsigned int position, + unsigned int n_items, + gpointer user_data) +{ + GtkNoSelection *self = GTK_NO_SELECTION (user_data); + + gtk_section_model_sections_changed (GTK_SECTION_MODEL (self), position, n_items); +} + static void gtk_no_selection_clear_model (GtkNoSelection *self) { if (self->model == NULL) return; - g_signal_handlers_disconnect_by_func (self->model, + g_signal_handlers_disconnect_by_func (self->model, gtk_no_selection_items_changed_cb, self); + g_signal_handlers_disconnect_by_func (self->model, + gtk_no_selection_sections_changed_cb, + self); g_clear_object (&self->model); } @@ -345,6 +361,9 @@ gtk_no_selection_set_model (GtkNoSelection *self, self->model = g_object_ref (model); g_signal_connect (self->model, "items-changed", G_CALLBACK (gtk_no_selection_items_changed_cb), self); + if (GTK_IS_SECTION_MODEL (self->model)) + g_signal_connect (self->model, "sections-changed", + G_CALLBACK (gtk_no_selection_sections_changed_cb), self); n_items_after = g_list_model_get_n_items (self->model); } else diff --git a/testsuite/gtk/noselection.c b/testsuite/gtk/noselection.c index 59e0526035..ab71d87b89 100644 --- a/testsuite/gtk/noselection.c +++ b/testsuite/gtk/noselection.c @@ -52,6 +52,42 @@ 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, n; + + if (!GTK_IS_SECTION_MODEL (model)) + return model_to_string (model); + + n = g_list_model_get_n_items (model); + + i = 0; + while (i < n) + { + gtk_section_model_get_section (GTK_SECTION_MODEL (model), i, &s, &e); + + 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 char * selection_to_string (GListModel *model) { @@ -136,6 +172,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 ignore_changes(model) G_STMT_START{ \ GString *changes = g_object_get_qdata (G_OBJECT (model), changes_quark); \ g_string_set_size (changes, 0); \ @@ -220,6 +264,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, @@ -252,16 +310,17 @@ free_changes (gpointer data) } static GtkSelectionModel * -new_model (GListStore *store, gboolean autoselect, gboolean can_unselect) +new_model (GListModel *store, gboolean autoselect, gboolean can_unselect) { GtkSelectionModel *result; GString *changes; - result = GTK_SELECTION_MODEL (gtk_no_selection_new (g_object_ref (G_LIST_MODEL (store)))); + result = GTK_SELECTION_MODEL (gtk_no_selection_new (g_object_ref (store))); changes = g_string_new (""); - g_object_set_qdata_full (G_OBJECT(result), changes_quark, changes, free_changes); + 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); changes = g_string_new (""); @@ -284,7 +343,7 @@ test_create (void) } store = new_store (1, 5, 2); - selection = new_model (store, FALSE, FALSE); + selection = new_model (G_LIST_MODEL (store), FALSE, FALSE); assert_model (selection, "1 3 5"); assert_changes (selection, ""); @@ -325,7 +384,7 @@ test_changes (void) } store = new_store (1, 5, 1); - selection = new_model (store, FALSE, FALSE); + selection = new_model (G_LIST_MODEL (store), FALSE, FALSE); assert_model (selection, "1 2 3 4 5"); assert_changes (selection, ""); assert_selection (selection, ""); @@ -367,7 +426,7 @@ test_selection (void) } store = new_store (1, 5, 1); - selection = new_model (store, TRUE, FALSE); + selection = new_model (G_LIST_MODEL (store), TRUE, FALSE); assert_selection (selection, ""); assert_selection_changes (selection, ""); @@ -444,7 +503,7 @@ test_query_range (void) GListStore *store; store = new_store (1, 5, 1); - selection = new_model (store, TRUE, TRUE); + selection = new_model (G_LIST_MODEL (store), TRUE, TRUE); check_get_selection (selection); gtk_selection_model_unselect_item (selection, 0); @@ -472,7 +531,7 @@ test_set_model (void) store = new_store (1, 5, 1); m1 = G_LIST_MODEL (store); m2 = G_LIST_MODEL (gtk_slice_list_model_new (g_object_ref (m1), 0, 3)); - selection = new_model (store, TRUE, TRUE); + selection = new_model (G_LIST_MODEL (store), TRUE, TRUE); assert_selection (selection, ""); assert_selection_changes (selection, ""); @@ -531,6 +590,67 @@ test_empty (void) g_object_unref (selection); } +static int +by_n (gconstpointer p1, + gconstpointer p2, + gpointer data) +{ + 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); + + n1 = n1 / n; + n2 = n2 / n; + + if (n1 < n2) + return -1; + else if (n1 > n2) + return 1; + else + return 0; +} + +static int +compare (gconstpointer first, + gconstpointer second, + gpointer unused) +{ + return GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (first), number_quark)) + - GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (second), number_quark)); +} + +static void +test_sections (void) +{ + GListStore *store; + GtkSortListModel *sorted; + GtkSelectionModel *selection; + GtkSorter *sorter; + + store = new_store (1, 10, 1); + sorted = gtk_sort_list_model_new (G_LIST_MODEL (store), + GTK_SORTER (gtk_custom_sorter_new (compare, NULL, NULL))); + selection = new_model (G_LIST_MODEL (sorted), TRUE, TRUE); + assert_model (selection, "1 2 3 4 5 6 7 8 9 10"); + assert_section_model (selection, "[1 2 3 4 5 6 7 8 9 10]"); + assert_changes (selection, ""); + + sorter = GTK_SORTER (gtk_custom_sorter_new (by_n, GUINT_TO_POINTER (3), NULL)); + gtk_sort_list_model_set_section_sorter (sorted, sorter); + g_object_unref (sorter); + + assert_section_model (selection, "[1 2] [3 4 5] [6 7 8] [9 10]"); + assert_changes (selection, "s0:10"); + + gtk_section_model_sections_changed (GTK_SECTION_MODEL (sorted), 0, 3); + assert_changes (selection, "s0:3"); + + gtk_section_model_sections_changed (GTK_SECTION_MODEL (sorted), 5, 3); + assert_changes (selection, "s5:3"); + + g_object_unref (selection); +} + int main (int argc, char *argv[]) { @@ -548,6 +668,7 @@ main (int argc, char *argv[]) g_test_add_func ("/noselection/changes", test_changes); g_test_add_func ("/noselection/set-model", test_set_model); g_test_add_func ("/noselection/empty", test_empty); + g_test_add_func ("/noselection/sections", test_sections); return g_test_run (); } -- 2.30.2