TreeCtrl: Traversal Across Siblings

I am having issues with traversing across siblings in a TreeCtrl. For example, pretend there is a tree:

Root

  • Cat
  • Dog
  • Lion

I want an operation OnShiftDown, applied to Cat once, make the tree look like this, with Cat below Dog:

Root

  • Dog
  • Cat
  • Lion

I know how to make OnShiftDown make the tree look like this, with Cat as Dog’s child, but it’s not what I want:

Root

  • Dog
    – Cat
  • Lion

I am unable to make a node simply cross its sibling without becoming the sibling’s child. My complete logic code is attached, but the methods in question are as follows:

def OnShiftDown(self, e):
“”" Bound to Ctrl-D “”"
old = self.tree.GetSelection()
target = self.tree.GetItemParent(old)

    if not target.IsOk():
        return
   
    new = self.tree.AppendItem(parent = target, text = self.tree.GetItemText(old), data = self.tree.GetItemData(old))
    self.tree.SelectItem

(new)
self.DeleteNode(old)

def OnShiftUp(self, e):
    """ Bound to Ctrl-U """
    old = self.tree.GetSelection()
    target = self.tree.GetItemParent

(old)

    if not target.IsOk():
        return
   
    new = self.tree.AppendItem(parent = target, text = self.tree.GetItemText(old), data = self.tree.GetItemData(old))
    self.tree.SelectItem

(new)
self.DeleteNode(old)

I am unable to find any method that appends a node at the same level without making it the child of another node in the level. I just want to move nodes up and down, without changing their parents. Nodes in my tree (except for the root node) should not have children. This is quite an annoying problem, because making the node a child of another node is simple, but leaving it at sibling level is complicated - but I am probably missing something.

Thanks in advance,
Saketh

NotalonEditor.py (2.49 KB)

Saketh Bhamidipati wrote:

I am unable to make a node simply cross its sibling without becoming the sibling's child. My complete logic code is attached, but the methods in question are as follows:

    def OnShiftDown(self, e):
        """ Bound to Ctrl-D """
        old = self.tree.GetSelection()
        target = self.tree.GetItemParent(old)

        if not target.IsOk():
            return
               new = self.tree.AppendItem(parent = target, text = self.tree.GetItemText(old), data = self.tree.GetItemData(old))
        self.tree.SelectItem (new)
        self.DeleteNode(old)

It looks to me like that should work, but only if you are wanting to move the item to the end of the parent node's children. If you are just wanting it to move down one item then something like this should work:

     def OnShiftDown(self, e):
         old = self.tree.GetSelection()
         next = self.tree.GetNextSibling(old)
         if not next.IsOk():
             return
         new = self.tree.InsertItem(
             self.tree.GetItemParent(old),
             next,
             self.tree.GetItemText(old),
             self.tree.GetItemImage(old),
             self.tree.GetItemImage(old, wx.TreeItemIcon_Selected))
         if self.tree.GetItemPyData():
             self.tree.SetItemPyData(self.tree.GetItemPyData())
         self.tree.Delete(old)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

I have already written the logical framework for the TreeCtrl-TextCtrl interface via the following two functions:
def SyncStacks(stack, nodetext):

"""
Synchronizes a stack (list) and a dictionary so that any deleted items in the
stack, which are keys in the dictionary, are deleted in the dictionary, and
any added items in the stack, which are not already in the dictionary, are

instantiated as keys in the dictionary with no corresponding text.

In Notalon, the stack contains the result of GetItemText() of every node. Since
it is a list, it keeps all of the items in order, unlike the dict. However, the

stack cannot map its items to the values in the TextCtrl; this is what the dict
is for. So the stack will contain the items' texts, while the dict will contain
the stack items as keys, and the TextCtrl's corresponding texts as values.

The calls to SyncStacks must occur in OnSelChanged, OnAddNode, and OnDeleteNode.

The end result is that the user can click on a node in the TreeCtrl, and the
text at the bottom changes accordingly.
"""
for item in stack:
    if item not in nodetext.keys():     # If it's in the stack, but not the dict
        nodetext[item] = ""             # Instantiate the key with no text

       
for item in nodetext.keys():
    if item not in stack:   # If it's in the dict, but not the stack
        del nodetext[item]  # Delete the item in the dict

def GetOrderedText(stack, nodetext):

"""
Returns the complete set of the dictionary's values in the order of the stack.
"""
text = ""   # Holds all of the text
for item in stack:

    text += (nodetext[item] + '\n')

return text

There will be a stack (list) and a dictionary that, combined with these two methods, mimic an ordered associative array. At least, I hope they will.

···

On 6/21/06, Robin Dunn robin@alldunn.com wrote:

Saketh Bhamidipati wrote:

Here’s another simpler problem. I am trying to associate each node on
the tree with a String that will be displayed in a TextCtrl. However, I
am having trouble deciding what data structure to use to map the nodes

to the strings. Although a dictionary seemed the best idea, I was not
sure if there is a better solution. If you’ve used Leo, then you
understand what I mean by associating the node with a String in the

TextCtrl.

Are you already using the item’s data object for something else? If not
then you can use that to hold the string, or perhaps just the key to a
dictionary value, whichever makes the most sense to you.


Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!


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

DASPRiD wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

is there any possibility to set padding for a wxTextCtrl?

I don't think so.

Google and the
documentation didn't tell me any, but it looks realy ugly if the text in
a multiline text control touches the borders.

wx.stc has a property for it, but for the native control it should be using platform defaults.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!