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

With tests!
This commit is contained in:
Benjamin Otte 2022-06-11 04:48:03 +02:00
parent c26962c4b1
commit fd4562aac9
3 changed files with 63 additions and 8 deletions

View File

@ -34,7 +34,9 @@
enum {
PROP_0,
PROP_AUTOEXPAND,
PROP_ITEM_TYPE,
PROP_MODEL,
PROP_N_ITEMS,
PROP_PASSTHROUGH,
NUM_PROPERTIES
};
@ -432,6 +434,8 @@ gtk_tree_list_model_items_changed_cb (GListModel *model,
tree_position,
tree_removed,
tree_added);
if (tree_removed != tree_added)
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
}
static void gtk_tree_list_row_destroy (GtkTreeListRow *row);
@ -645,10 +649,18 @@ gtk_tree_list_model_get_property (GObject *object,
g_value_set_boolean (value, self->autoexpand);
break;
case PROP_ITEM_TYPE:
g_value_set_gtype (value, gtk_tree_list_model_get_item_type (G_LIST_MODEL (self)));
break;
case PROP_MODEL:
g_value_set_object (value, self->root_node.model);
break;
case PROP_N_ITEMS:
g_value_set_uint (value, tree_node_get_n_children (&self->root_node));
break;
case PROP_PASSTHROUGH:
g_value_set_boolean (value, self->passthrough);
break;
@ -690,6 +702,18 @@ gtk_tree_list_model_class_init (GtkTreeListModelClass *class)
FALSE,
GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkTreeListModel: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);
/**
* GtkTreeListModel:model: (attributes org.gtk.Property.get=gtk_tree_list_model_get_model)
*
@ -700,6 +724,18 @@ gtk_tree_list_model_class_init (GtkTreeListModelClass *class)
G_TYPE_LIST_MODEL,
GTK_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkTreeListModel: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);
/**
* GtkTreeListModel:passthrough: (attributes org.gtk.Property.get=gtk_tree_list_model_get_passthrough)
*
@ -1192,13 +1228,19 @@ gtk_tree_list_row_set_expanded (GtkTreeListRow *self,
{
n_items = gtk_tree_list_model_expand_node (list, self->node);
if (n_items > 0)
g_list_model_items_changed (G_LIST_MODEL (list), tree_node_get_position (self->node) + 1, 0, n_items);
{
g_list_model_items_changed (G_LIST_MODEL (list), tree_node_get_position (self->node) + 1, 0, n_items);
g_object_notify_by_pspec (G_OBJECT (list), properties[PROP_N_ITEMS]);
}
}
else
{
n_items = gtk_tree_list_model_collapse_node (list, self->node);
if (n_items > 0)
g_list_model_items_changed (G_LIST_MODEL (list), tree_node_get_position (self->node) + 1, n_items, 0);
{
g_list_model_items_changed (G_LIST_MODEL (list), tree_node_get_position (self->node) + 1, n_items, 0);
g_object_notify_by_pspec (G_OBJECT (list), properties[PROP_N_ITEMS]);
}
}
g_object_notify_by_pspec (G_OBJECT (self), row_properties[ROW_PROP_EXPANDED]);

View File

@ -298,6 +298,10 @@ G_GNUC_END_IGNORE_DEPRECATIONS
strcmp (pspec->name, "model") == 0)
check = FALSE;
if (g_type_is_a (type, GTK_TYPE_TREE_LIST_MODEL) &&
(strcmp (pspec->name, "item-type") == 0)) /* might be a treelistrow */
check = FALSE;
/* This is set in init() */
if (g_type_is_a (type, GTK_TYPE_FONT_CHOOSER_WIDGET) &&
strcmp (pspec->name, "tweak-action") == 0)

View File

