class Gtk::SortListModel

Overview

A GListModel that sorts the elements of an underlying model according to a Gtk::Sorter.

The model is a stable sort. If two items compare equal according to the sorter, the one that appears first in the original model will also appear first after sorting. Note that if you change the sorter, the previous order will have no influence on the new order. If you want that, consider using a Gtk::MultiSorter and appending the previous sorter to it.

The model can be set up to do incremental sorting, so that sorting long lists doesn't block the UI. See Gtk::SortListModel#incremental= for details.

Gtk::SortListModel 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 Gtk::SortListModel, it is strongly recommended that you write your own sorting list model.

Included Modules

Defined in:

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

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Gio::ListModel

item(position : UInt32) : GObject::Object? item, item_type : UInt64 item_type, items_changed(position : UInt32, removed : UInt32, added : UInt32) : Nil items_changed, items_changed_signal items_changed_signal, n_items : UInt32 n_items, to_unsafe to_unsafe

Class methods inherited from module Gio::ListModel

g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

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)? data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, 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)? qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void)?) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void)? steal_data, steal_qdata(quark : UInt32) : Pointer(Void)? 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, 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

compat_control(what : UInt64, data : Pointer(Void)?) : 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

Constructor Detail

def self.new(model : Gio::ListModel?, sorter : Gtk::Sorter?) : self #

Creates a new sort list model that uses the @sorter to sort @model.


def self.new #

Initialize a new SortListModel.


def self.new(*, incremental : Bool? = nil, item_type : UInt64? = nil, model : Gio::ListModel? = nil, n_items : UInt32? = nil, pending : UInt32? = nil, sorter : Gtk::Sorter? = nil) #

Class Method Detail

def self.g_type : UInt64 #

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


Instance Method Detail

def incremental : Bool #

Returns whether incremental sorting is enabled.

See Gtk::SortListModel#incremental=.


def incremental=(incremental : Bool) : Nil #

Sets the sort model to do an incremental sort.

When incremental sorting is enabled, the Gtk::SortListModel will not do a complete sort immediately, but will instead queue an idle handler that incrementally sorts the items towards their correct position. This of course means that items do not instantly appear in the right place. It also means that the total sorting time is a lot slower.

When your filter blocks the UI while sorting, you might consider turning this on. Depending on your model and sorters, this may become interesting around 10,000 to 100,000 items.

By default, incremental sorting is disabled.

See Gtk::SortListModel#pending for progress information about an ongoing incremental sorting operation.


def incremental? : Bool #

def item_type : UInt64 #

def model : Gio::ListModel? #

Gets the model currently sorted or nil if none.


def model=(model : Gio::ListModel?) : Nil #

Sets the model to be sorted.

The @model's item type must conform to the item type of @self.


def n_items : UInt32 #

def pending : UInt32 #

Estimates progress of an ongoing sorting operation.

The estimate is the number of items that would still need to be sorted to finish the sorting operation if this was a linear algorithm. So this number is not related to how many items are already correctly sorted.

If you want to estimate the progress, you can use code like this:

WARNING ⚠️ The following code is in c ⚠️

pending = gtk_sort_list_model_get_pending (self);
model = gtk_sort_list_model_get_model (self);
progress = 1.0 - pending / (double) MAX (1, g_list_model_get_n_items (model));

If no sort operation is ongoing - in particular when Gtk::SortListModel#incremental is false - this function returns 0.


def sorter : Gtk::Sorter? #

Gets the sorter that is used to sort @self.


def sorter=(sorter : Gtk::Sorter?) : Nil #

Sets a new sorter on @self.