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