blob: 718b207aeb96110af4adc08a5894083b13579ae5 [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>
14
15#ifndef Q_MOC_RUN
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070016#include <assert.h>
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070017#include <memory>
18#endif
19
Yingdi Yueb692ac2015-02-10 18:46:18 -080020namespace chronochat {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070021
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070022static const double Pi = 3.14159265358979323846264338327950288419717;
23
Yingdi Yu0b0a7362014-08-05 16:31:30 -070024TrustTreeScene::TrustTreeScene(QWidget* parent)
Yingdi Yufa0b6a02014-04-30 14:26:42 -070025 : QGraphicsScene(parent)
Yingdi Yu0b0a7362014-08-05 16:31:30 -070026{
27}
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070028
29void
30TrustTreeScene::plotTrustTree(TrustTreeNodeList& nodeList)
31{
32 clear();
33
34 int nodeSize = 40;
Yingdi Yu0b0a7362014-08-05 16:31:30 -070035 int siblingDistance = 100;
36 int levelDistance = 100;
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070037
Yingdi Yu0b0a7362014-08-05 16:31:30 -070038 shared_ptr<MultipleLevelTreeLayout> layout(new MultipleLevelTreeLayout());
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070039 layout->setSiblingDistance(siblingDistance);
40 layout->setLevelDistance(levelDistance);
41 layout->setMultipleLevelTreeLayout(nodeList);
42
43 plotEdge(nodeList, nodeSize);
44 plotNode(nodeList, nodeSize);
45}
46
47void
48TrustTreeScene::plotEdge(const TrustTreeNodeList& nodeList, int nodeSize)
49{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070050 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 Yufa0b6a02014-04-30 14:26:42 -070056
Yingdi Yu0b0a7362014-08-05 16:31:30 -070057 double x1 = (*it)->x;
58 double y1 = (*it)->y;
59 double x2 = (*eeIt)->x;
60 double y2 = (*eeIt)->y;
Yingdi Yufa0b6a02014-04-30 14:26:42 -070061
Yingdi Yu0b0a7362014-08-05 16:31:30 -070062 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 Yufa0b6a02014-04-30 14:26:42 -070066
Yingdi Yu0b0a7362014-08-05 16:31:30 -070067 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 Yufa0b6a02014-04-30 14:26:42 -070075
Yingdi Yu0b0a7362014-08-05 16:31:30 -070076 addLine(QLineF(sourceArrowP0, endP0), QPen(Qt::black));
77 addPolygon(QPolygonF() << sourceArrowP0 << sourceArrowP1 << sourceArrowP2,
78 QPen(Qt::black), QBrush(Qt::black));
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070079 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -070080 }
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070081}
82
83void
84TrustTreeScene::plotNode(const TrustTreeNodeList& nodeList, int nodeSize)
85{
86 int rim = 3;
87
88 // plot nodes
Yingdi Yu0b0a7362014-08-05 16:31:30 -070089 for (TrustTreeNodeList::const_iterator it = nodeList.begin(); it != nodeList.end(); it++) {
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070090 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 Yuf4aaa8b2014-03-10 11:24:31 -0700100 nickItem->setDefaultTextColor(Qt::white);
101 nickItem->setFont(QFont("Cursive", 8, QFont::Bold));
102 nickItem->setPos(x - nodeSize / 2 + 10, y + nodeSize + 5);
103 }
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700104}
105
Yingdi Yueb692ac2015-02-10 18:46:18 -0800106} //namespace chronochat
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700107
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700108#if WAF
109#include "trust-tree-scene.moc"
Yingdi Yu42125862014-08-07 17:04:28 -0700110// #include "trust-tree-scene.cpp.moc"
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700111#endif