[gtk/prop-list: 162/179] Add GtkTreeListRowSorter



commit 97326fa6c2ffd9264bb026dd6cc5ac5d3eea82d2
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Dec 8 17:34:10 2019 -0500

    Add GtkTreeListRowSorter
    
    This is a special-purpose sorter that can
    apply the sorting of another sorter to the
    levels of a GtkTreeListModel.

 docs/reference/gtk/gtk4-docs.xml     |   6 +
 docs/reference/gtk/gtk4-sections.txt |  17 +++
 docs/reference/gtk/gtk4.types.in     |   1 +
 gtk/gtk.h                            |   1 +
 gtk/gtktreelistrowsorter.c           | 268 +++++++++++++++++++++++++++++++++++
 gtk/gtktreelistrowsorter.h           |  47 ++++++
 gtk/meson.build                      |   2 +
 7 files changed, 342 insertions(+)
---
diff --git a/docs/reference/gtk/gtk4-docs.xml b/docs/reference/gtk/gtk4-docs.xml
index 3ae88d8a7d..dc2761def5 100644
--- a/docs/reference/gtk/gtk4-docs.xml
+++ b/docs/reference/gtk/gtk4-docs.xml
@@ -55,6 +55,12 @@
       <xi:include href="xml/gtkflattenlistmodel.xml" />
       <xi:include href="xml/gtkmaplistmodel.xml" />
       <xi:include href="xml/gtkslicelistmodel.xml" />
+      <xi:include href="xml/gtksorter.xml" />
+      <xi:include href="xml/gtkstringsorter.xml" />
+      <xi:include href="xml/gtknumericsorter.xml" />
+      <xi:include href="xml/gtkcustomsorter.xml" />
+      <xi:include href="xml/gtkmultisorter.xml" />
+      <xi:include href="xml/gtktreelistrowsorter.xml" />
       <xi:include href="xml/gtksortlistmodel.xml" />
       <section>
         <xi:include href="xml/gtksorter.xml" />
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index dc891c00db..9f5def3d47 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -3024,6 +3024,23 @@ GTK_MULTI_SORTER_GET_CLASS
 gtk_multi_sorter_get_type
 </SECTION>
 
+<SECTION>
+<FILE>gtktreelistrowsorter</FILE>
+<TITLE>GtkTreeListRowSorter</TITLE>
+GtkTreeListRowSorter
+gtk_tree_list_row_sorter_new
+gtk_tree_list_row_sorter_get_sorter
+gtk_tree_list_row_sorter_set_sorter
+<SUBSECTION Standard>
+GTK_TREE_LIST_ROW_SORTER
+GTK_IS_TREE_LIST_ROW_SORTER
+GTK_TYPE_TREE_LIST_ROW_SORTER
+GTK_IS_TREE_LIST_ROW_SORTER_CLASS
+GTK_TREE_LIST_ROW_SORTER_GET_CLASS
+<SUBSECTION Private>
+gtk_tree_list_row_sorter_get_type
+</SECTION>
+
 <SECTION>
 <FILE>gtksortlistmodel</FILE>
 <TITLE>GtkSortListModel</TITLE>
diff --git a/docs/reference/gtk/gtk4.types.in b/docs/reference/gtk/gtk4.types.in
index 4c0b76e48c..3f09070b9e 100644
--- a/docs/reference/gtk/gtk4.types.in
+++ b/docs/reference/gtk/gtk4.types.in
@@ -227,6 +227,7 @@ gtk_tree_drag_source_get_type
 gtk_tree_expander_get_type
 gtk_tree_list_model_get_type
 gtk_tree_list_row_get_type
+gtk_tree_list_row_sorter_get_type
 gtk_tree_model_filter_get_type
 gtk_tree_model_get_type
 gtk_tree_model_sort_get_type
diff --git a/gtk/gtk.h b/gtk/gtk.h
index d534cacb3a..e606c7f0b5 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -267,6 +267,7 @@
 #include <gtk/gtktreednd.h>
 #include <gtk/gtktreeexpander.h>
 #include <gtk/gtktreelistmodel.h>
+#include <gtk/gtktreelistrowsorter.h>
 #include <gtk/gtktreemodel.h>
 #include <gtk/gtktreemodelfilter.h>
 #include <gtk/gtktreemodelsort.h>
