[gtk/prop-list: 217/224] column view: add sort model and sorter properties



commit e651a625ad22c0928475bf86ba136ff60375f7f0
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Dec 3 02:25:20 2019 -0500

    column view: add sort model and sorter properties
    
    The sort model has to be known to the view so it can set a sorter
    on it. The sorter property is readonly, since the column view will
    always use a GtkColumnViewSorter. To customize the sorting of the
    view, applications will set sorters on individual columns.

 docs/reference/gtk/gtk4-sections.txt |  2 +
 gtk/gtkcolumnview.c                  | 79 ++++++++++++++++++++++++++++++++++++
 gtk/gtkcolumnview.h                  |  9 ++++
 3 files changed, 90 insertions(+)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index d6ab5a78ca..6b48957675 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -562,6 +562,8 @@ gtk_column_view_new
 gtk_column_view_get_columns
 gtk_column_view_get_model
 gtk_column_view_set_model
+gtk_column_view_set_sort_model
+gtk_column_view_get_sorter
 gtk_column_view_get_show_separators
 gtk_column_view_set_show_separators
 <SUBSECTION Standard>
diff --git a/gtk/gtkcolumnview.c b/gtk/gtkcolumnview.c
index 59b304e053..4914e65083 100644
--- a/gtk/gtkcolumnview.c
+++ b/gtk/gtkcolumnview.c
@@ -26,6 +26,7 @@
 #include "gtkcolumnlistitemfactoryprivate.h"
 #include "gtkcolumnviewcolumnprivate.h"
 #include "gtkcolumnviewlayoutprivate.h"
+#include "gtkcolumnviewsorterprivate.h"
 #include "gtkcssnodeprivate.h"
 #include "gtkintl.h"
 #include "gtklistview.h"
@@ -54,6 +55,9 @@ struct _GtkColumnView
 
   GtkListView *listview;
   GtkColumnListItemFactory *factory;
+
+  GtkSortListModel *sort_model;
+  GtkSorter *sorter;
 };
 
 struct _GtkColumnViewClass
@@ -71,6 +75,8 @@ enum
   PROP_SHOW_SEPARATORS,
   PROP_VADJUSTMENT,
   PROP_VSCROLL_POLICY,
+  PROP_SORT_MODEL,
+  PROP_SORTER,
 
   N_PROPS
 };
@@ -250,6 +256,9 @@ gtk_column_view_dispose (GObject *object)
   g_clear_pointer ((GtkWidget **) &self->listview, gtk_widget_unparent);
   g_clear_object (&self->factory);
 
+  g_clear_object (&self->sort_model);
+  g_clear_object (&self->sorter);
+
   G_OBJECT_CLASS (gtk_column_view_parent_class)->dispose (object);
 }
 
@@ -301,6 +310,14 @@ gtk_column_view_get_property (GObject    *object,
       g_value_set_enum (value, gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (self->listview)));
       break;
 
+    case PROP_SORT_MODEL:
+      g_value_set_object (value, self->sort_model);
+      break;
+
+    case PROP_SORTER:
+      g_value_set_object (value, self->sorter);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -357,6 +374,10 @@ gtk_column_view_set_property (GObject      *object,
         }
       break;
 
