user2109788
user2109788

Reputation: 1336

add different length columns to gtk TreeStore(Treeview)

I want to display two levels of hierarchical data using gtk Treeview(with model gtk Treestore)

The data is in the following format:

**First(parent)** level
col_a, col_b, col_c, col_d, col_e
val_a, val_b, val_c, val_d, val_e

**Second(child)** level
col_x, col_y, col_z
val_x, val_y, val_z

And the hierarchy of data is as follows:

> val_a1, val_b1, val_c1, val_d1, val_e1
       val_x1, val_y1, val_z1
       val_x2, val_y2, val_z2

> val_a2, val_b2, val_c2, val_s2, val_e2
       val_x3, val_y3, val_z3

> val_a3, val_b3, val_c3, val_d3, val_e3

> val_a4, val_b4, val_c4, val_d4, val_e4
       val_x4, val_y4, val_z4
       val_x5, val_y5, val_z5

The following pygtk code is what I have tried(Modified the code from gtk tutorial)

import pygtk
pygtk.require('2.0')
import gtk

data = [
    [('val_a1', 'val_b1', 'val_c1', 'val_d1', 'val_e1'), ('val_x1', 'val_y1', 'val_z1'), ('val_x2', 'val_y2', 'val_z2')],
    [('val_a2', 'val_b2', 'val_c2', 'val_d2', 'val_e2'), ('val_x3', 'val_y3', 'val_z3')],
    [('val_a3', 'val_b3', 'val_c3', 'val_d3', 'val_e3')],
    [('val_a4', 'val_b4', 'val_c4', 'val_d4', 'val_e4'), ('val_x4', 'val_y4', 'val_z4'), ('val_x5', 'val_y5', 'val_z5')],
]

class BasicTreeViewExample:

    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Basic TreeView Example")
        self.window.set_size_request(200, 200)
        self.window.connect("delete_event", self.delete_event)
        self.treestore = gtk.TreeStore(str, str, str, str, str)
        for detail in data:
        for index, elem in enumerate(detail):
            if index == 0:
                piter = self.treestore.append(None, elem)
            else:
                self.treestore.append(piter, elem)

        self.treeview = gtk.TreeView(self.treestore)
        for i in range(5):
            tvcolumn = gtk.TreeViewColumn('Column %s' % (i))
            self.treeview.append_column(tvcolumn)
            cell = gtk.CellRendererText()
            tvcolumn.pack_start(cell, True)
            tvcolumn.add_attribute(cell, 'text', i)
        self.window.add(self.treeview)
        self.window.show_all()

def main():
    gtk.main()

if __name__ == "__main__":
    tvexample = BasicTreeViewExample()
    main()

But, I'm getting the following error when I try running the above code:

Traceback (most recent call last):
  File "test.py", line 55, in <module>
    tvexample = BasicTreeViewExample()
  File "test.py", line 33, in __init__
    self.treestore.append(piter, detail[index])
ValueError: row sequence has wrong length

So my questions are:

  1. How can I add data to gtk TreeStore with different number of columns in the different levels of hierarchy
  2. Also, Is it possible to display column names for each row in the gtk treestore

i.e In the Treeview I want to see the output as follows:

  col_a,  col_b,  col_c,  col_d,  col_e
> val_a1, val_b1, val_c1, val_d1, val_e1
       col_x,  col_y,  col_z
       val_x1, val_y1, val_z1

       col_x,  col_y,  col_z
       val_x2, val_y2, val_z2

  col_a,  col_b,  col_c,  col_d,  col_e
> val_a2, val_b2, val_c2, val_s2, val_e2
       col_x,  col_y,  col_z
       val_x3, val_y3, val_z3

  col_a,  col_b,  col_c,  col_d,  col_e
> val_a3, val_b3, val_c3, val_d3, val_e3

  col_a,  col_b,  col_c,  col_d,  col_e
> val_a4, val_b4, val_c4, val_d4, val_e4
       col_x, col_y, col_z
       val_x4, val_y4, val_z4

       col_x, col_y, col_z
       val_x5, val_y5, val_z5

If this is not possible using the treeview, is there any alternative/workarounds using which I can achieve the above?

Upvotes: 1

Views: 1567

Answers (1)

Cilyan
Cilyan

Reputation: 8491

