| 1 |
.. currentmodule:: gi.repository |
| 2 |
|
| 3 |
Interfaces |
| 4 |
========== |
| 5 |
GObject interfaces are a way of ensuring that objects passed to C code have the |
| 6 |
right capabilities. |
| 7 |
|
| 8 |
When a GObject implements an interface it should implement an expected set of |
| 9 |
methods, properties and signals. In the Python sense, an interface is another |
| 10 |
class that is inherited. |
| 11 |
|
| 12 |
For example in GTK, :class:`Gtk.Image` supports various sources for the image |
| 13 |
that it will display. |
| 14 |
Some of these sources can be a :class:`Gio.Icon` or a :class:`Gdk.Paintable`, |
| 15 |
both are actually interfaces so you don't pass a direct instance of these, |
| 16 |
instead you should use some of the gobjects that implement the interface. |
| 17 |
For :class:`Gio.Icon` those can be :class:`Gio.ThemedIcon` that represents an |
| 18 |
icon from the icon theme, or :class:`Gio.FileIcon` that represents an icon |
| 19 |
created from an image file. |
| 20 |
|
| 21 |
Another important interface of reference is :class:`Gio.ListModel`. |
| 22 |
It represents a mutable list of :class:`GObject.Objects <GObject.Object>`. |
| 23 |
If you want to implement :class:`Gio.ListModel` you must implement three methods, |
| 24 |
these are :meth:`Gio.ListModel.get_item_type`, :meth:`Gio.ListModel.get_n_items` |
| 25 |
and :meth:`Gio.ListModel.get_item`. |
| 26 |
The interfaces methods that you should implement are exposed as |
| 27 |
:ref:`virtual methods <virtual-methods>`. |
| 28 |
With these methods any consumer can iterate the list and use the objects for any |
| 29 |
purpose. |
| 30 |
|
| 31 |
.. tip:: |
| 32 |
A generic implementation of :class:`Gio.ListModel` is :class:`Gio.ListStore`. |
| 33 |
It allows you to set the :class:`GObject.Object` type that it will store and |
| 34 |
provides methods to append, insert, sort, find and remove gobjects. |
| 35 |
|
| 36 |
Example |
| 37 |
------- |
| 38 |
|
| 39 |
In this examples we'll be implementing a :class:`Gio.ListModel` in Python. |
| 40 |
It will store a custom :class:`GObject.Object` and provide some helper methods |
| 41 |
for it. |
| 42 |
|
| 43 |
.. literalinclude:: examples/listmodel.py |
| 44 |
:linenos: |