Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 23 | static const double Pi = 3.14159265358979323846264338327950288419717; |
| 24 | |
| 25 | TrustTreeScene::TrustTreeScene(QWidget *parent) |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 26 | : QGraphicsScene(parent) |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 27 | {} |
| 28 | |
| 29 | void |
| 30 | TrustTreeScene::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 | |
| 46 | void |
| 47 | TrustTreeScene::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 Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 56 | |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 57 | for(; eeIt != eeEnd; eeIt++) |
| 58 | { |
| 59 | if((*it)->level() >= (*eeIt)->level()) |
| 60 | continue; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 61 | |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 62 | double x1 = (*it)->x; |
| 63 | double y1 = (*it)->y; |
| 64 | double x2 = (*eeIt)->x; |
| 65 | double y2 = (*eeIt)->y; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 66 | |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 67 | 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 Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 71 | |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 72 | 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 | |
| 86 | void |
| 87 | TrustTreeScene::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 |