Short answers and introduction

How can I add data to gtk.TreeStore with different number of columns in the different levels of hierarchy?

Simple: you can't. GtkListStore as well as GtkTreeStore are designed to hold data as a table. Columns are defined in a fixed way with an index and a data type. The only difference between a ListStore and a TreeStore is that in a TreeStore, rows have a hierarchy. Even worse, the GtkTreeView widget also expects data to be stored as a table, as each row will unconditionally fetch the cells using their column index, and expects to find something there. Unless you write your own widget, but you probably don't want to (God, this file is 16570 lines long...).

However, if you can't write your own widget, you still could write your own model. And this will give you some flexibility.

Also, is it possible to display column names for each row in the gtk.TreeStore ?

Displaying data in the TreeView involve two components: the GtkTreeView itself, that fetches data in the TreeStore and display them. The TreeView widget doesn't have the feature of displaying headers for each row. But there are some tricks to process data between the model and the view, which could end to the desired effect, though not that nice probably.

Basics

So, the TreeView expects to work on a table of data, and we can't change that. OK. But we still can trick it into thinking the data is a table, when actually it isn't... Let's start with the view. We need at least five columns to display the data of the parents. The children can then use only three columns out of these five, so this is fine.

Note that columns of the model do not always map to a column in the tree view. They actually map to some properties of cell renderers. For example, you can have a column in the model that defines the background color of the row, or a column that defines an icon to display. Columns in the view are just a way to align groups of cell renderers, possibly under a header. But here, let's assume all values are text that should go into a single CellRendererText in its own column.

Parents will use all five columns while children will use only the columns 2, 3 and 4. We'll then trick the model to return an empty text when data isn't available for the target cell.

Creating a new TreeModel

Some explainations about implementing a custom GtkTreeModel in PyGTK are available in this tutorial. This is a sample implementation of it:

import pygtk
pygtk.require('2.0')
import gtk

data = [
    [('val_a1', 'val_b1', 'val_c1', 'val_d1', 'val_e1'), ('val_x1', 'val_y1', 'val_z1'), ('val_x2', 'val_y2', 'val_z2')],
    [('val_a2', 'val_b2', 'val_c2', 'val_d2', 'val_e2'), ('val_x3', 'val_y3', 'val_z3')],
    [('val_a3', 'val_b3', 'val_c3', 'val_d3', 'val_e3')],
    [('val_a4', 'val_b4', 'val_c4', 'val_d4', 'val_e4'), ('val_x4', 'val_y4', 'val_z4'), ('val_x5', 'val_y5', 'val_z5')],
]

