blob: d692d4e5a3d90c5f67dfa8070523f63b756fea9a [file] [log] [blame]
Yingdi Yu42f66462013-10-31 17:38:22 -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: Zhenkai Zhu <zhenkai@cs.ucla.edu>
9 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
10 */
11
12#include "digesttreescene.h"
13
14#include <QtGui>
15
16#ifndef Q_MOC_RUN
17#include <vector>
18#include <iostream>
19#include <assert.h>
20#include <boost/lexical_cast.hpp>
21#include <memory>
22#endif
23
24static const double Pi = 3.14159265358979323846264338327950288419717;
25
26//DisplayUserPtr DisplayUserNullPtr;
27
Yingdi Yua524a262014-02-11 22:54:11 -080028DigestTreeScene::DigestTreeScene(ndn::shared_ptr<boost::asio::io_service> ioService,
29 QWidget *parent)
Yingdi Yu42f66462013-10-31 17:38:22 -070030 : QGraphicsScene(parent)
Yingdi Yua524a262014-02-11 22:54:11 -080031 , m_scheduler(*ioService)
Yingdi Yu42f66462013-10-31 17:38:22 -070032{
33 previouslyUpdatedUser = DisplayUserNullPtr;
34}
35
36void
37DigestTreeScene::processUpdate(const std::vector<Sync::MissingDataInfo> &v, QString digest)
38{
39 int n = v.size();
40 bool rePlot = false;
41 for (int i = 0; i < n; i++)
42 {
43 Roster_iterator it = m_roster.find(v[i].prefix.c_str());
44 if (it == m_roster.end())
45 {
46 rePlot = true;
47 DisplayUserPtr p(new DisplayUser());
48 time_t tempTime = time(NULL) - FRESHNESS + 1;
49 p->setReceived(tempTime);
50 p->setPrefix(v[i].prefix.c_str());
51 p->setSeq(v[i].high);
52 m_roster.insert(p->getPrefix(), p);
53 }
54 else
55 {
56 it.value()->setSeq(v[i].high);
57 }
58 }
59
60 if (rePlot)
61 {
62 plot(digest);
Yingdi Yua524a262014-02-11 22:54:11 -080063 m_scheduler.scheduleEvent(ndn::time::milliseconds(600), ndn::bind(&DigestTreeScene::emitReplot, this));
Yingdi Yu42f66462013-10-31 17:38:22 -070064 }
65 else
66 {
67 for (int i = 0; i < n; i++)
68 {
69 Roster_iterator it = m_roster.find(v[i].prefix.c_str());
70 if (it != m_roster.end()) {
71 DisplayUserPtr p = it.value();
72 QGraphicsTextItem *item = p->getSeqTextItem();
73 QGraphicsRectItem *rectItem = p->getInnerRectItem();
74 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
75 item->setPlainText(s.c_str());
76 QRectF textBR = item->boundingRect();
77 QRectF rectBR = rectItem->boundingRect();
78 item->setPos(rectBR.x() + (rectBR.width() - textBR.width())/2, rectBR.y() + (rectBR.height() - textBR.height())/2);
79 }
80 }
81 m_rootDigest->setPlainText(digest);
82 }
83}
84
85void
86DigestTreeScene::emitReplot()
87{
88 emit replot();
89}
90
91QStringList
92DigestTreeScene::getRosterList()
93{
94 QStringList rosterList;
95 RosterIterator it(m_roster);
96 while(it.hasNext())
97 {
98 it.next();
99 DisplayUserPtr p = it.value();
100 if (p != DisplayUserNullPtr)
101 {
102 rosterList << "- " + p->getNick();
103 }
104 }
105 return rosterList;
106}
107
108void
109DigestTreeScene::msgReceived(QString prefix, QString nick)
110{
111 Roster_iterator it = m_roster.find(prefix);
112 if (it != m_roster.end())
113 {
114 std::cout << "Updating for prefix = " << prefix.toStdString() << " nick = " << nick.toStdString() << std::endl;
115 DisplayUserPtr p = it.value();
116 p->setReceived(time(NULL));
117 if (nick != p->getNick()) {
118 std::cout << "old nick = " << p->getNick().toStdString() << std::endl;
119 p->setNick(nick);
120 QGraphicsTextItem *nickItem = p->getNickTextItem();
121 QGraphicsRectItem *nickRectItem = p->getNickRectItem();
122 nickItem->setPlainText(p->getNick());
123 QRectF rectBR = nickRectItem->boundingRect();
124 QRectF nickBR = nickItem->boundingRect();
125 nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5);
126 emit rosterChanged(QStringList());
127 }
128
129 reDrawNode(p, Qt::red);
130
131 if (previouslyUpdatedUser != DisplayUserNullPtr && previouslyUpdatedUser != p)
132 {
133 reDrawNode(previouslyUpdatedUser, Qt::darkBlue);
134 }
135
136 previouslyUpdatedUser = p;
137 }
138}
139
140void
141DigestTreeScene::clearAll()
142{
143 clear();
144 m_roster.clear();
145}
146
147bool
148DigestTreeScene::removeNode(const QString prefix)
149{
150 int removedCount = m_roster.remove(prefix);
151 return (removedCount > 0);
152}
153
154void
155DigestTreeScene::plot(QString digest)
156{
Yingdi Yua1a688f2014-02-06 18:09:22 -0800157#ifdef _DEBUG
Yingdi Yu42f66462013-10-31 17:38:22 -0700158 std::cout << "Plotting at time: " << time(NULL) << std::endl;
159#endif
160 clear();
161
162 int nodeSize = 40;
163
164 int siblingDistance = 100, levelDistance = 100;
165 std::auto_ptr<TreeLayout> layout(new OneLevelTreeLayout());
166 layout->setSiblingDistance(siblingDistance);
167 layout->setLevelDistance(levelDistance);
168
169 // do some cleaning, get rid of stale member info
170 Roster_iterator it = m_roster.begin();
171 QStringList staleUserList;
172 while (it != m_roster.end())
173 {
174 DisplayUserPtr p = it.value();
175 if (p != DisplayUserNullPtr)
176 {
177 time_t now = time(NULL);
178 if (now - p->getReceived() >= FRESHNESS)
179 {
Yingdi Yua1a688f2014-02-06 18:09:22 -0800180#ifdef _DEBUG
Yingdi Yu42f66462013-10-31 17:38:22 -0700181 std::cout << "Removing user: " << p->getNick().toStdString() << std::endl;
182 std::cout << "now - last = " << now - p->getReceived() << std::endl;
183#endif
184 staleUserList << p->getNick();
185 p = DisplayUserNullPtr;
186 it = m_roster.erase(it);
187 }
188 else
189 {
190 if (!m_currentPrefix.startsWith("/private/local") && p->getPrefix().startsWith("/private/local"))
191 {
Yingdi Yua1a688f2014-02-06 18:09:22 -0800192#ifdef _DEBUG
Yingdi Yu42f66462013-10-31 17:38:22 -0700193 std::cout << "erasing: " << p->getPrefix().toStdString() << std::endl;
194#endif
195 staleUserList << p->getNick();
196 p = DisplayUserNullPtr;
197 it = m_roster.erase(it);
198 continue;
199 }
200 ++it;
201 }
202 }
203 else
204 {
205 it = m_roster.erase(it);
206 }
207 }
208
209 // for simpicity here, whenever we replot, we also redo the roster list
210 emit rosterChanged(staleUserList);
211
212 int n = m_roster.size();
213
214 std::vector<TreeLayout::Coordinate> childNodesCo(n);
215
216 layout->setOneLevelLayout(childNodesCo);
217
218 plotEdge(childNodesCo, nodeSize);
219 plotNode(childNodesCo, digest, nodeSize);
220
221 previouslyUpdatedUser = DisplayUserNullPtr;
222
223}
224
225void
226DigestTreeScene::plotEdge(const std::vector<TreeLayout::Coordinate> &childNodesCo, int nodeSize)
227{
228 int n = childNodesCo.size();
229 for (int i = 0; i < n; i++) {
230 double x1 = 0.0, y1 = 0.0;
231 double x2 = childNodesCo[i].x, y2 = childNodesCo[i].y;
232 QPointF src(x1 + nodeSize/2, y1 + nodeSize/2);
233 QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2);
234 QLineF line(src, dest);
235 double angle = ::acos(line.dx() / line.length());
236
237 double arrowSize = 10;
238 QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length());
239 QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize,
240 sin(angle + Pi / 3 - Pi/2) * arrowSize);
241 QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize,
242 sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize);
243
244 addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black));
245 addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
246 }
247}
248
249void
250DigestTreeScene::plotNode(const std::vector<TreeLayout::Coordinate> &childNodesCo, QString digest, int nodeSize)
251{
252 RosterIterator it(m_roster);
253 int n = childNodesCo.size();
254 int rim = 3;
255
256 // plot root node
257 QRectF rootBoundingRect(0, 0, nodeSize, nodeSize);
258 QRectF rootInnerBoundingRect(rim, rim, nodeSize - rim * 2, nodeSize - rim * 2);
259 addRect(rootBoundingRect, QPen(Qt::black), QBrush(Qt::darkRed));
260 addRect(rootInnerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
261 QRectF digestRect(- 5.5 * nodeSize , - nodeSize, 12 * nodeSize, 30);
262 addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
263
264 QGraphicsTextItem *digestItem = addText(digest);
265 QRectF digestBoundingRect = digestItem->boundingRect();
266 digestItem->setDefaultTextColor(Qt::black);
267 digestItem->setFont(QFont("Cursive", 12, QFont::Bold));
268 digestItem->setPos(- 4.5 * nodeSize + (12 * nodeSize - digestBoundingRect.width()) / 2, - nodeSize + 5);
269 m_rootDigest = digestItem;
270
271 // plot child nodes
272 for (int i = 0; i < n; i++)
273 {
274 if (it.hasNext())
275 {
276 it.next();
277 }
278 else
279 {
280 abort();
281 }
282
283 double x = childNodesCo[i].x;
284 double y = childNodesCo[i].y;
285 QRectF boundingRect(x, y, nodeSize, nodeSize);
286 QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2);
287 DisplayUserPtr p = it.value();
288 QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue));
289 p->setRimRectItem(rectItem);
290
291 QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
292 p->setInnerRectItem(innerRectItem);
293
294 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
295 QGraphicsTextItem *seqItem = addText(s.c_str());
296 seqItem->setFont(QFont("Cursive", 12, QFont::Bold));
297 QRectF seqBoundingRect = seqItem->boundingRect();
298 seqItem->setPos(x + nodeSize / 2 - seqBoundingRect.width() / 2, y + nodeSize / 2 - seqBoundingRect.height() / 2);
299 p->setSeqTextItem(seqItem);
300
301 QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30);
302 QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
303 p->setNickRectItem(nickRectItem);
304 QGraphicsTextItem *nickItem = addText(p->getNick());
305 QRectF textBoundingRect = nickItem->boundingRect();
306 nickItem->setDefaultTextColor(Qt::white);
307 nickItem->setFont(QFont("Cursive", 12, QFont::Bold));
308 nickItem->setPos(x + nodeSize / 2 - textBoundingRect.width() / 2, y + nodeSize + 5);
309 p->setNickTextItem(nickItem);
310 }
311
312}
313
314void
315DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor)
316{
317 QGraphicsRectItem *rimItem = p->getRimRectItem();
318 rimItem->setBrush(QBrush(rimColor));
319 QGraphicsRectItem *innerItem = p->getInnerRectItem();
320 innerItem->setBrush(QBrush(Qt::lightGray));
321 QGraphicsTextItem *seqTextItem = p->getSeqTextItem();
322 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
323 seqTextItem->setPlainText(s.c_str());
324 QRectF textBR = seqTextItem->boundingRect();
325 QRectF innerBR = innerItem->boundingRect();
326 seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2);
327}
328
329#if WAF
330#include "digesttreescene.moc"
331#include "digesttreescene.cpp.moc"
332#endif