blob: 14f95bd7d25258fd8345ac8ba3922da1299c9c5f [file] [log] [blame]
Yingdi Yu7989eb22013-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
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070012#include "digest-tree-scene.h"
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070013
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070014#include <QtGui>
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070015
16#ifndef Q_MOC_RUN
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070017#include <vector>
18#include <iostream>
19#include <assert.h>
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070020#include <boost/lexical_cast.hpp>
Zhenkai Zhu21d75f92012-06-04 21:23:34 -070021#include <memory>
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070022#endif
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070023
24static const double Pi = 3.14159265358979323846264338327950288419717;
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070025
Zhenkai Zhu7f52e1b2012-10-09 11:45:36 -070026//DisplayUserPtr DisplayUserNullPtr;
Zhenkai Zhud13acd02012-06-04 15:25:20 -070027
Yingdi Yu348f5ea2014-03-01 14:47:25 -080028DigestTreeScene::DigestTreeScene(QWidget *parent)
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070029 : QGraphicsScene(parent)
30{
Zhenkai Zhud13acd02012-06-04 15:25:20 -070031 previouslyUpdatedUser = DisplayUserNullPtr;
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -070032}
33
34void
Zhenkai Zhu82a62752012-06-04 17:11:04 -070035DigestTreeScene::processUpdate(const std::vector<Sync::MissingDataInfo> &v, QString digest)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070036{
37 int n = v.size();
38 bool rePlot = false;
39 for (int i = 0; i < n; i++)
40 {
41 Roster_iterator it = m_roster.find(v[i].prefix.c_str());
Zhenkai Zhud1c5a972012-06-05 14:07:41 -070042 if (it == m_roster.end())
43 {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070044 rePlot = true;
45 DisplayUserPtr p(new DisplayUser());
Zhenkai Zhuc61dbc22012-10-08 11:36:37 -070046 time_t tempTime = time(NULL) - FRESHNESS + 1;
Zhenkai Zhu86df7412012-09-27 16:30:20 -070047 p->setReceived(tempTime);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070048 p->setPrefix(v[i].prefix.c_str());
49 p->setSeq(v[i].high);
50 m_roster.insert(p->getPrefix(), p);
51 }
Zhenkai Zhud1c5a972012-06-05 14:07:41 -070052 else
53 {
54 it.value()->setSeq(v[i].high);
55 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070056 }
57
58 if (rePlot)
59 {
60 plot(digest);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080061 QTimer::singleShot(2100, this, SLOT(emitReplot()));
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070062 }
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 Zhu86df7412012-09-27 16:30:20 -070084DigestTreeScene::emitReplot()
85{
86 emit replot();
87}
88
Zhenkai Zhu9036e032012-09-27 20:59:33 -070089QStringList
Zhenkai Zhu6082ede2012-09-27 17:28:46 -070090DigestTreeScene::getRosterList()
91{
Zhenkai Zhu9036e032012-09-27 20:59:33 -070092 QStringList rosterList;
93 RosterIterator it(m_roster);
94 while(it.hasNext())
95 {
96 it.next();
97 DisplayUserPtr p = it.value();
98 if (p != DisplayUserNullPtr)
99 {
100 rosterList << "- " + p->getNick();
101 }
102 }
103 return rosterList;
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700104}
105
106void
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700107DigestTreeScene::msgReceived(QString prefix, QString nick)
108{
109 Roster_iterator it = m_roster.find(prefix);
110 if (it != m_roster.end())
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700111 {
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700112 std::cout << "Updating for prefix = " << prefix.toStdString() << " nick = " << nick.toStdString() << std::endl;
113 DisplayUserPtr p = it.value();
114 p->setReceived(time(NULL));
115 if (nick != p->getNick())
116 {
117 std::cout << "old nick = " << p->getNick().toStdString() << std::endl;
118 p->setNick(nick);
119 QGraphicsTextItem *nickItem = p->getNickTextItem();
120 QGraphicsRectItem *nickRectItem = p->getNickRectItem();
121 nickItem->setPlainText(p->getNick());
122 QRectF rectBR = nickRectItem->boundingRect();
123 QRectF nickBR = nickItem->boundingRect();
124 nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5);
125 emit rosterChanged(QStringList());
126 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700127
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700128 reDrawNode(p, Qt::red);
129
130 if (previouslyUpdatedUser != DisplayUserNullPtr && previouslyUpdatedUser != p)
131 {
132 reDrawNode(previouslyUpdatedUser, Qt::darkBlue);
133 }
134
135 previouslyUpdatedUser = p;
136 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700137}
138
139void
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700140DigestTreeScene::clearAll()
141{
142 clear();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700143 m_roster.clear();
144}
145
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700146bool
147DigestTreeScene::removeNode(const QString prefix)
148{
149 int removedCount = m_roster.remove(prefix);
150 return (removedCount > 0);
151}
152
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700153void
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700154DigestTreeScene::plot(QString digest)
155{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800156#ifdef _DEBUG
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700157 std::cout << "Plotting at time: " << time(NULL) << std::endl;
158#endif
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -0700159 clear();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700160
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700161 int nodeSize = 40;
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700162
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700163 int siblingDistance = 100, levelDistance = 100;
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700164 std::auto_ptr<TreeLayout> layout(new OneLevelTreeLayout());
165 layout->setSiblingDistance(siblingDistance);
166 layout->setLevelDistance(levelDistance);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700167
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700168 // do some cleaning, get rid of stale member info
169 Roster_iterator it = m_roster.begin();
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700170 QStringList staleUserList;
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700171 while (it != m_roster.end())
172 {
173 DisplayUserPtr p = it.value();
174 if (p != DisplayUserNullPtr)
175 {
176 time_t now = time(NULL);
Zhenkai Zhuc61dbc22012-10-08 11:36:37 -0700177 if (now - p->getReceived() >= FRESHNESS)
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700178 {
Yingdi Yufa4ce792014-02-06 18:09:22 -0800179#ifdef _DEBUG
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700180 std::cout << "Removing user: " << p->getNick().toStdString() << std::endl;
181 std::cout << "now - last = " << now - p->getReceived() << std::endl;
182#endif
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700183 staleUserList << p->getNick();
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700184 p = DisplayUserNullPtr;
185 it = m_roster.erase(it);
186 }
187 else
188 {
Zhenkai Zhu716fe852012-10-08 18:27:55 -0700189 if (!m_currentPrefix.startsWith("/private/local") && p->getPrefix().startsWith("/private/local"))
190 {
Yingdi Yufa4ce792014-02-06 18:09:22 -0800191#ifdef _DEBUG
Zhenkai Zhu716fe852012-10-08 18:27:55 -0700192 std::cout << "erasing: " << p->getPrefix().toStdString() << std::endl;
193#endif
194 staleUserList << p->getNick();
195 p = DisplayUserNullPtr;
196 it = m_roster.erase(it);
197 continue;
198 }
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700199 ++it;
200 }
201 }
202 else
203 {
204 it = m_roster.erase(it);
205 }
206 }
207
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700208 // for simpicity here, whenever we replot, we also redo the roster list
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700209 emit rosterChanged(staleUserList);
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700210
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700211 int n = m_roster.size();
Zhenkai Zhu716fe852012-10-08 18:27:55 -0700212
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700213 std::vector<TreeLayout::Coordinate> childNodesCo(n);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700214
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700215 layout->setOneLevelLayout(childNodesCo);
216
217 plotEdge(childNodesCo, nodeSize);
218 plotNode(childNodesCo, digest, nodeSize);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700219
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700220 previouslyUpdatedUser = DisplayUserNullPtr;
221
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700222}
223
224void
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700225DigestTreeScene::plotEdge(const std::vector<TreeLayout::Coordinate> &childNodesCo, int nodeSize)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700226{
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700227 int n = childNodesCo.size();
228 for (int i = 0; i < n; i++) {
229 double x1 = 0.0, y1 = 0.0;
230 double x2 = childNodesCo[i].x, y2 = childNodesCo[i].y;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700231 QPointF src(x1 + nodeSize/2, y1 + nodeSize/2);
232 QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700233 QLineF line(src, dest);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700234 double angle = ::acos(line.dx() / line.length());
235
236 double arrowSize = 10;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700237 QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length());
238 QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700239 sin(angle + Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700240 QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700241 sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700242
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700243 addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black));
244 addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
245 }
246}
247
248void
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700249DigestTreeScene::plotNode(const std::vector<TreeLayout::Coordinate> &childNodesCo, QString digest, int nodeSize)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700250{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700251 RosterIterator it(m_roster);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700252 int n = childNodesCo.size();
253 int rim = 3;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700254
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700255 // plot root node
256 QRectF rootBoundingRect(0, 0, nodeSize, nodeSize);
257 QRectF rootInnerBoundingRect(rim, rim, nodeSize - rim * 2, nodeSize - rim * 2);
258 addRect(rootBoundingRect, QPen(Qt::black), QBrush(Qt::darkRed));
259 addRect(rootInnerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700260 QRectF digestRect(- 5.5 * nodeSize , - nodeSize, 12 * nodeSize, 30);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700261 addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
Zhenkai Zhu27df8d42012-06-05 12:04:04 -0700262
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700263 QGraphicsTextItem *digestItem = addText(digest);
264 QRectF digestBoundingRect = digestItem->boundingRect();
Zhenkai Zhu27df8d42012-06-05 12:04:04 -0700265 digestItem->setDefaultTextColor(Qt::black);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700266 digestItem->setFont(QFont("Cursive", 12, QFont::Bold));
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700267 digestItem->setPos(- 4.5 * nodeSize + (12 * nodeSize - digestBoundingRect.width()) / 2, - nodeSize + 5);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700268 m_rootDigest = digestItem;
269
270 // plot child nodes
271 for (int i = 0; i < n; i++)
272 {
273 if (it.hasNext())
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700274 {
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700275 it.next();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700276 }
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700277 else
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700278 {
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700279 abort();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700280 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700281
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700282 double x = childNodesCo[i].x;
283 double y = childNodesCo[i].y;
284 QRectF boundingRect(x, y, nodeSize, nodeSize);
285 QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2);
286 DisplayUserPtr p = it.value();
287 QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue));
288 p->setRimRectItem(rectItem);
289
290 QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
291 p->setInnerRectItem(innerRectItem);
292
293 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
294 QGraphicsTextItem *seqItem = addText(s.c_str());
295 seqItem->setFont(QFont("Cursive", 12, QFont::Bold));
296 QRectF seqBoundingRect = seqItem->boundingRect();
297 seqItem->setPos(x + nodeSize / 2 - seqBoundingRect.width() / 2, y + nodeSize / 2 - seqBoundingRect.height() / 2);
298 p->setSeqTextItem(seqItem);
299
300 QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30);
301 QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
302 p->setNickRectItem(nickRectItem);
303 QGraphicsTextItem *nickItem = addText(p->getNick());
304 QRectF textBoundingRect = nickItem->boundingRect();
305 nickItem->setDefaultTextColor(Qt::white);
306 nickItem->setFont(QFont("Cursive", 12, QFont::Bold));
307 nickItem->setPos(x + nodeSize / 2 - textBoundingRect.width() / 2, y + nodeSize + 5);
308 p->setNickTextItem(nickItem);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700309 }
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700310
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700311}
312
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700313void
314DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor)
315{
316 QGraphicsRectItem *rimItem = p->getRimRectItem();
317 rimItem->setBrush(QBrush(rimColor));
318 QGraphicsRectItem *innerItem = p->getInnerRectItem();
319 innerItem->setBrush(QBrush(Qt::lightGray));
320 QGraphicsTextItem *seqTextItem = p->getSeqTextItem();
321 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
322 seqTextItem->setPlainText(s.c_str());
323 QRectF textBR = seqTextItem->boundingRect();
324 QRectF innerBR = innerItem->boundingRect();
325 seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2);
326}
327
Alexander Afanasyevb4b92292013-07-09 13:54:59 -0700328#if WAF
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700329#include "digest-tree-scene.moc"
330#include "digest-tree-scene.cpp.moc"
Alexander Afanasyevb4b92292013-07-09 13:54:59 -0700331#endif