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




commit 47398dcc5473d6dc59f61dd9d4d96edb15e6d485
Author: Benjamin Otte <otte redhat com>
Date:   Sat Jun 11 05:15:28 2022 +0200

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

 gtk/gtkmultiselection.c        | 38 ++++++++++++++++++++++++++++++++
 testsuite/gtk/multiselection.c | 50 +++++++++++++++++++++++++-----------------
 2 files changed, 68 insertions(+), 20 deletions(-)
---
diff --git a/gtk/gtkmultiselection.c b/gtk/gtkmultiselection.c
index 9cf0356c9e..0849d8eda7 100644
--- a/gtk/gtkmultiselection.c
+++ b/gtk/gtkmultiselection.c
@@ -49,7 +49,9 @@ struct _GtkMultiSelectionClass
 
 enum {
   PROP_0,
+  PROP_ITEM_TYPE,
   PROP_MODEL,
+  PROP_N_ITEMS,
 
   N_PROPS,
 };
@@ -266,6 +268,8 @@ gtk_multi_selection_items_changed_cb (GListModel        *model,
   g_clear_pointer (&pending, g_hash_table_unref);
 
   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
@@ -311,10 +315,18 @@ gtk_multi_selection_get_property (GObject    *object,
 
   switch (prop_id)
     {
+    case PROP_ITEM_TYPE:
+      g_value_set_gtype (value, gtk_multi_selection_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_multi_selection_get_n_items (G_LIST_MODEL (self)));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -343,6 +355,18 @@ gtk_multi_selection_class_init (GtkMultiSelectionClass *klass)
   gobject_class->set_property = gtk_multi_selection_set_property;
   gobject_class->dispose = gtk_multi_selection_dispose;
 
+  /**
+   * GtkMultiSelection: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);
+
   /**
    * GtkMultiSelection:model: (attributes org.gtk.Property.get=gtk_multi_selection_get_model 
org.gtk.Property.set=gtk_multi_selection_set_model)
    *
@@ -353,6 +377,18 @@ gtk_multi_selection_class_init (GtkMultiSelectionClass *klass)
                          G_TYPE_LIST_MODEL,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
+  /**
+   * GtkMultiSelection: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, N_PROPS, properties);
 }
 
@@ -446,6 +482,8 @@ gtk_multi_selection_set_model (GtkMultiSelection *self,
       gtk_bitset_remove_all (self->selected);
       g_hash_table_remove_all (self->items);
       g_list_model_items_changed (G_LIST_MODEL (self), 0, n_items_before, 0);
+      if (n_items_before)
+        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/multiselection.c b/testsuite/gtk/multiselection.c
index 7117efb4e7..5825ede9ac 100644
--- a/testsuite/gtk/multiselection.c
+++ b/testsuite/gtk/multiselection.c
@@ -224,6 +224,14 @@ items_changed (GListModel *model,
     }
 }
 
+static void
+notify_n_items (GObject    *object,
+                GParamSpec *pspec,
+                GString    *changes)
+{
+  g_string_append_c (changes, '*');
+}
+
 static void
 selection_changed (GListModel *model,
                    guint       position,
@@ -258,6 +266,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);
 
   changes = g_string_new ("");
   g_object_set_qdata_full (G_OBJECT(result), selection_quark, changes, free_changes);
@@ -277,6 +286,7 @@ new_filter_model (GtkSelectionModel *model)
   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;
 }
@@ -331,19 +341,19 @@ test_changes (void)
 
   g_list_store_remove (store, 3);
   assert_model (selection, "1 2 3 5");
-  assert_changes (selection, "-3");
+  assert_changes (selection, "-3*");
   assert_selection (selection, "");
   assert_selection_changes (selection, "");
 
   insert (store, 3, 99);
   assert_model (selection, "1 2 3 99 5");
-  assert_changes (selection, "+3");
+  assert_changes (selection, "+3*");
   assert_selection (selection, "");
   assert_selection_changes (selection, "");
 
   splice (store, 3, 2, (guint[]) { 97 }, 1);
   assert_model (selection, "1 2 3 97");
-  assert_changes (selection, "3-2+1");
+  assert_changes (selection, "3-2+1*");
   assert_selection (selection, "");
   assert_selection_changes (selection, "");
 
@@ -354,7 +364,7 @@ test_changes (void)
 
   insert (store, 2, 22);
   assert_model (selection, "1 2 22 3 97");
-  assert_changes (selection, "+2");
+  assert_changes (selection, "+2*");
   assert_selection (selection, "2 3");
   assert_selection_changes (selection, "");
 
@@ -543,74 +553,74 @@ test_selection_filter (void)
   assert_selection (selection, "4");
   assert_selection_changes (selection, "3:1");
   assert_model (filter, "4");
-  assert_changes (filter, "+0");
+  assert_changes (filter, "+0*");
 
   ret = gtk_selection_model_unselect_item (selection, 3);
   g_assert_true (ret);
   assert_selection (selection, "");
   assert_selection_changes (selection, "3:1");
   assert_model (filter, "");
-  assert_changes (filter, "-0");
+  assert_changes (filter, "-0*");
 
   ret = gtk_selection_model_select_item (selection, 1, FALSE);
   g_assert_true (ret);
   assert_selection (selection, "2");
   assert_selection_changes (selection, "1:1");
   assert_model (filter, "2");
-  assert_changes (filter, "+0");
+  assert_changes (filter, "+0*");
 
   ret = gtk_selection_model_select_item (selection, 0, FALSE);
   g_assert_true (ret);
   assert_selection (selection, "1 2");
   assert_selection_changes (selection, "0:1");
   assert_model (filter, "1 2");
-  assert_changes (filter, "+0");
+  assert_changes (filter, "+0*");
 
   ret = gtk_selection_model_unselect_item (selection, 0);
   g_assert_true (ret);
   assert_selection (selection, "2");
   assert_selection_changes (selection, "0:1");
   assert_model (filter, "2");
-  assert_changes (filter, "-0");
+  assert_changes (filter, "-0*");
 
   ret = gtk_selection_model_select_range (selection, 3, 2, FALSE);
   g_assert_true (ret);
   assert_selection (selection, "2 4 5");
   assert_selection_changes (selection, "3:2");
   assert_model (filter, "2 4 5");
-  assert_changes (filter, "1+2");
+  assert_changes (filter, "1+2*");
 
   ret = gtk_selection_model_unselect_range (selection, 3, 2);
   g_assert_true (ret);
   assert_selection (selection, "2");
   assert_selection_changes (selection, "3:2");
   assert_model (filter, "2");
-  assert_changes (filter, "1-2");
+  assert_changes (filter, "1-2*");
 
   ret = gtk_selection_model_select_all (selection);
   g_assert_true (ret);
   assert_selection (selection, "1 2 3 4 5");
   assert_selection_changes (selection, "0:5");
   assert_model (filter, "1 2 3 4 5");
-  assert_changes (filter, "0-1+5");
+  assert_changes (filter, "0-1+5*");
 
   ret = gtk_selection_model_unselect_all (selection);
   g_assert_true (ret);
   assert_selection (selection, "");
   assert_selection_changes (selection, "0:5");
   assert_model (filter, "");
-  assert_changes (filter, "0-5");
+  assert_changes (filter, "0-5*");
 
   ret = gtk_selection_model_select_range (selection, 1, 3, FALSE);
   g_assert_true (ret);
   assert_selection (selection, "2 3 4");
   assert_selection_changes (selection, "1:3");
   assert_model (filter, "2 3 4");
-  assert_changes (filter, "0+3");
+  assert_changes (filter, "0+3*");
 
   insert (store, 2, 22);
   assert_model (selection, "1 2 22 3 4 5");
-  assert_changes (selection, "+2");
+  assert_changes (selection, "+2*");
   assert_selection (selection, "2 3 4");
   assert_selection_changes (selection, "");
   assert_model (filter, "2 3 4");
@@ -618,7 +628,7 @@ test_selection_filter (void)
 
   g_list_store_remove (store, 2);
   assert_model (selection, "1 2 3 4 5");
-  assert_changes (selection, "-2");
+  assert_changes (selection, "-2*");
   assert_selection (selection, "2 3 4");
   assert_selection_changes (selection, "");
   assert_model (filter, "2 3 4");
@@ -651,17 +661,17 @@ test_set_model (void)
 
   /* we retain the selected item across model changes */
   gtk_multi_selection_set_model (GTK_MULTI_SELECTION (selection), m2);
-  assert_changes (selection, "0-5+3");
+  assert_changes (selection, "0-5+3*");
   assert_selection (selection, "2 3");
   assert_selection_changes (selection, "");
 
   gtk_multi_selection_set_model (GTK_MULTI_SELECTION (selection), NULL);
-  assert_changes (selection, "0-3");
+  assert_changes (selection, "0-3*");
   assert_selection (selection, "");
   assert_selection_changes (selection, "");
 
   gtk_multi_selection_set_model (GTK_MULTI_SELECTION (selection), m2);
-  assert_changes (selection, "0+3");
+  assert_changes (selection, "0+3*");
   assert_selection (selection, "");
   assert_selection_changes (selection, "");
 
@@ -672,7 +682,7 @@ test_set_model (void)
 
   /* we retain no selected item across model changes */
   gtk_multi_selection_set_model (GTK_MULTI_SELECTION (selection), m1);
-  assert_changes (selection, "0-3+5");
+  assert_changes (selection, "0-3+5*");
   assert_selection (selection, "1 2 3");
   assert_selection_changes (selection, "");
 


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