2018-09-14 02:34:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Benjamin Otte
|
|
|
|
*
|
|
|
|
* 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: Benjamin Otte <otte@gnome.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtksortlistmodel.h"
|
|
|
|
|
|
|
|
#include "gtkintl.h"
|
|
|
|
#include "gtkprivate.h"
|
2020-07-17 00:47:22 +00:00
|
|
|
#include "gtktimsortprivate.h"
|
2018-09-14 02:34:40 +00:00
|
|
|
|
2020-07-17 00:28:42 +00:00
|
|
|
typedef struct _SortItem SortItem;
|
|
|
|
struct _SortItem
|
|
|
|
{
|
|
|
|
GObject *item;
|
|
|
|
guint position;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
sort_item_clear (gpointer data)
|
|
|
|
{
|
|
|
|
SortItem *si = data;
|
|
|
|
|
|
|
|
g_clear_object (&si->item);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GDK_ARRAY_ELEMENT_TYPE SortItem
|
2020-07-16 23:56:18 +00:00
|
|
|
#define GDK_ARRAY_TYPE_NAME SortArray
|
|
|
|
#define GDK_ARRAY_NAME sort_array
|
2020-07-17 00:28:42 +00:00
|
|
|
#define GDK_ARRAY_FREE_FUNC sort_item_clear
|
|
|
|
#define GDK_ARRAY_BY_VALUE 1
|
2020-07-16 23:56:18 +00:00
|
|
|
#include "gdk/gdkarrayimpl.c"
|
|
|
|
|
2018-09-14 02:34:40 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gtksortlistmodel
|
|
|
|
* @title: GtkSortListModel
|
2019-02-24 20:29:08 +00:00
|
|
|
* @short_description: A list model that sorts its items
|
2019-12-03 04:44:22 +00:00
|
|
|
* @see_also: #GListModel, #GtkSorter
|
2018-09-14 02:34:40 +00:00
|
|
|
*
|
|
|
|
* #GtkSortListModel is a list model that takes a list model and
|
2019-12-03 04:44:22 +00:00
|
|
|
* sorts its elements according to a #GtkSorter.
|
2018-09-14 02:34:40 +00:00
|
|
|
*
|
|
|
|
* #GtkSortListModel is a generic model and because of that it
|
|
|
|
* cannot take advantage of any external knowledge when sorting.
|
|
|
|
* If you run into performance issues with #GtkSortListModel, it
|
|
|
|
* is strongly recommended that you write your own sorting list
|
|
|
|
* model.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_MODEL,
|
2019-12-03 04:44:22 +00:00
|
|
|
PROP_SORTER,
|
2018-09-14 02:34:40 +00:00
|
|
|
NUM_PROPERTIES
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkSortListModel
|
|
|
|
{
|
|
|
|
GObject parent_instance;
|
|
|
|
|
|
|
|
GListModel *model;
|
2019-12-03 04:44:22 +00:00
|
|
|
GtkSorter *sorter;
|
2018-09-14 02:34:40 +00:00
|
|
|
|
2020-07-20 23:46:09 +00:00
|
|
|
GtkTimSort sort; /* ongoing sort operation */
|
|
|
|
guint sort_cb; /* 0 or current ongoing sort callback */
|
2020-07-16 23:56:18 +00:00
|
|
|
SortArray items; /* empty if known unsorted */
|
2018-09-14 02:34:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkSortListModelClass
|
|
|
|
{
|
|
|
|
GObjectClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
|
|
|
|
|
|
|
|
static GType
|
|
|
|
gtk_sort_list_model_get_item_type (GListModel *list)
|
|
|
|
{
|
2020-07-04 19:47:48 +00:00
|
|
|
return G_TYPE_OBJECT;
|
2018-09-14 02:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gtk_sort_list_model_get_n_items (GListModel *list)
|
|
|
|
{
|
|
|
|
GtkSortListModel *self = GTK_SORT_LIST_MODEL (list);
|
|
|
|
|
|
|
|
if (self->model == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return g_list_model_get_n_items (self->model);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gtk_sort_list_model_get_item (GListModel *list,
|
|
|
|
guint position)
|
|
|
|
{
|
|
|
|
GtkSortListModel *self = GTK_SORT_LIST_MODEL (list);
|
|
|
|
|
|
|
|
if (self->model == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2020-07-16 23:56:18 +00:00
|
|
|
if (sort_array_is_empty (&self->items))
|
2018-09-14 02:34:40 +00:00
|
|
|
return g_list_model_get_item (self->model, position);
|
|
|
|
|
2020-07-16 23:56:18 +00:00
|
|
|
if (position >= sort_array_get_size (&self->items))
|
2019-12-11 00:11:59 +00:00
|
|
|
return NULL;
|
2018-09-14 02:34:40 +00:00
|
|
|
|
2020-07-17 00:28:42 +00:00
|
|
|
return g_object_ref (sort_array_get (&self->items, position)->item);
|
2018-09-14 02:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_sort_list_model_model_init (GListModelInterface *iface)
|
|
|
|
{
|
|
|
|
iface->get_item_type = gtk_sort_list_model_get_item_type;
|
|
|
|
iface->get_n_items = gtk_sort_list_model_get_n_items;
|
|
|
|
iface->get_item = gtk_sort_list_model_get_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkSortListModel, gtk_sort_list_model, G_TYPE_OBJECT,
|
|
|
|
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, gtk_sort_list_model_model_init))
|
|
|
|
|
2020-07-20 23:46:09 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_sort_list_model_is_sorting (GtkSortListModel *self)
|
|
|
|
{
|
|
|
|
return self->sort_cb != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_sort_list_model_stop_sorting (GtkSortListModel *self)
|
|
|
|
{
|
|
|
|
if (self->sort_cb == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gtk_tim_sort_finish (&self->sort);
|
|
|
|
g_clear_handle_id (&self->sort_cb, g_source_remove);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_sort_list_model_sort_cb (gpointer data)
|
|
|
|
{
|
|
|
|
GtkSortListModel *self = data;
|
|
|
|
|
|
|
|
if (gtk_tim_sort_step (&self->sort))
|
|
|
|
{
|
|
|
|
guint n_items = sort_array_get_size (&self->items);
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), 0, n_items, n_items);
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_sort_list_model_stop_sorting (self);
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_sort_list_model_start_sorting (GtkSortListModel *self)
|
|
|
|
{
|
|
|
|
if (self->sort_cb != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
self->sort_cb = g_idle_add (gtk_sort_list_model_sort_cb, self);
|
|
|
|
}
|
|
|
|
|
2018-09-14 02:34:40 +00:00
|
|
|
static void
|
2020-07-16 23:56:18 +00:00
|
|
|
gtk_sort_list_model_clear_items (GtkSortListModel *self)
|
2018-09-14 02:34:40 +00:00
|
|
|
{
|
2020-07-20 23:46:09 +00:00
|
|
|
gtk_sort_list_model_stop_sorting (self);
|
2020-07-16 23:56:18 +00:00
|
|
|
sort_array_clear (&self->items);
|
|
|
|
}
|
2018-09-14 02:34:40 +00:00
|
|
|
|
2020-07-17 00:28:42 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_sort_list_model_should_sort (GtkSortListModel *self)
|
|
|
|
{
|
|
|
|
return self->sorter != NULL &&
|
|
|
|
self->model != NULL &&
|
|
|
|
gtk_sorter_get_order (self->sorter) != GTK_SORTER_ORDER_NONE;
|
|
|
|
}
|
|
|
|
|
2020-07-16 23:56:18 +00:00
|
|
|
static void
|
|
|
|
gtk_sort_list_model_create_items (GtkSortListModel *self)
|
|
|
|
{
|
|
|
|
guint i, n_items;
|
2018-09-14 02:34:40 +00:00
|
|
|
|
2020-07-17 00:28:42 +00:00
|
|
|
if (!gtk_sort_list_model_should_sort (self))
|
2020-07-16 23:56:18 +00:00
|
|
|
return;
|
2018-09-14 02:34:40 +00:00
|
|
|
|
2020-07-16 23:56:18 +00:00
|
|
|
n_items = g_list_model_get_n_items (self->model);
|
|
|
|
sort_array_reserve (&self->items, n_items);
|
|
|
|
for (i = 0; i < n_items; i++)
|
|
|
|
{
|
2020-07-17 00:28:42 +00:00
|
|
|
sort_array_append (&self->items, &(SortItem) { g_list_model_get_item (self->model, i), i });
|
2018-09-14 02:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 04:44:22 +00:00
|
|
|
static int
|
2020-07-16 23:56:18 +00:00
|
|
|
sort_func (gconstpointer a,
|
|
|
|
gconstpointer b,
|
|
|
|
gpointer data)
|
2019-12-03 04:44:22 +00:00
|
|
|
{
|
2020-07-17 00:28:42 +00:00
|
|
|
SortItem *sa = (SortItem *) a;
|
|
|
|
SortItem *sb = (SortItem *) b;
|
|
|
|
|
|
|
|
return gtk_sorter_compare (data, sa->item, sb->item);
|
2019-12-03 04:44:22 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 02:34:40 +00:00
|
|
|
static void
|
2020-07-11 04:02:58 +00:00
|
|
|
gtk_sort_list_model_resort (GtkSortListModel *self,
|
|
|
|
guint already_sorted)
|
2018-09-14 02:34:40 +00:00
|
|
|
{
|
2020-07-20 23:46:09 +00:00
|
|
|
if (gtk_sort_list_model_is_sorting (self))
|
|
|
|
{
|
|
|
|
already_sorted = 0;
|
|
|
|
gtk_sort_list_model_stop_sorting (self);
|
|
|
|
}
|
2020-07-11 04:02:58 +00:00
|
|
|
|
2020-07-20 23:46:09 +00:00
|
|
|
gtk_tim_sort_init (&self->sort,
|
2020-07-11 04:02:58 +00:00
|
|
|
sort_array_get_data (&self->items),
|
|
|
|
sort_array_get_size (&self->items),
|
|
|
|
sizeof (SortItem),
|
|
|
|
sort_func,
|
|
|
|
self->sorter);
|
2020-07-20 23:46:09 +00:00
|
|
|
gtk_tim_sort_set_runs (&self->sort, (gsize[2]) { already_sorted, 0 });
|
2020-07-11 04:02:58 +00:00
|
|
|
|
2020-07-20 23:46:09 +00:00
|
|
|
gtk_sort_list_model_start_sorting (self);
|
2018-09-14 02:34:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 00:28:42 +00:00
|
|
|
static void
|
|
|
|
gtk_sort_list_model_remove_items (GtkSortListModel *self,
|
|
|
|
guint position,
|
|
|
|
guint removed,
|
|
|
|
guint added,
|
|
|
|
guint *unmodified_start,
|
|
|
|
guint *unmodified_end)
|
|
|
|
{
|
|
|
|
guint i, n_items, valid;
|
|
|
|
guint start, end;
|
|
|
|
|
|
|
|
n_items = sort_array_get_size (&self->items);
|
|
|
|
start = n_items;
|
|
|
|
end = n_items;
|
|
|
|
|
|
|
|
valid = 0;
|
|
|
|
for (i = 0; i < n_items; i++)
|
|
|
|
{
|
|
|
|
SortItem *si = sort_array_index (&self->items, i);
|
|
|
|
|
|
|
|
if (si->position >= position + removed)
|
|
|
|
si->position = si->position - removed + added;
|
|
|
|
else if (si->position >= position)
|
|
|
|
{
|
|
|
|
start = MIN (start, valid);
|
|
|
|
end = n_items - i - 1;
|
|
|
|
sort_item_clear (si);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (valid < i)
|
|
|
|
*sort_array_index (&self->items, valid) = *sort_array_index (&self->items, i);
|
|
|
|
valid++;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (valid == n_items - removed);
|
|
|
|
memset (sort_array_index (&self->items, valid), 0, sizeof (SortItem) * removed);
|
|
|
|
sort_array_set_size (&self->items, valid);
|
|
|
|
|
|
|
|
*unmodified_start = start;
|
|
|
|
*unmodified_end = end;
|
|
|
|
}
|
|
|
|
|
2018-09-14 02:34:40 +00:00
|
|
|
static void
|
|
|
|
gtk_sort_list_model_items_changed_cb (GListModel *model,
|
|
|
|
guint position,
|
|
|
|
guint removed,
|
|
|
|
guint added,
|
|
|
|
GtkSortListModel *self)
|
|
|
|
{
|
2020-07-17 00:28:42 +00:00
|
|
|
guint i, n_items, start, end;
|
2020-07-20 23:46:09 +00:00
|
|
|
gboolean was_sorting;
|
2018-09-14 02:34:40 +00:00
|
|
|
|
2020-07-17 00:28:42 +00:00
|
|
|
if (removed == 0 && added == 0)
|
|
|
|
return;
|
2018-09-14 02:34:40 +00:00
|
|
|
|
2020-07-17 00:28:42 +00:00
|
|
|
if (!gtk_sort_list_model_should_sort (self))
|
|
|
|
{
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), position, removed, added);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-20 23:46:09 +00:00
|
|
|
was_sorting = gtk_sort_list_model_is_sorting (self);
|
|
|
|
gtk_sort_list_model_stop_sorting (self);
|
|
|
|
|
2020-07-17 00:28:42 +00:00
|
|
|
gtk_sort_list_model_remove_items (self, position, removed, added, &start, &end);
|
2020-07-20 23:46:09 +00:00
|
|
|
|
2020-07-17 00:28:42 +00:00
|
|
|
if (added > 0)
|
|
|
|
{
|
|
|
|
sort_array_reserve (&self->items, sort_array_get_size (&self->items) + added);
|
|
|
|
for (i = position; i < position + added; i++)
|
|
|
|
{
|
|
|
|
sort_array_append (&self->items, &(SortItem) { g_list_model_get_item (self->model, i), i });
|
|
|
|
}
|
|
|
|
|
2020-07-20 23:46:09 +00:00
|
|
|
gtk_sort_list_model_resort (self, was_sorting ? 0 : sort_array_get_size (&self->items) - added);
|
|
|
|
end = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (was_sorting)
|
|
|
|
gtk_sort_list_model_resort (self, 0);
|
2020-07-17 00:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n_items = sort_array_get_size (&self->items) - start - end;
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), start, n_items - added + removed, n_items);
|
2018-09-14 02:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_sort_list_model_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkSortListModel *self = GTK_SORT_LIST_MODEL (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_MODEL:
|
|
|
|
gtk_sort_list_model_set_model (self, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
2019-12-03 04:44:22 +00:00
|
|
|
case PROP_SORTER:
|
|
|
|
gtk_sort_list_model_set_sorter (self, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
2018-09-14 02:34:40 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_sort_list_model_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkSortListModel *self = GTK_SORT_LIST_MODEL (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_MODEL:
|
|
|
|
g_value_set_object (value, self->model);
|
|
|
|
break;
|
|
|
|
|
2019-12-03 04:44:22 +00:00
|
|
|
case PROP_SORTER:
|
|
|
|
g_value_set_object (value, self->sorter);
|
|
|
|
break;
|
|
|
|
|
2018-09-14 02:34:40 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 04:44:22 +00:00
|
|
|
static void
|
|
|
|
gtk_sort_list_model_sorter_changed_cb (GtkSorter *sorter,
|
|
|
|
int change,
|
|
|
|
GtkSortListModel *self)
|
|
|
|
{
|
2020-07-16 23:56:18 +00:00
|
|
|
guint n_items;
|
|
|
|
|
2020-06-21 04:53:44 +00:00
|
|
|
if (gtk_sorter_get_order (sorter) == GTK_SORTER_ORDER_NONE)
|
2020-07-16 23:56:18 +00:00
|
|
|
gtk_sort_list_model_clear_items (self);
|
|
|
|
else if (sort_array_is_empty (&self->items))
|
|
|
|
gtk_sort_list_model_create_items (self);
|
|
|
|
|
2020-07-11 04:02:58 +00:00
|
|
|
gtk_sort_list_model_resort (self, 0);
|
2020-07-16 23:56:18 +00:00
|
|
|
|
|
|
|
n_items = g_list_model_get_n_items (G_LIST_MODEL (self));
|
|
|
|
if (n_items > 1)
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), 0, n_items, n_items);
|
2019-12-03 04:44:22 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 02:34:40 +00:00
|
|
|
static void
|
|
|
|
gtk_sort_list_model_clear_model (GtkSortListModel *self)
|
|
|
|
{
|
|
|
|
if (self->model == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (self->model, gtk_sort_list_model_items_changed_cb, self);
|
|
|
|
g_clear_object (&self->model);
|
2020-07-16 23:56:18 +00:00
|
|
|
gtk_sort_list_model_clear_items (self);
|
2018-09-14 02:34:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 04:44:22 +00:00
|
|
|
static void
|
|
|
|
gtk_sort_list_model_clear_sorter (GtkSortListModel *self)
|
|
|
|
{
|
|
|
|
if (self->sorter == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (self->sorter, gtk_sort_list_model_sorter_changed_cb, self);
|
|
|
|
g_clear_object (&self->sorter);
|
2020-07-16 23:56:18 +00:00
|
|
|
gtk_sort_list_model_clear_items (self);
|
2019-12-03 04:44:22 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 02:34:40 +00:00
|
|
|
static void
|
|
|
|
gtk_sort_list_model_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkSortListModel *self = GTK_SORT_LIST_MODEL (object);
|
|
|
|
|
|
|
|
gtk_sort_list_model_clear_model (self);
|
2019-12-03 04:44:22 +00:00
|
|
|
gtk_sort_list_model_clear_sorter (self);
|
2018-09-14 02:34:40 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_sort_list_model_parent_class)->dispose (object);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_sort_list_model_class_init (GtkSortListModelClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
gobject_class->set_property = gtk_sort_list_model_set_property;
|
|
|
|
gobject_class->get_property = gtk_sort_list_model_get_property;
|
|
|
|
gobject_class->dispose = gtk_sort_list_model_dispose;
|
|
|
|
|
|
|
|
/**
|
2019-12-03 04:44:22 +00:00
|
|
|
* GtkSortListModel:sorter:
|
2018-09-14 02:34:40 +00:00
|
|
|
*
|
2019-12-03 04:44:22 +00:00
|
|
|
* The sorter for this model
|
2018-09-14 02:34:40 +00:00
|
|
|
*/
|
2019-12-03 04:44:22 +00:00
|
|
|
properties[PROP_SORTER] =
|
|
|
|
g_param_spec_object ("sorter",
|
|
|
|
P_("Sorter"),
|
|
|
|
P_("The sorter for this model"),
|
|
|
|
GTK_TYPE_SORTER,
|
|
|
|
GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
|
2018-09-14 02:34:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkSortListModel:model:
|
|
|
|
*
|
|
|
|
* The model being sorted
|
|
|
|
*/
|
|
|
|
properties[PROP_MODEL] =
|
|
|
|
g_param_spec_object ("model",
|
|
|
|
P_("Model"),
|
|
|
|
P_("The model being sorted"),
|
|
|
|
G_TYPE_LIST_MODEL,
|
2020-07-07 21:18:46 +00:00
|
|
|
GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
|
2018-09-14 02:34:40 +00:00
|
|
|
|
|
|
|
g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_sort_list_model_init (GtkSortListModel *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_sort_list_model_new:
|
2020-07-04 19:47:48 +00:00
|
|
|
* @model: (allow-none): the model to sort
|
2019-12-03 04:44:22 +00:00
|
|
|
* @sorter: (allow-none): the #GtkSorter to sort @model with
|
2018-09-14 02:34:40 +00:00
|
|
|
*
|
2019-12-03 04:44:22 +00:00
|
|
|
* Creates a new sort list model that uses the @sorter to sort @model.
|
2018-09-14 02:34:40 +00:00
|
|
|
*
|
|
|
|
* Returns: a new #GtkSortListModel
|
|
|
|
**/
|
|
|
|
GtkSortListModel *
|
2019-12-03 04:44:22 +00:00
|
|
|
gtk_sort_list_model_new (GListModel *model,
|
|
|
|
GtkSorter *sorter)
|
2018-09-14 02:34:40 +00:00
|
|
|
{
|
|
|
|
GtkSortListModel *result;
|
|
|
|
|
2020-07-04 19:47:48 +00:00
|
|
|
g_return_val_if_fail (model == NULL || G_IS_LIST_MODEL (model), NULL);
|
2019-12-03 04:44:22 +00:00
|
|
|
g_return_val_if_fail (sorter == NULL || GTK_IS_SORTER (sorter), NULL);
|
2018-09-14 02:34:40 +00:00
|
|
|
|
|
|
|
result = g_object_new (GTK_TYPE_SORT_LIST_MODEL,
|
|
|
|
"model", model,
|
2019-12-03 04:44:22 +00:00
|
|
|
"sorter", sorter,
|
2018-09-14 02:34:40 +00:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_sort_list_model_set_model:
|
|
|
|
* @self: a #GtkSortListModel
|
|
|
|
* @model: (allow-none): The model to be sorted
|
|
|
|
*
|
|
|
|
* Sets the model to be sorted. The @model's item type must conform to
|
|
|
|
* the item type of @self.
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
gtk_sort_list_model_set_model (GtkSortListModel *self,
|
|
|
|
GListModel *model)
|
|
|
|
{
|
|
|
|
guint removed, added;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_SORT_LIST_MODEL (self));
|
|
|
|
g_return_if_fail (model == NULL || G_IS_LIST_MODEL (model));
|
|
|
|
|
|
|
|
if (self->model == model)
|
|
|
|
return;
|
|
|
|
|
|
|
|
removed = g_list_model_get_n_items (G_LIST_MODEL (self));
|
|
|
|
gtk_sort_list_model_clear_model (self);
|
|
|
|
|
|
|
|
if (model)
|
|
|
|
{
|
|
|
|
self->model = g_object_ref (model);
|
|
|
|
g_signal_connect (model, "items-changed", G_CALLBACK (gtk_sort_list_model_items_changed_cb), self);
|
|
|
|
added = g_list_model_get_n_items (model);
|
|
|
|
|
2020-07-16 23:56:18 +00:00
|
|
|
gtk_sort_list_model_create_items (self);
|
2020-07-11 04:02:58 +00:00
|
|
|
gtk_sort_list_model_resort (self, 0);
|
2018-09-14 02:34:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
added = 0;
|
|
|
|
|
|
|
|
if (removed > 0 || added > 0)
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), 0, removed, added);
|
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MODEL]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_sort_list_model_get_model:
|
|
|
|
* @self: a #GtkSortListModel
|
|
|
|
*
|
|
|
|
* Gets the model currently sorted or %NULL if none.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): The model that gets sorted
|
|
|
|
**/
|
|
|
|
GListModel *
|
|
|
|
gtk_sort_list_model_get_model (GtkSortListModel *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_SORT_LIST_MODEL (self), NULL);
|
|
|
|
|
|
|
|
return self->model;
|
|
|
|
}
|
|
|
|
|
2019-12-03 04:44:22 +00:00
|
|
|
/**
|
|
|
|
* gtk_sort_list_model_set_sorter:
|
|
|
|
* @self: a #GtkSortListModel
|
|
|
|
* @sorter: (allow-none): the #GtkSorter to sort @model with
|
|
|
|
*
|
|
|
|
* Sets a new sorter on @self.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_sort_list_model_set_sorter (GtkSortListModel *self,
|
|
|
|
GtkSorter *sorter)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_SORT_LIST_MODEL (self));
|
|
|
|
g_return_if_fail (sorter == NULL || GTK_IS_SORTER (sorter));
|
|
|
|
|
|
|
|
gtk_sort_list_model_clear_sorter (self);
|
|
|
|
|
|
|
|
if (sorter)
|
|
|
|
{
|
|
|
|
self->sorter = g_object_ref (sorter);
|
|
|
|
g_signal_connect (sorter, "changed", G_CALLBACK (gtk_sort_list_model_sorter_changed_cb), self);
|
2020-07-16 23:56:18 +00:00
|
|
|
gtk_sort_list_model_sorter_changed_cb (sorter, GTK_SORTER_CHANGE_DIFFERENT, self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
guint n_items = g_list_model_get_n_items (G_LIST_MODEL (self));
|
|
|
|
if (n_items > 1)
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), 0, n_items, n_items);
|
2019-12-03 04:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SORTER]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_sort_list_model_get_sorter:
|
2020-07-16 23:56:18 +00:00
|
|
|
* @self: a #GtkSortListModel
|
2019-12-03 04:44:22 +00:00
|
|
|
*
|
|
|
|
* Gets the sorter that is used to sort @self.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): the sorter of #self
|
|
|
|
*/
|
|
|
|
GtkSorter *
|
|
|
|
gtk_sort_list_model_get_sorter (GtkSortListModel *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_SORT_LIST_MODEL (self), NULL);
|
|
|
|
|
|
|
|
return self->sorter;
|
|
|
|
}
|