Re: [pygtk] swap rows in TreeView
Thanks a lot for the extensive reply! Its working well now! Johannes * Doug Quale <[EMAIL PROTECTED]> [2003-12-20 12:39]: > Johannes Jordens <[EMAIL PROTECTED]> writes: > > > Question: How do I get an iter to the row preceding the selected one? > > alternatively, how do I implement moving a row upwards? > > In a ListStore, you can do something like this: > > path = listiter.get_path() > row = path[0] > assert row > 0 > prev_row = row - 1 > prev_iter = listmodel.get_iter(prev_row) > > This makes use of the fact that in a ListStore the path is a 1-tuple > that gives the row number. > > Since we typically want to operate on the selected row, I use an up > button 'clicked' handler that looks something like this: > > def on_up_button__clicked(button, my_model): > """Move the selected row up. """ > selection = my_model.get_selection() > model, selected = selection.get_selected() > assert not selected is None > path = model.get_path(selected) > row = path[0] > assert row > 0 > this = model[row][0] > prev = model[row-1][0] > model[row-1][0] = this > model[row][0] = prev > selection.select_path(row-1) > > (Here I have passed the model as an extra argument to the handler. > Use any convenient means to get the treeview selection.) The code > works for a 1-column liststore. You need to make the obvious > modifications if the liststore has more columns. [We would like to > use the ListStore.move_before() and ListStore.move_after() methods, > but since they are gtk+ 2.2 API additions they aren't wrapped yet by > pygtk.] I make liberal use of the pygtk binding's Python list-like > API; you can use model.get_value() and model.set_value() if you > prefer. (This is explained in the excellent pygtk FAQ.) > > The assertions check to make sure that the move up makes sense since I > use a 'changed' handler on the selection to make the up button > insensitive when the selected row can't be moved up. If this function > might be called when the move can't be made you can instead simply > return rather than causing an assertion failure. In case you're > interested, the selection 'changed' handler method that I use looks > like this: > > def selection_changed(self, selection): > """Set button sensitivities after treeview selection changes. """ > model, selected = selection.get_selected() > has_selection = bool(selected) > self.remove_button.set_sensitive(has_selection) > path = has_selection and model.get_path(selected) > self.up_button.set_sensitive(path and path[0] > 0) > self.down_button.set_sensitive(path and path[0] < len(model)-1) > > Here again pygtk's nifty Python list API for ListStore helps by > letting us easily find the number of rows in the model with a simple > len(model). > ___ > pygtk mailing list [EMAIL PROTECTED] > http://www.daa.com.au/mailman/listinfo/pygtk > Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/ -- Those who can't write, write manuals. signature.asc Description: Digital signature ___ pygtk mailing list [EMAIL PROTECTED] http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
[pygtk] Re: swap rows in TreeView
Johannes Jordens writes: > I'd like to implement moving a row in a TreeView, > specifically in a ListStore, up/down via two buttons. I have implemented > moving the row downwards using the iter_next(iter) function so that > I have an iter on the row below the one currently selected. > But: there is no corresponding function to iter_next if one wants to > have an iter on the preceding row (at least I couldn't find one). > So I figured that there must be some function to turn an iter into a > path I think you need the .get_path(iter) method of TreeModel (just to make sure: iter_next and similar methods don't move a row, you have to do that by removing and reinserting the row. But I guess you already know that :) ) -- Abel Daniel ___ pygtk mailing list [EMAIL PROTECTED] http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
Re: [pygtk] swap rows in TreeView
Johannes Jordens <[EMAIL PROTECTED]> writes: > Question: How do I get an iter to the row preceding the selected one? > alternatively, how do I implement moving a row upwards? In a ListStore, you can do something like this: path = listiter.get_path() row = path[0] assert row > 0 prev_row = row - 1 prev_iter = listmodel.get_iter(prev_row) This makes use of the fact that in a ListStore the path is a 1-tuple that gives the row number. Since we typically want to operate on the selected row, I use an up button 'clicked' handler that looks something like this: def on_up_button__clicked(button, my_model): """Move the selected row up. """ selection = my_model.get_selection() model, selected = selection.get_selected() assert not selected is None path = model.get_path(selected) row = path[0] assert row > 0 this = model[row][0] prev = model[row-1][0] model[row-1][0] = this model[row][0] = prev selection.select_path(row-1) (Here I have passed the model as an extra argument to the handler. Use any convenient means to get the treeview selection.) The code works for a 1-column liststore. You need to make the obvious modifications if the liststore has more columns. [We would like to use the ListStore.move_before() and ListStore.move_after() methods, but since they are gtk+ 2.2 API additions they aren't wrapped yet by pygtk.] I make liberal use of the pygtk binding's Python list-like API; you can use model.get_value() and model.set_value() if you prefer. (This is explained in the excellent pygtk FAQ.) The assertions check to make sure that the move up makes sense since I use a 'changed' handler on the selection to make the up button insensitive when the selected row can't be moved up. If this function might be called when the move can't be made you can instead simply return rather than causing an assertion failure. In case you're interested, the selection 'changed' handler method that I use looks like this: def selection_changed(self, selection): """Set button sensitivities after treeview selection changes. """ model, selected = selection.get_selected() has_selection = bool(selected) self.remove_button.set_sensitive(has_selection) path = has_selection and model.get_path(selected) self.up_button.set_sensitive(path and path[0] > 0) self.down_button.set_sensitive(path and path[0] < len(model)-1) Here again pygtk's nifty Python list API for ListStore helps by letting us easily find the number of rows in the model with a simple len(model). ___ pygtk mailing list [EMAIL PROTECTED] http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/