TreeCtrl Traversal

Saketh Bhamidipati:

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.

Hi Saketh,

For the ComboTreeBox I was working on (see a previous message of mine, few days ago), I needed easier navigation of a tree. In particular I wanted to be able to find the next 'lower' item, whether it was a sibling or a child. Also, I wanted to be able to find the next 'higher' item, whether it would be a sibling or the parent or the child of a sibling. So I extended TreeCtrl a bit to provide those methods. Maybe they will help in your project?

Cheers, Frank

tree.py (3.71 KB)

These methods are exactly what I was looking for. I’m going to try to implement them in my application.

···

On 6/16/06, Frank Niessink frank@niessink.com wrote:

Saketh Bhamidipati:

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.

Hi Saketh,

For the ComboTreeBox I was working on (see a previous message of mine,
few days ago), I needed easier navigation of a tree. In particular I

wanted to be able to find the next ‘lower’ item, whether it was a
sibling or a child. Also, I wanted to be able to find the next ‘higher’
item, whether it would be a sibling or the parent or the child of a
sibling. So I extended TreeCtrl a bit to provide those methods. Maybe

they will help in your project?

Cheers, Frank


To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Saketh Bhamidipati:

def OnShiftDown(self, e):
        old = self.tree.GetSelection()
        self.tree.AppendItem(old, self.GetNextItem (old))
        self.DeleteNode(old)

First, maybe this is nothing, but why are you calling AppendItem on self.tree and GetNextItem on self?

Second, the documentation for TreeCtrl.AppendItem says:
wxTreeItemId AppendItem(const wxTreeItemId& parent, const wxString& text, int image = -1, int selImage = -1, wxTreeItemData* data = NULL)

but you are providing two TreeItemId's to AppendItem...

First, if a node has grandparents (Grandparent -> parent -> node), I can't shift it so that it becomes Grandparent -> node -> parent, regardless of whether I use either down or up. This is a predicament that I faced before I used the tree.py code as well.

It's hard to give suggestions without seeing all code.

Cheers, Frank

Saketh Bhamidipati:

def OnShiftDown(self, e):
        old = self.tree.GetSelection()
        oldtext = self.tree.GetItemText(old)
        olddata = self.tree.GetItemData(old)
        self.tree.AppendItem(self.GetPreviousItem(old), text = oldtext, data = olddata)
        self.DeleteNode(old)

I expect your method currently behaves like this:
root <- GetPreviousItem(item1)
    item1 <- selected

OnShiftDown will append an new item to the root item and then delete item1, leaving the tree essentially unchanged.

root
    item1 <- GetPreviousItem(item2)
    item2 <- selected

OnShiftDown will append a new item2 as child to item1

root
    item1
       item1.1 <- GetPreviousItem(item2)
    item2 <- selected

OnShiftDown will append a new item2 as a child of item1.1

root
    item1 <- GetPreviousItem(item1)
       item1.1 <- selected

OnShiftDown will append a new item1.1 as child of item1, leaving the tree essentially unchanged.

I think you may want to check whether GetPreviousItem(old) == GetItemParent(old) and do something different if that's the case.

Cheers, Frank

This may sound foolish, but I just realized that my application needs no support for grandchildren. It only goes down one level beyond the root node. Thus, my OnShiftDown and OnShiftUp code looks like this now:

def OnShiftDown(self, e):
old = self.tree.GetSelection()
target = self.tree.GetNextSibling(old)

    if not target.IsOk():
        return
   
    new = self.tree.InsertItem(parent = target, idPrevious = old, text = self.tree.GetItemText(old), data = self.tree.GetItemData(old))
    self.tree.SelectItem(new)
    self.DeleteNode(old)
   
def OnShiftUp(self, e):
    old = self.tree.GetSelection()
    target = self.tree.GetItemParent(old)

    if not target.IsOk():
        return
   
    new = self.tree.InsertItem

(parent = target, idPrevious = old, text = self.tree.GetItemText(old), data = self.tree.GetItemData(old))
self.tree.SelectItem(new)
self.DeleteNode(old)

All that I want it to do is shift above and below siblings. For example, if “Top” is above “Middle” is above “Bottom”, then two successive OnShiftDowns should put Top below Bottom. However, this is not working so well. I am currently trying to resolve this issue, because right now it just makes the node the sibling’s child.

···

On 6/19/06, Frank Niessink frank@niessink.com wrote:

Saketh Bhamidipati:

def OnShiftDown(self, e):
old = self.tree.GetSelection()
oldtext = self.tree.GetItemText(old)
olddata = self.tree.GetItemData(old)
self.tree.AppendItem(self.GetPreviousItem(old), text = oldtext,
data = olddata)
self.DeleteNode(old)

I expect your method currently behaves like this:
root ← GetPreviousItem(item1)

item1 <- selected

OnShiftDown will append an new item to the root item and then delete
item1, leaving the tree essentially unchanged.

root
item1 ← GetPreviousItem(item2)
item2 ← selected

OnShiftDown will append a new item2 as child to item1

root
item1
item1.1 ← GetPreviousItem(item2)
item2 ← selected

OnShiftDown will append a new item2 as a child of item1.1

root
item1 ← GetPreviousItem(item1)
item1.1 ← selected

OnShiftDown will append a new item1.1 as child of item1, leaving the
tree essentially unchanged.

I think you may want to check whether GetPreviousItem(old) ==

GetItemParent(old) and do something different if that’s the case.

Cheers, Frank


To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org