Hello,
In one of our tools, the lines drawn on the main TreeView must be different colours to help indicate state/relationship information. To achieve this, I had to modify the TreeViewItem code slightly (and then promptly forgot about it). The changes simply make it possible to override the line drawing per item. Now that I’m finally updating that tool to use the Juce modules, I’ve found I need to re-apply my changes, so thought I’d post them here in case you might put similar functionality into the library.
Here’s an example of what I needed it for:
Below are the small changes I had to make to support this…
All it consists of is two new virtual functions (one for vertical, one for horizontal), and replacement of the drawLine calls in paintRecursively() for these.
TreeViewItem.cpp
// New virtual function
void TreeViewItem::drawHorizontalConnectingLine (Graphics& g, const Line<float>& line)
{
g.setColour (ownerView->findColour (TreeView::linesColourId));
g.drawLine(line);
}
// New virtual function
void TreeViewItem::drawVerticalConnectingLine (Graphics& g, const Line<float>& line, TreeViewItem* parent)
{
g.setColour (ownerView->findColour (TreeView::linesColourId));
g.drawLine(line);
}
void TreeViewItem::paintRecursively (Graphics& g, int width)
{
// ...
if (depth >= 0 && ownerView->openCloseButtonsVisible)
{
float x = (depth + 0.5f) * indentWidth;
if (parentItem != nullptr && parentItem->drawLinesInside)
{
//g.drawLine (x, 0, x, isLastOfSiblings() ? halfH : (float) itemHeight);
Line<float> line(x,0,x,isLastOfSiblings() ? halfH : (float) itemHeight);
drawVerticalConnectingLine (g, line, nullptr);
}
if ((parentItem != nullptr && parentItem->drawLinesInside)
|| (parentItem == nullptr && drawLinesInside))
{
//g.drawLine (x, halfH, x + indentWidth / 2, halfH);
Line<float> line (x, halfH, x + indentWidth / 2, halfH);
drawHorizontalConnectingLine (g, line);
}
{
TreeViewItem* p = parentItem;
int d = depth;
while (p != nullptr && --d >= 0)
{
x -= (float) indentWidth;
if ((p->parentItem == nullptr || p->parentItem->drawLinesInside)
&& ! p->isLastOfSiblings())
{
//g.drawLine (x, 0, x, (float) itemHeight);
Line<float> line (x, 0, x, (float) itemHeight);
drawVerticalConnectingLine (g, line, p); // this is a bypass line, so we provide p; it's more an indicator for the parent item than this one
}
p = p->parentItem;
}
}
// ...
}
It’d probably make more sense to use the ‘parent’ itself for the bypass line, but I can’t remember if there might have been a reason for me not doing so at the time (or if it was just because I didn’t think of it)… but yeah. That’s what I did. It’d be nice if I didn’t have to keep merging the changes back in