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




commit 99fde9ac13a7d6c7403785d2bdb00888978692e7
Author: Benjamin Otte <otte redhat com>
Date:   Sat Jun 11 04:49:17 2022 +0200

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

 gtk/gtkslicelistmodel.c        | 50 ++++++++++++++++++++++++++++++++++++++++--
 testsuite/gtk/slicelistmodel.c | 23 +++++++++++++------
 2 files changed, 64 insertions(+), 9 deletions(-)
---
diff --git a/gtk/gtkslicelistmodel.c b/gtk/gtkslicelistmodel.c
index 3af75754ed..fd2a394851 100644
--- a/gtk/gtkslicelistmodel.c
+++ b/gtk/gtkslicelistmodel.c
@@ -38,7 +38,9 @@
 
 enum {
   PROP_0,
+  PROP_ITEM_TYPE,
   PROP_MODEL,
+  PROP_N_ITEMS,
   PROP_OFFSET,
   PROP_SIZE,
   NUM_PROPERTIES
@@ -162,6 +164,8 @@ gtk_slice_list_model_items_changed_cb (GListModel        *model,
       n_before = CLAMP (n_before, self->offset, self->offset + self->size) - self->offset;
 
       g_list_model_items_changed (G_LIST_MODEL (self), skip, n_before - skip, n_after - skip);
+      if (n_before != n_after)
+        g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
     }
 }
 
@@ -203,10 +207,18 @@ gtk_slice_list_model_get_property (GObject     *object,
 
   switch (prop_id)
     {
+    case PROP_ITEM_TYPE:
+      g_value_set_gtype (value, gtk_slice_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_slice_list_model_get_n_items (G_LIST_MODEL (self)));
+      break;
+
     case PROP_OFFSET:
       g_value_set_uint (value, self->offset);
       break;
@@ -250,6 +262,18 @@ gtk_slice_list_model_class_init (GtkSliceListModelClass *class)
   gobject_class->get_property = gtk_slice_list_model_get_property;
   gobject_class->dispose = gtk_slice_list_model_dispose;
 
+  /**
+   * GtkSliceListModel: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);
+
   /**
    * GtkSliceListModel:model: (attributes org.gtk.Property.get=gtk_slice_list_model_get_model 
org.gtk.Property.set=gtk_slice_list_model_set_model)
    *
@@ -260,6 +284,18 @@ gtk_slice_list_model_class_init (GtkSliceListModelClass *class)
                            G_TYPE_LIST_MODEL,
                            GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
 
+  /**
+   * GtkSliceListModel: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);
+
   /**
    * GtkSliceListModel:offset: (attributes org.gtk.Property.get=gtk_slice_list_model_get_offset 
org.gtk.Property.set=gtk_slice_list_model_set_offset)
    *
@@ -360,6 +396,8 @@ gtk_slice_list_model_set_model (GtkSliceListModel *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]);
 }
@@ -409,6 +447,8 @@ gtk_slice_list_model_set_offset (GtkSliceListModel *self,
 
   if (before > 0 || after > 0)
     g_list_model_items_changed (G_LIST_MODEL (self), 0, before, after);
+  if (before != after)
+    g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
 
   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_OFFSET]);
 }
@@ -458,9 +498,15 @@ gtk_slice_list_model_set_size (GtkSliceListModel *self,
   after = g_list_model_get_n_items (G_LIST_MODEL (self));
 
   if (before > after)
-    g_list_model_items_changed (G_LIST_MODEL (self), after, before - after, 0);
+    {
+      g_list_model_items_changed (G_LIST_MODEL (self), after, before - after, 0);
+      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
+    }
   else if (before < after)
-    g_list_model_items_changed (G_LIST_MODEL (self), before, 0, after - before);
+    {
+      g_list_model_items_changed (G_LIST_MODEL (self), before, 0, after - before);
+      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
+    }
   /* else nothing */
 
   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SIZE]);
diff --git a/testsuite/gtk/slicelistmodel.c b/testsuite/gtk/slicelistmodel.c
index eab5bfec56..da87c8bd30 100644
--- a/testsuite/gtk/slicelistmodel.c
+++ b/testsuite/gtk/slicelistmodel.c
@@ -174,6 +174,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)
 {
@@ -198,6 +206,7 @@ new_model (GListStore *store, guint offset, guint size)
   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;
 }
@@ -245,11 +254,11 @@ test_set_model (void)
   store = new_store (1, 7, 2);
   gtk_slice_list_model_set_model (slice, G_LIST_MODEL (store));
   assert_model (slice, "1 3");
-  assert_changes (slice, "0+2");
+  assert_changes (slice, "0+2*");
 
   gtk_slice_list_model_set_model (slice, NULL);
   assert_model (slice, "");
-  assert_changes (slice, "0-2");
+  assert_changes (slice, "0-2*");
 
   g_object_unref (store);
   g_object_unref (slice);
@@ -272,11 +281,11 @@ test_set_slice (void)
 
   gtk_slice_list_model_set_size (slice, 2);
   assert_model (slice, "3 5");
-  assert_changes (slice, "-2");
+  assert_changes (slice, "-2*");
 
   gtk_slice_list_model_set_size (slice, 10);
   assert_model (slice, "3 5 7");
-  assert_changes (slice, "+2");
+  assert_changes (slice, "+2*");
 
   g_object_unref (store);
   g_object_unref (slice);
@@ -306,15 +315,15 @@ test_changes (void)
 
   splice (store, 13, 6, (guint[]) { 97 }, 1);
   assert_model (slice, "12 13 99 97");
-  assert_changes (slice, "3-2+1");
+  assert_changes (slice, "3-2+1*");
 
   splice (store, 13, 1, (guint[]) { 36, 37, 38 }, 3);
   assert_model (slice, "12 13 99 36 37");
-  assert_changes (slice, "3-1+2");
+  assert_changes (slice, "3-1+2*");
 
   g_list_store_remove_all (store);
   assert_model (slice, "");
-  assert_changes (slice, "0-5");
+  assert_changes (slice, "0-5*");
 
   g_object_unref (store);
   g_object_unref (slice);


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