blob: b79962e3bcdb1b8e58d05f302b6806517b222302 [file] [log] [blame]
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -07001#include <QtGui>
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -07002#include <vector>
3#include <iostream>
4#include <assert.h>
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -07005#include <boost/lexical_cast.hpp>
Zhenkai Zhu21d75f92012-06-04 21:23:34 -07006#include <memory>
7#include "digesttreescene.h"
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -07008
9static const double Pi = 3.14159265358979323846264338327950288419717;
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070010
Zhenkai Zhud13acd02012-06-04 15:25:20 -070011DigestTreeScene::DisplayUserPtr DigestTreeScene::DisplayUserNullPtr;
12
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070013DigestTreeScene::DigestTreeScene(QWidget *parent)
14 : QGraphicsScene(parent)
15{
Zhenkai Zhud13acd02012-06-04 15:25:20 -070016 previouslyUpdatedUser = DisplayUserNullPtr;
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -070017}
18
19void
Zhenkai Zhu82a62752012-06-04 17:11:04 -070020DigestTreeScene::processUpdate(const std::vector<Sync::MissingDataInfo> &v, QString digest)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070021{
22 int n = v.size();
23 bool rePlot = false;
24 for (int i = 0; i < n; i++)
25 {
26 Roster_iterator it = m_roster.find(v[i].prefix.c_str());
Zhenkai Zhud1c5a972012-06-05 14:07:41 -070027 if (it == m_roster.end())
28 {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070029 rePlot = true;
30 DisplayUserPtr p(new DisplayUser());
Zhenkai Zhu86df7412012-09-27 16:30:20 -070031 time_t tempTime = time(NULL) - 2 * FRESHNESS + 1;
32 p->setReceived(tempTime);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070033 p->setPrefix(v[i].prefix.c_str());
34 p->setSeq(v[i].high);
35 m_roster.insert(p->getPrefix(), p);
36 }
Zhenkai Zhud1c5a972012-06-05 14:07:41 -070037 else
38 {
39 it.value()->setSeq(v[i].high);
40 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070041 }
42
43 if (rePlot)
44 {
45 plot(digest);
Zhenkai Zhu86df7412012-09-27 16:30:20 -070046 QTimer::singleShot(2100, this, SLOT(emitReplot()));
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070047 }
48 else
49 {
50 for (int i = 0; i < n; i++)
51 {
52 Roster_iterator it = m_roster.find(v[i].prefix.c_str());
53 if (it != m_roster.end()) {
54 DisplayUserPtr p = it.value();
55 QGraphicsTextItem *item = p->getSeqTextItem();
Zhenkai Zhud13acd02012-06-04 15:25:20 -070056 QGraphicsRectItem *rectItem = p->getInnerRectItem();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070057 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
58 item->setPlainText(s.c_str());
Zhenkai Zhud13acd02012-06-04 15:25:20 -070059 QRectF textBR = item->boundingRect();
60 QRectF rectBR = rectItem->boundingRect();
61 item->setPos(rectBR.x() + (rectBR.width() - textBR.width())/2, rectBR.y() + (rectBR.height() - textBR.height())/2);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070062 }
63 }
64 m_rootDigest->setPlainText(digest);
65 }
66}
67
68void
Zhenkai Zhu86df7412012-09-27 16:30:20 -070069DigestTreeScene::emitReplot()
70{
71 emit replot();
72}
73
74void
Zhenkai Zhu6082ede2012-09-27 17:28:46 -070075DigestTreeScene::getRosterList()
76{
77}
78
79void
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070080DigestTreeScene::msgReceived(QString prefix, QString nick)
81{
Zhenkai Zhu097bfe72012-06-05 14:30:17 -070082#ifdef __DEBUG
83 std::cout << "Finding " << prefix.toStdString() << std::endl;
84#endif
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070085 Roster_iterator it = m_roster.find(prefix);
86 if (it != m_roster.end())
87 {
88 DisplayUserPtr p = it.value();
Zhenkai Zhu86df7412012-09-27 16:30:20 -070089 p->setReceived(time(NULL));
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070090 if (nick != p->getNick()) {
91 p->setNick(nick);
92 QGraphicsTextItem *nickItem = p->getNickTextItem();
Zhenkai Zhud13acd02012-06-04 15:25:20 -070093 QGraphicsRectItem *nickRectItem = p->getNickRectItem();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070094 nickItem->setPlainText(p->getNick());
Zhenkai Zhud13acd02012-06-04 15:25:20 -070095 QRectF rectBR = nickRectItem->boundingRect();
96 QRectF nickBR = nickItem->boundingRect();
97 nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070098 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -070099
100 reDrawNode(p, Qt::red);
101
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700102 if (previouslyUpdatedUser != DisplayUserNullPtr && previouslyUpdatedUser != p)
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700103 {
104 reDrawNode(previouslyUpdatedUser, Qt::darkBlue);
105 }
106
107 previouslyUpdatedUser = p;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700108 }
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700109#ifdef __DEBUG
110 else
111 {
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700112 std::cout << "Couldn't find prefix, " << prefix.toStdString() << ": let's check"<< std::endl;
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700113 Roster_iterator ii = m_roster.begin();
114 while (ii != m_roster.end())
115 {
116 std::cout <<"Prefix: " << ii.key().toStdString() << std::endl;
117 ++ii;
118 }
119 }
120#endif
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700121}
122
123void
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700124DigestTreeScene::clearAll()
125{
126 clear();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700127 m_roster.clear();
128}
129
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700130bool
131DigestTreeScene::removeNode(const QString prefix)
132{
133 int removedCount = m_roster.remove(prefix);
134 return (removedCount > 0);
135}
136
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700137void
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700138DigestTreeScene::plot(QString digest)
139{
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700140#ifdef __DEBUG
141 std::cout << "Plotting at time: " << time(NULL) << std::endl;
142#endif
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -0700143 clear();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700144
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700145 int nodeSize = 40;
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700146
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700147 int siblingDistance = 100, levelDistance = 100;
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700148 std::auto_ptr<TreeLayout> layout(new OneLevelTreeLayout());
149 layout->setSiblingDistance(siblingDistance);
150 layout->setLevelDistance(levelDistance);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700151
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700152 // do some cleaning, get rid of stale member info
153 Roster_iterator it = m_roster.begin();
154 while (it != m_roster.end())
155 {
156 DisplayUserPtr p = it.value();
157 if (p != DisplayUserNullPtr)
158 {
159 time_t now = time(NULL);
160 if (now - p->getReceived() >= FRESHNESS * 2)
161 {
162#ifdef __DEBUG
163 std::cout << "Removing user: " << p->getNick().toStdString() << std::endl;
164 std::cout << "now - last = " << now - p->getReceived() << std::endl;
165#endif
166 p = DisplayUserNullPtr;
167 it = m_roster.erase(it);
168 }
169 else
170 {
171 ++it;
172 }
173 }
174 else
175 {
176 it = m_roster.erase(it);
177 }
178 }
179
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700180 // for simpicity here, whenever we replot, we also redo the roster list
181 emit rosterChanged();
182
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700183 int n = m_roster.size();
184 std::vector<TreeLayout::Coordinate> childNodesCo(n);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700185
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700186 layout->setOneLevelLayout(childNodesCo);
187
188 plotEdge(childNodesCo, nodeSize);
189 plotNode(childNodesCo, digest, nodeSize);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700190
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700191 previouslyUpdatedUser = DisplayUserNullPtr;
192
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700193}
194
195void
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700196DigestTreeScene::plotEdge(const std::vector<TreeLayout::Coordinate> &childNodesCo, int nodeSize)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700197{
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700198 int n = childNodesCo.size();
199 for (int i = 0; i < n; i++) {
200 double x1 = 0.0, y1 = 0.0;
201 double x2 = childNodesCo[i].x, y2 = childNodesCo[i].y;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700202 QPointF src(x1 + nodeSize/2, y1 + nodeSize/2);
203 QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700204 QLineF line(src, dest);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700205 double angle = ::acos(line.dx() / line.length());
206
207 double arrowSize = 10;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700208 QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length());
209 QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700210 sin(angle + Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700211 QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700212 sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700213
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700214 addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black));
215 addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
216 }
217}
218
219void
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700220DigestTreeScene::plotNode(const std::vector<TreeLayout::Coordinate> &childNodesCo, QString digest, int nodeSize)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700221{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700222 RosterIterator it(m_roster);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700223 int n = childNodesCo.size();
224 int rim = 3;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700225
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700226 // plot root node
227 QRectF rootBoundingRect(0, 0, nodeSize, nodeSize);
228 QRectF rootInnerBoundingRect(rim, rim, nodeSize - rim * 2, nodeSize - rim * 2);
229 addRect(rootBoundingRect, QPen(Qt::black), QBrush(Qt::darkRed));
230 addRect(rootInnerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700231 QRectF digestRect(- 5.5 * nodeSize , - nodeSize, 12 * nodeSize, 30);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700232 addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
Zhenkai Zhu27df8d42012-06-05 12:04:04 -0700233
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700234 QGraphicsTextItem *digestItem = addText(digest);
235 QRectF digestBoundingRect = digestItem->boundingRect();
Zhenkai Zhu27df8d42012-06-05 12:04:04 -0700236 digestItem->setDefaultTextColor(Qt::black);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700237 digestItem->setFont(QFont("Cursive", 12, QFont::Bold));
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700238 digestItem->setPos(- 4.5 * nodeSize + (12 * nodeSize - digestBoundingRect.width()) / 2, - nodeSize + 5);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700239 m_rootDigest = digestItem;
240
241 // plot child nodes
242 for (int i = 0; i < n; i++)
243 {
244 if (it.hasNext())
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700245 {
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700246 it.next();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700247 }
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700248 else
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700249 {
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700250 abort();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700251 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700252
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700253 double x = childNodesCo[i].x;
254 double y = childNodesCo[i].y;
255 QRectF boundingRect(x, y, nodeSize, nodeSize);
256 QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2);
257 DisplayUserPtr p = it.value();
258 QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue));
259 p->setRimRectItem(rectItem);
260
261 QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
262 p->setInnerRectItem(innerRectItem);
263
264 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
265 QGraphicsTextItem *seqItem = addText(s.c_str());
266 seqItem->setFont(QFont("Cursive", 12, QFont::Bold));
267 QRectF seqBoundingRect = seqItem->boundingRect();
268 seqItem->setPos(x + nodeSize / 2 - seqBoundingRect.width() / 2, y + nodeSize / 2 - seqBoundingRect.height() / 2);
269 p->setSeqTextItem(seqItem);
270
271 QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30);
272 QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
273 p->setNickRectItem(nickRectItem);
274 QGraphicsTextItem *nickItem = addText(p->getNick());
275 QRectF textBoundingRect = nickItem->boundingRect();
276 nickItem->setDefaultTextColor(Qt::white);
277 nickItem->setFont(QFont("Cursive", 12, QFont::Bold));
278 nickItem->setPos(x + nodeSize / 2 - textBoundingRect.width() / 2, y + nodeSize + 5);
279 p->setNickTextItem(nickItem);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700280 }
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700281
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700282}
283
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700284void
285DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor)
286{
287 QGraphicsRectItem *rimItem = p->getRimRectItem();
288 rimItem->setBrush(QBrush(rimColor));
289 QGraphicsRectItem *innerItem = p->getInnerRectItem();
290 innerItem->setBrush(QBrush(Qt::lightGray));
291 QGraphicsTextItem *seqTextItem = p->getSeqTextItem();
292 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
293 seqTextItem->setPlainText(s.c_str());
294 QRectF textBR = seqTextItem->boundingRect();
295 QRectF innerBR = innerItem->boundingRect();
296 seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2);
297}
298