glibmm  2.76.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | Related Functions | List of all members
Gio::ListModel Class Reference

A dynamic list of objects. More...

#include <giomm/listmodel.h>

Inheritance diagram for Gio::ListModel:
Inheritance graph
[legend]

Public Member Functions

 ListModel (ListModel && src) noexcept
 
ListModeloperator= (ListModel && src) noexcept
 
 ~ListModel () noexcept override
 
GListModel * gobj ()
 Provides access to the underlying C GObject. More...
 
const GListModel * gobj () const
 Provides access to the underlying C GObject. More...
 
GType get_item_type () const
 Gets the type of the items in list. More...
 
guint get_n_items () const
 Gets the number of items in list. More...
 
Glib::RefPtr< Glib::ObjectBaseget_object (guint position)
 Get the item at position. More...
 
Glib::RefPtr< const Glib::ObjectBaseget_object (guint position) const
 Get the item at position. More...
 
template<typename T_item >
Glib::RefPtr< T_item > get_typed_object (guint position)
 Get the item at position. More...
 
template<typename T_item >
Glib::RefPtr< const T_item > get_typed_object (guint position) const
 Get the item at position. More...
 
Glib::SignalProxy< void(guint, guint, guint)> signal_items_changed ()
 
- Public Member Functions inherited from Glib::Interface
 Interface ()
 A Default constructor. More...
 
 Interface (Interface && src) noexcept
 
Interfaceoperator= (Interface && src) noexcept
 
 Interface (const Glib::Interface_Class & interface_class)
 Called by constructors of derived classes. More...
 
 Interface (GObject * castitem)
 Called by constructors of derived classes. More...
 
 ~Interface () noexcept override
 
 Interface (const Interface &)=delete
 
Interfaceoperator= (const Interface &)=delete
 
GObject * gobj ()
 
const GObject * gobj () const
 
- Public Member Functions inherited from Glib::ObjectBase
 ObjectBase (const ObjectBase &)=delete
 
ObjectBaseoperator= (const ObjectBase &)=delete
 
void set_property_value (const Glib::ustring & property_name, const Glib::ValueBase & value)
 You probably want to use a specific property_*() accessor method instead. More...
 
void get_property_value (const Glib::ustring & property_name, Glib::ValueBase & value) const
 You probably want to use a specific property_*() accessor method instead. More...
 
template<class PropertyType >
void set_property (const Glib::ustring & property_name, const PropertyType & value)
 You probably want to use a specific property_*() accessor method instead. More...
 
template<class PropertyType >
void get_property (const Glib::ustring & property_name, PropertyType & value) const
 You probably want to use a specific property_*() accessor method instead. More...
 
template<class PropertyType >
PropertyType get_property (const Glib::ustring & property_name) const
 You probably want to use a specific property_*() accessor method instead. More...
 
sigc::connection connect_property_changed (const Glib::ustring & property_name, const sigc::slot< void()> & slot)
 You can use the signal_changed() signal of the property proxy instead. More...
 
sigc::connection connect_property_changed (const Glib::ustring & property_name, sigc::slot< void()> && slot)
 You can use the signal_changed() signal of the property proxy instead. More...
 
void freeze_notify ()
 Increases the freeze count on object. More...
 
void thaw_notify ()
 Reverts the effect of a previous call to freeze_notify(). More...
 
virtual void reference () const
 Increment the reference count for this object. More...
 
virtual void unreference () const
 Decrement the reference count for this object. More...
 
GObject * gobj ()
 Provides access to the underlying C GObject. More...
 
const GObject * gobj () const
 Provides access to the underlying C GObject. More...
 
GObject * gobj_copy () const
 Give a ref-ed copy to someone. Use for direct struct access. More...
 

Static Public Member Functions

static void add_interface (GType gtype_implementer)
 
static GType get_type ()
 Get the GType for this class, for use with the underlying GObject type system. More...
 

Protected Member Functions

 ListModel ()
 You should derive from this class to use it. More...
 
void items_changed (guint position, guint removed, guint added)
 Emits the ListModel::signal_items_changed() signal on list. More...
 
virtual GType get_item_type_vfunc ()
 
virtual guint get_n_items_vfunc ()
 
virtual gpointer get_item_vfunc (guint position)
 
- Protected Member Functions inherited from Glib::ObjectBase
 ObjectBase ()
 This default constructor is called implicitly from the constructor of user-derived classes, even if, for instance, Gtk::Button calls a different ObjectBase constructor. More...
 
 ObjectBase (const char * custom_type_name)
 A derived constructor always overrides this choice. More...
 
 ObjectBase (const std::type_info & custom_type_info)
 This constructor is a special feature to allow creation of derived types on the fly, without having to use g_object_new() manually. More...
 
 ObjectBase (ObjectBase && src) noexcept
 
ObjectBaseoperator= (ObjectBase && src) noexcept
 
virtual ~ObjectBase () noexcept=0
 
void initialize (GObject * castitem)
 
void initialize_move (GObject * castitem, Glib::ObjectBase * previous_wrapper)
 

Related Functions

(Note that these are not member functions.)

Glib::RefPtr< Gio::ListModelwrap (GListModel * object, bool take_copy=false)
 A Glib::wrap() method for this object. More...
 

Detailed Description

A dynamic list of objects.

A ListModel represents a mutable list of Glib::Objects. 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 Glib::PropertyProxy<>::signal_changed() signal of one or more of the object's properties). Taken together with the signal_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 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 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 nullptr.

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.

Since glibmm 2.50:

Constructor & Destructor Documentation

◆ ListModel() [1/2]

