blob: 5133e4c5cee8c9db6287303fdae44c8b5b6924db [file] [log] [blame]
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -07001#include "digesttreescene.h"
2#include <QtGui>
3#include "ogdf/basic/Array.h"
4#include "ogdf/basic/Graph_d.h"
5#include "ogdf/tree/TreeLayout.h"
6#include <vector>
7#include <iostream>
8#include <assert.h>
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -07009#include <boost/lexical_cast.hpp>
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070010
11static const double Pi = 3.14159265358979323846264338327950288419717;
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070012
Zhenkai Zhud13acd02012-06-04 15:25:20 -070013static void
14testDraw(DigestTreeScene * scene)
15{
16 std::string prefix[5] = {"/ndn/1", "/ndn/2", "/ndn/3", "/ndn/4", "/ndn/5"};
17 std::string nick[5] = {"tom", "jerry", "jason", "michael", "hurry"};
18 std::vector<Sync::MissingDataInfo> v;
19 for (int i = 0; i < 5; i++)
20 {
21 Sync::MissingDataInfo mdi = {prefix[i], Sync::SeqNo(0), Sync::SeqNo(i * (2 << i) )};
22 v.push_back(mdi);
23 }
24
25 scene->processUpdate(v, "12341234@!#%!@");
26
27 for (int i = 0; i < 5; i++)
28 {
29 scene-> msgReceived(prefix[i].c_str(), nick[i].c_str());
30 }
31}
32
33DigestTreeScene::DisplayUserPtr DigestTreeScene::DisplayUserNullPtr;
34
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070035DigestTreeScene::DigestTreeScene(QWidget *parent)
36 : QGraphicsScene(parent)
37{
Zhenkai Zhud13acd02012-06-04 15:25:20 -070038 previouslyUpdatedUser = DisplayUserNullPtr;
39 testDraw(this);
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -070040}
41
42void
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070043DigestTreeScene::processUpdate(std::vector<Sync::MissingDataInfo> &v, QString digest)
44{
45 int n = v.size();
46 bool rePlot = false;
47 for (int i = 0; i < n; i++)
48 {
49 Roster_iterator it = m_roster.find(v[i].prefix.c_str());
50 if (it == m_roster.end()) {
51 rePlot = true;
52 DisplayUserPtr p(new DisplayUser());
53 p->setPrefix(v[i].prefix.c_str());
54 p->setSeq(v[i].high);
55 m_roster.insert(p->getPrefix(), p);
56 }
57 }
58
59 if (rePlot)
60 {
61 plot(digest);
62 }
63 else
64 {
65 for (int i = 0; i < n; i++)
66 {
67 Roster_iterator it = m_roster.find(v[i].prefix.c_str());
68 if (it != m_roster.end()) {
69 DisplayUserPtr p = it.value();
70 QGraphicsTextItem *item = p->getSeqTextItem();
Zhenkai Zhud13acd02012-06-04 15:25:20 -070071 QGraphicsRectItem *rectItem = p->getInnerRectItem();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070072 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
73 item->setPlainText(s.c_str());
Zhenkai Zhud13acd02012-06-04 15:25:20 -070074 QRectF textBR = item->boundingRect();
75 QRectF rectBR = rectItem->boundingRect();
76 item->setPos(rectBR.x() + (rectBR.width() - textBR.width())/2, rectBR.y() + (rectBR.height() - textBR.height())/2);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070077 }
78 }
79 m_rootDigest->setPlainText(digest);
80 }
81}
82
83void
Zhenkai Zhud13acd02012-06-04 15:25:20 -070084DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor)
85{
86 QGraphicsRectItem *rimItem = p->getRimRectItem();
87 rimItem->setBrush(QBrush(rimColor));
88 QGraphicsRectItem *innerItem = p->getInnerRectItem();
89 innerItem->setBrush(QBrush(Qt::lightGray));
90 QGraphicsTextItem *seqTextItem = p->getSeqTextItem();
91 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
92 seqTextItem->setPlainText(s.c_str());
93 QRectF textBR = seqTextItem->boundingRect();
94 QRectF innerBR = innerItem->boundingRect();
95 seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2);
96}
97
98void
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070099DigestTreeScene::msgReceived(QString prefix, QString nick)
100{
101 Roster_iterator it = m_roster.find(prefix);
102 if (it != m_roster.end())
103 {
104 DisplayUserPtr p = it.value();
105 if (nick != p->getNick()) {
106 p->setNick(nick);
107 QGraphicsTextItem *nickItem = p->getNickTextItem();
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700108 QGraphicsRectItem *nickRectItem = p->getNickRectItem();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700109 nickItem->setPlainText(p->getNick());
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700110 QRectF rectBR = nickRectItem->boundingRect();
111 QRectF nickBR = nickItem->boundingRect();
112 nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700113 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700114
115 reDrawNode(p, Qt::red);
116
117 if (previouslyUpdatedUser != DisplayUserNullPtr)
118 {
119 reDrawNode(previouslyUpdatedUser, Qt::darkBlue);
120 }
121
122 previouslyUpdatedUser = p;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700123 }
124}
125
126void
127DigestTreeScene::plot(QString digest)
128{
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -0700129 clear();
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700130 m_graph.clear();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700131 int n = m_roster.size();
132 ogdf::node root = m_graph.newNode();
133 int rootIndex = root->index();
134 for (int i = 0; i < n; i++) {
135 ogdf::node leaf = m_graph.newNode();
136 m_graph.newEdge(root, leaf);
137 }
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700138 ogdf::GraphAttributes GA(m_graph);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700139
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700140 int nodeSize = 40;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700141 int siblingDistance = 100, levelDistance = 100;
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700142 ogdf::TreeLayout layout;
143 layout.siblingDistance(siblingDistance);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700144 layout.levelDistance(levelDistance);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700145 layout.callSortByPositions(GA, m_graph);
146
147 int width = GA.boundingBox().width();
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700148 int height = GA.boundingBox().height();
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700149 //setSceneRect(QRect(- (width + nodeSize) / 2, - 50, width + nodeSize, height + nodeSize));
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700150 GA.setAllWidth(nodeSize);
151 GA.setAllHeight(nodeSize);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700152
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700153 plotEdge(GA);
154 plotNode(GA, rootIndex, digest);
155
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700156 previouslyUpdatedUser = DisplayUserNullPtr;
157
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700158}
159
160void
161DigestTreeScene::plotEdge(ogdf::GraphAttributes &GA)
162{
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700163 ogdf::edge e;
164 forall_edges(e, m_graph) {
165 ogdf::node source = e->source();
166 ogdf::node target = e->target();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700167 int nodeSize = GA.width(target);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700168 int x1 = GA.x(source), y1 = -GA.y(source);
169 int x2 = GA.x(target), y2 = -GA.y(target);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700170 QPointF src(x1 + nodeSize/2, y1 + nodeSize/2);
171 QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700172 QLineF line(src, dest);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700173 double angle = ::acos(line.dx() / line.length());
174
175 double arrowSize = 10;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700176 QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length());
177 QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700178 sin(angle + Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700179 QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700180 sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700181
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700182 addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black));
183 addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
184 }
185}
186
187void
188DigestTreeScene::plotNode(ogdf::GraphAttributes &GA, int rootIndex, QString digest)
189{
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700190 ogdf::node n;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700191 RosterIterator it(m_roster);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700192 forall_nodes(n, m_graph) {
193 double x = GA.x(n);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700194 double y = -GA.y(n);
195 double w = GA.width(n);
196 double h = GA.height(n);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700197 int rim = 3;
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700198 QRectF boundingRect(x, y, w, h);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700199 QRectF innerBoundingRect(x + rim, y + rim, w - rim * 2, h - rim * 2);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700200
201 if (n->index() == rootIndex)
202 {
203 addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkRed));
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700204 addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700205
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700206 QRectF digestRect(x - w, y - h, 3 * w, 30);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700207 addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
208 QGraphicsTextItem *digestItem = addText(digest);
209 QRectF digestBoundingRect = digestItem->boundingRect();
210 digestItem->setDefaultTextColor(Qt::white);
211 digestItem->setFont(QFont("Cursive", 12, QFont::Bold));
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700212 digestItem->setPos(x - w + (3 * w - digestBoundingRect.width()) / 2, y - h + 5);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700213 m_rootDigest = digestItem;
214 }
215 else
216 {
217 if (it.hasNext())
218 {
219 it.next();
220 }
221 else
222 {
223 abort();
224 }
225 DisplayUserPtr p = it.value();
226 QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue));
227 p->setRimRectItem(rectItem);
228
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700229 QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
230 p->setInnerRectItem(innerRectItem);
231
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700232 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
233 QGraphicsTextItem *seqItem = addText(s.c_str());
234 seqItem->setFont(QFont("Cursive", 12, QFont::Bold));
235 QRectF seqBoundingRect = seqItem->boundingRect();
236 seqItem->setPos(x + w / 2 - seqBoundingRect.width() / 2, y + h / 2 - seqBoundingRect.height() / 2);
237 p->setSeqTextItem(seqItem);
238
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700239 QRectF textRect(x - w / 2, y + h, 2 * w, 30);
240 QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
241 p->setNickRectItem(nickRectItem);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700242 QGraphicsTextItem *nickItem = addText(p->getNick());
243 QRectF textBoundingRect = nickItem->boundingRect();
244 nickItem->setDefaultTextColor(Qt::white);
245 nickItem->setFont(QFont("Cursive", 12, QFont::Bold));
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700246 nickItem->setPos(x + w / 2 - textBoundingRect.width() / 2, y + h + 5);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700247 p->setNickTextItem(nickItem);
248 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700249
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700250 }
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700251}
252