blob: 483f1f6bdd14e95254c8e391e0380267649306ef [file] [log] [blame]
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -07001#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
10static const double Pi = 3.14159265358979323846264338327950288419717;
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070011
12DigestTreeScene::DigestTreeScene(QWidget *parent)
13 : QGraphicsScene(parent)
14{
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -070015}
16
17void
18DigestTreeScene::plot() {
19 clear();
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070020 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 Zhu6fcdee42012-05-30 17:02:49 -070081 addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070082 }
83
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070084 ogdf::node n;
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -070085 int i = 0;
86 std::string s[5] = {"A", "B", "C", "D", "E"};
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070087 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 Zhuf474a0a2012-05-30 15:06:29 -070094 addEllipse(boundingRect, QPen(Qt::black), QBrush(Qt::green));
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -070095 QGraphicsTextItem *text = addText(s[i].c_str());
96 text->setPos(x + 5, y + 5);
97 i++;
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070098 }
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070099}
100