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