blob: 3a2371890181cec301430a04f60856ee005937d9 [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
Yingdi Yu0b0a7362014-08-05 16:31:30 -070011#include "trust-tree-scene.hpp"
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070012
13#include <QtGui>
Varun Patil3d850902020-11-23 12:19:14 +053014#include <QGraphicsTextItem>
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070015
16#ifndef Q_MOC_RUN
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070017#include <assert.h>
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070018#include <memory>
19#endif
20
Yingdi Yueb692ac2015-02-10 18:46:18 -080021namespace chronochat {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070022
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070023static const double Pi = 3.14159265358979323846264338327950288419717;
24
Varun Patil3d850902020-11-23 12:19:14 +053025TrustTreeScene::TrustTreeScene(QObject* parent)
Yingdi Yufa0b6a02014-04-30 14:26:42 -070026 : QGraphicsScene(parent)
Yingdi Yu0b0a7362014-08-05 16:31:30 -070027{
28}
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070029
30void
31TrustTreeScene::plotTrustTree(TrustTreeNodeList& nodeList)
32{
33 clear();
34
35 int nodeSize = 40;
Yingdi Yu0b0a7362014-08-05 16:31:30 -070036 int siblingDistance = 100;
37 int levelDistance = 100;
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070038
Yingdi Yu0b0a7362014-08-05 16:31:30 -070039 shared_ptr<MultipleLevelTreeLayout> layout(new MultipleLevelTreeLayout());
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070040 layout->setSiblingDistance(siblingDistance);
41 layout->setLevelDistance(levelDistance);
42 layout->setMultipleLevelTreeLayout(nodeList);
43
44 plotEdge(nodeList, nodeSize);
45 plotNode(nodeList, nodeSize);
46}
47
48void
49TrustTreeScene::plotEdge(const TrustTreeNodeList& nodeList, int nodeSize)
50{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070051 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 Yufa0b6a02014-04-30 14:26:42 -070057
Yingdi Yu0b0a7362014-08-05 16:31:30 -070058 double x1 = (*it)->x;
59 double y1 = (*it)->y;
60 double x2 = (*eeIt)->x;
61 double y2 = (*eeIt)->y;
Yingdi Yufa0b6a02014-04-30 14:26:42 -070062
Yingdi Yu0b0a7362014-08-05 16:31:30 -070063 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 Yufa0b6a02014-04-30 14:26:42 -070067
Yingdi Yu0b0a7362014-08-05 16:31:30 -070068 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 Yufa0b6a02014-04-30 14:26:42 -070076
Yingdi Yu0b0a7362014-08-05 16:31:30 -070077 addLine(QLineF(sourceArrowP0, endP0), QPen(Qt::black));
78 addPolygon(QPolygonF() << sourceArrowP0 << sourceArrowP1 << sourceArrowP2,
79 QPen(Qt::black), QBrush(Qt::black));
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070080 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -070081 }
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070082}
83
84void
85TrustTreeScene::plotNode(const TrustTreeNodeList& nodeList, int nodeSize)
86{
87 int rim = 3;
88
89 // plot nodes
Yingdi Yu0b0a7362014-08-05 16:31:30 -070090 for (TrustTreeNodeList::const_iterator it = nodeList.begin(); it != nodeList.end(); it++) {
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070091 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 Yuf4aaa8b2014-03-10 11:24:31 -0700101 nickItem->setDefaultTextColor(Qt::white);
102 nickItem->setFont(QFont("Cursive", 8, QFont::Bold));
103 nickItem->setPos(x - nodeSize / 2 + 10, y + nodeSize + 5);
104 }
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700105}
106
Yingdi Yueb692ac2015-02-10 18:46:18 -0800107} //namespace chronochat
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700108
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700109#if WAF
110#include "trust-tree-scene.moc"
Yingdi Yu42125862014-08-07 17:04:28 -0700111// #include "trust-tree-scene.cpp.moc"
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700112#endif