Gio::ListModel::ListModel ( )
protected

You should derive from this class to use it.

◆ ListModel() [2/2]

Gio::ListModel::ListModel ( ListModel &&  src)
noexcept

◆ ~ListModel()

Gio::ListModel::~ListModel ( )
overridenoexcept

Member Function Documentation

◆ add_interface()

static void Gio::ListModel::add_interface ( GType  gtype_implementer)
static

◆ get_item_type()

GType Gio::ListModel::get_item_type ( ) const

Gets the type of the items in list.

All items returned from g_list_model_get_item() 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 ListModel can not change during the life of the model.

Since glibmm 2.50:
Returns
The Type of the items contained in list.

◆ get_item_type_vfunc()

virtual GType Gio::ListModel::get_item_type_vfunc ( )
protectedvirtual

◆ get_item_vfunc()

virtual gpointer Gio::ListModel::get_item_vfunc ( guint  position)
protectedvirtual

◆ get_n_items()

guint Gio::ListModel::get_n_items ( ) const

Gets the number of items in list.

Depending on the model implementation, calling this function may be less efficient than iterating the list with increasing values for position until g_list_model_get_item() returns nullptr.

Since glibmm 2.50:
Returns
The number of items in list.

◆ get_n_items_vfunc()

virtual guint Gio::ListModel::get_n_items_vfunc ( )
protectedvirtual

◆ get_object() [1/2]

Glib::RefPtr<Glib::ObjectBase> Gio::ListModel::get_object ( guint  position)

Get the item at position.

If position is greater than the number of items in list, nullptr is returned.

nullptr is never returned for an index that is smaller than the length of the list.

This function is meant to be used by language bindings in place of g_list_model_get_item().

See also: g_list_model_get_n_items()

Since glibmm 2.50:
Parameters
positionThe position of the item to fetch.
Returns
The object at position.

◆ get_object() [2/2]

Glib::RefPtr<const Glib::ObjectBase> Gio::ListModel::get_object ( guint  position) const

Get the item at position.

If position is greater than the number of items in list, nullptr is returned.

nullptr is never returned for an index that is smaller than the length of the list.

This function is meant to be used by language bindings in place of g_list_model_get_item().

See also: g_list_model_get_n_items()

Since glibmm 2.50:
Parameters
positionThe position of the item to fetch.
Returns
The object at position.

◆ get_type()

static GType Gio::ListModel::get_type ( )
static

Get the GType for this class, for use with the underlying GObject type system.

◆ get_typed_object() [1/2]

template <typename T_item >
Glib::RefPtr<T_item> Gio::ListModel::get_typed_object ( guint  position)

Get the item at position.

If position is greater than the number of items in list, an empty RefPtr is returned.

Glib::RefPtr<T_item> item = list_model->get_typed_object<T_item>(position);

is often equivalent to

Glib::RefPtr<T_item> item = std::dynamic_pointer_cast<T_item>(list_model->get_object(position));

If T_item is an interface, and the underlying C object is an instance of a C class that implements that interface, but there is no corresponding C++ class, get_typed_object() may manage to fetch the item even if get_object() fails and returns an empty RefPtr.

See also
get_n_items() and get_object()
Since glibmm 2.76:
Template Parameters
T_itemThe item to fetch must be of type T_item or a type derived from T_item, otherwise an empty RefPtr is returned. T_item must be Glib::ObjectBase or a type derived from Glib::ObjectBase.
Parameters
positionThe position of the item to fetch.
Returns
The object at position.

◆ get_typed_object() [2/2]

template <typename T_item >
Glib::RefPtr<const T_item> Gio::ListModel::get_typed_object ( guint  position) const

Get the item at position.

See the non-const version.

Since glibmm 2.76:
Template Parameters
T_itemThe item to fetch must be of type T_item or a type derived from T_item, otherwise an empty RefPtr is returned. T_item must be Glib::ObjectBase or a type derived from Glib::ObjectBase.
Parameters
positionThe position of the item to fetch.
Returns
The object at position.

◆ gobj() [1/2]

GListModel* Gio::ListModel::gobj ( )
inline

Provides access to the underlying C GObject.

◆ gobj() [2/2]

const GListModel* Gio::ListModel::gobj ( ) const
inline

Provides access to the underlying C GObject.

◆ items_changed()

void Gio::ListModel::items_changed ( guint  position,
guint  removed,
guint  added 
)
protected

Emits the ListModel::signal_items_changed() signal on list.

This function should only be called by classes implementing ListModel. It has to be called after the internal representation of 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 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 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.

Since glibmm 2.50:
Parameters
positionThe position at which list changed.
removedThe number of items removed.
addedThe number of items added.

◆ operator=()

ListModel& Gio::ListModel::operator= ( ListModel &&  src)
noexcept

◆ signal_items_changed()

Glib::SignalProxy<void(guint, guint, guint)> Gio::ListModel::signal_items_changed ( )
Slot Prototype:
void on_my_items_changed(guint position, guint removed, guint added)

Flags: Run Last

This signal is emitted whenever items were added to or removed from 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.
Since glibmm 2.50:
Parameters
positionThe position at which list changed.
removedThe number of items removed.
addedThe number of items added.

Friends And Related Function Documentation

◆ wrap()

Glib::RefPtr< Gio::ListModel > wrap ( GListModel *  object,
bool  take_copy = false 
)
related

A Glib::wrap() method for this object.

Parameters
objectThe C instance.
take_copyFalse if the result should take ownership of the C instance. True if it should take a new copy or ref.
Returns
A C++ instance that wraps this C instance.