fattenlistmodel: Add ::item-type and ::n-items

With tests!
This commit is contained in:
Benjamin Otte 2022-06-11 06:43:25 +02:00
parent b91f60b4f9
commit f7b8184b00
2 changed files with 59 additions and 9 deletions

View File

@ -36,7 +36,10 @@
enum { enum {
PROP_0, PROP_0,
PROP_ITEM_TYPE,
PROP_MODEL, PROP_MODEL,
PROP_N_ITEMS,
NUM_PROPERTIES NUM_PROPERTIES
}; };
@ -239,6 +242,8 @@ gtk_flatten_list_model_items_changed_cb (GListModel *model,
} }
g_list_model_items_changed (G_LIST_MODEL (self), real_position, removed, added); g_list_model_items_changed (G_LIST_MODEL (self), real_position, removed, added);
if (removed != added)
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
} }
static void static void
@ -332,10 +337,18 @@ gtk_flatten_list_model_get_property (GObject *object,
switch (prop_id) switch (prop_id)
{ {
case PROP_ITEM_TYPE:
g_value_set_gtype (value, gtk_flatten_list_model_get_item_type (G_LIST_MODEL (self)));
break;
case PROP_MODEL: case PROP_MODEL:
g_value_set_object (value, self->model); g_value_set_object (value, self->model);
break; break;
case PROP_N_ITEMS:
g_value_set_uint (value, gtk_flatten_list_model_get_n_items (G_LIST_MODEL (self)));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -367,6 +380,8 @@ gtk_flatten_list_model_model_items_changed_cb (GListModel *model,
if (real_removed > 0 || real_added > 0) if (real_removed > 0 || real_added > 0)
g_list_model_items_changed (G_LIST_MODEL (self), real_position, real_removed, real_added); g_list_model_items_changed (G_LIST_MODEL (self), real_position, real_removed, real_added);
if (real_removed != real_added)
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
} }
static void static void
@ -399,6 +414,18 @@ gtk_flatten_list_model_class_init (GtkFlattenListModelClass *class)
gobject_class->get_property = gtk_flatten_list_model_get_property; gobject_class->get_property = gtk_flatten_list_model_get_property;
gobject_class->dispose = gtk_flatten_list_model_dispose; gobject_class->dispose = gtk_flatten_list_model_dispose;
/**
* GtkFlattenListModel:item-type:
*
* The type of items. See [method@Gio.ListModel.get_item_type].
*
* Since: 4.8
**/
properties[PROP_ITEM_TYPE] =
g_param_spec_gtype ("item-type", NULL, NULL,
G_TYPE_OBJECT,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
/** /**
* GtkFlattenListModel:model: (attributes org.gtk.Property.get=gtk_flatten_list_model_get_model org.gtk.Property.set=gtk_flatten_list_model_set_model) * GtkFlattenListModel:model: (attributes org.gtk.Property.get=gtk_flatten_list_model_get_model org.gtk.Property.set=gtk_flatten_list_model_set_model)
* *
@ -409,6 +436,18 @@ gtk_flatten_list_model_class_init (GtkFlattenListModelClass *class)
G_TYPE_LIST_MODEL, G_TYPE_LIST_MODEL,
GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY); GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkFlattenListModel:n-items:
*
* The number of items. See [method@Gio.ListModel.get_n_items].
*
* Since: 4.8
**/
properties[PROP_N_ITEMS] =
g_param_spec_uint ("n-items", NULL, NULL,
0, G_MAXUINT, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties); g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
} }
@ -481,6 +520,8 @@ gtk_flatten_list_model_set_model (GtkFlattenListModel *self,
if (removed > 0 || added > 0) if (removed > 0 || added > 0)
g_list_model_items_changed (G_LIST_MODEL (self), 0, removed, added); g_list_model_items_changed (G_LIST_MODEL (self), 0, removed, added);
if (removed != added)
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MODEL]); g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MODEL]);
} }

View File

