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

With tests!
This commit is contained in:
Benjamin Otte 2022-06-11 04:49:51 +02:00
parent 3fca865625
commit 0f829c4599
2 changed files with 76 additions and 28 deletions

View File

@ -57,9 +57,11 @@ enum {
PROP_0,
PROP_AUTOSELECT,
PROP_CAN_UNSELECT,
PROP_ITEM_TYPE,
PROP_MODEL,
PROP_N_ITEMS,
PROP_SELECTED,
PROP_SELECTED_ITEM,
PROP_MODEL,
N_PROPS
};
@ -271,6 +273,8 @@ gtk_single_selection_items_changed_cb (GListModel *model,
}
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]);
g_object_thaw_notify (G_OBJECT (self));
}
@ -337,10 +341,19 @@ gtk_single_selection_get_property (GObject *object,
case PROP_CAN_UNSELECT:
g_value_set_boolean (value, self->can_unselect);
break;
case PROP_ITEM_TYPE:
g_value_set_gtype (value, gtk_single_selection_get_item_type (G_LIST_MODEL (self)));
break;
case PROP_MODEL:
g_value_set_object (value, self->model);
break;
case PROP_N_ITEMS:
g_value_set_uint (value, gtk_single_selection_get_n_items (G_LIST_MODEL (self)));
break;
case PROP_SELECTED:
g_value_set_uint (value, self->selected);
break;
@ -397,6 +410,40 @@ gtk_single_selection_class_init (GtkSingleSelectionClass *klass)
FALSE,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
* GtkSingleSelection: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);
/**
* GtkSingleSelection:model: (attributes org.gtk.Property.get=gtk_single_selection_get_model org.gtk.Property.set=gtk_single_selection_set_model)
*
* The model being managed.
*/
properties[PROP_MODEL] =
g_param_spec_object ("model", NULL, NULL,
G_TYPE_LIST_MODEL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
/**
* GtkSingleSelection: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);
/**
* GtkSingleSelection:selected: (attributes org.gtk.Property.get=gtk_single_selection_get_selected org.gtk.Property.set=gtk_single_selection_set_selected)
*
@ -414,18 +461,8 @@ gtk_single_selection_class_init (GtkSingleSelectionClass *klass)
*/
properties[PROP_SELECTED_ITEM] =
g_param_spec_object ("selected-item", NULL, NULL,
G_TYPE_OBJECT,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
/**
* GtkSingleSelection:model: (attributes org.gtk.Property.get=gtk_single_selection_get_model org.gtk.Property.set=gtk_single_selection_set_model)
*
* The model being managed.
*/
properties[PROP_MODEL] =
g_param_spec_object ("model", NULL, NULL,
G_TYPE_LIST_MODEL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
G_TYPE_OBJECT,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class, N_PROPS, properties);
}
@ -528,6 +565,8 @@ gtk_single_selection_set_model (GtkSingleSelection *self,
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED_ITEM]);
}
g_list_model_items_changed (G_LIST_MODEL (self), 0, n_items_before, 0);
if (n_items_before)
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
}
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MODEL]);

View File

@ -220,6 +220,14 @@ items_changed (GListModel *model,
}
}
static void
notify_n_items (GObject *object,
GParamSpec *pspec,
GString *changes)
{
g_string_append_c (changes, '*');
}
static void
selection_changed (GListModel *model,
guint position,
@ -265,6 +273,7 @@ new_model (GListStore *store, gboolean autoselect, gboolean can_unselect)
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);
changes = g_string_new ("");
g_object_set_qdata_full (G_OBJECT(result), selection_quark, changes, free_changes);
@ -336,19 +345,19 @@ test_changes (void)
g_list_store_remove (store, 3);
assert_model (selection, "1 2 3 5");
assert_changes (selection, "-3");
assert_changes (selection, "-3*");
assert_selection (selection, "");
assert_selection_changes (selection, "");
insert (store, 3, 99);
assert_model (selection, "1 2 3 99 5");
assert_changes (selection, "+3");
assert_changes (selection, "+3*");
assert_selection (selection, "");
assert_selection_changes (selection, "");
splice (store, 3, 2, (guint[]) { 97 }, 1);
assert_model (selection, "1 2 3 97");
assert_changes (selection, "3-2+1");
assert_changes (selection, "3-2+1*");
assert_selection (selection, "");
assert_selection_changes (selection, "");
@ -434,31 +443,31 @@ test_autoselect (void)
add (store, 1);
assert_model (selection, "1");
assert_changes (selection, "+0");
assert_changes (selection, "+0*");
assert_selection (selection, "1");
assert_selection_changes (selection, "");
splice (store, 0, 1, (guint[]) { 7, 8, 9 }, 3);
assert_model (selection, "7 8 9");
assert_changes (selection, "0-1+3");
assert_changes (selection, "0-1+3*");
assert_selection (selection, "7");
assert_selection_changes (selection, "");
splice (store, 0, 0, (guint[]) { 5, 6 }, 2);
assert_model (selection, "5 6 7 8 9");
assert_changes (selection, "0+2");
assert_changes (selection, "0+2*");
assert_selection (selection, "7");
assert_selection_changes (selection, "");
g_list_store_remove (store, 2);
assert_model (selection, "5 6 8 9");
assert_changes (selection, "2-2+1");
assert_changes (selection, "2-2+1*");
assert_selection (selection, "8");
assert_selection_changes (selection, "");
splice (store, 2, 2, NULL, 0);
assert_model (selection, "5 6");
assert_changes (selection, "1-3+1");
assert_changes (selection, "1-3+1*");
assert_selection (selection, "6");
assert_selection_changes (selection, "");
@ -470,13 +479,13 @@ test_autoselect (void)
g_list_store_remove (store, 0);
assert_model (selection, "2");
assert_changes (selection, "-0");
assert_changes (selection, "-0*");
assert_selection (selection, "2");
assert_selection_changes (selection, "");
g_list_store_remove (store, 0);
assert_model (selection, "");
assert_changes (selection, "-0");
assert_changes (selection, "-0*");
assert_selection (selection, "");
assert_selection_changes (selection, "");
@ -671,24 +680,24 @@ test_set_model (void)
/* we retain the selected item across model changes */
gtk_single_selection_set_model (GTK_SINGLE_SELECTION (selection), m2);
assert_changes (selection, "0-5+3");
assert_changes (selection, "0-5+3*");
assert_selection (selection, "1");
assert_selection_changes (selection, "");
gtk_single_selection_set_model (GTK_SINGLE_SELECTION (selection), NULL);
assert_changes (selection, "0-3");
assert_changes (selection, "0-3*");
assert_selection (selection, "");
assert_selection_changes (selection, "");
gtk_single_selection_set_autoselect (GTK_SINGLE_SELECTION (selection), FALSE);
gtk_single_selection_set_model (GTK_SINGLE_SELECTION (selection), m2);
assert_changes (selection, "0+3");
assert_changes (selection, "0+3*");
assert_selection (selection, "");
assert_selection_changes (selection, "");
/* we retain no selected item across model changes */
gtk_single_selection_set_model (GTK_SINGLE_SELECTION (selection), m1);
assert_changes (selection, "0-3+5");
assert_changes (selection, "0-3+5*");
assert_selection (selection, "");
assert_selection_changes (selection, "");
@ -697,7 +706,7 @@ test_set_model (void)
assert_selection_changes (selection, "4:1");
gtk_single_selection_set_model (GTK_SINGLE_SELECTION (selection), m2);
assert_changes (selection, "0-5+3");
assert_changes (selection, "0-5+3*");
assert_selection (selection, "");
assert_selection_changes (selection, "");