Nick Bastin wrote:
Is there a pattern in wxwindows for comparing tree items? I need to find out if the previous sibling of the current node is also its' parent, and it's apparently not a simple matter of equality:
self.GetPrevSibling (item) will never equal self.GetItemParent(item)
Because the parent is not the sibling (unless you are in certain parts of the country where it is not uncommon for your sister to also be your mother...
)
Robin says that's because each function really returns a wrapper on the item, which is fine, but is there any way I can compare the wrapped items (natively)?
I took another look at the code and I do have a __cmp__ implemented for wxTreeItemId, so you can compare equality of the items with ==. (__cmp__returns -1, zero, or 1, with zero meaning that they are equal.) For example, I just did this in PyShell using my development version of wxPython, but 2.4.0.7 should work nearly the same:
>>> import wx
>>> f = wx.Frame(None, -1, "Test")
>>> tree = wx.TreeCtrl(f, -1)
>>> root = tree.AddRoot("The Root")
>>> f.Show()
1
>>> root
<wxPython.controls2.wxTreeItemIdPtr instance; proxy of C++ wxTreeItemId instance at _87dbd08_wxTreeItemId_p>
>>> i1 = tree.AppendItem(root, "Item 1")
>>> i2 = tree.AppendItem(root, "Item 2")
>>> i3 = tree.AppendItem(root, "Item 3")
>>>
>>> i1 == i2
0
>>> i1.__cmp__(i2)
1
>>> i1.__cmp__(None)
-1
>>> i1.__cmp__(i1)
0
>>> ps = tree.GetPrevSibling(i2)
>>> ps == i2
0
>>> ps == i1
1
i2's previous sibling is i1.
>>> ps = tree.GetPrevSibling(i1)
>>> ps.Ok()
0
>>> ps == root
0
i1 has no previous sibling, so an invalid item is returned. (ps.Ok() returns false.)
>>> ps = tree.GetPrevVisible(i1)
Traceback (most recent call last):
File "<input>", line 1, in ?
File "/projects/wx/wxPython/wxPython/controls2.py", line 1076, in GetPrevVisible
val = controls2c.wxTreeCtrl_GetPrevVisible(self, *_args, **_kwargs)
wxPyAssertionError: C++ assertion "wxAssertFailure" failed in ../src/generic/treectlg.cpp(1270): not implemented
It turns out that GetPreviousVisible is not implemented for the generic tree ctrl (used for wxGTK and wxMac.) You will only get this assertion exception if using a debug or hybrid build of wxPython, but since you are working in Chandler you are probably using a release build...
The obvious workaround here is to set the PyData to include unique keys, but I don't want that overhead if I can do this within wxPython.
Well, as can be seen above if the GetPreviousSibling method returns an invalid item then you know that you are at the begining of the sublist...
···
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!