@ -193,6 +193,14 @@ items_changed (GListModel *model,
} }
} }
static void
notify_n_items (GObject *object,
GParamSpec *pspec,
GString *changes)
{
g_string_append_c (changes, '*');
}
static void static void
free_changes (gpointer data) free_changes (gpointer data)
{ {
@ -216,6 +224,7 @@ new_model (GListStore *store)
changes = g_string_new (""); 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, "items-changed", G_CALLBACK (items_changed), changes);
g_signal_connect (result, "notify::n-items", G_CALLBACK (notify_n_items), changes);
return result; return result;
} }
@ -271,7 +280,7 @@ test_model_add (void)
add_store (model, 8, 10, 1); add_store (model, 8, 10, 1);
assert_model (flat, "1 2 3 4 5 6 7 8 9 10"); assert_model (flat, "1 2 3 4 5 6 7 8 9 10");
assert_changes (flat, "0+3, +3, 4+3, 7+3"); assert_changes (flat, "0+3*, +3*, 4+3*, 7+3*");
g_object_unref (model); g_object_unref (model);
g_object_unref (flat); g_object_unref (flat);
@ -293,13 +302,13 @@ test_submodel_add (void)
store[2] = add_store (model, 5, 4, 1); store[2] = add_store (model, 5, 4, 1);
store[3] = add_store (model, 8, 8, 1); store[3] = add_store (model, 8, 8, 1);
assert_model (flat, "2 3 4 8"); assert_model (flat, "2 3 4 8");
assert_changes (flat, "0+2, +2, +3"); assert_changes (flat, "0+2*, +2*, +3*");
insert (store[0], 0, 1); insert (store[0], 0, 1);
splice (store[2], 0, 0, (guint[3]) { 5, 6, 7 }, 3); splice (store[2], 0, 0, (guint[3]) { 5, 6, 7 }, 3);
splice (store[3], 1, 0, (guint[2]) { 9, 10 }, 2); splice (store[3], 1, 0, (guint[2]) { 9, 10 }, 2);
assert_model (flat, "1 2 3 4 5 6 7 8 9 10"); assert_model (flat, "1 2 3 4 5 6 7 8 9 10");
assert_changes (flat, "+0, 4+3, 8+2"); assert_changes (flat, "+0*, 4+3*, 8+2*");
g_object_unref (model); g_object_unref (model);
g_object_unref (flat); g_object_unref (flat);
@ -325,19 +334,19 @@ test_submodel_add2 (void)
add (store[0], 1); add (store[0], 1);
assert_model (flat, "1"); assert_model (flat, "1");
assert_changes (flat, "+0"); assert_changes (flat, "+0*");
add (store[1], 3); add (store[1], 3);
assert_model (flat, "1 3"); assert_model (flat, "1 3");
assert_changes (flat, "+1"); assert_changes (flat, "+1*");
add (store[0], 2); add (store[0], 2);
assert_model (flat, "1 2 3"); assert_model (flat, "1 2 3");
assert_changes (flat, "+1"); assert_changes (flat, "+1*");
add (store[1], 4); add (store[1], 4);
assert_model (flat, "1 2 3 4"); assert_model (flat, "1 2 3 4");
assert_changes (flat, "+3"); assert_changes (flat, "+3*");
g_object_unref (model); g_object_unref (model);
g_object_unref (flat); g_object_unref (flat);
@ -363,7 +372,7 @@ test_model_remove (void)
g_list_store_remove (model, 0); g_list_store_remove (model, 0);
g_object_unref (model); g_object_unref (model);
assert_model (flat, ""); assert_model (flat, "");
assert_changes (flat, "3-4, 3-3, 0-3"); assert_changes (flat, "3-4*, 3-3*, 0-3*");
g_object_unref (flat); g_object_unref (flat);
} }
@ -389,7 +398,7 @@ test_submodel_remove (void)
g_object_unref (model); g_object_unref (model);
assert_model (flat, "2 3 4 8"); assert_model (flat, "2 3 4 8");
assert_changes (flat, "-0, 3-3, 4-2"); assert_changes (flat, "-0*, 3-3*, 4-2*");
g_object_unref (flat); g_object_unref (flat);
} }