Blame platform-demos/C/samples/treeview_cellrenderertoggle.py

Packit 1470ea
from gi.repository import Gtk
Packit 1470ea
from gi.repository import Pango
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
books = [["Tolstoy, Leo", ["War and Peace", True], ["Anna Karenina", False]],
Packit 1470ea
         ["Shakespeare, William", ["Hamlet", False],
Packit 1470ea
             ["Macbeth", True], ["Othello", False]],
Packit 1470ea
         ["Tolkien, J.R.R.", ["The Lord of the Rings", False]]]
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="Library", application=app)
Packit 1470ea
        self.set_default_size(250, 100)
Packit 1470ea
        self.set_border_width(10)
Packit 1470ea
Packit 1470ea
        # the data are stored in the model
Packit 1470ea
        # create a treestore with two columns
Packit 1470ea
        self.store = Gtk.TreeStore(str, bool)
Packit 1470ea
        # fill in the model
Packit 1470ea
        for i in range(len(books)):
Packit 1470ea
            # the iter piter is returned when appending the author in the first column
Packit 1470ea
            # and False in the second
Packit 1470ea
            piter = self.store.append(None, [books[i][0], False])
Packit 1470ea
            # append the books and the associated boolean value as children of
Packit 1470ea
            # the author
Packit 1470ea
            j = 1
Packit 1470ea
            while j < len(books[i]):
Packit 1470ea
                self.store.append(piter, books[i][j])
Packit 1470ea
                j += 1
Packit 1470ea
Packit 1470ea
        # the treeview shows the model
Packit 1470ea
        # create a treeview on the model self.store
Packit 1470ea
        view = Gtk.TreeView()
Packit 1470ea
        view.set_model(self.store)
Packit 1470ea
Packit 1470ea
        # the cellrenderer for the first column - text
Packit 1470ea
        renderer_books = Gtk.CellRendererText()
Packit 1470ea
        # the first column is created
Packit 1470ea
        column_books = Gtk.TreeViewColumn("Books", renderer_books, text=0)
Packit 1470ea
        # and it is appended to the treeview
Packit 1470ea
        view.append_column(column_books)
Packit 1470ea
Packit 1470ea
        # the cellrenderer for the second column - boolean rendered as a toggle
Packit 1470ea
        renderer_in_out = Gtk.CellRendererToggle()
Packit 1470ea
        # the second column is created
Packit 1470ea
        column_in_out = Gtk.TreeViewColumn("Out?", renderer_in_out, active=1)
Packit 1470ea
        # and it is appended to the treeview
Packit 1470ea
        view.append_column(column_in_out)
Packit 1470ea
        # connect the cellrenderertoggle with a callback function
Packit 1470ea
        renderer_in_out.connect("toggled", self.on_toggled)
Packit 1470ea
Packit 1470ea
        # add the treeview to the window
Packit 1470ea
        self.add(view)
Packit 1470ea
Packit 1470ea
    # callback function for the signal emitted by the cellrenderertoggle
Packit 1470ea
    def on_toggled(self, widget, path):
Packit 1470ea
        # the boolean value of the selected row
Packit 1470ea
        current_value = self.store[path][1]
Packit 1470ea
        # change the boolean value of the selected row in the model
Packit 1470ea
        self.store[path][1] = not current_value
Packit 1470ea
        # new current value!
Packit 1470ea
        current_value = not current_value
Packit 1470ea
        # if length of the path is 1 (that is, if we are selecting an author)
Packit 1470ea
        if len(path) == 1:
Packit 1470ea
            # get the iter associated with the path
Packit 1470ea
            piter = self.store.get_iter(path)
Packit 1470ea
            # get the iter associated with its first child
Packit 1470ea
            citer = self.store.iter_children(piter)
Packit 1470ea
            # while there are children, change the state of their boolean value
Packit 1470ea
            # to the value of the author
Packit 1470ea
            while citer is not None:
Packit 1470ea
                self.store[citer][1] = current_value
Packit 1470ea
                citer = self.store.iter_next(citer)
Packit 1470ea
        # if the length of the path is not 1 (that is, if we are selecting a
Packit 1470ea
        # book)
Packit 1470ea
        elif len(path) != 1:
Packit 1470ea
            # get the first child of the parent of the book (the first book of
Packit 1470ea
            # the author)
Packit 1470ea
            citer = self.store.get_iter(path)
Packit 1470ea
            piter = self.store.iter_parent(citer)
Packit 1470ea
            citer = self.store.iter_children(piter)
Packit 1470ea
            # check if all the children are selected
Packit 1470ea
            all_selected = True
Packit 1470ea
            while citer is not None:
Packit 1470ea
                if self.store[citer][1] == False:
Packit 1470ea
                    all_selected = False
Packit 1470ea
                    break
Packit 1470ea
                citer = self.store.iter_next(citer)
Packit 1470ea
            # if they do, the author as well is selected; otherwise it is not
Packit 1470ea
            self.store[piter][1] = all_selected
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyApplication(Gtk.Application):
Packit 1470ea
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        win.show_all()
Packit 1470ea
Packit 1470ea
    def do_startup(self):
Packit 1470ea
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
app = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)