class Gtk::TreeModelSort

Overview

A Gtk::TreeModel which makes an underlying tree model sortable

The Gtk::TreeModelSort is a model which implements the Gtk::TreeSortable interface. It does not hold any data itself, but rather is created with a child model and proxies its data. It has identical column types to this child model, and the changes in the child are propagated. The primary purpose of this model is to provide a way to sort a different model without modifying it. Note that the sort function used by Gtk::TreeModelSort is not guaranteed to be stable.

The use of this is best demonstrated through an example. In the following sample code we create two Gtk::TreeView widgets each with a view of the same data. As the model is wrapped here by a Gtk::TreeModelSort, the two Gtk::TreeViews can each sort their view of the data without affecting the other. By contrast, if we simply put the same model in each widget, then sorting the first would sort the second.

Using a Gtk::TreeModelSort

|[ { Gtk::TreeView *tree_view1; Gtk::TreeView *tree_view2; Gtk::TreeModel *sort_model1; Gtk::TreeModel *sort_model2; Gtk::TreeModel *child_model;

// get the child model child_model = get_my_model ();

// Create the first tree sort_model1 = gtk_tree_model_sort_new_with_model (child_model); tree_view1 = gtk_tree_view_new_with_model (sort_model1);

// Create the second tree sort_model2 = gtk_tree_model_sort_new_with_model (child_model); tree_view2 = gtk_tree_view_new_with_model (sort_model2);

// Now we can sort the two models independently gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort_model1), COLUMN_1, GTK_SORT_ASCENDING); gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort_model2), COLUMN_1, GTK_SORT_DESCENDING); } ]|

To demonstrate how to access the underlying child model from the sort model, the next example will be a callback for the Gtk::TreeSelection Gtk::TreeSelection::changed signal. In this callback, we get a string from COLUMN_1 of the model. We then modify the string, find the same selected row on the child model, and change the row there.

Accessing the child model of in a selection changed callback

|[ void selection_changed (Gtk::TreeSelection *selection, gpointer data) { Gtk::TreeModel *sort_model = NULL; Gtk::TreeModel *child_model; Gtk::TreeIter sort_iter; Gtk::TreeIter child_iter; char *some_data = NULL; char *modified_data;

// Get the current selected row and the model. if (! gtk_tree_selection_get_selected (selection, &sort_model, &sort_iter)) return;

// Look up the current value on the selected row and get // a new value to change it to. gtk_tree_model_get (GTK_TREE_MODEL (sort_model), &sort_iter, COLUMN_1, &some_data, -1);

modified_data = change_the_data (some_data); g_free (some_data);

// Get an iterator on the child model, instead of the sort model. gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (sort_model), &child_iter, &sort_iter);

// Get the child model and change the value of the row. In this // example, the child model is a Gtk::ListStore. It could be any other // type of model, though. child_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model)); gtk_list_store_set (GTK_LIST_STORE (child_model), &child_iter, COLUMN_1, &modified_data, -1); g_free (modified_data); } ]|

Included Modules

Defined in:

lib/gi-crystal/src/auto/gtk-4.0/tree_model_sort.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Gtk::TreeSortable

default_sort_func=(sort_func : Gtk::TreeIterCompareFunc) : Nil default_sort_func=, has_default_sort_func : Bool has_default_sort_func, set_sort_column_id(sort_column_id : Int32, order : Gtk::SortType) : Nil set_sort_column_id, set_sort_func(sort_column_id : Int32, sort_func : Gtk::TreeIterCompareFunc) : Nil set_sort_func, sort_column_changed : Nil sort_column_changed, sort_column_changed_signal sort_column_changed_signal, sort_column_id(sort_column_id : Int32, order : Gtk::SortType) : Bool sort_column_id, to_unsafe to_unsafe

Constructor methods inherited from module Gtk::TreeSortable

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gtk::TreeSortable

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from module Gtk::TreeModel

column_type(index_ : Int32) : UInt64 column_type, filter_new(root : Gtk::TreePath | Nil) : Gtk::TreeModel filter_new, flags : Gtk::TreeModelFlags flags, foreach(func : Gtk::TreeModelForeachFunc, user_data : Pointer(Void) | Nil) : Nil foreach, iter(path : Gtk::TreePath) : Gtk::TreeIter iter, iter_children(parent : Gtk::TreeIter | Nil) : Gtk::TreeIter iter_children, iter_first : Gtk::TreeIter iter_first, iter_from_string(path_string : String) : Gtk::TreeIter iter_from_string, iter_has_child(iter : Gtk::TreeIter) : Bool iter_has_child, iter_n_children(iter : Gtk::TreeIter | Nil) : Int32 iter_n_children, iter_next(iter : Gtk::TreeIter) : Bool iter_next, iter_nth_child(parent : Gtk::TreeIter | Nil, n : Int32) : Gtk::TreeIter iter_nth_child, iter_parent(child : Gtk::TreeIter) : Gtk::TreeIter iter_parent, iter_previous(iter : Gtk::TreeIter) : Bool iter_previous, n_columns : Int32 n_columns, path(iter : Gtk::TreeIter) : Gtk::TreePath path, ref_node(iter : Gtk::TreeIter) : Nil ref_node, row_changed(path : Gtk::TreePath, iter : Gtk::TreeIter) : Nil row_changed, row_changed_signal row_changed_signal, row_deleted(path : Gtk::TreePath) : Nil row_deleted, row_deleted_signal row_deleted_signal, row_has_child_toggled(path : Gtk::TreePath, iter : Gtk::TreeIter) : Nil row_has_child_toggled, row_has_child_toggled_signal row_has_child_toggled_signal, row_inserted(path : Gtk::TreePath, iter : Gtk::TreeIter) : Nil row_inserted, row_inserted_signal row_inserted_signal, rows_reordered(path : Gtk::TreePath, iter : Gtk::TreeIter | Nil, new_order : Enumerable(Int32)) : Nil rows_reordered, string_from_iter(iter : Gtk::TreeIter) : String | Nil string_from_iter, to_unsafe to_unsafe, unref_node(iter : Gtk::TreeIter) : Nil unref_node, value(iter : Gtk::TreeIter, column : Int32) : GObject::Value value

Constructor methods inherited from module Gtk::TreeModel

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gtk::TreeModel

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from module Gtk::TreeDragSource

drag_data_delete(path : Gtk::TreePath) : Bool drag_data_delete, drag_data_get(path : Gtk::TreePath) : Gdk::ContentProvider | Nil drag_data_get, row_draggable(path : Gtk::TreePath) : Bool row_draggable, to_unsafe to_unsafe

Constructor methods inherited from module Gtk::TreeDragSource

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gtk::TreeDragSource

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def self.new #

Initialize a new TreeModelSort.


[View source]
def self.new(*, model : Gtk::TreeModel | Nil = nil) #

[View source]
def self.new_with_model(child_model : Gtk::TreeModel) : self #

Creates a new Gtk::TreeModelSort, with child_model as the child model.


[View source]

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def clear_cache : Nil #

This function should almost never be called. It clears the tree_model_sort of any cached iterators that haven’t been reffed with gtk_tree_model_ref_node(). This might be useful if the child model being sorted is static (and doesn’t change often) and there has been a lot of unreffed access to nodes. As a side effect of this function, all unreffed iters will be invalid.


[View source]
def convert_child_iter_to_iter(child_iter : Gtk::TreeIter) : Gtk::TreeIter #

Sets sort_iter to point to the row in tree_model_sort that corresponds to the row pointed at by child_iter. If sort_iter was not set, false is returned. Note: a boolean is only returned since 2.14.


[View source]
def convert_child_path_to_path(child_path : Gtk::TreePath) : Gtk::TreePath | Nil #

Converts child_path to a path relative to tree_model_sort. That is, child_path points to a path in the child model. The returned path will point to the same row in the sorted model. If child_path isn’t a valid path on the child model, then nil is returned.


[View source]
def convert_iter_to_child_iter(sorted_iter : Gtk::TreeIter) : Gtk::TreeIter #

Sets child_iter to point to the row pointed to by sorted_iter.


[View source]
def convert_path_to_child_path(sorted_path : Gtk::TreePath) : Gtk::TreePath | Nil #

Converts sorted_path to a path on the child model of tree_model_sort. That is, sorted_path points to a location in tree_model_sort. The returned path will point to the same location in the model not being sorted. If sorted_path does not point to a location in the child model, nil is returned.


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def iter_is_valid(iter : Gtk::TreeIter) : Bool #

This function is slow. Only use it for debugging and/or testing purposes.

Checks if the given iter is a valid iter for this Gtk::TreeModelSort.


[View source]
def model : Gtk::TreeModel #

Returns the model the Gtk::TreeModelSort is sorting.


[View source]
def model=(value : Gtk::TreeModel | Nil) : Gtk::TreeModel | Nil #

[View source]
def reset_default_sort_func : Nil #

This resets the default sort function to be in the “unsorted” state. That is, it is in the same order as the child model. It will re-sort the model to be in the same order as the child model only if the Gtk::TreeModelSort is in “unsorted” state.


[View source]