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 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 11 | #include "trust-tree-scene.hpp" |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 12 | |
| 13 | #include <QtGui> |
| 14 | |
| 15 | #ifndef Q_MOC_RUN |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 16 | #include <assert.h> |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 17 | #include <memory> |
| 18 | #endif |
| 19 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 20 | namespace chronos { |
| 21 | |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 22 | static const double Pi = 3.14159265358979323846264338327950288419717; |
| 23 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 24 | TrustTreeScene::TrustTreeScene(QWidget* parent) |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 25 | : QGraphicsScene(parent) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 26 | { |
| 27 | } |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 28 | |
| 29 | void |
| 30 | TrustTreeScene::plotTrustTree(TrustTreeNodeList& nodeList) |
| 31 | { |
| 32 | clear(); |
| 33 | |
| 34 | int nodeSize = 40; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 35 | int siblingDistance = 100; |
| 36 | int levelDistance = 100; |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 37 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 38 | shared_ptr<MultipleLevelTreeLayout> layout(new MultipleLevelTreeLayout()); |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 39 | layout->setSiblingDistance(siblingDistance); |
| 40 | layout->setLevelDistance(levelDistance); |
| 41 | layout->setMultipleLevelTreeLayout(nodeList); |
| 42 | |
| 43 | plotEdge(nodeList, nodeSize); |
| 44 | plotNode(nodeList, nodeSize); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | TrustTreeScene::plotEdge(const TrustTreeNodeList& nodeList, int nodeSize) |
| 49 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 50 | for (TrustTreeNodeList::const_iterator it = nodeList.begin(); it != nodeList.end(); it++) { |
| 51 | TrustTreeNodeList& introducees = (*it)->getIntroducees(); |
| 52 | for (TrustTreeNodeList::iterator eeIt = introducees.begin(); |
| 53 | eeIt != introducees.end(); eeIt++) { |
| 54 | if ((*it)->level() >= (*eeIt)->level()) |
| 55 | continue; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 56 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 57 | double x1 = (*it)->x; |
| 58 | double y1 = (*it)->y; |
| 59 | double x2 = (*eeIt)->x; |
| 60 | double y2 = (*eeIt)->y; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 61 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 62 | QPointF src(x1 + nodeSize/2, y1 + nodeSize/2); |
| 63 | QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2); |
| 64 | QLineF line(src, dest); |
| 65 | double angle = ::acos(line.dx() / line.length()); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 66 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 67 | double arrowSize = 10; |
| 68 | QPointF endP0 = src + QPointF((nodeSize/2) * line.dx() / line.dy(), nodeSize/2); |
| 69 | QPointF sourceArrowP0 = dest + QPointF((-nodeSize/2) * line.dx() / line.dy(), |
| 70 | -nodeSize/2); |
| 71 | QPointF sourceArrowP1 = sourceArrowP0 + QPointF(-cos(angle - Pi / 6) * arrowSize, |
| 72 | -sin(angle - Pi / 6) * arrowSize); |
| 73 | QPointF sourceArrowP2 = sourceArrowP0 + QPointF(-cos(angle + Pi / 6) * arrowSize, |
| 74 | -sin(angle + Pi / 6) * arrowSize); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 75 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 76 | addLine(QLineF(sourceArrowP0, endP0), QPen(Qt::black)); |
| 77 | addPolygon(QPolygonF() << sourceArrowP0 << sourceArrowP1 << sourceArrowP2, |
| 78 | QPen(Qt::black), QBrush(Qt::black)); |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 79 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 80 | } |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void |
| 84 | TrustTreeScene::plotNode(const TrustTreeNodeList& nodeList, int nodeSize) |
| 85 | { |
| 86 | int rim = 3; |
| 87 | |
| 88 | // plot nodes |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 89 | for (TrustTreeNodeList::const_iterator it = nodeList.begin(); it != nodeList.end(); it++) { |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 90 | double x = (*it)->x; |
| 91 | double y = (*it)->y; |
| 92 | QRectF boundingRect(x, y, nodeSize, nodeSize); |
| 93 | QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2); |
| 94 | addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue)); |
| 95 | addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray)); |
| 96 | |
| 97 | QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30); |
| 98 | addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
| 99 | QGraphicsTextItem *nickItem = addText(QString::fromStdString((*it)->name().toUri())); |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 100 | nickItem->setDefaultTextColor(Qt::white); |
| 101 | nickItem->setFont(QFont("Cursive", 8, QFont::Bold)); |
| 102 | nickItem->setPos(x - nodeSize / 2 + 10, y + nodeSize + 5); |
| 103 | } |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 106 | } //namespace chronos |
| 107 | |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 108 | #if WAF |
| 109 | #include "trust-tree-scene.moc" |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 110 | // #include "trust-tree-scene.cpp.moc" |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 111 | #endif |