blob: d3f84b58f4e32c435e036f78fc36f620ff208e06 [file] [log] [blame]
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
11#include "trust-tree-scene.h"
12
13#include <QtGui>
14
15#ifndef Q_MOC_RUN
16#include <vector>
17#include <iostream>
18#include <assert.h>
19#include <boost/lexical_cast.hpp>
20#include <memory>
21#endif
22
23static const double Pi = 3.14159265358979323846264338327950288419717;
24
25TrustTreeScene::TrustTreeScene(QWidget *parent)
Yingdi Yufa0b6a02014-04-30 14:26:42 -070026 : QGraphicsScene(parent)
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070027{}
28
29void
30TrustTreeScene::plotTrustTree(TrustTreeNodeList& nodeList)
31{
32 clear();
33
34 int nodeSize = 40;
35 int siblingDistance = 100, levelDistance = 100;
36
37 boost::shared_ptr<MultipleLevelTreeLayout> layout(new MultipleLevelTreeLayout());
38 layout->setSiblingDistance(siblingDistance);
39 layout->setLevelDistance(levelDistance);
40 layout->setMultipleLevelTreeLayout(nodeList);
41
42 plotEdge(nodeList, nodeSize);
43 plotNode(nodeList, nodeSize);
44}
45
46void
47TrustTreeScene::plotEdge(const TrustTreeNodeList& nodeList, int nodeSize)
48{
49 TrustTreeNodeList::const_iterator it = nodeList.begin();
50 TrustTreeNodeList::const_iterator end = nodeList.end();
51 for(; it != end; it++)
52 {
53 TrustTreeNodeList& introducees = (*it)->getIntroducees();
54 TrustTreeNodeList::iterator eeIt = introducees.begin();
55 TrustTreeNodeList::iterator eeEnd = introducees.end();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070056
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070057 for(; eeIt != eeEnd; eeIt++)
58 {
59 if((*it)->level() >= (*eeIt)->level())
60 continue;
Yingdi Yufa0b6a02014-04-30 14:26:42 -070061
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070062 double x1 = (*it)->x;
63 double y1 = (*it)->y;
64 double x2 = (*eeIt)->x;
65 double y2 = (*eeIt)->y;
Yingdi Yufa0b6a02014-04-30 14:26:42 -070066
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070067 QPointF src(x1 + nodeSize/2, y1 + nodeSize/2);
68 QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2);
69 QLineF line(src, dest);
70 double angle = ::acos(line.dx() / line.length());
Yingdi Yufa0b6a02014-04-30 14:26:42 -070071
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070072 double arrowSize = 10;
73 QPointF endP0 = src + QPointF((nodeSize/2) * line.dx() / line.dy(), nodeSize/2);
74 QPointF sourceArrowP0 = dest + QPointF((-nodeSize/2) * line.dx() / line.dy(), -nodeSize/2);
75 QPointF sourceArrowP1 = sourceArrowP0 + QPointF(-cos(angle - Pi / 6) * arrowSize,
76 -sin(angle - Pi / 6) * arrowSize);
77 QPointF sourceArrowP2 = sourceArrowP0 + QPointF(-cos(angle + Pi / 6) * arrowSize,
78 -sin(angle + Pi / 6) * arrowSize);
79
80 addLine(QLineF(sourceArrowP0, endP0), QPen(Qt::black));
81 addPolygon(QPolygonF() << sourceArrowP0 << sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
82 }
83 }
84}
85
86void
87TrustTreeScene::plotNode(const TrustTreeNodeList& nodeList, int nodeSize)
88{
89 int rim = 3;
90
91 // plot nodes
92 TrustTreeNodeList::const_iterator it = nodeList.begin();
93 TrustTreeNodeList::const_iterator end = nodeList.end();
94 for(; it != end; it++)
95 {
96 double x = (*it)->x;
97 double y = (*it)->y;
98 QRectF boundingRect(x, y, nodeSize, nodeSize);
99 QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2);
100 addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue));
101 addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
102
103 QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30);
104 addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
105 QGraphicsTextItem *nickItem = addText(QString::fromStdString((*it)->name().toUri()));
106 QRectF textBoundingRect = nickItem->boundingRect();
107 nickItem->setDefaultTextColor(Qt::white);
108 nickItem->setFont(QFont("Cursive", 8, QFont::Bold));
109 nickItem->setPos(x - nodeSize / 2 + 10, y + nodeSize + 5);
110 }
111
112}
113
114#if WAF
115#include "trust-tree-scene.moc"
116#include "trust-tree-scene.cpp.moc"
117#endif