CodeCompass
121 строка · 3.4 Кб
1#include <util/legendbuilder.h>2
3namespace cc4{
5namespace util6{
7
8LegendBuilder::LegendBuilder(const std::string& title_)9{
10_graph.setAttribute("rankdir", "LR");11_graph.setAttribute("nodesep", "0.25");12_root = _graph.createNode();13_graph.setNodeAttribute(_root, "label", "");14_graph.setNodeAttribute(_root, "shape", "none");15_graph.setNodeAttribute(_root, "width", "0.1");16_graph.setAttribute("label", title_);17}
18
19Graph::Node LegendBuilder::registerSubgraph(const Graph::Subgraph& sub_)20{
21Graph::Node first = _graph.createNode(sub_);22Graph::Edge invis = _graph.createEdge(_root, first);23_graph.setEdgeAttribute(invis, "style", "invis");24
25_graph.setNodeAttribute(first, "label", "");26_graph.setNodeAttribute(first, "shape", "none");27_graph.setNodeAttribute(first, "width", "0.01");28
29return first;30}
31
32std::string LegendBuilder::getOutput() const33{
34return _graph.output(Graph::SVG);35}
36
37void LegendBuilder::addNode(38const std::string& label_,39const std::vector<std::pair<std::string, std::string>>& attrs_,40bool html_)41{
42Graph::Subgraph sub = _graph.getOrCreateSubgraph(label_);43
44Graph::Node ref1 = registerSubgraph(sub); // padding node45_graph.setSubgraphAttribute(sub, "rankdir", "LR");46
47Graph::Node node = _graph.createNode(sub);48_graph.setNodeAttribute(node, "label", "");49Graph::Edge edge = _graph.createEdge(ref1, node); // padding edge50_graph.setEdgeAttribute(edge, "style", "invis");51
52Graph::Node desc = _graph.createNode(sub);53_graph.setNodeAttribute(desc, "shape", "none");54_graph.setNodeAttribute(desc, "label", label_);55
56Graph::Edge invis = _graph.createEdge(node, desc);57_graph.setEdgeAttribute(invis, "style", "invis");58
59for (const auto& attr : attrs_)60_graph.setNodeAttribute(node, attr.first, attr.second, html_);61}
62
63void LegendBuilder::addEdge(64const std::string& label_,65const std::vector<std::pair<std::string, std::string>>& attrs_,66bool html_)67{
68Graph::Subgraph sub = _graph.getOrCreateSubgraph(label_);69_graph.setSubgraphAttribute(sub, "rankdir", "LR");70
71Graph::Node ref1 = registerSubgraph(sub);72
73Graph::Node ref2 = _graph.createNode(sub);74_graph.setNodeAttribute(ref2, "label", "");75_graph.setNodeAttribute(ref2, "shape", "none");76_graph.setNodeAttribute(ref2, "width", "0.01");77Graph::Edge edge = _graph.createEdge(ref1, ref2);78
79Graph::Node desc = _graph.createNode(sub);80_graph.setNodeAttribute(desc, "shape", "none");81_graph.setNodeAttribute(desc, "label", label_);82
83Graph::Edge invis = _graph.createEdge(ref2, desc);84_graph.setEdgeAttribute(invis, "style", "invis");85
86for (const auto& attr : attrs_)87_graph.setEdgeAttribute(edge, attr.first, attr.second, html_);88}
89
90Graph::Subgraph LegendBuilder::addSubgraph(91const std::string& label_,92Graph::Node& hook_)93{
94Graph::Subgraph sub = _graph.getOrCreateSubgraph(label_);95_graph.setSubgraphAttribute(sub, "rankdir", "LR");96Graph::Node ref = registerSubgraph(sub); // padding node97
98hook_ = ref;99return sub;100}
101
102void LegendBuilder::setNodeStyle(103const Graph::Node& node_,104const std::vector<std::pair<std::string, std::string>>& attrs_,105bool html_)106{
107for (const auto& attr : attrs_)108_graph.setNodeAttribute(node_, attr.first, attr.second, html_);109}
110
111void LegendBuilder::setEdgeStyle(112const Graph::Edge& edge_,113const std::vector<std::pair<std::string, std::string>>& attrs_,114bool html_)115{
116for (const auto& attr : attrs_)117_graph.setEdgeAttribute(edge_, attr.first, attr.second, html_);118}
119
120} // util121} // cc122