[gtk/wip/otte/listmodels: 3/21] treelistmodel: Add ::item-type and ::n-items




commit 6b1fb7ca4370af5f97e2fd1e6568d0d74800a4e0
Author: Benjamin Otte <otte redhat com>
Date:   Sat Jun 11 04:48:03 2022 +0200

    treelistmodel: Add ::item-type and ::n-items
    
    With tests!

 gtk/gtktreelistmodel.c        | 46 +++++++++++++++++++++++++++++++++++++++++--
 testsuite/gtk/defaultvalue.c  |  4 ++++
 testsuite/gtk/treelistmodel.c | 21 ++++++++++++++------
 3 files changed, 63 insertions(+), 8 deletions(-)
---
diff --git a/gtk/gtktreelistmodel.c b/gtk/gtktreelistmodel.c
index 4da56b45f4..387af07e3c 100644
--- a/gtk/gtktreelistmodel.c
+++ b/gtk/gtktreelistmodel.c
@@ -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 [func@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]);
diff --git a/testsuite/gtk/defaultvalue.c b/testsuite/gtk/defaultvalue.c
index d55e6d5381..455c2b3b25 100644
--- a/testsuite/gtk/defaultvalue.c
+++ b/testsuite/gtk/defaultvalue.c
@@ -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)
diff --git a/testsuite/gtk/treelistmodel.c b/testsuite/gtk/treelistmodel.c
index 0ea7a37b6c..bf3f12caf3 100644
--- a/testsuite/gtk/treelistmodel.c
+++ b/testsuite/gtk/treelistmodel.c
@@ -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);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]