blob: 2bb1c537f4035bf79666f178b4417cfc77076e53 [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 Zhu71b42cb2012-06-04 09:42:53 -070075DigestTreeScene::msgReceived(QString prefix, QString nick)
76{
Zhenkai Zhu097bfe72012-06-05 14:30:17 -070077#ifdef __DEBUG
78 std::cout << "Finding " << prefix.toStdString() << std::endl;
79#endif
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070080 Roster_iterator it = m_roster.find(prefix);
81 if (it != m_roster.end())
82 {
83 DisplayUserPtr p = it.value();
Zhenkai Zhu86df7412012-09-27 16:30:20 -070084 p->setReceived(time(NULL));
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070085 if (nick != p->getNick()) {
86 p->setNick(nick);
87 QGraphicsTextItem *nickItem = p->getNickTextItem();
Zhenkai Zhud13acd02012-06-04 15:25:20 -070088 QGraphicsRectItem *nickRectItem = p->getNickRectItem();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070089 nickItem->setPlainText(p->getNick());
Zhenkai Zhud13acd02012-06-04 15:25:20 -070090 QRectF rectBR = nickRectItem->boundingRect();
91 QRectF nickBR = nickItem->boundingRect();
92 nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070093 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -070094
95 reDrawNode(p, Qt::red);
96
Zhenkai Zhud1c5a972012-06-05 14:07:41 -070097 if (previouslyUpdatedUser != DisplayUserNullPtr && previouslyUpdatedUser != p)
Zhenkai Zhud13acd02012-06-04 15:25:20 -070098 {
99 reDrawNode(previouslyUpdatedUser, Qt::darkBlue);
100 }
101
102 previouslyUpdatedUser = p;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700103 }
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700104#ifdef __DEBUG
105 else
106 {
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700107 std::cout << "Couldn't find prefix, " << prefix.toStdString() << ": let's check"<< std::endl;
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700108 Roster_iterator ii = m_roster.begin();
109 while (ii != m_roster.end())
110 {
111 std::cout <<"Prefix: " << ii.key().toStdString() << std::endl;
112 ++ii;
113 }
114 }
115#endif
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700116}
117
118void
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700119DigestTreeScene::clearAll()
120{
121 clear();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700122 m_roster.clear();
123}
124
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700125bool
126DigestTreeScene::removeNode(const QString prefix)
127{
128 int removedCount = m_roster.remove(prefix);
129 return (removedCount > 0);
130}
131
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700132void
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700133DigestTreeScene::plot(QString digest)
134{
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700135#ifdef __DEBUG
136 std::cout << "Plotting at time: " << time(NULL) << std::endl;
137#endif
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -0700138 clear();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700139
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700140 int nodeSize = 40;
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700141
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700142 int siblingDistance = 100, levelDistance = 100;
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700143 std::auto_ptr<TreeLayout> layout(new OneLevelTreeLayout());
144 layout->setSiblingDistance(siblingDistance);
145 layout->setLevelDistance(levelDistance);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700146
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700147 // do some cleaning, get rid of stale member info
148 Roster_iterator it = m_roster.begin();
149 while (it != m_roster.end())
150 {
151 DisplayUserPtr p = it.value();
152 if (p != DisplayUserNullPtr)
153 {
154 time_t now = time(NULL);
155 if (now - p->getReceived() >= FRESHNESS * 2)
156 {
157#ifdef __DEBUG
158 std::cout << "Removing user: " << p->getNick().toStdString() << std::endl;
159 std::cout << "now - last = " << now - p->getReceived() << std::endl;
160#endif
161 p = DisplayUserNullPtr;
162 it = m_roster.erase(it);
163 }
164 else
165 {
166 ++it;
167 }
168 }
169 else
170 {
171 it = m_roster.erase(it);
172 }
173 }
174
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700175 int n = m_roster.size();
176 std::vector<TreeLayout::Coordinate> childNodesCo(n);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700177
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700178 layout->setOneLevelLayout(childNodesCo);
179
180 plotEdge(childNodesCo, nodeSize);
181 plotNode(childNodesCo, digest, nodeSize);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700182
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700183 previouslyUpdatedUser = DisplayUserNullPtr;
184
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700185}
186
187void
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700188DigestTreeScene::plotEdge(const std::vector<TreeLayout::Coordinate> &childNodesCo, int nodeSize)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700189{
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700190 int n = childNodesCo.size();
191 for (int i = 0; i < n; i++) {
192 double x1 = 0.0, y1 = 0.0;
193 double x2 = childNodesCo[i].x, y2 = childNodesCo[i].y;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700194 QPointF src(x1 + nodeSize/2, y1 + nodeSize/2);
195 QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700196 QLineF line(src, dest);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700197 double angle = ::acos(line.dx() / line.length());
198
199 double arrowSize = 10;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700200 QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length());
201 QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700202 sin(angle + Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700203 QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700204 sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700205
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700206 addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black));
207 addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
208 }
209}
210
211void
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700212DigestTreeScene::plotNode(const std::vector<TreeLayout::Coordinate> &childNodesCo, QString digest, int nodeSize)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700213{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700214 RosterIterator it(m_roster);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700215 int n = childNodesCo.size();
216 int rim = 3;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700217
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700218 // plot root node
219 QRectF rootBoundingRect(0, 0, nodeSize, nodeSize);
220 QRectF rootInnerBoundingRect(rim, rim, nodeSize - rim * 2, nodeSize - rim * 2);
221 addRect(rootBoundingRect, QPen(Qt::black), QBrush(Qt::darkRed));
222 addRect(rootInnerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700223 QRectF digestRect(- 5.5 * nodeSize , - nodeSize, 12 * nodeSize, 30);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700224 addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
Zhenkai Zhu27df8d42012-06-05 12:04:04 -0700225
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700226 QGraphicsTextItem *digestItem = addText(digest);
227 QRectF digestBoundingRect = digestItem->boundingRect();
Zhenkai Zhu27df8d42012-06-05 12:04:04 -0700228 digestItem->setDefaultTextColor(Qt::black);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700229 digestItem->setFont(QFont("Cursive", 12, QFont::Bold));
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700230 digestItem->setPos(- 4.5 * nodeSize + (12 * nodeSize - digestBoundingRect.width()) / 2, - nodeSize + 5);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700231 m_rootDigest = digestItem;
232
233 // plot child nodes
234 for (int i = 0; i < n; i++)
235 {
236 if (it.hasNext())
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700237 {
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700238 it.next();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700239 }
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700240 else
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700241 {
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700242 abort();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700243 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700244
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700245 double x = childNodesCo[i].x;
246 double y = childNodesCo[i].y;
247 QRectF boundingRect(x, y, nodeSize, nodeSize);
248 QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2);
249 DisplayUserPtr p = it.value();
250 QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue));
251 p->setRimRectItem(rectItem);
252
253 QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
254 p->setInnerRectItem(innerRectItem);
255
256 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
257 QGraphicsTextItem *seqItem = addText(s.c_str());
258 seqItem->setFont(QFont("Cursive", 12, QFont::Bold));
259 QRectF seqBoundingRect = seqItem->boundingRect();
260 seqItem->setPos(x + nodeSize / 2 - seqBoundingRect.width() / 2, y + nodeSize / 2 - seqBoundingRect.height() / 2);
261 p->setSeqTextItem(seqItem);
262
263 QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30);
264 QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
265 p->setNickRectItem(nickRectItem);
266 QGraphicsTextItem *nickItem = addText(p->getNick());
267 QRectF textBoundingRect = nickItem->boundingRect();
268 nickItem->setDefaultTextColor(Qt::white);
269 nickItem->setFont(QFont("Cursive", 12, QFont::Bold));
270 nickItem->setPos(x + nodeSize / 2 - textBoundingRect.width() / 2, y + nodeSize + 5);
271 p->setNickTextItem(nickItem);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700272 }
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700273
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700274}
275
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700276void
277DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor)
278{
279 QGraphicsRectItem *rimItem = p->getRimRectItem();
280 rimItem->setBrush(QBrush(rimColor));
281 QGraphicsRectItem *innerItem = p->getInnerRectItem();
282 innerItem->setBrush(QBrush(Qt::lightGray));
283 QGraphicsTextItem *seqTextItem = p->getSeqTextItem();
284 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
285 seqTextItem->setPlainText(s.c_str());
286 QRectF textBR = seqTextItem->boundingRect();
287 QRectF innerBR = innerItem->boundingRect();
288 seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2);
289}
290