[pygtk] Editable tree and CellRenderToggle

Tony Nelson tonynelson at georgeanelson.com
Fri Dec 9 13:32:51 WST 2005


At 11:42 AM +0100 12/8/05, nicoe at no-log.org wrote:
>Hello,
>
>I'm stuck with a problem, where I'm using Tab, Shift Tab, Up and Down
>to navigate into an editable tree (I use the 'editing-started' signal
>to bind the key events to a function of my customized ViewTree).
>Evrything work flawlessly until I tried to put a toggle into a cell.
>
>When entering the cell the checkbox is toggled  and the navigation
>stops.
>
>What I would like to do, is:
>
>    - entering the cell
>    - pressing Space to (de)activate the toggle
>    - and the continue to move using my keys.
>
>Is this possible ? Or should I run for the easiest way to do it : a
>TextEntry with either 0 or 1 in it ?

I haven't used TreeView before.  I don't see any interface intended to let
one get access to the actual widgets used (assuming they actually /use/ a
button to show their buttons).  I'm also not quite sure how you are
"binding" to your TreeView, or how it is "customized".

Using the tree_view.py demo, I find that the spacebar already taggles the
activatable widgets (the demo sets some to not be), and the active column
is set by the right and left arrow keys.  Unfortunately, the keyboard focus
and sensitivity aren't shown, so it is rather mysterious to use.

What I tried that seems to work, is to connect to the TreeView widget's
keypress signal.  I was able to add expanding and hiding subtrees, take
control of the column selection (still without any visible indication), and
toggle the "selected" cell.  I couldn't find a way to respect the cell's
sensitivity, the way the built-in stuff does.

I connected the following to TreeView's keypress in the tree_view.py demo:

    def on_tree_keypress(self, widget, event):
        if event.type == gtk.gdk.KEY_PRESS:
            print hex(event.keyval)
            if event.keyval == 0xFF53:      # right arrow
                sel = widget.get_selection()
                model, rowlist = sel.get_selected_rows()
                if rowlist:
                    ri = model.get_iter(rowlist[0])
                    if model.iter_has_child(ri):
                        widget.expand_row(rowlist[0], False)
                    else:
                        path, col = widget.get_cursor()
                        if path and col:
                            cols = widget.get_columns()
                            ci = cols.index(col)
                            ci += 1
                            if ci < len(cols):
                                widget.set_cursor(path, cols[ci])
                                print ci
                    return True
            elif event.keyval == 0xFF51:    # left arrow
                sel = widget.get_selection()
                model, rowlist = sel.get_selected_rows()
                if rowlist:
                    ri = model.get_iter(rowlist[0])
                    if model.iter_has_child(ri):
                        widget.collapse_row(rowlist[0])
                    else:
                        path, col = widget.get_cursor()
                        if path and col:
                            cols = widget.get_columns()
                            ci = cols.index(col)
                            ci -= 1
                            if ci > 0:
                                widget.set_cursor(path, cols[ci])
                                print ci
                    return True
            elif event.keyval == 0x0020:
                sel = widget.get_selection()
                model, rowlist = sel.get_selected_rows()
                if rowlist:
                    rit = model.get_iter(rowlist[0])
                    if not model.iter_has_child(rit):
                        #ci =
widget.get_columns().index(widget.get_cursor()[1])
                        #model.set_value(rit, ci, not model.get_value(rit, ci))
                        return False
        else:
            print 'other event'

This code isn't being very careful.  I couldn't be bothered to get the
symbolic constants; I just typed what printed out.  I don't check which of
the column headers or the data area is active.
____________________________________________________________________
TonyN.:'                       <mailto:tonynelson at georgeanelson.com>
      '                              <http://www.georgeanelson.com/>


More information about the pygtk mailing list