mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2024-11-08 17:50:10 +00:00
singleselection: 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
8825917140
commit
a1352a88ff
@ -300,15 +300,29 @@ gtk_single_selection_items_changed_cb (GListModel *model,
|
||||
g_object_thaw_notify (G_OBJECT (self));
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_single_selection_sections_changed_cb (GtkSectionModel *model,
|
||||
unsigned int position,
|
||||
unsigned int n_items,
|
||||
gpointer user_data)
|
||||
{
|
||||
GtkSingleSelection *self = GTK_SINGLE_SELECTION (user_data);
|
||||
|
||||
gtk_section_model_sections_changed (GTK_SECTION_MODEL (self), position, n_items);
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_single_selection_clear_model (GtkSingleSelection *self)
|
||||
{
|
||||
if (self->model == NULL)
|
||||
return;
|
||||
|
||||
g_signal_handlers_disconnect_by_func (self->model,
|
||||
g_signal_handlers_disconnect_by_func (self->model,
|
||||
gtk_single_selection_items_changed_cb,
|
||||
self);
|
||||
g_signal_handlers_disconnect_by_func (self->model,
|
||||
gtk_single_selection_sections_changed_cb,
|
||||
self);
|
||||
g_clear_object (&self->model);
|
||||
}
|
||||
|
||||
@ -558,7 +572,7 @@ gtk_single_selection_set_model (GtkSingleSelection *self,
|
||||
return;
|
||||
|
||||
g_object_freeze_notify (G_OBJECT (self));
|
||||
|
||||
|
||||
n_items_before = self->model ? g_list_model_get_n_items (self->model) : 0;
|
||||
gtk_single_selection_clear_model (self);
|
||||
|
||||
@ -567,6 +581,9 @@ gtk_single_selection_set_model (GtkSingleSelection *self,
|
||||
self->model = g_object_ref (model);
|
||||
g_signal_connect (self->model, "items-changed",
|
||||
G_CALLBACK (gtk_single_selection_items_changed_cb), self);
|
||||
if (GTK_IS_SECTION_MODEL (self->model))
|
||||
g_signal_connect (self->model, "sections-changed",
|
||||
G_CALLBACK (gtk_single_selection_sections_changed_cb), self);
|
||||
gtk_single_selection_items_changed_cb (self->model,
|
||||
0,
|
||||
n_items_before,
|
||||
|
@ -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,12 +310,12 @@ 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_single_selection_new (g_object_ref (G_LIST_MODEL (store))));
|
||||
result = GTK_SELECTION_MODEL (gtk_single_selection_new (g_object_ref (store)));
|
||||
|
||||
/* We want to return an empty selection unless autoselect is true,
|
||||
* so undo the initial selection due to autoselect defaulting to TRUE.
|
||||
@ -271,8 +329,9 @@ new_model (GListStore *store, gboolean autoselect, gboolean can_unselect)
|
||||
gtk_single_selection_set_can_unselect (GTK_SINGLE_SELECTION (result), can_unselect);
|
||||
|
||||
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 ("");
|
||||
@ -296,7 +355,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);
|
||||
g_assert_false (gtk_single_selection_get_autoselect (GTK_SINGLE_SELECTION (selection)));
|
||||
|
||||
assert_model (selection, "1 3 5");
|
||||
@ -344,7 +403,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, "");
|
||||
@ -386,7 +445,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, "1");
|
||||
assert_selection_changes (selection, "");
|
||||
|
||||
@ -442,7 +501,7 @@ test_autoselect (void)
|
||||
}
|
||||
|
||||
store = new_empty_store ();
|
||||
selection = new_model (store, TRUE, FALSE);
|
||||
selection = new_model (G_LIST_MODEL (store), TRUE, FALSE);
|
||||
assert_model (selection, "");
|
||||
assert_changes (selection, "");
|
||||
assert_selection (selection, "");
|
||||
@ -513,7 +572,7 @@ test_autoselect_toggle (void)
|
||||
}
|
||||
|
||||
store = new_store (1, 1, 1);
|
||||
selection = new_model (store, TRUE, TRUE);
|
||||
selection = new_model (G_LIST_MODEL (store), TRUE, TRUE);
|
||||
assert_model (selection, "1");
|
||||
assert_changes (selection, "");
|
||||
assert_selection (selection, "1");
|
||||
@ -555,7 +614,7 @@ test_can_unselect (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, "1");
|
||||
assert_selection_changes (selection, "");
|
||||
|
||||
@ -600,7 +659,7 @@ test_persistence (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, "1");
|
||||
assert_selection_changes (selection, "");
|
||||
g_assert_true (gtk_selection_model_is_selected (selection, 0));
|
||||
@ -653,7 +712,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);
|
||||
@ -681,7 +740,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, "1");
|
||||
assert_selection_changes (selection, "");
|
||||
|
||||
@ -743,6 +802,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[])
|
||||
{
|
||||
@ -764,6 +884,7 @@ main (int argc, char *argv[])
|
||||
g_test_add_func ("/singleselection/changes", test_changes);
|
||||
g_test_add_func ("/singleselection/set-model", test_set_model);
|
||||
g_test_add_func ("/singleselection/empty", test_empty);
|
||||
g_test_add_func ("/singleselection/sections", test_sections);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user