@ -142,6 +142,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)
{
@ -174,6 +182,7 @@ new_model (guint size,
changes = g_string_new ("");
g_object_set_qdata_full (G_OBJECT(tree), changes_quark, changes, free_changes);
g_signal_connect (tree, "items-changed", G_CALLBACK (items_changed), changes);
g_signal_connect (tree, "notify::n-items", G_CALLBACK (notify_n_items), changes);
return tree;
}
@ -193,7 +202,7 @@ test_expand (void)
g_object_unref (row);
}
assert_model (tree, "100 100 90 80 70 60 50 40 30 20 10");
assert_changes (tree, "1+10");
assert_changes (tree, "1+10*");
for (i = g_list_model_get_n_items (G_LIST_MODEL (tree)); i > 0; i--)
{
@ -202,7 +211,7 @@ test_expand (void)
g_object_unref (row);
}
assert_model (tree, "100 100 100 99 98 97 96 95 94 93 92 91 90 90 89 88 87 86 85 84 83 82 81 80 80 79 78 77 76 75 74 73 72 71 70 70 69 68 67 66 65 64 63 62 61 60 60 59 58 57 56 55 54 53 52 51 50 50 49 48 47 46 45 44 43 42 41 40 40 39 38 37 36 35 34 33 32 31 30 30 29 28 27 26 25 24 23 22 21 20 20 19 18 17 16 15 14 13 12 11 10 10 9 8 7 6 5 4 3 2 1");
assert_changes (tree, "11+10, 10+10, 9+10, 8+10, 7+10, 6+10, 5+10, 4+10, 3+10, 2+10");
assert_changes (tree, "11+10*, 10+10*, 9+10*, 8+10*, 7+10*, 6+10*, 5+10*, 4+10*, 3+10*, 2+10*");
for (i = g_list_model_get_n_items (G_LIST_MODEL (tree)); i > 0; i--)
{
@ -229,25 +238,25 @@ test_remove_some (void)
g_assert_true (G_IS_LIST_MODEL (item));
g_list_store_remove (item, 3);
assert_model (tree, "100 100 100 99 98 96 95 94 93 92 91 90 90 89 88 87 86 85 84 83 82 81 80 80 79 78 77 76 75 74 73 72 71 70 70 69 68 67 66 65 64 63 62 61 60 60 59 58 57 56 55 54 53 52 51 50 50 49 48 47 46 45 44 43 42 41 40 40 39 38 37 36 35 34 33 32 31 30 30 29 28 27 26 25 24 23 22 21 20 20 19 18 17 16 15 14 13 12 11 10 10 9 8 7 6 5 4 3 2 1");
assert_changes (tree, "-5");
assert_changes (tree, "-5*");
item = g_list_model_get_item (G_LIST_MODEL (tree), 0);
g_assert_true (G_IS_LIST_MODEL (item));
g_list_store_remove (item, 3);
assert_model (tree, "100 100 100 99 98 96 95 94 93 92 91 90 90 89 88 87 86 85 84 83 82 81 80 80 79 78 77 76 75 74 73 72 71 60 60 59 58 57 56 55 54 53 52 51 50 50 49 48 47 46 45 44 43 42 41 40 40 39 38 37 36 35 34 33 32 31 30 30 29 28 27 26 25 24 23 22 21 20 20 19 18 17 16 15 14 13 12 11 10 10 9 8 7 6 5 4 3 2 1");
assert_changes (tree, "33-11");
assert_changes (tree, "33-11*");
item = g_list_model_get_item (G_LIST_MODEL (tree), 88);
g_assert_true (G_IS_LIST_MODEL (item));
g_list_store_remove (item, 9);
assert_model (tree, "100 100 100 99 98 96 95 94 93 92 91 90 90 89 88 87 86 85 84 83 82 81 80 80 79 78 77 76 75 74 73 72 71 60 60 59 58 57 56 55 54 53 52 51 50 50 49 48 47 46 45 44 43 42 41 40 40 39 38 37 36 35 34 33 32 31 30 30 29 28 27 26 25 24 23 22 21 20 20 19 18 17 16 15 14 13 12 11 10 10 9 8 7 6 5 4 3 2");
assert_changes (tree, "-98");
assert_changes (tree, "-98*");
item = g_list_model_get_item (G_LIST_MODEL (tree), 0);
g_assert_true (G_IS_LIST_MODEL (item));
g_list_store_remove (item, 8);
assert_model (tree, "100 100 100 99 98 96 95 94 93 92 91 90 90 89 88 87 86 85 84 83 82 81 80 80 79 78 77 76 75 74 73 72 71 60 60 59 58 57 56 55 54 53 52 51 50 50 49 48 47 46 45 44 43 42 41 40 40 39 38 37 36 35 34 33 32 31 30 30 29 28 27 26 25 24 23 22 21 20 20 19 18 17 16 15 14 13 12 11");
assert_changes (tree, "88-10");
assert_changes (tree, "88-10*");
g_object_unref (tree);
}