blob: c62d82f1616f0a206bd50a73239193132b003e84 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2013, Regents of the University of California
* Yingdi Yu
*
* BSD license, See the LICENSE file for more information
*
* Author: Yingdi Yu <yingdi@cs.ucla.edu>
*/
#include "trust-tree-scene.h"
#include <QtGui>
#ifndef Q_MOC_RUN
#include <vector>
#include <iostream>
#include <assert.h>
#include <boost/lexical_cast.hpp>
#include <memory>
#endif
static const double Pi = 3.14159265358979323846264338327950288419717;
TrustTreeScene::TrustTreeScene(QWidget *parent)
: QGraphicsScene(parent)
{}
void
TrustTreeScene::plotTrustTree(TrustTreeNodeList& nodeList)
{
clear();
int nodeSize = 40;
int siblingDistance = 100, levelDistance = 100;
boost::shared_ptr<MultipleLevelTreeLayout> layout(new MultipleLevelTreeLayout());
layout->setSiblingDistance(siblingDistance);
layout->setLevelDistance(levelDistance);
layout->setMultipleLevelTreeLayout(nodeList);
plotEdge(nodeList, nodeSize);
plotNode(nodeList, nodeSize);
}
void
TrustTreeScene::plotEdge(const TrustTreeNodeList& nodeList, int nodeSize)
{
TrustTreeNodeList::const_iterator it = nodeList.begin();
TrustTreeNodeList::const_iterator end = nodeList.end();
for(; it != end; it++)
{
TrustTreeNodeList& introducees = (*it)->getIntroducees();
TrustTreeNodeList::iterator eeIt = introducees.begin();
TrustTreeNodeList::iterator eeEnd = introducees.end();
for(; eeIt != eeEnd; eeIt++)
{
if((*it)->level() >= (*eeIt)->level())
continue;
double x1 = (*it)->x;
double y1 = (*it)->y;
double x2 = (*eeIt)->x;
double y2 = (*eeIt)->y;
QPointF src(x1 + nodeSize/2, y1 + nodeSize/2);
QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2);
QLineF line(src, dest);
double angle = ::acos(line.dx() / line.length());
double arrowSize = 10;
QPointF endP0 = src + QPointF((nodeSize/2) * line.dx() / line.dy(), nodeSize/2);
QPointF sourceArrowP0 = dest + QPointF((-nodeSize/2) * line.dx() / line.dy(), -nodeSize/2);
QPointF sourceArrowP1 = sourceArrowP0 + QPointF(-cos(angle - Pi / 6) * arrowSize,
-sin(angle - Pi / 6) * arrowSize);
QPointF sourceArrowP2 = sourceArrowP0 + QPointF(-cos(angle + Pi / 6) * arrowSize,
-sin(angle + Pi / 6) * arrowSize);
addLine(QLineF(sourceArrowP0, endP0), QPen(Qt::black));
addPolygon(QPolygonF() << sourceArrowP0 << sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
}
}
}
void
TrustTreeScene::plotNode(const TrustTreeNodeList& nodeList, int nodeSize)
{
int rim = 3;
// plot nodes
TrustTreeNodeList::const_iterator it = nodeList.begin();
TrustTreeNodeList::const_iterator end = nodeList.end();
for(; it != end; it++)
{
double x = (*it)->x;
double y = (*it)->y;
QRectF boundingRect(x, y, nodeSize, nodeSize);
QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2);
addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue));
addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30);
addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
QGraphicsTextItem *nickItem = addText(QString::fromStdString((*it)->name().toUri()));
QRectF textBoundingRect = nickItem->boundingRect();
nickItem->setDefaultTextColor(Qt::white);
nickItem->setFont(QFont("Cursive", 8, QFont::Bold));
nickItem->setPos(x - nodeSize / 2 + 10, y + nodeSize + 5);
}
}
#if WAF
#include "trust-tree-scene.moc"
#include "trust-tree-scene.cpp.moc"
#endif