from gi.repository import Gtk, GObject

TEST = 4


class Model(GObject.GObject, Gtk.TreeModel):

    __gtype_name__ = 'Model'

    values = [[0], [1], [2], [3]]

    def __init__(self):
        super(Model, self).__init__()

    def append(self, val):
        self.values.append(val)

    def do_get_n_columns(self):
        print 'do_get_n_columns'
        return 1

    def do_get_column_type(self, idx):
        print 'do_get_column_type'
        return str

    def do_get_iter_first(self):
        print "do_get_iter_first"
        iter_ = Gtk.TreeIter()
        iter_.stamp = 1
        iter_.user_data = (0,)
        return True, iter_

    def do_get_iter(self, path):
        print 'do_get_iter', path
        iter_ = Gtk.TreeIter()
        iter_.stamp = 1
        t = tuple(path.get_indices())
        iter_.user_data = t
        return True, iter_

    def do_get_path(self, iter_):
        print 'do_get_path', iter_.stamp, iter_.user_data
        if iter_.stamp != 1:
            return Gtk.TreePath()
        path = Gtk.TreePath(iter_.user_data)
        return path

    def do_get_value(self, iter_, col_idx):
        print "do_get_value"
        return True, self.values[iter_.user_data[0]][0]

    def do_iter_next(self, iter_):
        print "do_iter_next", iter_.stamp, iter_.user_data
        if not iter_.user_data:
            iter_.stamp = 1
            iter_.user_data = (0,)
            return True, iter_
        if iter_.user_data[0] < len(self.values) - 1:
            iter_.user_data = (iter_.user_data[0] + 1,)
            return True, iter_
        else:
            return False, None

    def do_iter_previous(self, iter_):
        print 'do_iter_previous'
        p = iter_.user_data
        if p[0] == 0:
            iter_.stamp = 0
            return False
        iter_.user_data = (p[0] - 1,)
        return True

    def do_iter_children(self, parent):
        print 'do_iter_children'
        return False, None

    def do_iter_has_child(self, iter_):
        print 'do_iter_has_child'
        return False

    def do_iter_n_children(self, iter_):
        print 'do_iter_n_children'
        if iter_ is None:
            return len(self.values)
        return 0

    def do_iter_nth_child(self, parent, n):
        print 'do_iter_nth_child'
        if parent is None:
            if 0 <= n < len(self.values):
                self.my_iter = (n,)
                return True, self.my_iter
            else:
                self.my_iter = None
                return False, self.my_iter
        else:
            self.my_iter = None
            return False, self.my_iter

    def do_iter_parent(self, iter_, child):
        print 'do_iter_parent'
        iter_.stamp = 0
        return False


def add_line(button, store):
    global TEST
    store.append([str(TEST)])
    TEST += 1

def print_store(button, store):
    print store

def show(column, renderer, store, treeiter, *args):
    renderer.set_property('text', 'Test')
    #if treeiter.user_data:
        #renderer.set_property('text', store.values[treeiter.user_data[0]][0])

def main():
    store = Model()
    tv = Gtk.TreeView()
    tv.set_model(store)
    cell = Gtk.CellRendererText()
    col = Gtk.TreeViewColumn('Text', cell)
    col.set_cell_data_func(cell, show)
    tv.append_column(col)

    win = Gtk.Dialog('Test')
    win.set_size_request(300, 200)
    win.connect('destroy', Gtk.main_quit)
    win.vbox.pack_start(tv, True, True, 0)
    button_add = Gtk.Button('Add')
    button_add.connect('clicked', add_line, store)
    win.action_area.add(button_add)
    button_print = Gtk.Button('Print')
    button_print.connect('clicked', print_store, store)
    win.action_area.add(button_print)
    win.show_all()


if __name__ == '__main__':
    main()
    Gtk.main()
