[pygtk] columns: how to stop the sorting?
John Finlay
finlay at moeraki.com
Mon May 24 00:44:20 WST 2004
Riccardo Galli wrote:
>Hi all.
>I made a function which let remove a parent-row in a treeview without lose his children.
>
>the normal behaviour would be
>
>aaaa
>bbbb
> cccc
>dddd
>
>if I delete the second row
>
>aaaa
>cccc
>dddd
>
>The code work while there's no sorting.
>If there's sorting on some column, the behaviour is undefined.
>
>I would like to stop the sorting before launching my function and start it again when my function exit, but I can't find how to do it.
>
>I tried with set model.set_sort_colum_id(-1,0) , but doesn't work
>
>I attach a simple app demonstrating what appens.
>
>
I think you should use a TreeModelSort. I hacked up your example to
illustrate.
John
-------------- next part --------------
#!/usr/bin/env python
import gtk
class Window(gtk.Window):
def __init__(self,*args):
super(Window,self).__init__(*args)
win=self
win.connect('delete-event', lambda w,e: gtk.main_quit())
win.set_size_request(200,200)
vbox=gtk.VBox()
win.add(vbox)
self.cmodel=gtk.TreeStore(str)
self.model = gtk.TreeModelSort(self.cmodel)
iter1=self.cmodel.append(None, ['bbb'])
iter2=self.cmodel.append(None, ['ccc'])
iter3=self.cmodel.append(iter2, ['aaa'])
iter4=self.cmodel.append(None, ['ddd'])
self.tree = gtk.TreeView(self.model)
cell = gtk.CellRendererText()
column = gtk.TreeViewColumn("tuples", cell, text=0)
self.tree.append_column(column)
vbox.pack_start(self.tree,1,10)
self.tree.expand_all()
######################
######################
"""if this line is commented, the alghorithm works"""
self.model.set_sort_column_id(0,0)
######################
######################
btn=gtk.Button('GO!')
btn.connect('clicked',self.on_btn_clicked)
vbox.pack_start(btn,0,0,0)
def on_btn_clicked(self,btn):
path=self.tree.get_cursor()[0]
print path
path = self.model.convert_path_to_child_path(path)
self.delete_row_saving_children(path)
def copy_siblings_with_children(self,iter,cpAfterWho,asChild=False):
if not iter: return
#before calling functions which invalidate iters adding rows
#I must save paths: so then I can regenerate iters
pathIter=self.cmodel.get_path(iter)
if asChild:
cpHere=self.cmodel.append(cpAfterWho)
else:
print 'cpAfterWho',self.cmodel.get_path(cpAfterWho)
cpHere=self.cmodel.insert_after(None,cpAfterWho)
print 'pathCphere',self.cmodel.get_path(cpHere)
iter=self.cmodel.get_iter(pathIter)
for i in range(self.cmodel.get_n_columns()):
self.cmodel.set_value(cpHere,i,self.cmodel.get_value(iter,i))
pathCpHere=self.cmodel.get_path(cpHere)
if self.cmodel.iter_has_child(iter):
self.copy_siblings_with_children(self.cmodel.iter_children(iter),cpHere,asChild=True)
cpHere=self.cmodel.get_iter(pathCpHere)
self.copy_siblings_with_children(self.cmodel.iter_next(iter),cpHere)
def delete_row_saving_children(self,path):
iter=self.cmodel.get_iter(path)
print path, iter
if self.cmodel.iter_has_child(iter):
self.copy_siblings_with_children(self.cmodel.iter_children(iter),iter)
#previous function can have changed iters
iter=self.cmodel.get_iter(path)
self.cmodel.remove(iter)
def run(self):
self.show_all()
gtk.main()
if __name__ == '__main__':
win=Window()
win.run()
More information about the pygtk
mailing list