class MyTreeModel(gtk.GenericTreeModel):

    # The columns exposed by the model to the view
    column_types = (str, str, str, str, str)

    def __init__(self, data):
        gtk.GenericTreeModel.__init__(self)
        self.data = data

    def on_get_flags(self):
        """
            Get Model capabilities
        """
        return gtk.TREE_MODEL_ITERS_PERSIST

    def on_get_n_columns(self):
        """
            Get number of columns in the model
        """
        return len(self.column_types)

    def on_get_column_type(self, n):
        """
            Get data type of a specified column in the model
        """
        return self.column_types[n]

    def on_get_iter(self, path):
        """
            Obtain a reference to the row at path. For us, this is a tuple that
            contain the position of the row in the double list of data.
        """
        if len(path) > 2:
            return None # Invalid path
        parent_idx = path[0]
        if parent_idx >= len(self.data):
            return None # Invalid path
        first_level_list = self.data[parent_idx]
        if len(path) == 1:
            # Access the parent at index 0 in the first level list
            return (parent_idx, 0)
        else:
            # Access a child, at index path[1] + 1 (0 is the parent)
            child_idx = path[1] + 1
            if child_idx >= len(first_level_list):
                return None # Invalid path
            else:
                return (parent_idx, child_idx)

    def on_get_path(self, iter_):
        """
            Get a path from a rowref (this is the inverse of on_get_iter)
        """
        parent_idx, child_idx = iter_
        if child_idx == 0:
            return (parent_idx, )
        else:
            (parent_idx, child_idx-1)

    def on_get_value(self, iter_, column):
        """
            This is where the view asks for values. This is thus where we
            start mapping our data model to a fake table to present to the view
        """
        parent_idx, child_idx = iter_
        item = self.data[parent_idx][child_idx]
        # For parents, map columns 1:1 to data
        if child_idx == 0:
            return item[column]
        # For children, we have to fake some columns
        else:
            if column == 0 or column == 4:
                return "" # Fake empty text
            else:
                return item[column-1] # map 1, 2, 3 to 0, 1, 2.

    def on_iter_next(self, iter_):
        """
            Get the next sibling of the item pointed by iter_
        """
        parent_idx, child_idx = iter_
        # For parents, point to the next parent
        if child_idx == 0:
            next_parent_idx = parent_idx + 1
            if next_parent_idx < len(self.data):
                return (next_parent_idx, 0)
            else:
                return None
        # For children, get next tuple in the list
        else:
            next_child_idx = child_idx + 1
            if next_child_idx < len(self.data[parent_idx]):
                return (parent_idx, next_child_idx)
            else:
                return None

    def on_iter_has_child(self, iter_):
        """
            Tells if the row referenced by iter_ has children
        """
        parent_idx, child_idx = iter_
        if child_idx == 0 and len(self.data[parent_idx]) > 1:
            return True
        else:
            return False

    def on_iter_children(self, iter_):
        """
            Return a row reference to the first child row of the row specified
            by iter_. If iter_ is None, a reference to the first top level row
            is returned. If there is no child row None is returned.
        """
        if iter_ is None:
            return (0, 0)
        parent_idx, child_idx = iter_
        if self.on_iter_has_child(iter_):
            return (parent_idx, 1)
        else:
            return None

    def on_iter_n_children(self, iter_):
        """
            Return the number of child rows that the row specified by iter_
            has. If iter_ is None, the number of top level rows is returned.
        """
        if iter_ is None:
            return len(self.data)
        else:
            parent_idx, child_idx = iter_
            if child_idx == 0:
                return len(self.data[parent_idx]) - 1
            else:
                return 0

    def on_iter_nth_child(self, iter_, n):
        """
            Return a row reference to the nth child row of the row specified by
            iter_. If iter_ is None, a reference to the nth top level row is
            returned.
        """
        if iter_ is None:
            if n < len(self.data):
                return (n, 0)
            else:
                return None
        else:
            parent_idx, child_idx = iter_
            if child_idx == 0:
                if n+1 < len(self.data[parent_idx]):
                    return (parent_idx, n+1)
                else:
                    return None
            else:
                return None

    def on_iter_parent(self, iter_):
        """
            Get a reference to the parent
        """
        parent_idx, child_idx = iter_
        if child_idx == 0:
            return None
        else:
            return (parent_idx, 0)

class BasicTreeViewExample:

    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Basic TreeView Example")
        self.window.set_size_request(200, 200)
        self.window.connect("delete_event", self.delete_event)
        # Create the model with data in it
        self.model = MyTreeModel(data)
        self.treeview = gtk.TreeView(self.model)
        for i in range(5):
            tvcolumn = gtk.TreeViewColumn('Column %s' % (i))
            self.treeview.append_column(tvcolumn)
            cell = gtk.CellRendererText()
            tvcolumn.pack_start(cell, True)
            tvcolumn.add_attribute(cell, 'text', i)
        self.window.add(self.treeview)
        self.window.show_all()

def main():
    gtk.main()

if __name__ == "__main__":
    tvexample = BasicTreeViewExample()
    main()

And the result:

Custom GenericTreeModel

Faking headers in cells

Let's now add some kind of title in each cell using the model to generate the desired data. Full code is here.

class MyTreeModel(gtk.GenericTreeModel):

    # The columns exposed by the model to the view
    column_types = (str, str, str, str, str)
    # Column headers
    parent_headers = ("P.Col 1", "P.Col 2", "P.Col 3", "P.Col 4", "P.Col 5")
    child_headers = ("C.Col 1", "C.Col 2", "C.Col 3")

    ...

    def on_get_value(self, iter_, column):
        """
            This is where the view asks for values. This is thus where we
            start mapping our data model to a fake table to present to the view
        """
        parent_idx, child_idx = iter_
        item = self.data[parent_idx][child_idx]
        # For parents, map columns 1:1 to data
        if child_idx == 0:
            return self.markup(item[column], column, False)
        # For children, we have to fake some columns
        else:
            if column == 0 or column == 4:
                return "" # Fake empty text
            else:
                # map 1, 2, 3 to 0, 1, 2.
                return self.markup(item[column-1], column-1, True)

    def markup(self, text, column, is_child):
        """
            Produce a markup for a cell with a title and a text
        """
        headers = self.child_headers if is_child else self.parent_headers
        title = headers[column]
        return "<b>%s</b>\n%s"%(title, text)

    ...

