2018-09-02 22:48:28 +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 "gtkflattenlistmodel.h"
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
#include "gtkrbtreeprivate.h"
|
2018-09-02 22:48:28 +00:00
|
|
|
#include "gtkprivate.h"
|
|
|
|
|
|
|
|
/**
|
2021-03-01 06:37:37 +00:00
|
|
|
* GtkFlattenListModel:
|
2018-09-02 22:48:28 +00:00
|
|
|
*
|
2021-03-01 06:37:37 +00:00
|
|
|
* `GtkFlattenListModel` is a list model that concatenates other list models.
|
2018-09-02 22:48:28 +00:00
|
|
|
*
|
2021-03-01 06:37:37 +00:00
|
|
|
* `GtkFlattenListModel` takes a list model containing list models,
|
|
|
|
* and flattens it into a single model.
|
2018-09-02 22:48:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
2022-06-11 04:43:25 +00:00
|
|
|
PROP_ITEM_TYPE,
|
2018-09-02 22:48:28 +00:00
|
|
|
PROP_MODEL,
|
2022-06-11 04:43:25 +00:00
|
|
|
PROP_N_ITEMS,
|
|
|
|
|
2018-09-02 22:48:28 +00:00
|
|
|
NUM_PROPERTIES
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _FlattenNode FlattenNode;
|
|
|
|
typedef struct _FlattenAugment FlattenAugment;
|
|
|
|
|
|
|
|
struct _FlattenNode
|
|
|
|
{
|
|
|
|
GListModel *model;
|
|
|
|
GtkFlattenListModel *list;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _FlattenAugment
|
|
|
|
{
|
|
|
|
guint n_items;
|
|
|
|
guint n_models;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkFlattenListModel
|
|
|
|
{
|
|
|
|
GObject parent_instance;
|
|
|
|
|
|
|
|
GListModel *model;
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbTree *items; /* NULL if model == NULL */
|
2018-09-02 22:48:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkFlattenListModelClass
|
|
|
|
{
|
|
|
|
GObjectClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
|
|
|
|
|
|
|
|
static FlattenNode *
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_flatten_list_model_get_nth (GtkRbTree *tree,
|
2022-02-26 03:36:24 +00:00
|
|
|
guint position,
|
|
|
|
guint *model_position)
|
2018-09-02 22:48:28 +00:00
|
|
|
{
|
|
|
|
FlattenNode *node, *tmp;
|
|
|
|
guint model_n_items;
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
node = gtk_rb_tree_get_root (tree);
|
2018-09-02 22:48:28 +00:00
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
2019-01-14 01:12:02 +00:00
|
|
|
tmp = gtk_rb_tree_node_get_left (node);
|
2018-09-02 22:48:28 +00:00
|
|
|
if (tmp)
|
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
FlattenAugment *aug = gtk_rb_tree_get_augment (tree, tmp);
|
2018-09-02 22:48:28 +00:00
|
|
|
if (position < aug->n_items)
|
|
|
|
{
|
|
|
|
node = tmp;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
position -= aug->n_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
model_n_items = g_list_model_get_n_items (node->model);
|
|
|
|
if (position < model_n_items)
|
|
|
|
break;
|
|
|
|
position -= model_n_items;
|
|
|
|
|
2019-01-14 01:12:02 +00:00
|
|
|
node = gtk_rb_tree_node_get_right (node);
|
2018-09-02 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (model_position)
|
|
|
|
*model_position = node ? position : 0;
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FlattenNode *
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_flatten_list_model_get_nth_model (GtkRbTree *tree,
|
2022-02-26 03:36:24 +00:00
|
|
|
guint position,
|
|
|
|
guint *items_before)
|
2018-09-02 22:48:28 +00:00
|
|
|
{
|
|
|
|
FlattenNode *node, *tmp;
|
|
|
|
guint before;
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
node = gtk_rb_tree_get_root (tree);
|
2018-09-02 22:48:28 +00:00
|
|
|
before = 0;
|
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
2019-01-14 01:12:02 +00:00
|
|
|
tmp = gtk_rb_tree_node_get_left (node);
|
2018-09-02 22:48:28 +00:00
|
|
|
if (tmp)
|
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
FlattenAugment *aug = gtk_rb_tree_get_augment (tree, tmp);
|
2018-09-02 22:48:28 +00:00
|
|
|
if (position < aug->n_models)
|
|
|
|
{
|
|
|
|
node = tmp;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
position -= aug->n_models;
|
|
|
|
before += aug->n_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position == 0)
|
|
|
|
break;
|
|
|
|
position--;
|
|
|
|
before += g_list_model_get_n_items (node->model);
|
|
|
|
|
2019-01-14 01:12:02 +00:00
|
|
|
node = gtk_rb_tree_node_get_right (node);
|
2018-09-02 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (items_before)
|
|
|
|
*items_before = before;
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GType
|
|
|
|
gtk_flatten_list_model_get_item_type (GListModel *list)
|
|
|
|
{
|
2020-07-04 19:47:48 +00:00
|
|
|
return G_TYPE_OBJECT;
|
2018-09-02 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gtk_flatten_list_model_get_n_items (GListModel *list)
|
|
|
|
{
|
|
|
|
GtkFlattenListModel *self = GTK_FLATTEN_LIST_MODEL (list);
|
|
|
|
FlattenAugment *aug;
|
|
|
|
FlattenNode *node;
|
|
|
|
|
|
|
|
if (!self->items)
|
|
|
|
return 0;
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
node = gtk_rb_tree_get_root (self->items);
|
2018-09-02 22:48:28 +00:00
|
|
|
if (node == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
aug = gtk_rb_tree_get_augment (self->items, node);
|
2018-09-02 22:48:28 +00:00
|
|
|
return aug->n_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gtk_flatten_list_model_get_item (GListModel *list,
|
|
|
|
guint position)
|
|
|
|
{
|
|
|
|
GtkFlattenListModel *self = GTK_FLATTEN_LIST_MODEL (list);
|
|
|
|
FlattenNode *node;
|
|
|
|
guint model_pos;
|
|
|
|
|
|
|
|
if (!self->items)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
node = gtk_flatten_list_model_get_nth (self->items, position, &model_pos);
|
|
|
|
if (node == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return g_list_model_get_item (node->model, model_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_flatten_list_model_model_init (GListModelInterface *iface)
|
|
|
|
{
|
|
|
|
iface->get_item_type = gtk_flatten_list_model_get_item_type;
|
|
|
|
iface->get_n_items = gtk_flatten_list_model_get_n_items;
|
|
|
|
iface->get_item = gtk_flatten_list_model_get_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
static void
|
2022-02-26 03:36:24 +00:00
|
|
|
gtk_flatten_list_model_items_changed_cb (GListModel *model,
|
|
|
|
guint position,
|
|
|
|
guint removed,
|
|
|
|
guint added,
|
|
|
|
gpointer _node)
|
2018-09-02 22:48:28 +00:00
|
|
|
{
|
2019-07-01 23:57:20 +00:00
|
|
|
FlattenNode *node = _node, *parent, *left;
|
2018-09-02 22:48:28 +00:00
|
|
|
GtkFlattenListModel *self = node->list;
|
|
|
|
guint real_position;
|
|
|
|
|
2019-01-14 01:12:02 +00:00
|
|
|
gtk_rb_tree_node_mark_dirty (node);
|
2019-07-01 23:57:20 +00:00
|
|
|
real_position = position;
|
2018-09-02 22:48:28 +00:00
|
|
|
|
2019-07-01 23:57:20 +00:00
|
|
|
left = gtk_rb_tree_node_get_left (node);
|
|
|
|
if (left)
|
|
|
|
{
|
|
|
|
FlattenAugment *aug = gtk_rb_tree_get_augment (self->items, left);
|
|
|
|
real_position += aug->n_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;
|
2019-01-14 01:12:02 +00:00
|
|
|
(parent = gtk_rb_tree_node_get_parent (node)) != NULL;
|
2018-09-02 22:48:28 +00:00
|
|
|
node = parent)
|
|
|
|
{
|
2019-07-01 23:57:20 +00:00
|
|
|
left = gtk_rb_tree_node_get_left (parent);
|
2018-09-02 22:48:28 +00:00
|
|
|
if (left != node)
|
|
|
|
{
|
|
|
|
if (left)
|
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
FlattenAugment *aug = gtk_rb_tree_get_augment (self->items, left);
|
2018-09-02 22:48:28 +00:00
|
|
|
real_position += aug->n_items;
|
|
|
|
}
|
|
|
|
real_position += g_list_model_get_n_items (parent->model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), real_position, removed, added);
|
2022-06-11 04:43:25 +00:00
|
|
|
if (removed != added)
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
|
2018-09-02 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_flatten_list_model_clear_node (gpointer _node)
|
|
|
|
{
|
|
|
|
FlattenNode *node= _node;
|
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (node->model, gtk_flatten_list_model_items_changed_cb, node);
|
|
|
|
g_object_unref (node->model);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_flatten_list_model_augment (GtkRbTree *flatten,
|
|
|
|
gpointer _aug,
|
|
|
|
gpointer _node,
|
|
|
|
gpointer left,
|
|
|
|
gpointer right)
|
2018-09-02 22:48:28 +00:00
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
FlattenNode *node = _node;
|
2018-09-02 22:48:28 +00:00
|
|
|
FlattenAugment *aug = _aug;
|
|
|
|
|
|
|
|
aug->n_items = g_list_model_get_n_items (node->model);
|
|
|
|
aug->n_models = 1;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
FlattenAugment *left_aug = gtk_rb_tree_get_augment (flatten, left);
|
2018-09-02 22:48:28 +00:00
|
|
|
aug->n_items += left_aug->n_items;
|
|
|
|
aug->n_models += left_aug->n_models;
|
|
|
|
}
|
|
|
|
if (right)
|
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
FlattenAugment *right_aug = gtk_rb_tree_get_augment (flatten, right);
|
2018-09-02 22:48:28 +00:00
|
|
|
aug->n_items += right_aug->n_items;
|
|
|
|
aug->n_models += right_aug->n_models;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gtk_flatten_list_model_add_items (GtkFlattenListModel *self,
|
|
|
|
FlattenNode *after,
|
|
|
|
guint position,
|
|
|
|
guint n)
|
|
|
|
{
|
|
|
|
FlattenNode *node;
|
|
|
|
guint added, i;
|
|
|
|
|
|
|
|
added = 0;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
node = gtk_rb_tree_insert_before (self->items, after);
|
2018-09-02 22:48:28 +00:00
|
|
|
node->model = g_list_model_get_item (self->model, position + i);
|
|
|
|
g_signal_connect (node->model,
|
|
|
|
"items-changed",
|
|
|
|
G_CALLBACK (gtk_flatten_list_model_items_changed_cb),
|
|
|
|
node);
|
|
|
|
node->list = self;
|
2020-07-16 12:47:59 +00:00
|
|
|
added += g_list_model_get_n_items (node->model);
|
2018-09-02 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_flatten_list_model_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkFlattenListModel *self = GTK_FLATTEN_LIST_MODEL (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_MODEL:
|
|
|
|
gtk_flatten_list_model_set_model (self, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-26 03:36:24 +00:00
|
|
|
gtk_flatten_list_model_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2018-09-02 22:48:28 +00:00
|
|
|
{
|
|
|
|
GtkFlattenListModel *self = GTK_FLATTEN_LIST_MODEL (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
2022-06-11 04:43:25 +00:00
|
|
|
case PROP_ITEM_TYPE:
|
|
|
|
g_value_set_gtype (value, gtk_flatten_list_model_get_item_type (G_LIST_MODEL (self)));
|
|
|
|
break;
|
|
|
|
|
2018-09-02 22:48:28 +00:00
|
|
|
case PROP_MODEL:
|
|
|
|
g_value_set_object (value, self->model);
|
|
|
|
break;
|
|
|
|
|
2022-06-11 04:43:25 +00:00
|
|
|
case PROP_N_ITEMS:
|
|
|
|
g_value_set_uint (value, gtk_flatten_list_model_get_n_items (G_LIST_MODEL (self)));
|
|
|
|
break;
|
|
|
|
|
2018-09-02 22:48:28 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_flatten_list_model_model_items_changed_cb (GListModel *model,
|
|
|
|
guint position,
|
|
|
|
guint removed,
|
|
|
|
guint added,
|
|
|
|
GtkFlattenListModel *self)
|
|
|
|
{
|
|
|
|
FlattenNode *node;
|
|
|
|
guint i, real_position, real_removed, real_added;
|
|
|
|
|
|
|
|
node = gtk_flatten_list_model_get_nth_model (self->items, position, &real_position);
|
|
|
|
|
|
|
|
real_removed = 0;
|
|
|
|
for (i = 0; i < removed; i++)
|
|
|
|
{
|
2019-01-14 01:12:02 +00:00
|
|
|
FlattenNode *next = gtk_rb_tree_node_get_next (node);
|
2018-09-02 22:48:28 +00:00
|
|
|
real_removed += g_list_model_get_n_items (node->model);
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_remove (self->items, node);
|
2018-09-02 22:48:28 +00:00
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
real_added = gtk_flatten_list_model_add_items (self, node, position, added);
|
|
|
|
|
|
|
|
if (real_removed > 0 || real_added > 0)
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), real_position, real_removed, real_added);
|
2022-06-11 04:43:25 +00:00
|
|
|
if (real_removed != real_added)
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
|
2018-09-02 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_flatten_list_clear_model (GtkFlattenListModel *self)
|
|
|
|
{
|
|
|
|
if (self->model)
|
|
|
|
{
|
|
|
|
g_signal_handlers_disconnect_by_func (self->model, gtk_flatten_list_model_model_items_changed_cb, self);
|
|
|
|
g_clear_object (&self->model);
|
2019-01-07 04:37:32 +00:00
|
|
|
g_clear_pointer (&self->items, gtk_rb_tree_unref);
|
2018-09-02 22:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_flatten_list_model_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkFlattenListModel *self = GTK_FLATTEN_LIST_MODEL (object);
|
|
|
|
|
|
|
|
gtk_flatten_list_clear_model (self);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_flatten_list_model_parent_class)->dispose (object);
|
2019-01-07 04:37:32 +00:00
|
|
|
}
|
2018-09-02 22:48:28 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_flatten_list_model_class_init (GtkFlattenListModelClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
gobject_class->set_property = gtk_flatten_list_model_set_property;
|
|
|
|
gobject_class->get_property = gtk_flatten_list_model_get_property;
|
|
|
|
gobject_class->dispose = gtk_flatten_list_model_dispose;
|
|
|
|
|
2022-06-11 04:43:25 +00:00
|
|
|
/**
|
|
|
|
* GtkFlattenListModel:item-type:
|
|
|
|
*
|
|
|
|
* The type of items. See [method@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);
|
|
|
|
|
2018-09-02 22:48:28 +00:00
|
|
|
/**
|
2021-03-01 06:37:37 +00:00
|
|
|
* GtkFlattenListModel:model: (attributes org.gtk.Property.get=gtk_flatten_list_model_get_model org.gtk.Property.set=gtk_flatten_list_model_set_model)
|
2018-09-02 22:48:28 +00:00
|
|
|
*
|
2021-03-01 06:37:37 +00:00
|
|
|
* The model being flattened.
|
2018-09-02 22:48:28 +00:00
|
|
|
*/
|
|
|
|
properties[PROP_MODEL] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_object ("model", NULL, NULL,
|
2018-09-02 22:48:28 +00:00
|
|
|
G_TYPE_LIST_MODEL,
|
|
|
|
GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
|
|
|
|
|
2022-06-11 04:43:25 +00:00
|
|
|
/**
|
|
|
|
* GtkFlattenListModel:n-items:
|
|
|
|
*
|
|
|
|
* The number of items. See [method@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);
|
|
|
|
|
2018-09-02 22:48:28 +00:00
|
|
|
g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_flatten_list_model_init (GtkFlattenListModel *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_flatten_list_model_new:
|
2020-07-26 14:53:55 +00:00
|
|
|
* @model: (nullable) (transfer full): the model to be flattened
|
2018-09-02 22:48:28 +00:00
|
|
|
*
|
2021-03-01 06:37:37 +00:00
|
|
|
* Creates a new `GtkFlattenListModel` that flattens @list.
|
2018-09-02 22:48:28 +00:00
|
|
|
*
|
2021-03-01 06:37:37 +00:00
|
|
|
* Returns: a new `GtkFlattenListModel`
|
|
|
|
*/
|
2018-09-02 22:48:28 +00:00
|
|
|
GtkFlattenListModel *
|
2020-07-04 19:47:48 +00:00
|
|
|
gtk_flatten_list_model_new (GListModel *model)
|
2018-09-02 22:48:28 +00:00
|
|
|
{
|
|
|
|
GtkFlattenListModel *result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (model == NULL || G_IS_LIST_MODEL (model), NULL);
|
|
|
|
|
|
|
|
result = g_object_new (GTK_TYPE_FLATTEN_LIST_MODEL,
|
|
|
|
"model", model,
|
|
|
|
NULL);
|
|
|
|
|
2020-07-26 14:53:55 +00:00
|
|
|
/* we consume the reference */
|
|
|
|
g_clear_object (&model);
|
|
|
|
|
2018-09-02 22:48:28 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-01 06:37:37 +00:00
|
|
|
* gtk_flatten_list_model_set_model: (attributes org.gtk.Method.set_property=model)
|
|
|
|
* @self: a `GtkFlattenListModel`
|
2021-05-21 00:45:06 +00:00
|
|
|
* @model: (nullable) (transfer none): the new model
|
2018-09-02 22:48:28 +00:00
|
|
|
*
|
2020-07-04 19:47:48 +00:00
|
|
|
* Sets a new model to be flattened.
|
2021-03-01 06:37:37 +00:00
|
|
|
*/
|
2018-09-02 22:48:28 +00:00
|
|
|
void
|
|
|
|
gtk_flatten_list_model_set_model (GtkFlattenListModel *self,
|
|
|
|
GListModel *model)
|
|
|
|
{
|
2018-09-20 03:45:33 +00:00
|
|
|
guint removed, added = 0;
|
2018-09-02 22:48:28 +00:00
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_FLATTEN_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_flatten_list_clear_model (self);
|
|
|
|
|
|
|
|
self->model = model;
|
|
|
|
|
|
|
|
if (model)
|
|
|
|
{
|
|
|
|
g_object_ref (model);
|
|
|
|
g_signal_connect (model, "items-changed", G_CALLBACK (gtk_flatten_list_model_model_items_changed_cb), self);
|
2019-01-07 04:37:32 +00:00
|
|
|
self->items = gtk_rb_tree_new (FlattenNode,
|
|
|
|
FlattenAugment,
|
|
|
|
gtk_flatten_list_model_augment,
|
|
|
|
gtk_flatten_list_model_clear_node,
|
|
|
|
NULL);
|
2018-09-02 22:48:28 +00:00
|
|
|
|
|
|
|
added = gtk_flatten_list_model_add_items (self, NULL, 0, g_list_model_get_n_items (model));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removed > 0 || added > 0)
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), 0, removed, added);
|
2022-06-11 04:43:25 +00:00
|
|
|
if (removed != added)
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
|
2018-09-02 22:48:28 +00:00
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MODEL]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-01 06:37:37 +00:00
|
|
|
* gtk_flatten_list_model_get_model: (attributes org.gtk.Method.get_property=model)
|
|
|
|
* @self: a `GtkFlattenListModel`
|
2018-09-02 22:48:28 +00:00
|
|
|
*
|
|
|
|
* Gets the model set via gtk_flatten_list_model_set_model().
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): The model flattened by @self
|
|
|
|
**/
|
|
|
|
GListModel *
|
|
|
|
gtk_flatten_list_model_get_model (GtkFlattenListModel *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_FLATTEN_LIST_MODEL (self), NULL);
|
|
|
|
|
|
|
|
return self->model;
|
|
|
|
}
|
2020-03-22 17:19:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_flatten_list_model_get_model_for_item:
|
2021-03-01 06:37:37 +00:00
|
|
|
* @self: a `GtkFlattenListModel`
|
2020-03-22 17:19:55 +00:00
|
|
|
* @position: a position
|
|
|
|
*
|
|
|
|
* Returns the model containing the item at the given position.
|
|
|
|
*
|
2022-01-01 18:07:13 +00:00
|
|
|
* Returns: (transfer none) (nullable): the model containing the item at @position
|
2020-03-22 17:19:55 +00:00
|
|
|
*/
|
|
|
|
GListModel *
|
|
|
|
gtk_flatten_list_model_get_model_for_item (GtkFlattenListModel *self,
|
|
|
|
guint position)
|
|
|
|
{
|
|
|
|
FlattenNode *node;
|
|
|
|
|
|
|
|
if (!self->items)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
node = gtk_flatten_list_model_get_nth (self->items, position, NULL);
|
|
|
|
if (node == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return node->model;
|
|
|
|
}
|