flattenlistmodel: Implement GtkSectionModel
authorBenjamin Otte <otte@redhat.com>
Sat, 26 Feb 2022 03:52:16 +0000 (04:52 +0100)
committerBenjamin Otte <otte@redhat.com>
Tue, 9 May 2023 15:00:39 +0000 (17:00 +0200)
Each child model is reported as one section.

gtk/gtkflattenlistmodel.c
gtk/gtksectionmodel.c

index 04cf04c6fd0ee3a8694a8d5b85ab5a1fa6d9ec5d..f80e80ff6d5512fc1dcb1d39dc358039f2f2b8d4 100644 (file)
@@ -21,8 +21,8 @@
 
 #include "gtkflattenlistmodel.h"
 
+#include "gtksectionmodel.h"
 #include "gtkrbtreeprivate.h"
-#include "gtkprivate.h"
 
 /**
  * GtkFlattenListModel:
@@ -200,8 +200,39 @@ gtk_flatten_list_model_model_init (GListModelInterface *iface)
   iface->get_item = gtk_flatten_list_model_get_item;
 }
 
+static void
+gtk_flatten_list_model_get_section (GtkSectionModel *model,
+                                    guint            position,
+                                    guint           *out_start,
+                                    guint           *out_end)
+{
+  GtkFlattenListModel *self = GTK_FLATTEN_LIST_MODEL (model);
+  FlattenNode *node;
+  guint model_pos;
+
+  node = gtk_flatten_list_model_get_nth (self->items, position, &model_pos);
+  if (node == NULL)
+    {
+      *out_start = gtk_flatten_list_model_get_n_items (G_LIST_MODEL (self));
+      *out_end = G_MAXUINT;
+      return;
+    }
+
+  *out_start = position - model_pos;
+  *out_end = position - model_pos + g_list_model_get_n_items (node->model);
+}
+
+static void
+gtk_flatten_list_model_section_model_init (GtkSectionModelInterface *iface)
+{
+  iface->get_section = gtk_flatten_list_model_get_section;
+}
+
 G_DEFINE_TYPE_WITH_CODE (GtkFlattenListModel, gtk_flatten_list_model, G_TYPE_OBJECT,
-                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, gtk_flatten_list_model_model_init))
+                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
+                                                gtk_flatten_list_model_model_init)
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_SECTION_MODEL,
+                                                gtk_flatten_list_model_section_model_init))
 
 static void
 gtk_flatten_list_model_items_changed_cb (GListModel *model,
@@ -433,7 +464,7 @@ gtk_flatten_list_model_class_init (GtkFlattenListModelClass *class)
   properties[PROP_MODEL] =
       g_param_spec_object ("model", NULL, NULL,
                            G_TYPE_LIST_MODEL,
-                           GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
+                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
 
   /**
    * GtkFlattenListModel:n-items:
index 2df8562e01795b5d5409751d5526aa0401053ad0..a5084ab340ab3bd6356cfb256f16746df298b987 100644 (file)
@@ -93,7 +93,7 @@ gtk_section_model_default_init (GtkSectionModelInterface *iface)
    * model, though it would be rather useless to emit such a signal.
    *
    * The [signal@Gio.ListModel::items-changed] implies the effect of the
-   * [signal@Gtk.SectionModel::section-changed] signal for all the items
+   * [signal@Gtk.SectionModel::sections-changed] signal for all the items
    * it covers.
    *
    * Since: 4.12