Skip to content

Commit

Permalink
#228 Add mixed org tree layout support.
Browse files Browse the repository at this point in the history
Signed-off-by: cneben <[email protected]>
  • Loading branch information
cneben committed Aug 15, 2024
1 parent 1407a35 commit c33a552
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions samples/layouts/layouts.qml
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ ApplicationWindow {
graph.insertEdge(n1, n13);
graph.insertEdge(n12, n121);
graph.insertEdge(n12, n122);
graph.insertEdge(n121, n1211);
//graph.insertEdge(n121, n1211);
graph.insertEdge(n13, n131);

orgTreeLayout.layoutOrientation = Qan.OrgTreeLayout.Horizontal
orgTreeLayout.layoutOrientation = Qan.OrgTreeLayout.Mixed
orgTreeLayout.layout(n1);
}
Qan.OrgTreeLayout {
Expand Down
27 changes: 25 additions & 2 deletions src/qanTreeLayouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,29 @@ void OrgTreeLayout::layout(qan::Node& root, qreal xSpacing, qreal ySpacing) n
return br;
};

auto layoutMixed_rec = [xSpacing, ySpacing, layoutHoriz_rec](auto&& self, auto& childNodes, QRectF br) -> QRectF {
auto childsAreLeafs = true;
for (const auto child: childNodes)
if (child->get_out_nodes().size() != 0) {
childsAreLeafs = false;
break;
}
if (childsAreLeafs)
return layoutHoriz_rec(self, childNodes, br);
else {
const auto x = br.right() + xSpacing;
for (auto child: childNodes) {
child->getItem()->setX(x);
child->getItem()->setY(br.bottom() + ySpacing);
// Take into account this level maximum width
br = br.united(child->getItem()->boundingRect().translated(child->getItem()->position()));
const auto childBr = self(self, child->get_out_nodes(), br);
br.setBottom(childBr.bottom()); // Note: Do not take full child BR into account to avoid x drifting
}
}
return br;
};

// Note: QQuickItem boundingRect() is in item local CS, translate to "scene" CS.
switch (getLayoutOrientation()) {
case LayoutOrientation::Undefined: return;
Expand All @@ -210,8 +233,8 @@ void OrgTreeLayout::layout(qan::Node& root, qreal xSpacing, qreal ySpacing) n
root.getItem()->boundingRect().translated(root.getItem()->position()));
break;
case LayoutOrientation::Mixed:
//layoutHoriz_rec(layout_rec, root.get_out_nodes(),
// root.getItem()->boundingRect().translated(root.getItem()->position()));
layoutMixed_rec(layoutMixed_rec, root.get_out_nodes(),
root.getItem()->boundingRect().translated(root.getItem()->position()));
break;
}
}
Expand Down

0 comments on commit c33a552

Please sign in to comment.