class BasicTreeViewExample:

    def __init__(self):
        ...
        self.treeview = gtk.TreeView(self.model)
        self.treeview.set_headers_visible(False)
        for i in range(5):
            ...
            tvcolumn.pack_start(cell, True)
            tvcolumn.add_attribute(cell, 'markup', i)

...

And the result:

GenericTreeModel faking headers

Using set_cell_data_func or TreeModelFilter

Provided that you manage to fit your data into a ListStore or TreeStore, that is to say you find a trick so that parents and children share the same amount and type of columns, you can then manipulate data using either a GtkTreeCellDataFunc or a GtkTreeModelFilter.

PyGTK documentation provides examplefor Cell Data Functions and Tree Model Filters.

Adding column headers using these concepts for example maybe easier than creating a full custom model.

Here is the code using TreeCellDataFunc. Note how the data input has been formatted so that children and parents have the same amount of data. This is a condition to be able to use GtkTreeStore.

import pygtk
pygtk.require('2.0')
import gtk

data = [
    [('val_a1', 'val_b1', 'val_c1', 'val_d1', 'val_e1'), ('', 'val_x1', 'val_y1', 'val_z1', ''), ('', 'val_x2', 'val_y2', 'val_z2', '')],
    [('val_a2', 'val_b2', 'val_c2', 'val_d2', 'val_e2'), ('', 'val_x3', 'val_y3', 'val_z3', '')],
    [('val_a3', 'val_b3', 'val_c3', 'val_d3', 'val_e3')],
    [('val_a4', 'val_b4', 'val_c4', 'val_d4', 'val_e4'), ('', 'val_x4', 'val_y4', 'val_z4', ''), ('', 'val_x5', 'val_y5', 'val_z5', '')],
]

class BasicTreeViewExample:

    parent_headers = ("P.Col 1", "P.Col 2", "P.Col 3", "P.Col 4", "P.Col 5")
    child_headers = ("C.Col 1", "C.Col 2", "C.Col 3")

    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Basic TreeView Example")
        self.window.set_size_request(200, 200)
        self.window.connect("delete_event", self.delete_event)
        self.treestore = gtk.TreeStore(str, str, str, str, str)
        for detail in data:
            for index, elem in enumerate(detail):
                if index == 0:
                    piter = self.treestore.append(None, elem)
                else:
                    self.treestore.append(piter, elem)

        self.treeview = gtk.TreeView(self.treestore)
        for i in range(5):
            tvcolumn = gtk.TreeViewColumn('Column %s' % (i))
            self.treeview.append_column(tvcolumn)
            cell = gtk.CellRendererText()
            tvcolumn.pack_start(cell, True)
            # Delegate data fetching to callback
            tvcolumn.set_cell_data_func(cell, self.cell_add_header, i)
        self.window.add(self.treeview)
        self.window.show_all()

    def cell_add_header(self, treeviewcolumn, cell, model, iter_, column):
        text = model.get_value(iter_, column)
        if model.iter_parent(iter_) is None:
            # This is a parent
            title = self.parent_headers[column]
            markup = "<b>%s</b>\n%s"%(title, text)
        else:
            # We have a child
            if column == 0 or column == 4:
                # Cell is not used by child, leave it empty
                markup = ""
            else:
                title = self.child_headers[column-1]
                markup = "<b>%s</b>\n%s"%(title, text)
        cell.set_property('markup', markup)

def main():
    gtk.main()

if __name__ == "__main__":
    tvexample = BasicTreeViewExample()
    main()

GtkTreeModelFilter leads to pretty much the same thing. The result is the same than in Faking headers in cells (except that I forgot to set the headers invisible):

Faking headers with CellDataFunc

I hope this helped you, and the others that will have the same kind of question!

Upvotes: 8

Related Questions