diff --git a/gtk/gtktreelistrowsorter.c b/gtk/gtktreelistrowsorter.c
new file mode 100644
index 0000000000..861fc379da
--- /dev/null
+++ b/gtk/gtktreelistrowsorter.c
@@ -0,0 +1,268 @@
+
+/*
+ * Copyright © 2019 Matthias Clasen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Matthias Clasen <mclasen redhat com>
+ */
+
+#include "config.h"
+
+#include "gtktreelistrowsorter.h"
+
+#include "gtktreelistmodel.h"
+
+#include "gtkintl.h"
+#include "gtktypebuiltins.h"
+
+/**
+ * SECTION:gtktreelistrowsorter
+ * @title: GtkTreeListRowSorter
+ * @Short_description: Sort trees by levels
+ * @See_also: #GtkTreeListModel
+ *
+ * #GtkTreeListSorter is a special-purpose sorter that will apply a given sorter
+ * to the levels in a tree, while respecting the tree structure.
+ */
+
+struct _GtkTreeListRowSorter
+{
+  GtkSorter parent_instance;
+
+  GtkSorter *sorter;
+};
+
+enum {
+  PROP_0,
+  PROP_SORTER,
+  NUM_PROPERTIES
+};
+
+static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
+
+G_DEFINE_TYPE (GtkTreeListRowSorter, gtk_tree_list_row_sorter, GTK_TYPE_SORTER)
+
+static GtkOrdering
+gtk_tree_list_row_sorter_compare (GtkSorter *sorter,
+                                  gpointer   item1,
+                                  gpointer   item2)
+{
+  GtkTreeListRowSorter *self = GTK_TREE_LIST_ROW_SORTER (sorter);
+  GtkTreeListRow *r1, *r2;
+  GtkTreeListRow *p1, *p2;
+  guint d1, d2;
+  GtkOrdering result = GTK_ORDERING_EQUAL;;
+
+  if (!GTK_IS_TREE_LIST_ROW (item1))
+    return GTK_IS_TREE_LIST_ROW (item2) ? GTK_ORDERING_LARGER : GTK_ORDERING_EQUAL;
+  else if (!GTK_IS_TREE_LIST_ROW (item2))
+    return GTK_ORDERING_SMALLER;
+
+  if (self->sorter == NULL)
+    return GTK_ORDERING_EQUAL;
+
+  r1 = GTK_TREE_LIST_ROW (item1);
+  r2 = GTK_TREE_LIST_ROW (item2);
+
+  g_object_ref (r1);
+  g_object_ref (r2);
+
+  d1 = gtk_tree_list_row_get_depth (r1);
+  d2 = gtk_tree_list_row_get_depth (r2);
+
+  /* First, get to the same depth */
+  while (d1 > d2)
+    {
+      p1 = gtk_tree_list_row_get_parent (r1);
+      g_object_unref (r1);
+      r1 = p1;
+      d1--;
+    }
+  while (d2 > d1)
+    {
+      p2 = gtk_tree_list_row_get_parent (r2);
+      g_object_unref (r2);
+      r2 = p2;
+      d2--;
+    }
+
+  /* Now walk up until we find a common parent */
+  while (TRUE)
+    {
+      p1 = gtk_tree_list_row_get_parent (r1);
+      p2 = gtk_tree_list_row_get_parent (r2);
+      if (p1 == p2)
+        {
+          gpointer obj1 = gtk_tree_list_row_get_item (r1);
+          gpointer obj2 = gtk_tree_list_row_get_item (r2);
+      
+          result = gtk_sorter_compare (self->sorter, obj1, obj2);
+
+          g_object_unref (obj1);
+          g_object_unref (obj2);
+
+          break;
+        }
+      else
+        {
+          g_object_unref (r1);
+          r1 = p1;
+          g_object_unref (r2);
+          r2 = p2;
+        }
+    }
+
+  g_object_unref (r1);
+  g_object_unref (r2);
+
+  return result;
+}
+
+static void
+propagate_changed (GtkSorter *sorter, GtkSorterChange change, gpointer data)
+{
+  gtk_sorter_changed (GTK_SORTER (data), change);
+}
+
+static void
+gtk_tree_list_row_sorter_dispose (GObject *object)
+{
+  GtkTreeListRowSorter *self = GTK_TREE_LIST_ROW_SORTER (object);
+
+  if (self->sorter)
+    g_signal_handlers_disconnect_by_func (self->sorter, propagate_changed, self);
+  g_clear_object (&self->sorter);
+
+  G_OBJECT_CLASS (gtk_tree_list_row_sorter_parent_class)->dispose (object);
+}
+
+static void
+gtk_tree_list_row_sorter_set_property (GObject      *object,
+                                       guint         prop_id,
+                                       const GValue *value,
+                                       GParamSpec   *pspec)
+{
+  GtkTreeListRowSorter *self = GTK_TREE_LIST_ROW_SORTER (object);
+
+  switch (prop_id)
+    {
+    case PROP_SORTER:
+      gtk_tree_list_row_sorter_set_sorter (self, GTK_SORTER (g_value_get_object (value)));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_tree_list_row_sorter_get_property (GObject     *object,
+                                       guint        prop_id,
+                                       GValue      *value,
+                                       GParamSpec  *pspec)
+{
+  GtkTreeListRowSorter *self = GTK_TREE_LIST_ROW_SORTER (object);
+
+  switch (prop_id)
+    {
+    case PROP_SORTER:
+      g_value_set_object (value, self->sorter);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_tree_list_row_sorter_class_init (GtkTreeListRowSorterClass *class)
+{
+  GtkSorterClass *sorter_class = GTK_SORTER_CLASS (class);
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  object_class->dispose = gtk_tree_list_row_sorter_dispose;
+  object_class->set_property = gtk_tree_list_row_sorter_set_property;
+  object_class->get_property = gtk_tree_list_row_sorter_get_property;
+
+  sorter_class->compare = gtk_tree_list_row_sorter_compare;
+
+  /**
+   * GtkTreeListRowSorter:sorter:
+   *
+   * The underlying sorter
+   */
+  properties[PROP_SORTER] =
+      g_param_spec_object ("sorter",
+                          P_("Sorter"),
+                          P_("The sorter"),
+                          GTK_TYPE_SORTER,
+                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+
+  g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
+}
+
+static void
+gtk_tree_list_row_sorter_init (GtkTreeListRowSorter *self)
+{
+}
+
+/**
+ * gtk_tree_list_row_sorter_new:
+ * @sorter: a #GtkSorter
+ *
+ * Create a special-purpose sorter that applies the sorting
+ * of @sorter to the levels of a #GtkTreeListModel.
+ *
+ * Note that this sorter relies on #GtkTreeListModel:passthrough
+ * being %FALSE.
+ *
+ * Returns: a new #GtkSorter
+ */
+GtkSorter *
+gtk_tree_list_row_sorter_new (GtkSorter *sorter)
+{
+  return g_object_new (GTK_TYPE_TREE_LIST_ROW_SORTER,
+                       "sorter", sorter,
+                       NULL);
+}
+
+void
+gtk_tree_list_row_sorter_set_sorter (GtkTreeListRowSorter *self,
+                                     GtkSorter            *sorter)
+{
+  g_return_if_fail (GTK_IS_TREE_LIST_ROW_SORTER (self));
+  g_return_if_fail (sorter == NULL || GTK_IS_SORTER (sorter));
+
+  if (self->sorter == sorter)
+    return;
+
+  if (self->sorter)
+    g_signal_handlers_disconnect_by_func (self->sorter, propagate_changed, self);
+  g_set_object (&self->sorter, sorter);
+  if (self->sorter)
+    g_signal_connect (sorter, "changed", G_CALLBACK (propagate_changed), self);
+
+#if 0
+  gtk_sorter_changed (GTK_SORTER (self), sorter == NULL
+                                         ? GTK_SORTER_CHANGE_NO_SORT
+                                         : GTK_SORTER_CHANGE_DIFFERENT);
+#else
+  gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_DIFFERENT);
+#endif
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SORTER]); 
+}
diff --git a/gtk/gtktreelistrowsorter.h b/gtk/gtktreelistrowsorter.h
new file mode 100644
index 0000000000..8f2db0e51a
--- /dev/null
+++ b/gtk/gtktreelistrowsorter.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright © 2019 Matthias Clasen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Matthias Clasen <mclasen redhat com>
+ */
+
+#ifndef __GTK_TREE_LIST_ROW_SORTER_H__
+#define __GTK_TREE_LIST_ROW_SORTER_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtkexpression.h>
+#include <gtk/gtksorter.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_TREE_LIST_ROW_SORTER             (gtk_tree_list_row_sorter_get_type ())
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (GtkTreeListRowSorter, gtk_tree_list_row_sorter, GTK, TREE_LIST_ROW_SORTER, GtkSorter)
+
+GDK_AVAILABLE_IN_ALL
+GtkSorter *             gtk_tree_list_row_sorter_new                   (GtkSorter            *sorter);
+
+GDK_AVAILABLE_IN_ALL
+GtkSorter *             gtk_tree_list_row_sorter_get_sorter            (GtkTreeListRowSorter *self);
+GDK_AVAILABLE_IN_ALL
+void                    gtk_tree_list_row_sorter_set_sorter            (GtkTreeListRowSorter *self,
+                                                                        GtkSorter            *sorter);
+
+G_END_DECLS
+
+#endif /* __GTK_TREE_LIST_ROW_SORTER_H__ */
diff --git a/gtk/meson.build b/gtk/meson.build
index 44a4f206d3..e41030ad5f 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -423,6 +423,7 @@ gtk_public_sources = files([
   'gtktreednd.c',
   'gtktreeexpander.c',
   'gtktreelistmodel.c',
+  'gtktreelistrowsorter.c',
   'gtktreemenu.c',
   'gtktreemodel.c',
   'gtktreemodelfilter.c',
@@ -688,6 +689,7 @@ gtk_public_headers = files([
   'gtktreednd.h',
   'gtktreeexpander.h',
   'gtktreelistmodel.h',
+  'gtktreelistrowsorter.h',
   'gtktreemodel.h',
   'gtktreemodelfilter.h',
   'gtktreemodelsort.h',


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