
Gnome::Gio::R-ListModel
Description
Gnome::Gio::R-ListModel is an interface that represents a mutable list of GObjects. Its main intention is as a model for various widgets in user interfaces, such as list views, but it can also be used as a convenient method of returning lists of data, with support for updates.
Each object in the list may also report changes in itself via some mechanism (normally the notify signal defined in Gnome::Gio::ListModel). Taken together with the items-changed signal, this provides for a list that can change its membership, and in which the members can change their individual properties.
A good example would be the list of visible wireless network access points, where each access point can report dynamic properties such as signal strength.
It is important to note that the Gnome::Gio::R-ListModel itself does not report changes to the individual items. It only reports changes to the list membership. If you want to observe changes to the objects themselves then you need to connect signals to the objects that you are interested in.
All items in a Gnome::Gio::R-ListModel are of (or derived from) the same type. .get-item-type() returns that type. The type may be an interface, in which case all objects in the list must implement it.
The semantics are close to that of an array: .get-n-items() returns the number of items in the list and .get-object() returns an item at a (0-based) position. In order to allow implementations to calculate the list length lazily, you can also iterate over items: starting from 0, repeatedly call .get-object() until it returns undefined.
An implementation may create objects lazily, but must take care to return the same object for a given position until all references to it are gone.
On the other side, a consumer is expected only to hold references on objects that are currently "user visible", in order to facilitate the maximum level of laziness in the implementation of the list and to reduce the required number of signal connections at a given time.
This interface is intended only to be used from a single thread. The thread in which it is appropriate to use it depends on the particular implementation, but typically it will be from the thread that owns the thread-default main context in effect at the time that the model was created.
Over time, it has established itself as good practice for list model implementations to provide properties item-type and n-items to ease working with them. While it is not required, it is recommended that implementations provide these two properties. They should return the values of .get-item-type().
Example
A Gnome::Gtk4::StringList implements the role Gnome::Gio::ListModel. This example shows some code in relation with that class
my Gnome::Gtk4::StringList $stringlist;
$stringlist .= new-stringlist(CArray[Str].new( 'a string', '2nd string'));
# Easiest way to get the string
say 'List item at position 1: ', $stringlist.get-string(1);
# Add more items
$stringlist.append('3rd string');
# Show length of list
say 'Length of list is: ', $stringlist.get-n-items;
# More elaborate way to get the string
my Gnome::Gtk4::StringObject() $so = $stringlist.get-object(2);
say 'List item at position 2: ', $so.get-string;
Methods
get-item-type
Gets the type of the items in the list.
All items returned from .get-object() are of the type returned by this function, or a subtype, or if the type is an interface, they are an implementation of that interface.
The item type of a Gnome::Gio::R-ListModel can not change during the life of the model.
method get-item-type ( --> GType )
Return value; the GType of the items contained in list.
get-n-items
Gets the number of items in the list.
Depending on the model implementation, calling this function may be less efficient than iterating the list with increasing values for $position until .get-object() returns undefined.
method get-n-items ( --> UInt )
Return value; the number of items in the list.
get-object
Get the item at $position.
If $position is greater than the number of items in the list, undefined is returned.
Undefined is never returned for an index that is smaller than the length of the list.
method get-object ( UInt() $position --> N-Object )
$position; the position of the item to fetch.
Return value; the object at $position.
Example
The example shows a way to get the 2nd item from a list managed by the list model.
my Gnome::Gtk4::StringObject() $so = $stringlist.get-item(1)); my Str $string = $so.get-string(1);
items-changed
Emits the items-changed signal on the list.
This function should only be called by classes implementing Gnome::Gio::R-ListModel. It has to be called after the internal representation of the list has been updated, because handlers connected to this signal might query the new state of the list.
Implementations must only make changes to the model (as visible to its consumer) in places that will not cause problems for that consumer. For models that are driven directly by a write API (such as Gnome::Gio::ListStore), changes can be reported in response to uses of that API. For models that represent remote data, changes should only be made from a fresh mainloop dispatch. It is particularly not permitted to make changes in response to a call to the Gnome::Gio::R-ListModel consumer API.
Stated another way: in general, it is assumed that code making a series of accesses to the model via the API, without returning to the mainloop, and without calling other code, will continue to view the same contents of the model.
method items-changed ( UInt() $position, UInt() $removed, UInt() $added )
$position; the position at which the list changed.
$removed; the number of items removed.
$added; the number of items added.
Signals
items-changed
This signal is emitted whenever items were added to or removed from the list. At $position, $removed items were removed and $added items were added in their place.
Note: If removed != added, the positions of all later items in the model change.
method handler ( guint $position, guint $removed, guint $added, Int :$_handle_id, N-GObject :$_native-object, *%user-options )
$position; the position at which the list changed.
$removed; the number of items removed.
$added; the number of items added.
$_handle_id; The registered event handler id.
$_native-object; The native object provided by the Raku object which registered this event. This a native Gnome::Gio::ListModel object.
%user-options; A list of named arguments provided at the
.register-signal()method from Gnome::GObject::Object.
About my projects, examples and tutorials