[wxPython] Moving/copying a branch within wxTreeCtrl

hi,

i checked out the same problem. Following is my
solution. HTH.

    def OnBeginDrag(self, event):
        item = event.GetItem()
        if item != self.GetRootItem():
            self.draggingItem = item
            event.Allow()

        # Setzt das Element neu und verschiebt ggf Children
    def OnEndDrag(self, evt):
        itemSrc = self.draggingItem
        itemDst = evt.GetItem()
        if itemDst in self.childcount(itemSrc):
            print "Can't drag item to items children..."
            return
        if itemSrc==itemDst:
            print "Can't drag item to itself..."
            return
        self.draggingItem = None
        if not itemDst.IsOk():
            print "Can't drag to here..."
            return
        text = self.GetItemText(itemSrc)
        data = self.GetItemData(itemSrc).GetData()
        datap=wxTreeItemData()
        datap.SetData(data)
        self.Updatedb(itemSrc,itemDst)
        newitem=self.AppendItem(itemDst, text, 0, 1) # I use own bitmaps....
        self.SetItemData(newitem,datap)
        if self.ItemHasChildren(itemSrc):
            self.MoveChildren(itemSrc,newitem)
        self.Delete(itemSrc)

        # Wenn Ein Objekt verschoben wird, so werden
        # hier die Children mitgezogen
    def MoveChildren(self,parentitem,newparentitem):
        list=self.childcount(parentitem, flat=1)
        for item in list:
            data=self.GetItemData(item).GetData()
            text=self.GetItemText(item)
            datap=wxTreeItemData()
            datap.SetData(data)
            newitem=self.AppendItem(newparentitem, text, 0,1) # I use own bitmaps....
            self.SetItemData(newitem,datap)
            if self.ItemHasChildren(item):
                self.MoveChildren(item,newitem)
            self.Delete(item)

        # serves the itemids of all children of itemid, or only the first generation (flat)
        # or with the rootitem(itemid) inside the list (root)
    def childcount(self,itemid,flat=0,root=0):
        if self.ItemHasChildren(itemid):
            noc=self.GetChildrenCount(itemid, false) #Number of Children
            if root:
                a=[itemid]
            else:
                a=[]
            cookie=1
            id, cookie=self.GetFirstChild(itemid,cookie)
            a.append(id)
            while noc>1:
                id,cookie=self.GetNextChild(itemid,cookie)
                a.append(id)
                noc=noc-1
            for x in a:
                if not flat:
                    if self.ItemHasChildren(x):
                        if not x == itemid:
                            a=a+self.childcount(x)
            return a

        else:
            return []

reen

···

----
Rene Freund nur ein toter bug ist ein guter bug
rene@meder.de

i checked out the same problem. Following is my
solution. HTH.

Hi Rene,

Thanks for this code snippet! Can you turn it into a full example and put
it on the wxPyWiki in the Cookbook section?

Thanks!

···

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