Gtk.TreeModelFilter


Gtk.TreeModelFilter — A Gtk.TreeModel which hides parts of an underlying tree model

Object Hierarchy:

    GObject
    ╰── Gtk.TreeModelFilter

See also:

Gtk.TreeModelSort


Functions:

Description:

A Gtk.TreeModelFilter is a tree model which wraps another tree model, and can do the following things:

  • Filter specific rows, based on data from a “visible column”, a column storing booleans indicating whether the row should be filtered or not, or based on the return value of a “visible function”, which gets a model, iter and user_data and returns a boolean indicating whether the row should be filtered or not.

  • Modify the “appearance” of the model, using a modify function. This is extremely powerful and allows for just changing some values and also for creating a completely different model based on the given child model.

  • Set a different root node, also known as a “virtual root”. You can pass in a Gtk.TreePath indicating the root node for the filter at construction time.

The basic API is similar to Gtk.TreeModelSort. For an example on its usage, see the section on Gtk.TreeModelSort.

When using Gtk.TreeModelFilter, it is important to realize that Gtk.TreeModelFilter maintains an internal cache of all nodes which are visible in its clients. The cache is likely to be a subtree of the tree exposed by the child model. Gtk.TreeModelFilter will not cache the entire child model when unnecessary to not compromise the caching mechanism that is exposed by the reference counting scheme. If the child model implements reference counting, unnecessary signals may not be emitted because of reference counting rule 3, see the Gtk.TreeModel documentation. (Note that e.g. Gtk.TreeStore does not implement reference counting and will always emit all signals, even when the receiving node is not visible).

Because of this, limitations for possible visible functions do apply.

In general, visible functions should only use data or properties from the node for which the visibility state must be determined, its siblings or its parents. Usually, having a dependency on the state of any child node is not possible, unless references are taken on these explicitly.

When no such reference exists, no signals may be received for these child nodes (see reference couting rule number 3 in the Gtk.TreeModel section).

Determining the visibility state of a given node based on the state of its child nodes is a frequently occurring use case. Therefore, Gtk.TreeModelFilter explicitly supports this. For example, when a node does not have any children, you might not want the node to be visible.

As soon as the first row is added to the node’s child level (or the last row removed), the node’s visibility should be updated.

This introduces a dependency from the node on its child nodes. In order to accommodate this, Gtk.TreeModelFilter must make sure the necessary signals are received from the child model. This is achieved by building, for all nodes which are exposed as visible nodes to Gtk.TreeModelFilter's clients, the child level (if any) and take a reference on the first node in this level. Furthermore, for every row-inserted, row-changed or row-deleted signal (also these which were not handled because the node was not cached), Gtk.TreeModelFilter will check if the visibility state of any parent node has changed.

Beware, however, that this explicit support is limited to these two cases. For example, if you want a node to be visible only if two nodes in a child’s child level (2 levels deeper) are visible, you are on your own. In this case, either rely on Gtk.TreeStore to emit all signals because it does not implement reference counting, or for models that do implement reference counting, obtain references on these child levels yourself.


Function Details:

new()

new (object_type:GType, parameters:list) -> GObject.Object

Creates a new Gtk.TreeModel, with child_model as the child_modeland root as the virtual root.

  • Returns: A new Gtk.TreeModel.

  • Since: 2.4


set_visible_func()

set_visible_func (self, func:Gtk.TreeModelFilterVisibleFunc, data=None)

