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




commit 6504d2ee6e4e756e138b6942430631bd52fc92b9
Author: Benjamin Otte <otte redhat com>
Date:   Sat Jun 11 05:32:29 2022 +0200

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

 gtk/gtkmaplistmodel.c        | 41 +++++++++++++++++++++++++++++++++++++++++
 testsuite/gtk/maplistmodel.c | 13 +++++++++++--
 2 files changed, 52 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkmaplistmodel.c b/gtk/gtkmaplistmodel.c
index 7088f63d7d..886b4ef463 100644
--- a/gtk/gtkmaplistmodel.c
+++ b/gtk/gtkmaplistmodel.c
@@ -60,7 +60,10 @@
 enum {
   PROP_0,
   PROP_HAS_MAP,
+  PROP_ITEM_TYPE,
   PROP_MODEL,
+  PROP_N_ITEMS,
+
   NUM_PROPERTIES
 };
 
@@ -221,6 +224,8 @@ gtk_map_list_model_items_changed_cb (GListModel      *model,
   if (self->items == NULL)
     {
       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]);
       return;
     }
 
@@ -269,6 +274,8 @@ gtk_map_list_model_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]);
 }
 
 static void
@@ -305,10 +312,18 @@ gtk_map_list_model_get_property (GObject     *object,
       g_value_set_boolean (value, self->items != NULL);
       break;
 
+    case PROP_ITEM_TYPE:
+      g_value_set_gtype (value, gtk_map_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_map_list_model_get_n_items (G_LIST_MODEL (self)));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -360,6 +375,18 @@ gtk_map_list_model_class_init (GtkMapListModelClass *class)
                             FALSE,
                             GTK_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
 
+  /**
+   * GtkMapListModel: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);
+
   /**
    * GtkMapListModel:model: (attributes org.gtk.Property.get=gtk_map_list_model_get_model 
org.gtk.Property.set=gtk_map_list_model_set_model)
    *
@@ -370,6 +397,18 @@ gtk_map_list_model_class_init (GtkMapListModelClass *class)
                            G_TYPE_LIST_MODEL,
                            GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_EXPLICIT_NOTIFY);
 
+  /**
+   * GtkMapListModel: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);
 }
 
@@ -578,6 +617,8 @@ gtk_map_list_model_set_model (GtkMapListModel *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/maplistmodel.c b/testsuite/gtk/maplistmodel.c
index 6e42479624..2ee0c4cc64 100644
--- a/testsuite/gtk/maplistmodel.c
+++ b/testsuite/gtk/maplistmodel.c
@@ -164,6 +164,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)
 {
@@ -202,6 +210,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;
 }
@@ -249,11 +258,11 @@ test_set_model (void)
   store = new_store (1, 5, 1);
   gtk_map_list_model_set_model (map, G_LIST_MODEL (store));
   assert_model (map, "2 4 6 8 10");
-  assert_changes (map, "0+5");
+  assert_changes (map, "0+5*");
 
   gtk_map_list_model_set_model (map, NULL);
   assert_model (map, "");
-  assert_changes (map, "0-5");
+  assert_changes (map, "0-5*");
 
   g_object_unref (store);
   g_object_unref (map);


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