update tree if items are added or removed - fix #278

This commit is contained in:
Vadim Lopatin 2017-09-11 11:58:32 +03:00
parent 65df1c5195
commit 12b19ed1ff
3 changed files with 63 additions and 12 deletions

View File

@ -580,7 +580,7 @@ extern (C) int UIAppMain(string[] args) {
gbgrid.addChild(grid); gbgrid.addChild(grid);
line4.addChild(gbgrid); 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"); auto tree = new TreeWidget("gbtree");
//tree.layoutWidth(WRAP_CONTENT).layoutHeight(FILL_PARENT); //tree.layoutWidth(WRAP_CONTENT).layoutHeight(FILL_PARENT);
tree.maxHeight(200.pointsToPixels); 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_5", "Group 3 item 5"d);
tree3.newChild("g3_6", "Group 3 item 6"d); tree3.newChild("g3_6", "Group 3 item 6"d);
gbtree.addChild(tree); 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); line4.addChild(gbtree);
controls.addChild(line4); controls.addChild(line4);

View File

@ -242,6 +242,7 @@ struct ObjectList(T) {
protected int _count; protected int _count;
/** returns count of items */ /** returns count of items */
@property int count() const { return _count; } @property int count() const { return _count; }
alias length = count;
/** get item by index */ /** get item by index */
T get(int index) { T get(int index) {
assert(index >= 0 && index < _count, "child index out of range"); assert(index >= 0 && index < _count, "child index out of range");
@ -280,7 +281,7 @@ struct ObjectList(T) {
if (item is null) if (item is null)
return -1; return -1;
for (int i = 0; i < _count; i++) for (int i = 0; i < _count; i++)
if (_list[i] == item) if (_list[i] is item)
return i; return i;
return -1; return -1;
} }

View File

@ -253,32 +253,50 @@ class TreeItem {
/// returns child by index /// returns child by index
TreeItem child(int index) { return _children.get(index); } TreeItem child(int index) { return _children.get(index); }
/// adds child, returns added item /// adds child, returns added item
TreeItem addChild(TreeItem item) { TreeItem addChild(TreeItem item, int index = -1) {
return _children.add(item).parent(this).level(_level + 1); TreeItem res = _children.insert(item, index).parent(this).level(_level + 1);
root.onUpdate(res);
return res;
} }
/// removes child, returns removed item /// removes child, returns removed item
TreeItem removeChild(int index) { TreeItem removeChild(int index) {
if (index < 0 || index >= _children.count)
return null;
TreeItem res = _children.remove(index); TreeItem res = _children.remove(index);
if (res !is null) TreeItem newSelection = null;
if (res !is null) {
res.parent = 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; 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 /// removes child by ID, returns removed item
TreeItem removeChild(string ID) { TreeItem removeChild(string ID) {
TreeItem res = null; TreeItem res = null;
int index = _children.indexOf(ID); int index = _children.indexOf(ID);
if (index < 0) return removeChild(index);
return null;
res = _children.remove(index);
if (res !is null)
res.parent = null;
return res;
} }
/// returns index of widget in child list, -1 if passed widget is not a child of this widget /// 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); } int childIndex(TreeItem item) { return _children.indexOf(item); }
/// notify listeners /// notify listeners
protected void onUpdate(TreeItem item) { protected void onUpdate(TreeItem item) {
root.onUpdate(item); if (root)
root.onUpdate(item);
} }
protected void toggleExpand(TreeItem item) { protected void toggleExpand(TreeItem item) {
root.toggleExpand(item); root.toggleExpand(item);