TreeCtrl Traversal

Saketh Bhamidipati saketh.bhamidipati at gmail.com
Fri Jun 16 13:38:23 PDT 2006


I am trying to create methods to shift nodes in a tree in four directions -
up, down, left, and right, like in Leo. I have created two methods that work
to an extent. They fail in traversing across siblings and in traversing
upwards. I have not yet begun to work on the left and right methods.

Also, why is it that GetLastChild() returns a TreeItemId, but
GetFirstChild() returns a PyObject*? I had to write my own GetFirstChild()
in order to get my method to work.
def OnShiftDown(self, e):
        # TODO: Make children shiftable as well
        # Unless it's the root node, we're good to go
        if (self.tree.ItemHasChildren(self.tree.GetSelection())):
            return
        elif (self.tree.GetSelection() =3D=3D self.tree.GetRootItem()):
            print "You cannot shift the root node."
            return

        new =3D self.tree.GetSelection()
        newtext =3D self.tree.GetItemText(self.tree.GetSelection())

        # Try to get the next sibling node
        target =3D self.tree.GetNextSibling(new)

        if not target.IsOk():
            target =3D self.tree.GetNextVisible(new)

        if not target.IsOk():
            self.DeleteNode(self.tree.GetSelection())
            new =3D self.tree.PrependItem(self.tree.GetRootItem(), text =3D
newtext, data =3D self.tree.GetItemData(new))
            self.tree.SelectItem(new)
            return

        self.DeleteNode(self.tree.GetSelection())
        new =3D self.tree.AppendItem(target, text =3D newtext, data =3D
self.tree.GetItemData(new))
        self.tree.SelectItem(new)

    def GetFirstChild(self, item):
        return self.tree.GetNextVisible(item)

    def OnShiftUp(self, e):
        # TODO: Make children shiftable as well
        # Unless it's the root node, we're good to go
        if (self.tree.ItemHasChildren(self.tree.GetSelection())):
            return
        elif (self.tree.GetSelection() =3D=3D self.tree.GetRootItem()):
            print "You cannot shift the root node."
            return

        new =3D self.tree.GetSelection()
        newtext =3D self.tree.GetItemText(self.tree.GetSelection())

        newprev =3D self.tree.GetPrevVisible(new)

        # By default, it goes to the previous sibling
        target =3D self.tree.GetPrevSibling(newprev)

        if target =3D=3D self.tree.GetLastChild(self.tree.GetItemParent(new=
)):
            # But if there is no previous sibling, then go to the parent
            target =3D self.tree.GetPrevVisible(self.tree.GetItemParent
(newprev))
            return

        elif not target.IsOk():
            return

        # I don't know why this conditional works...it just does!
        # I made it through trial and error.
        elif target =3D=3D self.tree.GetRootItem():
            self.DeleteNode(self.tree.GetSelection())
            new =3D self.tree.PrependItem(self.tree.GetRootItem(), text =3D
newtext, data =3D self.tree.GetItemData(new))
            self.tree.SelectItem(new)
            return

        self.DeleteNode(self.tree.GetSelection())
        new =3D self.tree.PrependItem(target, text =3D newtext, data =3D
self.tree.GetItemData(new))
        self.tree.SelectItem(new)

It does not work in traversing upwards past a parent (i.e. it is stuck at
Root->New Root->Moving Node). It also fails to traverse downwards without
skipping across all of the siblings - this is probably because I used
AppendItem.

I was confused because I thought that once I figured out shifting down,
shifting up would be easy. But there are significant differences between the
two when TreeCtrl was written. For instance, I now have to use two
GetPrevVisibles in order to target the previous node - does the first return
the parameter node itself?

This problem has been frustrating me for several hours now, but I haven't
been able to make any headway. There is probably a simpler way of writing
the algorithm, but I'm just not seeing it.

Thanks in advance,
Saketh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200606=
16/d9afb9aa/attachment.htm


More information about the wxpython-users mailing list