I’m trying to implement a TreeView component that mirrors a ValueTree structure. I got it working by following the ValueTreesDemo in the Demo runner. But in this example, I noticed that the entire tree is recalculated every time a TreeViewItem is clicked and every time something changes in the ValueTree (assuming that I am reading refreshSubItems()
correctly). This seems a bit inefficient, especially for longer ValueTrees, so I thought that I might be able to improve on it by making the TreeViewItems automatically assemble itself in parallel with the ValueTree.
I’m trying to do this by registering the TreeViewItem as a listener to the ValueTree, but I can’t seem to get it working. I haven’t worked much with Listeners yet, so it’s possible that I’m getting something wrong at a fairly basic level. (In fact I’m new to all of this, but I have at least gotten ValueTrees and TreeViewItems up and running elsewhere).
Note: My TreeView doesn’t need to have drag and drop, text editing, or any other kind of feedback to the ValueTree.
The relevant part of my code is this:
class MapItem : public TreeViewItem,
public ValueTree::Listener
{
public:
MapItem(ValueTree)
{
mapItemTree.addListener(this);
}
private:
ValueTree mapItemTree;
// intention: whenever a new ValueTree child is added, add a corresponding MapItem
void valueTreeChildAdded (ValueTree& parentTree, ValueTree& newChild) override
{
this->addSubItem(new MapItem(newChild), parentTree.indexOf(newChild));
}
};
I’ll similarly override other functions from the ValueTree::Listener
class:
valueTreePropertyChanged()
, valueTreeChildRemoved()
, valueTreeChildOrderChanged()
, valueTreeParentChanged()
, treeChildrenChanged()
but I’m not focusing on them until I can get valueTreeChildAdded()
working.
My code compiles, but the TreeViewItem tree doesn’t match the ValueTree–it only displays the first item (which is set elsewhere with setRootItem()
).
Can anyone see what I’m missing?
My ValueTree is created in a constructor, and it struck me that MapItem
, which is constructed after the ValueTree, might not be receiving these initial messages. Is this a possibility, or are Listeners clever enough to send messages only after their receivers have been constructed?