Sets the visible function used when filtering the filter to be func.The function should return True if the given row should be visible andFALSE otherwise. If the condition calculated by the function changes over time (e.g.because it depends on some global parameters), you must call Gtk.TreeModelFilter:refilter() to keep the visibility informationof the model up-to-date. Note that func is called whenever a row is inserted, when it may stillbe empty. The visible function should therefore take special care of emptyrows, like in the example below. static gbooleanvisible_func (Gtk.TreeModel model, Gtk.TreeIter iter, gpointer data){ // Visible if row is non-empty and first column is “HI” gchar *str; gboolean visible = FALSE; Gtk.TreeModel:get (model, iter, 0, &str, -1); if (str && strcmp (str, "HI") == 0) visible = True; g_free (str); return visible;} Note that Gtk.TreeModelFilter:set_visible_func() orGtk.TreeModelFilter:set_visible_column() can only be calledonce for a given filter model.

  • Since: 2.4

set_modify_func()

set_modify_func (self, types:list, func:Gtk.TreeModelFilterModifyFunc, data=None)

With the n_columns and types parameters, you give an array of columntypes for this model (which will be exposed to the parent model/view).The func, data and destroy parameters are for specifying the modifyfunction. The modify function will get called for eachdata access, the goal of the modify function is to return the data which should be displayed at the location specified using the parameters of the modify function. Note that Gtk.TreeModelFilter:set_modify_func()can only be called once for a given filter model.

  • Since: 2.4

set_visible_column()

set_visible_column (self, column:int)

Sets column of the child_model to be the column where filter shouldlook for visibility information. columns should be a column of typeG_TYPE_BOOLEAN, where True means that a row is visible, and FALSEif not. Note that Gtk.TreeModelFilter:set_visible_func() orGtk.TreeModelFilter:set_visible_column() can only be calledonce for a given filter model.

  • Since: 2.4

get_model()

get_model (self) -> Gtk.TreeModel

Returns a pointer to the child model of filter.

  • Returns: A pointer to a Gtk.TreeModel.

  • Since: 2.4


convert_child_iter_to_iter()

convert_child_iter_to_iter (self, child_iter:Gtk.TreeIter) -> bool, filter_iter:Gtk.TreeIter

Sets filter_iter to point to the row in filter that corresponds to therow pointed at by child_iter. If filter_iter was not set, FALSE isreturned.

  • Returns: True, if filter_iterwas set, i.e. if child_iteris avalid iterator pointing to a visible row in child model.

  • Since: 2.4


convert_iter_to_child_iter()

convert_iter_to_child_iter (self, filter_iter:Gtk.TreeIter) -> child_iter:Gtk.TreeIter

Sets child_iter to point to the row pointed to by filter_iter.

  • Since: 2.4

convert_child_path_to_path()

convert_child_path_to_path (self, child_path:Gtk.TreePath) -> Gtk.TreePath

Converts child_path to a path relative to filter. That is, child_pathpoints to a path in the child model. The rerturned path will point to thesame row in the filtered model. If child_path isn’t a valid path on thechild model or points to a row which is not visible in filter, then Noneis returned.

  • Returns: A newly allocated Gtk.TreePath, or None.

  • Since: 2.4


convert_path_to_child_path()

convert_path_to_child_path (self, filter_path:Gtk.TreePath) -> Gtk.TreePath

Converts filter_path to a path on the child model of filter. That is,filter_path points to a location in filter. The returned path willpoint to the same location in the model not being filtered. If filter_pathdoes not point to a location in the child model, None is returned.

  • Returns: A newly allocated Gtk.TreePath, or None.

  • Since: 2.4


refilter()

refilter (self)

Emits ::row_changed for each row in the child model, which causesthe filter to re-evaluate whether a row is visible or not.

  • Since: 2.4

clear_cache()

clear_cache (self)

This function should almost never be called. It clears the filterof any cached iterators that haven’t been reffed with`Gtk.TreeModel:ref_node()`. This might be useful if the child modelbeing filtered is static (and doesn’t change often) and there has beena lot of unreffed access to nodes. As a side effect of this function,all unreffed iters will be invalid.

  • Since: 2.4

Example:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

products = (("Apple", "Fruit", "£0.20"),
            ("Bleach", "Cleaning", "£1.20"),
            ("Bird Seed", "Pets", "£2.50"),
            ("Banana", "Fruit", "£0.35"),
            ("Beer", "Alcohol", "£2.75"),
            ("Cornflakes", "Cereal", "£1.10"),
            ("Pineapple", "Fruit", "£0.75"),
           )

class TreeModelFilter(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect("destroy", Gtk.main_quit)

        grid = Gtk.Grid()
        grid.set_row_spacing(5)
        self.add(grid)

        scrolledwindow = Gtk.ScrolledWindow()
        scrolledwindow.set_vexpand(True)
        scrolledwindow.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.NEVER)
        grid.attach(scrolledwindow, 0, 0, 1, 1)

        liststore = Gtk.ListStore(str, str, str)
        self.treemodelfilter = liststore.filter_new()
        self.treemodelfilter.set_visible_func(self.filter_visible, products)

        self.combobox = Gtk.ComboBoxText()
        self.combobox.append_text("All")
        self.combobox.set_active(0)
        self.combobox.connect("changed", self.on_category_changed)
        grid.attach(self.combobox, 0, 1, 1, 1)

        for product in products:
            liststore.append(product)

            self.combobox.append_text(product[1])

        treeview = Gtk.TreeView()
        treeview.set_model(self.treemodelfilter)
        scrolledwindow.add(treeview)

        cellrenderertext = Gtk.CellRendererText()
        treeviewcolumn = Gtk.TreeViewColumn("Product", cellrenderertext, text=0)
        treeview.append_column(treeviewcolumn)
        treeviewcolumn = Gtk.TreeViewColumn("Category", cellrenderertext, text=1)
        treeview.append_column(treeviewcolumn)
        treeviewcolumn = Gtk.TreeViewColumn("Price", cellrenderertext, text=2)
        treeview.append_column(treeviewcolumn)

    def on_category_changed(self, combobox):
        self.treemodelfilter.refilter()

    def filter_visible(self, model, treeiter, data):
        show = False

        if model[treeiter][1] == self.combobox.get_active_text():
            show = True
        elif self.combobox.get_active_text() == "All":
            show = True

        return show

window = TreeModelFilter()
window.show_all()

Gtk.main()

results matching ""

    No results matching ""