+    case PROP_SORT_MODEL:
+      gtk_column_view_set_sort_model (self, g_value_get_object (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -429,6 +450,20 @@ gtk_column_view_class_init (GtkColumnViewClass *klass)
                           FALSE,
                           G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
 
+  properties[PROP_SORT_MODEL] =
+    g_param_spec_object ("sort-model",
+                         P_("Sort Model"),
+                         P_("Sort Model for the items displayed"),
+                         GTK_TYPE_SORT_LIST_MODEL,
+                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
+  properties[PROP_SORTER] =
+    g_param_spec_object ("sorter",
+                         P_("Sorter"),
+                         P_("Sorter"),
+                         GTK_TYPE_SORTER,
+                         G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
   g_object_class_install_properties (gobject_class, N_PROPS, properties);
 
   /**
@@ -468,6 +503,7 @@ gtk_column_view_init (GtkColumnView *self)
   gtk_widget_set_layout_manager (self->header, gtk_column_view_layout_new (self));
   gtk_widget_set_parent (self->header, GTK_WIDGET (self));
 
+  self->sorter = gtk_column_view_sorter_new ();
   self->factory = gtk_column_list_item_factory_new (self);
   self->listview = GTK_LIST_VIEW (gtk_list_view_new_with_factory (
         GTK_LIST_ITEM_FACTORY (g_object_ref (self->factory))));
@@ -643,6 +679,7 @@ gtk_column_view_remove_column (GtkColumnView       *self,
         break;
     }
 
+  gtk_column_view_sorter_remove_column (GTK_COLUMN_VIEW_SORTER (self->sorter), column);
   gtk_column_view_column_set_column_view (column, NULL);
   g_list_store_remove (self->columns, i);
 }
@@ -681,6 +718,48 @@ gtk_column_view_get_header_widget (GtkColumnView *self)
   return GTK_LIST_ITEM_WIDGET (self->header);
 }
 
+/**
+ * gtk_column_view_set_sort_model:
+ * @self: a #GtkColumnView
+ * @sort_model: (nullable): a #GtkSortModel, or %NULL
+ *
+ * Informs the column view about the sort model that should be
+ * used for sorting. This will typically be a model underneath the
+ * list model set with gtk_column_view_set_model(), since the top-most
+ * one will always be a selection model.
+ */
+void
+gtk_column_view_set_sort_model (GtkColumnView    *self,
+                                GtkSortListModel *sort_model)
+{
+  g_return_if_fail (GTK_IS_COLUMN_VIEW (self));
+  g_return_if_fail (sort_model == NULL || GTK_IS_SORT_LIST_MODEL (sort_model));
+
+  if (!g_set_object (&self->sort_model, sort_model))
+    return;
+
+  if (self->sort_model)
+    gtk_sort_list_model_set_sorter (self->sort_model, self->sorter);
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SORT_MODEL]);
+}
+
+/**
+ * gtk_column_view_get_sorter:
+ * @self: a #GtkColumnView
+ *
+ * Returns the sorter associated with the column view.
+ *
+ * Returns: (transfer none): the #GtkSorter of @self
+ */
+GtkSorter *
+gtk_column_view_get_sorter (GtkColumnView *self)
+{
+  g_return_val_if_fail (GTK_IS_COLUMN_VIEW (self), NULL);
+
+  return self->sorter;
+}
+
 void
 gtk_column_view_active_sorter_changed (GtkColumnView *self)
 {
diff --git a/gtk/gtkcolumnview.h b/gtk/gtkcolumnview.h
index a4ac08a8b1..178c856b3b 100644
--- a/gtk/gtkcolumnview.h
+++ b/gtk/gtkcolumnview.h
@@ -25,6 +25,8 @@
 #endif
 
 #include <gtk/gtktypes.h>
+#include <gtk/gtksortlistmodel.h>
+#include <gtk/gtksorter.h>
 
 G_BEGIN_DECLS
 
@@ -65,12 +67,19 @@ GListModel *    gtk_column_view_get_model                       (GtkColumnView
 GDK_AVAILABLE_IN_ALL
 void            gtk_column_view_set_model                       (GtkColumnView          *self,
                                                                  GListModel             *model);
+
 GDK_AVAILABLE_IN_ALL
 gboolean        gtk_column_view_get_show_separators             (GtkColumnView          *self);
 GDK_AVAILABLE_IN_ALL
 void            gtk_column_view_set_show_separators             (GtkColumnView          *self,
                                                                  gboolean                show_separators);
 
+GDK_AVAILABLE_IN_ALL
+void            gtk_column_view_set_sort_model                  (GtkColumnView          *self,
+                                                                 GtkSortListModel       *model);
+GDK_AVAILABLE_IN_ALL
+GtkSorter *     gtk_column_view_get_sorter                      (GtkColumnView          *self);
+
 G_END_DECLS
 
 #endif  /* __GTK_COLUMN_VIEW_H__ */


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