Пример #1
0
 def on_delete_item(self, event):
     item = event.GetItem()
     editor = self._GetItemData( item )
     if DEBUG:
         print("on_delete_item", utilities.hx(item), editor, editor and editor.item or None)
     if editor is not None and editor.item is item:
         editor.item = None
Пример #2
0
 def add2(self, child, parent, index, item=None):
     "insert an item for child into the list of parent's items; optionally re-use old item"
     image = self.images.get( child._get_tree_image(), -1)
     label = child._get_tree_label()
     if item is None:
         if index is None:
             item = child.item = self.AppendItem(parent.item, label, image)
         else:
             item = child.item = compat.wx_Tree_InsertItemBefore(self, parent.item, index, label, image)
     else:
         # re-use
         child.item = item
         self.refresh(child)
     self._SetItemData(item, child)
     if self.auto_expand:
         self.Expand(parent.item)
     if DEBUG:
         print("added item", utilities.hx(item), child, child.item)
     return item
Пример #3
0
    def _build_children(self, editor, item, recursive=True):
        # XXX a better algorithm would be nice
        # currently it's checked from the start and from the end how many are matching; all inbetween are replaced
        if DEBUG: print("_build_children", editor)
        children = editor.get_all_children()
        items = self._get_children_items(editor.item)
        if DEBUG: print("children", children)
        if DEBUG: print("items", items)
        item_editors = []
        for child_item in items:
            child = self._GetItemData(child_item)
            if child is not None and (not children or not child in children):
                self._SetItemData(child_item, None)
                if child.item is child_item:
                    if DEBUG: print("removed child.item", utilities.hx(child), utilities.hx(child.item))
                    child.item = None  # is probably None already
                item_editors.append(None)
            else:
                item_editors.append(child)
        if DEBUG: print("item_editors", item_editors)
        if DEBUG: print()
        match_beginning = 0
        for c,child in enumerate(children):
            if c<len(item_editors) and item_editors[c] is child:
                match_beginning = c+1
            else:
                break
        match_end = 0
        c = -1
        while match_beginning+match_end<len(children):
            if match_end<len(item_editors) and item_editors[c] is children[c]:
                match_end += 1
            else:
                break
            c -= 1

        if len(children) > len(item_editors):
            # insert or add items, right after match_beginning
            for n in range( len(children) - len(item_editors) ):
                index = match_beginning + n
                child = children[index]
                item = self.add2(child, parent=editor, index=index)
                items.insert(index, item)
        elif len(children) < len(item_editors):
            # remove items, right after match_beginning
            for n in range( len(item_editors) - len(children) ):
                index = match_beginning 
                child_item = items[index]
                self.Delete(child_item)
                del items[match_beginning]
        if match_beginning+match_end<len(children):
            # length matches, re-use item in the middle
            for index in range(match_beginning, len(children)-match_end):
                child = children[index]
                item = self.add2(child, parent=editor, index=index, item=items[index])
        
        if not recursive:
            # update labels and images, called e.g. when notebook pages change
            for child in children:
                self.refresh(child)
            return
        for child, item in zip(children, items):
            self._build_children(child, item)