:right-sidebar: True SelectionModel =================================================================== .. currentmodule:: gi.repository.Gtk .. class:: SelectionModel(*args, **kwargs) :no-contents-entry: Implementations: :class:`~gi.repository.Gtk.MultiSelection`, :class:`~gi.repository.Gtk.NoSelection`, :class:`~gi.repository.Gtk.SingleSelection` ``GtkSelectionModel`` is an interface that add support for selection to list models. This support is then used by widgets using list models to add the ability to select and unselect various items. GTK provides default implementations of the most common selection modes such as :obj:`~gi.repository.Gtk.SingleSelection`, so you will only need to implement this interface if you want detailed control about how selections should be handled. A ``GtkSelectionModel`` supports a single boolean per item indicating if an item is selected or not. This can be queried via :obj:`~gi.repository.Gtk.SelectionModel.is_selected`. When the selected state of one or more items changes, the model will emit the :obj:`~gi.repository.Gtk.SelectionModel.signals.selection_changed` signal by calling the :obj:`~gi.repository.Gtk.SelectionModel.selection_changed` function. The positions given in that signal may have their selection state changed, though that is not a requirement. If new items added to the model via the :obj:`~gi.repository.Gio.ListModel.signals.items_changed` signal are selected or not is up to the implementation. Note that items added via :obj:`~gi.repository.Gio.ListModel.signals.items_changed` may already be selected and no :obj:`~gi.repository.Gtk.SelectionModel.signals.selection_changed` will be emitted for them. So to track which items are selected, it is necessary to listen to both signals. Additionally, the interface can expose functionality to select and unselect items. If these functions are implemented, GTK's list widgets will allow users to select and unselect items. However, ``GtkSelectionModel``'s are free to only implement them partially or not at all. In that case the widgets will not support the unimplemented operations. When selecting or unselecting is supported by a model, the return values of the selection functions do *not* indicate if selection or unselection happened. They are only meant to indicate complete failure, like when this mode of selecting is not supported by the model. Selections may happen asynchronously, so the only reliable way to find out when an item was selected is to listen to the signals that indicate selection. Methods ------- .. rst-class:: interim-class .. class:: SelectionModel :no-index: .. method:: get_selection() -> ~gi.repository.Gtk.Bitset Gets the set containing all currently selected items in the model. This function may be slow, so if you are only interested in single item, consider using :obj:`~gi.repository.Gtk.SelectionModel.is_selected` or if you are only interested in a few, consider :obj:`~gi.repository.Gtk.SelectionModel.get_selection_in_range`. .. method:: get_selection_in_range(position: int, n_items: int) -> ~gi.repository.Gtk.Bitset Gets the set of selected items in a range. This function is an optimization for :obj:`~gi.repository.Gtk.SelectionModel.get_selection` when you are only interested in part of the model's selected state. A common use case is in response to the :obj:`~gi.repository.Gtk.SelectionModel.signals.selection_changed` signal. :param position: start of the queried range :param n_items: number of items in the queried range .. method:: is_selected(position: int) -> bool Checks if the given item is selected. :param position: the position of the item to query .. method:: select_all() -> bool Requests to select all items in the model. .. method:: select_item(position: int, unselect_rest: bool) -> bool Requests to select an item in the model. :param position: the position of the item to select :param unselect_rest: whether previously selected items should be unselected .. method:: select_range(position: int, n_items: int, unselect_rest: bool) -> bool Requests to select a range of items in the model. :param position: the first item to select :param n_items: the number of items to select :param unselect_rest: whether previously selected items should be unselected .. method:: selection_changed(position: int, n_items: int) -> None Helper function for implementations of ``GtkSelectionModel``. Call this when the selection changes to emit the :obj:`~gi.repository.Gtk.SelectionModel.signals.selection_changed` signal. :param position: the first changed item :param n_items: the number of changed items .. method:: set_selection(selected: ~gi.repository.Gtk.Bitset, mask: ~gi.repository.Gtk.Bitset) -> bool Make selection changes. This is the most advanced selection updating method that allows the most fine-grained control over selection changes. If you can, you should try the simpler versions, as implementations are more likely to implement support for those. Requests that the selection state of all positions set in ``mask`` be updated to the respective value in the ``selected`` bitmask. In pseudocode, it would look something like this: .. code-block:: c :dedent: for (i = 0; i < n_items; i++) { // don't change values not in the mask if (!gtk_bitset_contains (mask, i)) continue; if (gtk_bitset_contains (selected, i)) select_item (i); else unselect_item (i); } gtk_selection_model_selection_changed (model, first_changed_item, n_changed_items); ``mask`` and ``selected`` must not be modified. They may refer to the same bitset, which would mean that every item in the set should be selected. :param selected: bitmask specifying if items should be selected or unselected :param mask: bitmask specifying which items should be updated .. method:: unselect_all() -> bool Requests to unselect all items in the model. .. method:: unselect_item(position: int) -> bool Requests to unselect an item in the model. :param position: the position of the item to unselect .. method:: unselect_range(position: int, n_items: int) -> bool Requests to unselect a range of items in the model. :param position: the first item to unselect :param n_items: the number of items to unselect Signals ------- .. rst-class:: interim-class .. class:: SelectionModel.signals :no-index: .. method:: selection_changed(position: int, n_items: int) -> None The type of the None singleton. :param position: The first item that may have changed :param n_items: number of items with changes Virtual Methods --------------- .. rst-class:: interim-class .. class:: SelectionModel :no-index: .. method:: do_get_selection_in_range(type, self, position: int, n_items: int) -> ~gi.repository.Gtk.Bitset Gets the set of selected items in a range. This function is an optimization for :obj:`~gi.repository.Gtk.SelectionModel.get_selection` when you are only interested in part of the model's selected state. A common use case is in response to the :obj:`~gi.repository.Gtk.SelectionModel.signals.selection_changed` signal. :param type: :param self: :param position: start of the queried range :param n_items: number of items in the queried range .. method:: do_is_selected(type, self, position: int) -> bool Checks if the given item is selected. :param type: :param self: :param position: the position of the item to query .. method:: do_select_all(type, self) -> bool Requests to select all items in the model. :param type: :param self: .. method:: do_select_item(type, self, position: int, unselect_rest: bool) -> bool Requests to select an item in the model. :param type: :param self: :param position: the position of the item to select :param unselect_rest: whether previously selected items should be unselected .. method:: do_select_range(type, self, position: int, n_items: int, unselect_rest: bool) -> bool Requests to select a range of items in the model. :param type: :param self: :param position: the first item to select :param n_items: the number of items to select :param unselect_rest: whether previously selected items should be unselected .. method:: do_set_selection(type, self, selected: ~gi.repository.Gtk.Bitset, mask: ~gi.repository.Gtk.Bitset) -> bool Make selection changes. This is the most advanced selection updating method that allows the most fine-grained control over selection changes. If you can, you should try the simpler versions, as implementations are more likely to implement support for those. Requests that the selection state of all positions set in ``mask`` be updated to the respective value in the ``selected`` bitmask. In pseudocode, it would look something like this: .. code-block:: c :dedent: for (i = 0; i < n_items; i++) { // don't change values not in the mask if (!gtk_bitset_contains (mask, i)) continue; if (gtk_bitset_contains (selected, i)) select_item (i); else unselect_item (i); } gtk_selection_model_selection_changed (model, first_changed_item, n_changed_items); ``mask`` and ``selected`` must not be modified. They may refer to the same bitset, which would mean that every item in the set should be selected. :param type: :param self: :param selected: bitmask specifying if items should be selected or unselected :param mask: bitmask specifying which items should be updated .. method:: do_unselect_all(type, self) -> bool Requests to unselect all items in the model. :param type: :param self: .. method:: do_unselect_item(type, self, position: int) -> bool Requests to unselect an item in the model. :param type: :param self: :param position: the position of the item to unselect .. method:: do_unselect_range(type, self, position: int, n_items: int) -> bool Requests to unselect a range of items in the model. :param type: :param self: :param position: the first item to unselect :param n_items: the number of items to unselect