mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2024-12-25 21:21:21 +00:00
multiselection: Pass through sections-changed
If our underlying model emits sections-changed, we need to pass it on. Add a test for this too.
This commit is contained in:
parent
a1352a88ff
commit
9c1049e710
@ -291,6 +291,17 @@ gtk_multi_selection_items_changed_cb (GListModel *model,
|
||||
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_multi_selection_sections_changed_cb (GtkSectionModel *model,
|
||||
unsigned int position,
|
||||
unsigned int n_items,
|
||||
gpointer user_data)
|
||||
{
|
||||
GtkMultiSelection *self = GTK_MULTI_SELECTION (user_data);
|
||||
|
||||
gtk_section_model_sections_changed (GTK_SECTION_MODEL (self), position, n_items);
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_multi_selection_clear_model (GtkMultiSelection *self)
|
||||
{
|
||||
@ -300,6 +311,9 @@ gtk_multi_selection_clear_model (GtkMultiSelection *self)
|
||||
g_signal_handlers_disconnect_by_func (self->model,
|
||||
gtk_multi_selection_items_changed_cb,
|
||||
self);
|
||||
g_signal_handlers_disconnect_by_func (self->model,
|
||||
gtk_multi_selection_sections_changed_cb,
|
||||
self);
|
||||
g_clear_object (&self->model);
|
||||
}
|
||||
|
||||
@ -490,6 +504,9 @@ gtk_multi_selection_set_model (GtkMultiSelection *self,
|
||||
"items-changed",
|
||||
G_CALLBACK (gtk_multi_selection_items_changed_cb),
|
||||
self);
|
||||
if (GTK_IS_SECTION_MODEL (self->model))
|
||||
g_signal_connect (self->model, "sections-changed",
|
||||
G_CALLBACK (gtk_multi_selection_sections_changed_cb), self);
|
||||
gtk_multi_selection_items_changed_cb (self->model,
|
||||
0,
|
||||
n_items_before,
|
||||
|
@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright (C) 2019, Red Hat, Inc.
|
||||
* Authors: Matthias Clasen <mclasen@redhat.com>
|
||||
*
|
||||
@ -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)
|
||||
{
|
||||
@ -140,6 +176,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); \
|
||||
@ -224,6 +268,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,
|
||||
@ -256,16 +314,17 @@ free_changes (gpointer data)
|
||||
}
|
||||
|
||||
static GtkSelectionModel *
|
||||
new_model (GListStore *store)
|
||||
new_model (GListModel *store)
|
||||
{
|
||||
GtkSelectionModel *result;
|
||||
GString *changes;
|
||||
|
||||
result = GTK_SELECTION_MODEL (gtk_multi_selection_new (g_object_ref (G_LIST_MODEL (store))));
|
||||
result = GTK_SELECTION_MODEL (gtk_multi_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 ("");
|
||||
@ -299,7 +358,7 @@ test_create (void)
|
||||
guint start, end;
|
||||
|
||||
store = new_store (1, 5, 2);
|
||||
selection = new_model (store);
|
||||
selection = new_model (G_LIST_MODEL (store));
|
||||
|
||||
assert_model (selection, "1 3 5");
|
||||
assert_changes (selection, "");
|
||||
@ -340,7 +399,7 @@ test_changes (void)
|
||||
gboolean ret;
|
||||
|
||||
store = new_store (1, 5, 1);
|
||||
selection = new_model (store);
|
||||
selection = new_model (G_LIST_MODEL (store));
|
||||
assert_model (selection, "1 2 3 4 5");
|
||||
assert_changes (selection, "");
|
||||
assert_selection (selection, "");
|
||||
@ -387,7 +446,7 @@ test_selection (void)
|
||||
gboolean ret;
|
||||
|
||||
store = new_store (1, 5, 1);
|
||||
selection = new_model (store);
|
||||
selection = new_model (G_LIST_MODEL (store));
|
||||
assert_selection (selection, "");
|
||||
assert_selection_changes (selection, "");
|
||||
|
||||
@ -442,7 +501,7 @@ test_select_range (void)
|
||||
gboolean ret;
|
||||
|
||||
store = new_store (1, 5, 1);
|
||||
selection = new_model (store);
|
||||
selection = new_model (G_LIST_MODEL (store));
|
||||
assert_selection (selection, "");
|
||||
assert_selection_changes (selection, "");
|
||||
|
||||
@ -477,7 +536,7 @@ test_readd (void)
|
||||
|
||||
store = new_store (1, 5, 1);
|
||||
|
||||
selection = new_model (store);
|
||||
selection = new_model (G_LIST_MODEL (store));
|
||||
assert_model (selection, "1 2 3 4 5");
|
||||
assert_selection (selection, "");
|
||||
assert_selection_changes (selection, "");
|
||||
@ -506,7 +565,7 @@ test_set_selection (void)
|
||||
|
||||
store = new_store (1, 10, 1);
|
||||
|
||||
selection = new_model (store);
|
||||
selection = new_model (G_LIST_MODEL (store));
|
||||
assert_model (selection, "1 2 3 4 5 6 7 8 9 10");
|
||||
assert_selection (selection, "");
|
||||
assert_selection_changes (selection, "");
|
||||
@ -547,7 +606,7 @@ test_selection_filter (void)
|
||||
gboolean ret;
|
||||
|
||||
store = new_store (1, 5, 1);
|
||||
selection = new_model (store);
|
||||
selection = new_model (G_LIST_MODEL (store));
|
||||
assert_selection (selection, "");
|
||||
assert_selection_changes (selection, "");
|
||||
|
||||
@ -660,7 +719,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);
|
||||
selection = new_model (G_LIST_MODEL (store));
|
||||
assert_selection (selection, "");
|
||||
assert_selection_changes (selection, "");
|
||||
|
||||
@ -742,6 +801,67 @@ test_empty_filter (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));
|
||||
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[])
|
||||
{
|
||||
@ -764,6 +884,7 @@ main (int argc, char *argv[])
|
||||
g_test_add_func ("/multiselection/set-model", test_set_model);
|
||||
g_test_add_func ("/multiselection/empty", test_empty);
|
||||
g_test_add_func ("/multiselection/selection-filter/empty", test_empty_filter);
|
||||
g_test_add_func ("/multiselection/sections", test_sections);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user