slicelistmodel: Pass through sections

Implement GtkSectionModel in the obvious way.

Tests included.
This commit is contained in:
Matthias Clasen 2023-05-23 06:24:34 -04:00
parent aeba1e08e8
commit 8f3d3ca587
2 changed files with 206 additions and 11 deletions

View File

@ -20,6 +20,7 @@
#include "config.h"
#include "gtkslicelistmodel.h"
#include "gtksectionmodelprivate.h"
#include "gtkprivate.h"
@ -31,6 +32,8 @@
* This is useful when implementing paging by setting the size to the number
* of elements per page and updating the offset whenever a different page is
* opened.
*
* `GtkSliceListModel` passes through sections from the underlying model.
*/
#define DEFAULT_SIZE 10
@ -52,8 +55,6 @@ struct _GtkSliceListModel
GListModel *model;
guint offset;
guint size;
guint n_items;
};
struct _GtkSliceListModelClass
@ -111,8 +112,69 @@ gtk_slice_list_model_model_init (GListModelInterface *iface)
iface->get_item = gtk_slice_list_model_get_item;
}
static void
gtk_slice_list_model_get_section (GtkSectionModel *model,
guint position,
guint *start,
guint *end)
{
GtkSliceListModel *self = GTK_SLICE_LIST_MODEL (model);
unsigned int n_items;
n_items = g_list_model_get_n_items (G_LIST_MODEL (self));
if (position >= n_items)
{
*start = n_items;
*end = G_MAXUINT;
}
else
{
gtk_list_model_get_section (self->model, position + self->offset, start, end);
*start = MAX (*start, self->offset) - self->offset;
*end = MIN (*end - self->offset, n_items);
}
}
static void
gtk_slice_list_model_sections_changed_cb (GtkSectionModel *model,
unsigned int position,
unsigned int n_items,
gpointer user_data)
{
GtkSliceListModel *self = GTK_SLICE_LIST_MODEL (user_data);
unsigned int start = position;
unsigned int end = position + n_items;
unsigned int size;
if (end <= self->offset)
return;
size = g_list_model_get_n_items (G_LIST_MODEL (self));
end = MIN (end - self->offset, size);
if (start <= self->offset)
start = 0;
else
start = start - self->offset;
if (start >= size)
return;
gtk_section_model_sections_changed (GTK_SECTION_MODEL (self), start, end - start);
}
static void
gtk_slice_list_model_section_model_init (GtkSectionModelInterface *iface)
{
iface->get_section = gtk_slice_list_model_get_section;
}
G_DEFINE_TYPE_WITH_CODE (GtkSliceListModel, gtk_slice_list_model, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, gtk_slice_list_model_model_init))
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, gtk_slice_list_model_model_init)
G_IMPLEMENT_INTERFACE (GTK_TYPE_SECTION_MODEL, gtk_slice_list_model_section_model_init))
static void
gtk_slice_list_model_items_changed_cb (GListModel *model,
@ -238,6 +300,7 @@ gtk_slice_list_model_clear_model (GtkSliceListModel *self)
if (self->model == NULL)
return;
g_signal_handlers_disconnect_by_func (self->model, gtk_slice_list_model_sections_changed_cb, self);
g_signal_handlers_disconnect_by_func (self->model, gtk_slice_list_model_items_changed_cb, self);
g_clear_object (&self->model);
}
@ -387,6 +450,9 @@ gtk_slice_list_model_set_model (GtkSliceListModel *self,
self->model = g_object_ref (model);
g_signal_connect (model, "items-changed", G_CALLBACK (gtk_slice_list_model_items_changed_cb), self);
added = g_list_model_get_n_items (G_LIST_MODEL (self));
if (GTK_IS_SECTION_MODEL (model))
g_signal_connect (model, "sections-changed", G_CALLBACK (gtk_slice_list_model_sections_changed_cb), self);
}
else
{

View File

@ -51,6 +51,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 GListStore *
new_store (guint start,
guint end,
@ -116,6 +151,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)) \
@ -174,6 +217,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,
@ -194,18 +251,19 @@ free_changes (gpointer data)
}
static GtkSliceListModel *
new_model (GListStore *store, guint offset, guint size)
new_model (GListModel *store, guint offset, guint size)
{
GtkSliceListModel *result;
GString *changes;
if (store)
g_object_ref (store);
result = gtk_slice_list_model_new (G_LIST_MODEL (store), offset, size);
result = gtk_slice_list_model_new (store, offset, size);
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);
return result;
@ -230,7 +288,7 @@ test_create (void)
GListStore *store;
store = new_store (1, 5, 2);
slice = new_model (store, 0, 10);
slice = new_model (G_LIST_MODEL (store), 0, 10);
assert_model (slice, "1 3 5");
assert_changes (slice, "");
@ -273,7 +331,7 @@ test_set_slice (void)
GListStore *store;
store = new_store (1, 7, 2);
slice = new_model (store, 0, 3);
slice = new_model (G_LIST_MODEL (store), 0, 3);
assert_model (slice, "1 3 5");
assert_changes (slice, "");
@ -302,7 +360,7 @@ test_changes (void)
GListStore *store;
store = new_store (1, 20, 1);
slice = new_model (store, 10, 5);
slice = new_model (G_LIST_MODEL (store), 10, 5);
assert_model (slice, "11 12 13 14 15");
assert_changes (slice, "");
@ -349,7 +407,7 @@ test_bug_added_equals_removed (void)
GListStore *store;
store = new_store (1, 10, 1);
slice = new_model (store, 0, 10);
slice = new_model (G_LIST_MODEL (store), 0, 10);
assert_model (slice, "1 2 3 4 5 6 7 8 9 10");
assert_changes (slice, "");
@ -368,7 +426,7 @@ test_bug_skip_amount (void)
GListStore *store;
store = new_store (1, 5, 1);
slice = new_model (store, 2, 2);
slice = new_model (G_LIST_MODEL (store), 2, 2);
assert_model (slice, "3 4");
assert_changes (slice, "");
@ -380,6 +438,76 @@ test_bug_skip_amount (void)
g_object_unref (slice);
}
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;
GtkSliceListModel *slice;
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)));
slice = new_model (G_LIST_MODEL (sorted), 0, 10);
assert_model (slice, "1 2 3 4 5 6 7 8 9 10");
assert_section_model (slice, "[1 2 3 4 5 6 7 8 9 10]");
assert_changes (slice, "");
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 (slice, "[1 2] [3 4 5] [6 7 8] [9 10]");
assert_changes (slice, "s0:10");
gtk_slice_list_model_set_size (slice, 5);
assert_section_model (slice, "[1 2] [3 4 5]");
assert_changes (slice, "5-5*");
gtk_slice_list_model_set_offset (slice, 1);
assert_section_model (slice, "[2] [3 4 5] [6]");
assert_changes (slice, "0-5+5");
gtk_section_model_sections_changed (GTK_SECTION_MODEL (sorted), 0, 3);
assert_changes (slice, "s0:2");
gtk_section_model_sections_changed (GTK_SECTION_MODEL (sorted), 5, 3);
assert_changes (slice, "s4:1");
g_object_unref (slice);
}
int
main (int argc, char *argv[])
{
@ -396,6 +524,7 @@ main (int argc, char *argv[])
g_test_add_func ("/slicelistmodel/changes", test_changes);
g_test_add_func ("/slicelistmodel/bug/added_equals_removed", test_bug_added_equals_removed);
g_test_add_func ("/slicelistmodel/bug/skip_amount", test_bug_skip_amount);
g_test_add_func ("/slicelistmodel/sections", test_sections);
return g_test_run ();
}