blob: b876a17fd184feb3afc615a995154882c67759b2 [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 Zhuc61dbc22012-10-08 11:36:37 -070031 time_t tempTime = time(NULL) - FRESHNESS + 1;
Zhenkai Zhu86df7412012-09-27 16:30:20 -070032 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
Zhenkai Zhu9036e032012-09-27 20:59:33 -070074QStringList
Zhenkai Zhu6082ede2012-09-27 17:28:46 -070075DigestTreeScene::getRosterList()
76{
Zhenkai Zhu9036e032012-09-27 20:59:33 -070077 QStringList rosterList;
78 RosterIterator it(m_roster);
79 while(it.hasNext())
80 {
81 it.next();
82 DisplayUserPtr p = it.value();
83 if (p != DisplayUserNullPtr)
84 {
85 rosterList << "- " + p->getNick();
86 }
87 }
88 return rosterList;
Zhenkai Zhu6082ede2012-09-27 17:28:46 -070089}
90
91void
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070092DigestTreeScene::msgReceived(QString prefix, QString nick)
93{
94 Roster_iterator it = m_roster.find(prefix);
95 if (it != m_roster.end())
96 {
Zhenkai Zhu716fe852012-10-08 18:27:55 -070097 std::cout << "Updating for prefix = " << prefix.toStdString() << " nick = " << nick.toStdString() << std::endl;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070098 DisplayUserPtr p = it.value();
Zhenkai Zhu86df7412012-09-27 16:30:20 -070099 p->setReceived(time(NULL));
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700100 if (nick != p->getNick()) {
Zhenkai Zhu716fe852012-10-08 18:27:55 -0700101 std::cout << "old nick = " << p->getNick().toStdString() << std::endl;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700102 p->setNick(nick);
103 QGraphicsTextItem *nickItem = p->getNickTextItem();
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700104 QGraphicsRectItem *nickRectItem = p->getNickRectItem();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700105 nickItem->setPlainText(p->getNick());
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700106 QRectF rectBR = nickRectItem->boundingRect();
107 QRectF nickBR = nickItem->boundingRect();
108 nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700109 emit rosterChanged(QStringList());
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700110 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700111
112 reDrawNode(p, Qt::red);
113
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700114 if (previouslyUpdatedUser != DisplayUserNullPtr && previouslyUpdatedUser != p)
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700115 {
116 reDrawNode(previouslyUpdatedUser, Qt::darkBlue);
117 }
118
119 previouslyUpdatedUser = p;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700120 }
121}
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();
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700154 QStringList staleUserList;
Zhenkai Zhu716fe852012-10-08 18:27:55 -0700155#ifdef __DEBUG
156 std::cout << "------------------------------------------------------" << std::endl;
157#endif
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700158 while (it != m_roster.end())
159 {
160 DisplayUserPtr p = it.value();
161 if (p != DisplayUserNullPtr)
162 {
163 time_t now = time(NULL);
Zhenkai Zhuc61dbc22012-10-08 11:36:37 -0700164 if (now - p->getReceived() >= FRESHNESS)
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700165 {
166#ifdef __DEBUG
167 std::cout << "Removing user: " << p->getNick().toStdString() << std::endl;
168 std::cout << "now - last = " << now - p->getReceived() << std::endl;
169#endif
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700170 staleUserList << p->getNick();
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700171 p = DisplayUserNullPtr;
172 it = m_roster.erase(it);
173 }
174 else
175 {
Zhenkai Zhu716fe852012-10-08 18:27:55 -0700176#ifdef __DEBUG
177 std::cout << "having user: " << p->getNick().toStdString() << std::endl;
178 std::cout << "prefix: " << p->getPrefix().toStdString() << std::endl;
179#endif
180 if (!m_currentPrefix.startsWith("/private/local") && p->getPrefix().startsWith("/private/local"))
181 {
182#ifdef __DEBUG
183 std::cout << "erasing: " << p->getPrefix().toStdString() << std::endl;
184#endif
185 staleUserList << p->getNick();
186 p = DisplayUserNullPtr;
187 it = m_roster.erase(it);
188 continue;
189 }
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700190 ++it;
191 }
192 }
193 else
194 {
195 it = m_roster.erase(it);
196 }
Zhenkai Zhu716fe852012-10-08 18:27:55 -0700197#ifdef __DEBUG
198 std::cout << "------------------------------------------------------" << std::endl;
199#endif
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700200 }
201
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700202 // for simpicity here, whenever we replot, we also redo the roster list
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700203 emit rosterChanged(staleUserList);
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700204
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700205 int n = m_roster.size();
Zhenkai Zhu716fe852012-10-08 18:27:55 -0700206
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700207 std::vector<TreeLayout::Coordinate> childNodesCo(n);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700208
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700209 layout->setOneLevelLayout(childNodesCo);
210
211 plotEdge(childNodesCo, nodeSize);
212 plotNode(childNodesCo, digest, nodeSize);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700213
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700214 previouslyUpdatedUser = DisplayUserNullPtr;
215
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700216}
217
218void
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700219DigestTreeScene::plotEdge(const std::vector<TreeLayout::Coordinate> &childNodesCo, int nodeSize)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700220{
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700221 int n = childNodesCo.size();
222 for (int i = 0; i < n; i++) {
223 double x1 = 0.0, y1 = 0.0;
224 double x2 = childNodesCo[i].x, y2 = childNodesCo[i].y;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700225 QPointF src(x1 + nodeSize/2, y1 + nodeSize/2);
226 QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700227 QLineF line(src, dest);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700228 double angle = ::acos(line.dx() / line.length());
229
230 double arrowSize = 10;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700231 QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length());
232 QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700233 sin(angle + Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700234 QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700235 sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700236
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700237 addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black));
238 addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
239 }
240}
241
242void
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700243DigestTreeScene::plotNode(const std::vector<TreeLayout::Coordinate> &childNodesCo, QString digest, int nodeSize)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700244{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700245 RosterIterator it(m_roster);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700246 int n = childNodesCo.size();
247 int rim = 3;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700248
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700249 // plot root node
250 QRectF rootBoundingRect(0, 0, nodeSize, nodeSize);
251 QRectF rootInnerBoundingRect(rim, rim, nodeSize - rim * 2, nodeSize - rim * 2);
252 addRect(rootBoundingRect, QPen(Qt::black), QBrush(Qt::darkRed));
253 addRect(rootInnerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700254 QRectF digestRect(- 5.5 * nodeSize , - nodeSize, 12 * nodeSize, 30);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700255 addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
Zhenkai Zhu27df8d42012-06-05 12:04:04 -0700256
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700257 QGraphicsTextItem *digestItem = addText(digest);
258 QRectF digestBoundingRect = digestItem->boundingRect();
Zhenkai Zhu27df8d42012-06-05 12:04:04 -0700259 digestItem->setDefaultTextColor(Qt::black);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700260 digestItem->setFont(QFont("Cursive", 12, QFont::Bold));
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700261 digestItem->setPos(- 4.5 * nodeSize + (12 * nodeSize - digestBoundingRect.width()) / 2, - nodeSize + 5);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700262 m_rootDigest = digestItem;
263
264 // plot child nodes
265 for (int i = 0; i < n; i++)
266 {
267 if (it.hasNext())
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700268 {
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700269 it.next();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700270 }
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700271 else
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700272 {
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700273 abort();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700274 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700275
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700276 double x = childNodesCo[i].x;
277 double y = childNodesCo[i].y;
278 QRectF boundingRect(x, y, nodeSize, nodeSize);
279 QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2);
280 DisplayUserPtr p = it.value();
281 QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue));
282 p->setRimRectItem(rectItem);
283
284 QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
285 p->setInnerRectItem(innerRectItem);
286
287 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
288 QGraphicsTextItem *seqItem = addText(s.c_str());
289 seqItem->setFont(QFont("Cursive", 12, QFont::Bold));
290 QRectF seqBoundingRect = seqItem->boundingRect();
291 seqItem->setPos(x + nodeSize / 2 - seqBoundingRect.width() / 2, y + nodeSize / 2 - seqBoundingRect.height() / 2);
292 p->setSeqTextItem(seqItem);
293
294 QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30);
295 QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
296 p->setNickRectItem(nickRectItem);
297 QGraphicsTextItem *nickItem = addText(p->getNick());
298 QRectF textBoundingRect = nickItem->boundingRect();
299 nickItem->setDefaultTextColor(Qt::white);
300 nickItem->setFont(QFont("Cursive", 12, QFont::Bold));
301 nickItem->setPos(x + nodeSize / 2 - textBoundingRect.width() / 2, y + nodeSize + 5);
302 p->setNickTextItem(nickItem);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700303 }
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700304
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700305}
306
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700307void
308DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor)
309{
310 QGraphicsRectItem *rimItem = p->getRimRectItem();
311 rimItem->setBrush(QBrush(rimColor));
312 QGraphicsRectItem *innerItem = p->getInnerRectItem();
313 innerItem->setBrush(QBrush(Qt::lightGray));
314 QGraphicsTextItem *seqTextItem = p->getSeqTextItem();
315 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
316 seqTextItem->setPlainText(s.c_str());
317 QRectF textBR = seqTextItem->boundingRect();
318 QRectF innerBR = innerItem->boundingRect();
319 seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2);
320}
321