[gtk/wip/otte/listmodels: 14/18] fattenlistmodel: Add ::item-type and ::n-items




commit 865a484c5992981526dd6b0fa3ece2ffb6baae14
Author: Benjamin Otte <otte redhat com>
Date:   Sat Jun 11 06:43:25 2022 +0200

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

 gtk/gtkflattenlistmodel.c        | 41 ++++++++++++++++++++++++++++++++++++++++
 testsuite/gtk/flattenlistmodel.c | 27 +++++++++++++++++---------
 2 files changed, 59 insertions(+), 9 deletions(-)
---
diff --git a/gtk/gtkflattenlistmodel.c b/gtk/gtkflattenlistmodel.c
index 384f82e081..39b5cbe9ae 100644
--- a/gtk/gtkflattenlistmodel.c
+++ b/gtk/gtkflattenlistmodel.c
@@ -36,7 +36,10 @@
 
 enum {
   PROP_0,
+  PROP_ITEM_TYPE,
   PROP_MODEL,
+  PROP_N_ITEMS,
+
   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);
+  if (removed != added)
+    g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
 }
 
 static void
@@ -332,10 +337,18 @@ gtk_flatten_list_model_get_property (GObject    *object,
 
   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:
       g_value_set_object (value, self->model);
       break;
 
+    case PROP_N_ITEMS:
+      g_value_set_uint (value, gtk_flatten_list_model_get_n_items (G_LIST_MODEL (self)));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -367,6 +380,8 @@ gtk_flatten_list_model_model_items_changed_cb (GListModel          *model,
 
   if (real_removed > 0 || real_added > 0)
     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
@@ -399,6 +414,18 @@ gtk_flatten_list_model_class_init (GtkFlattenListModelClass *class)
   gobject_class->get_property = gtk_flatten_list_model_get_property;
   gobject_class->dispose = gtk_flatten_list_model_dispose;
 
+  /**
+   * GtkFlattenListModel: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);
+
   /**
    * 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,
                            GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
 
+  /**
+   * GtkFlattenListModel:n-items:
+   *
+   * The number of items. See [func@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);
 }
 
@@ -481,6 +520,8 @@ gtk_flatten_list_model_set_model (GtkFlattenListModel *self,
 
   if (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]);
 
   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MODEL]);
 }
diff --git a/testsuite/gtk/flattenlistmodel.c b/testsuite/gtk/flattenlistmodel.c
index 9144eac64a..4a310956fd 100644
--- a/testsuite/gtk/flattenlistmodel.c
+++ b/testsuite/gtk/flattenlistmodel.c
@@ -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
 free_changes (gpointer data)
 {
@@ -216,6 +224,7 @@ new_model (GListStore *store)
   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;
 }
@@ -271,7 +280,7 @@ test_model_add (void)
   add_store (model, 8, 10, 1);
 
   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 (flat);
@@ -293,13 +302,13 @@ test_submodel_add (void)
   store[2] = add_store (model, 5, 4, 1);
   store[3] = add_store (model, 8, 8, 1);
   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);
   splice (store[2], 0, 0, (guint[3]) { 5, 6, 7 }, 3);
   splice (store[3], 1, 0, (guint[2]) { 9, 10 }, 2);
   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 (flat);
@@ -325,19 +334,19 @@ test_submodel_add2 (void)
 
   add (store[0], 1);
   assert_model (flat, "1");
-  assert_changes (flat, "+0");
+  assert_changes (flat, "+0*");
 
   add (store[1], 3);
   assert_model (flat, "1 3");
-  assert_changes (flat, "+1");
+  assert_changes (flat, "+1*");
 
   add (store[0], 2);
   assert_model (flat, "1 2 3");
-  assert_changes (flat, "+1");
+  assert_changes (flat, "+1*");
 
   add (store[1], 4);
   assert_model (flat, "1 2 3 4");
-  assert_changes (flat, "+3");
+  assert_changes (flat, "+3*");
 
   g_object_unref (model);
   g_object_unref (flat);
@@ -363,7 +372,7 @@ test_model_remove (void)
   g_list_store_remove (model, 0);
   g_object_unref (model);
   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);
 }
@@ -389,7 +398,7 @@ test_submodel_remove (void)
   g_object_unref (model);
 
   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);
 }


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