Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 1 | #include "digesttreescene.h" |
| 2 | #include <QtGui> |
| 3 | #include "ogdf/basic/Array.h" |
| 4 | #include "ogdf/basic/Graph_d.h" |
| 5 | #include "ogdf/tree/TreeLayout.h" |
| 6 | #include <vector> |
| 7 | #include <iostream> |
| 8 | #include <assert.h> |
| 9 | |
| 10 | static const double Pi = 3.14159265358979323846264338327950288419717; |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 11 | |
| 12 | DigestTreeScene::DigestTreeScene(QWidget *parent) |
| 13 | : QGraphicsScene(parent) |
| 14 | { |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | void |
| 18 | DigestTreeScene::plot() { |
| 19 | clear(); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 20 | m_graph.clear(); |
| 21 | std::vector<ogdf::node> v(5); |
| 22 | for (int i = 0; i < 5; i++) |
| 23 | v[i] = m_graph.newNode(); |
| 24 | |
| 25 | m_graph.newEdge(v[0], v[1]); |
| 26 | m_graph.newEdge(v[0], v[2]); |
| 27 | m_graph.newEdge(v[0], v[3]); |
| 28 | m_graph.newEdge(v[0], v[4]); |
| 29 | |
| 30 | ogdf::GraphAttributes GA(m_graph); |
| 31 | GA.initAttributes(ogdf::GraphAttributes::nodeLabel); |
| 32 | int nodeWidth = 30, nodeHeight = 30, siblingDistance = 60; |
| 33 | ogdf::TreeLayout layout; |
| 34 | layout.siblingDistance(siblingDistance); |
| 35 | layout.rootSelection(ogdf::TreeLayout::rootIsSource); |
| 36 | //layout.call(GA); |
| 37 | layout.callSortByPositions(GA, m_graph); |
| 38 | |
| 39 | int width = GA.boundingBox().width(); |
| 40 | std::cout << "GA width " << width << std::endl; |
| 41 | int height = GA.boundingBox().height(); |
| 42 | std::cout << "GA height " << height << std::endl; |
| 43 | setSceneRect(QRect(0, 0, width + nodeWidth, height + nodeHeight)); |
| 44 | GA.setAllWidth(nodeWidth); |
| 45 | GA.setAllHeight(nodeHeight); |
| 46 | |
| 47 | ogdf::edge e; |
| 48 | forall_edges(e, m_graph) { |
| 49 | ogdf::node source = e->source(); |
| 50 | ogdf::node target = e->target(); |
| 51 | int x1 = GA.x(source), y1 = -GA.y(source); |
| 52 | int x2 = GA.x(target), y2 = -GA.y(target); |
| 53 | // QPainterPath p; |
| 54 | QPointF src(x1 + nodeWidth/2, y1 + nodeHeight/2); |
| 55 | QPointF dest(x2 + nodeWidth/2, y2 + nodeHeight/2); |
| 56 | QLineF line(src, dest); |
| 57 | //p.moveTo(x1 + nodeWidth/2, y1 + nodeHeight/2); |
| 58 | //p.lineTo(x2 + nodeWidth/2, y2 + nodeHeight/2); |
| 59 | //addPath(p, QPen(Qt::black), QBrush(Qt::black)); |
| 60 | addLine(line, QPen(Qt::black)); |
| 61 | |
| 62 | double angle = ::acos(line.dx() / line.length()); |
| 63 | |
| 64 | double arrowSize = 10; |
| 65 | |
| 66 | QPointF sourceArrowP0 = src + QPointF(nodeWidth/2 * line.dx() / line.length(), nodeHeight/2 * line.dy() / line.length()); |
| 67 | //QPointF sourceArrowP0 = src + QPointF(cos(angle) * nodeHeight/2, sin(angle) * nodeHeight/2); |
| 68 | |
| 69 | std::cout << "src " << src.x() << ", " << src.y() << std::endl; |
| 70 | std::cout << "dest " << dest.x() << ", " << dest.y() << std::endl; |
| 71 | std::cout << "line dx " << line.dx() << ", dy " << line.dy() << ", lenght << " << line.length() << std::endl; |
| 72 | std::cout << "sap " << sourceArrowP0.x() << ", " << sourceArrowP0.y() << std::endl; |
| 73 | |
| 74 | QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize, |
| 75 | sin(angle + Pi / 3 - Pi/2) * arrowSize); |
| 76 | QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize, |
| 77 | sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize); |
| 78 | QLineF line1(sourceArrowP1, sourceArrowP2); |
| 79 | addLine(line1, QPen(Qt::black)); |
| 80 | |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 81 | addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black)); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 84 | ogdf::node n; |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 85 | int i = 0; |
| 86 | std::string s[5] = {"A", "B", "C", "D", "E"}; |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 87 | forall_nodes(n, m_graph) { |
| 88 | double x = GA.x(n); |
| 89 | |
| 90 | double y = -GA.y(n); |
| 91 | double w = GA.width(n); |
| 92 | double h = GA.height(n); |
| 93 | QRectF boundingRect(x, y, w, h); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 94 | addEllipse(boundingRect, QPen(Qt::black), QBrush(Qt::green)); |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 95 | QGraphicsTextItem *text = addText(s[i].c_str()); |
| 96 | text->setPos(x + 5, y + 5); |
| 97 | i++; |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 98 | } |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 99 | } |
| 100 | |