From 12b19ed1ff37806459d1eed270dad7d46eed4a8b Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 11 Sep 2017 11:58:32 +0300 Subject: [PATCH] update tree if items are added or removed - fix #278 --- examples/example1/src/example1.d | 34 +++++++++++++++++++++++++++- src/dlangui/core/collections.d | 3 ++- src/dlangui/widgets/tree.d | 38 +++++++++++++++++++++++--------- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/examples/example1/src/example1.d b/examples/example1/src/example1.d index 9da248c4..5d56e5c4 100644 --- a/examples/example1/src/example1.d +++ b/examples/example1/src/example1.d @@ -580,7 +580,7 @@ extern (C) int UIAppMain(string[] args) { gbgrid.addChild(grid); line4.addChild(gbgrid); - GroupBox gbtree = new GroupBox("tree", "TreeWidget"d, Orientation.Horizontal); + GroupBox gbtree = new GroupBox("tree", "TreeWidget"d, Orientation.Vertical); auto tree = new TreeWidget("gbtree"); //tree.layoutWidth(WRAP_CONTENT).layoutHeight(FILL_PARENT); tree.maxHeight(200.pointsToPixels); @@ -606,6 +606,38 @@ extern (C) int UIAppMain(string[] args) { tree3.newChild("g3_5", "Group 3 item 5"d); tree3.newChild("g3_6", "Group 3 item 6"d); gbtree.addChild(tree); + tree.items.selectItem(tree1); + // test adding new tree items + HorizontalLayout newTreeItem = new HorizontalLayout(); + newTreeItem.layoutWidth = FILL_PARENT; + EditLine edNewTreeItem = new EditLine("newTreeItem", "new item"d); + edNewTreeItem.layoutWidth = FILL_PARENT; + Button btnAddItem = new Button("btnAddTreeItem", "Add"d); + Button btnRemoveItem = new Button("btnRemoveTreeItem", "Remove"d); + newTreeItem.addChild(edNewTreeItem); + newTreeItem.addChild(btnAddItem); + newTreeItem.addChild(btnRemoveItem); + btnAddItem.click = delegate(Widget source) { + import std.random; + dstring label = edNewTreeItem.text; + string id = "item%d".format(uniform(1000000, 9999999, rndGen)); + TreeItem item = tree.items.selectedItem; + if (item) { + Log.d("Creating new tree item ", id, " ", label); + TreeItem newItem = new TreeItem(id, label); + item.addChild(newItem); + } + return true; + }; + btnRemoveItem.click = delegate(Widget source) { + TreeItem item = tree.items.selectedItem; + if (item) { + Log.d("Removing tree item ", item.id, " ", item.text); + item.parent.removeChild(item); + } + return true; + }; + gbtree.addChild(newTreeItem); line4.addChild(gbtree); controls.addChild(line4); diff --git a/src/dlangui/core/collections.d b/src/dlangui/core/collections.d index ac3b36d0..223508d4 100644 --- a/src/dlangui/core/collections.d +++ b/src/dlangui/core/collections.d @@ -242,6 +242,7 @@ struct ObjectList(T) { protected int _count; /** returns count of items */ @property int count() const { return _count; } + alias length = count; /** get item by index */ T get(int index) { assert(index >= 0 && index < _count, "child index out of range"); @@ -280,7 +281,7 @@ struct ObjectList(T) { if (item is null) return -1; for (int i = 0; i < _count; i++) - if (_list[i] == item) + if (_list[i] is item) return i; return -1; } diff --git a/src/dlangui/widgets/tree.d b/src/dlangui/widgets/tree.d index 77832aec..27c3c639 100644 --- a/src/dlangui/widgets/tree.d +++ b/src/dlangui/widgets/tree.d @@ -253,32 +253,50 @@ class TreeItem { /// returns child by index TreeItem child(int index) { return _children.get(index); } /// adds child, returns added item - TreeItem addChild(TreeItem item) { - return _children.add(item).parent(this).level(_level + 1); + TreeItem addChild(TreeItem item, int index = -1) { + TreeItem res = _children.insert(item, index).parent(this).level(_level + 1); + root.onUpdate(res); + return res; } /// removes child, returns removed item TreeItem removeChild(int index) { + if (index < 0 || index >= _children.count) + return null; TreeItem res = _children.remove(index); - if (res !is null) + TreeItem newSelection = null; + if (res !is null) { res.parent = null; + if (root && root.selectedItem is res) { + if (index < _children.count) + newSelection = _children[index]; + else if (index > 0) + newSelection = _children[index - 1]; + else + newSelection = this; + } + } + root.selectItem(newSelection); + root.onUpdate(this); return res; } + /// removes child by reference, returns removed item + TreeItem removeChild(TreeItem child) { + TreeItem res = null; + int index = _children.indexOf(child); + return removeChild(index); + } /// removes child by ID, returns removed item TreeItem removeChild(string ID) { TreeItem res = null; int index = _children.indexOf(ID); - if (index < 0) - return null; - res = _children.remove(index); - if (res !is null) - res.parent = null; - return res; + return removeChild(index); } /// returns index of widget in child list, -1 if passed widget is not a child of this widget int childIndex(TreeItem item) { return _children.indexOf(item); } /// notify listeners protected void onUpdate(TreeItem item) { - root.onUpdate(item); + if (root) + root.onUpdate(item); } protected void toggleExpand(TreeItem item) { root.toggleExpand(item);