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

With tests!
This commit is contained in:
Benjamin Otte 2022-06-11 04:52:34 +02:00
parent e1845f5e90
commit 67e336992f
2 changed files with 36 additions and 6 deletions

View File

@ -39,6 +39,7 @@
enum {
PROP_0,
PROP_ITEM_TYPE,
PROP_N_ITEMS,
PROP_OBJECT,
PROP_PROPERTY,
NUM_PROPERTIES
@ -184,6 +185,8 @@ gtk_property_lookup_list_model_notify_cb (GObject *object,
if (removed > 0 || added > 0)
g_list_model_items_changed (G_LIST_MODEL (self), position, removed, added);
if (removed != added)
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
}
static guint
@ -318,6 +321,10 @@ gtk_property_lookup_list_model_get_property (GObject *object,
g_value_set_gtype (value, self->item_type);
break;
case PROP_N_ITEMS:
g_value_set_uint (value, gtk_property_lookup_list_model_get_n_items (G_LIST_MODEL (self)));
break;
case PROP_OBJECT:
g_value_set_object (value, gtk_property_lookup_list_model_get_object (self));
break;
@ -366,13 +373,25 @@ gtk_property_lookup_list_model_class_init (GtkPropertyLookupListModelClass *klas
/**
* GtkPropertyLookupListModel:item-type:
*
* The `GType` for elements of this object
* The `GType` for elements of this object. See [method@Gio.ListModel.get_item_type].
*/
properties[PROP_ITEM_TYPE] =
g_param_spec_gtype ("item-type", NULL, NULL,
G_TYPE_OBJECT,
GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkPropertyLookupListModel: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);
/**
* GtkPropertyLookupListModel:property:
*
@ -456,6 +475,8 @@ gtk_property_lookup_list_model_set_object (GtkPropertyLookupListModel *self,
g_assert (removed != 0 || added != 0);
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]);
}
gpointer

View File

@ -90,6 +90,14 @@ items_changed (GListModel *model,
}
}
static void
notify_n_items (GObject *object,
GParamSpec *pspec,
GString *changes)
{
g_string_append_c (changes, '*');
}
static void
free_changes (gpointer data)
{
@ -145,6 +153,7 @@ new_model (gboolean fill)
changes = g_string_new ("");
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, "notify::n-items", G_CALLBACK (notify_n_items), changes);
return result;
}
@ -185,14 +194,14 @@ test_set_object (void)
model = new_model (FALSE);
gtk_property_lookup_list_model_set_object (model, widget);
assert_model (model, "GtkLabel GtkGrid GtkBox GtkWindow");
assert_changes (model, "+0");
assert_changes (model, "+0*");
g_object_unref (model);
model = new_model (FALSE);
assert_model (model, "");
gtk_property_lookup_list_model_set_object (model, widget);
assert_model (model, "GtkLabel GtkGrid GtkBox GtkWindow");
assert_changes (model, "0+4");
assert_changes (model, "0+4*");
g_object_unref (model);
destroy_widgets ();
@ -212,15 +221,15 @@ test_change_property (void)
assert_model (model, ""); /* make sure the model has a definite size */
gtk_property_lookup_list_model_set_object (model, widget);
assert_model (model, "GtkLabel GtkGrid GtkBox GtkWindow");
assert_changes (model, "0+4");
assert_changes (model, "0+4*");
gtk_grid_remove (GTK_GRID (parent), widget);
assert_model (model, "GtkLabel");
assert_changes (model, "1-3");
assert_changes (model, "1-3*");
gtk_box_append (GTK_BOX (grandparent), widget);
assert_model (model, "GtkLabel GtkBox GtkWindow");
assert_changes (model, "1+2");
assert_changes (model, "1+2*");
g_object_unref (model);
destroy_widgets ();