#!/usr/bin/python

import time
from gi.repository import Gtk, GObject

nr_cell_data_func_calls = 0

def _cell_data_func(col, cell, model, it, user_data):
    path = model.get_path(it)
    cell.set_property('markup', model[it][0])
    global nr_cell_data_func_calls
    nr_cell_data_func_calls += 1

def timeout():
    print "delayed set_model is not so fast anymore"
    set_model()
    print 

def set_model(amount=20*1000):
    model = Gtk.ListStore(str)
    for i in range(amount):
        model.append(["%s" % i])

    now = time.time()
    treeview.set_model(model)
    print "set_model takes: ", time.time() - now
    print "nr of nr_cell_data_func_calls", nr_cell_data_func_calls 

if __name__ == "__main__":
    win = Gtk.Window()
    win.set_size_request(600, 400)
    treeview = Gtk.TreeView()
    treeview.set_fixed_height_mode(True)

    tr = Gtk.CellRendererText()

    column = Gtk.TreeViewColumn("foo", tr, text=0)
    column.set_cell_data_func(tr, _cell_data_func)
    column.set_fixed_width(200)
    column.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
    treeview.append_column(column)
    
    scroll = Gtk.ScrolledWindow()
    scroll.add(treeview)
    win.add(scroll)
    win.connect("destroy", Gtk.main_quit)
    win.show_all()

    # initial set_model is fast
    print "not visible set_model is fast"
    set_model()
    print

    GObject.timeout_add(1000, timeout)

    Gtk.main()
