blob: 4bf699e06b26255ab3f33be7586e37980ee77b1e [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 -070011
12DigestTreeScene::DisplayUserPtr DigestTreeScene::DisplayUserNullPtr;
13
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -070014DigestTreeScene::DigestTreeScene(QWidget *parent)
15 : QGraphicsScene(parent)
16{
Zhenkai Zhud13acd02012-06-04 15:25:20 -070017 previouslyUpdatedUser = DisplayUserNullPtr;
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -070018}
19
20void
Zhenkai Zhu82a62752012-06-04 17:11:04 -070021DigestTreeScene::processUpdate(const std::vector<Sync::MissingDataInfo> &v, QString digest)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070022{
23 int n = v.size();
24 bool rePlot = false;
25 for (int i = 0; i < n; i++)
26 {
27 Roster_iterator it = m_roster.find(v[i].prefix.c_str());
Zhenkai Zhud1c5a972012-06-05 14:07:41 -070028 if (it == m_roster.end())
29 {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070030 rePlot = true;
31 DisplayUserPtr p(new DisplayUser());
32 p->setPrefix(v[i].prefix.c_str());
33 p->setSeq(v[i].high);
34 m_roster.insert(p->getPrefix(), p);
35 }
Zhenkai Zhud1c5a972012-06-05 14:07:41 -070036 else
37 {
38 it.value()->setSeq(v[i].high);
39 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070040 }
41
42 if (rePlot)
43 {
44 plot(digest);
45 }
46 else
47 {
48 for (int i = 0; i < n; i++)
49 {
50 Roster_iterator it = m_roster.find(v[i].prefix.c_str());
51 if (it != m_roster.end()) {
52 DisplayUserPtr p = it.value();
53 QGraphicsTextItem *item = p->getSeqTextItem();
Zhenkai Zhud13acd02012-06-04 15:25:20 -070054 QGraphicsRectItem *rectItem = p->getInnerRectItem();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070055 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
56 item->setPlainText(s.c_str());
Zhenkai Zhud13acd02012-06-04 15:25:20 -070057 QRectF textBR = item->boundingRect();
58 QRectF rectBR = rectItem->boundingRect();
59 item->setPos(rectBR.x() + (rectBR.width() - textBR.width())/2, rectBR.y() + (rectBR.height() - textBR.height())/2);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070060 }
61 }
62 m_rootDigest->setPlainText(digest);
63 }
64}
65
66void
67DigestTreeScene::msgReceived(QString prefix, QString nick)
68{
69 Roster_iterator it = m_roster.find(prefix);
70 if (it != m_roster.end())
71 {
72 DisplayUserPtr p = it.value();
73 if (nick != p->getNick()) {
74 p->setNick(nick);
75 QGraphicsTextItem *nickItem = p->getNickTextItem();
Zhenkai Zhud13acd02012-06-04 15:25:20 -070076 QGraphicsRectItem *nickRectItem = p->getNickRectItem();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070077 nickItem->setPlainText(p->getNick());
Zhenkai Zhud13acd02012-06-04 15:25:20 -070078 QRectF rectBR = nickRectItem->boundingRect();
79 QRectF nickBR = nickItem->boundingRect();
80 nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070081 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -070082
83 reDrawNode(p, Qt::red);
84
Zhenkai Zhud1c5a972012-06-05 14:07:41 -070085 if (previouslyUpdatedUser != DisplayUserNullPtr && previouslyUpdatedUser != p)
Zhenkai Zhud13acd02012-06-04 15:25:20 -070086 {
87 reDrawNode(previouslyUpdatedUser, Qt::darkBlue);
88 }
89
90 previouslyUpdatedUser = p;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070091 }
92}
93
94void
Zhenkai Zhu82a62752012-06-04 17:11:04 -070095DigestTreeScene::clearAll()
96{
97 clear();
Zhenkai Zhu82a62752012-06-04 17:11:04 -070098 m_roster.clear();
99}
100
101void
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700102DigestTreeScene::plot(QString digest)
103{
Zhenkai Zhu6fcdee42012-05-30 17:02:49 -0700104 clear();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700105
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700106 int nodeSize = 40;
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700107
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700108 int siblingDistance = 100, levelDistance = 100;
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700109 std::auto_ptr<TreeLayout> layout(new OneLevelTreeLayout());
110 layout->setSiblingDistance(siblingDistance);
111 layout->setLevelDistance(levelDistance);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700112
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700113 int n = m_roster.size();
114 std::vector<TreeLayout::Coordinate> childNodesCo(n);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700115
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700116 layout->setOneLevelLayout(childNodesCo);
117
118 plotEdge(childNodesCo, nodeSize);
119 plotNode(childNodesCo, digest, nodeSize);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700120
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700121 previouslyUpdatedUser = DisplayUserNullPtr;
122
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700123}
124
125void
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700126DigestTreeScene::plotEdge(const std::vector<TreeLayout::Coordinate> &childNodesCo, int nodeSize)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700127{
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700128 int n = childNodesCo.size();
129 for (int i = 0; i < n; i++) {
130 double x1 = 0.0, y1 = 0.0;
131 double x2 = childNodesCo[i].x, y2 = childNodesCo[i].y;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700132 QPointF src(x1 + nodeSize/2, y1 + nodeSize/2);
133 QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700134 QLineF line(src, dest);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700135 double angle = ::acos(line.dx() / line.length());
136
137 double arrowSize = 10;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700138 QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length());
139 QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700140 sin(angle + Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700141 QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize,
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700142 sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700143
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700144 addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black));
145 addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
146 }
147}
148
149void
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700150DigestTreeScene::plotNode(const std::vector<TreeLayout::Coordinate> &childNodesCo, QString digest, int nodeSize)
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700151{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700152 RosterIterator it(m_roster);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700153 int n = childNodesCo.size();
154 int rim = 3;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700155
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700156 // plot root node
157 QRectF rootBoundingRect(0, 0, nodeSize, nodeSize);
158 QRectF rootInnerBoundingRect(rim, rim, nodeSize - rim * 2, nodeSize - rim * 2);
159 addRect(rootBoundingRect, QPen(Qt::black), QBrush(Qt::darkRed));
160 addRect(rootInnerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700161 QRectF digestRect(- 5.5 * nodeSize , - nodeSize, 12 * nodeSize, 30);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700162 addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
Zhenkai Zhu27df8d42012-06-05 12:04:04 -0700163
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700164 QGraphicsTextItem *digestItem = addText(digest);
165 QRectF digestBoundingRect = digestItem->boundingRect();
Zhenkai Zhu27df8d42012-06-05 12:04:04 -0700166 digestItem->setDefaultTextColor(Qt::black);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700167 digestItem->setFont(QFont("Cursive", 12, QFont::Bold));
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700168 digestItem->setPos(- 4.5 * nodeSize + (12 * nodeSize - digestBoundingRect.width()) / 2, - nodeSize + 5);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700169 m_rootDigest = digestItem;
170
171 // plot child nodes
172 for (int i = 0; i < n; i++)
173 {
174 if (it.hasNext())
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700175 {
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700176 it.next();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700177 }
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700178 else
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700179 {
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700180 abort();
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700181 }
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700182
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700183 double x = childNodesCo[i].x;
184 double y = childNodesCo[i].y;
185 QRectF boundingRect(x, y, nodeSize, nodeSize);
186 QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2);
187 DisplayUserPtr p = it.value();
188 QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue));
189 p->setRimRectItem(rectItem);
190
191 QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
192 p->setInnerRectItem(innerRectItem);
193
194 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
195 QGraphicsTextItem *seqItem = addText(s.c_str());
196 seqItem->setFont(QFont("Cursive", 12, QFont::Bold));
197 QRectF seqBoundingRect = seqItem->boundingRect();
198 seqItem->setPos(x + nodeSize / 2 - seqBoundingRect.width() / 2, y + nodeSize / 2 - seqBoundingRect.height() / 2);
199 p->setSeqTextItem(seqItem);
200
201 QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30);
202 QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
203 p->setNickRectItem(nickRectItem);
204 QGraphicsTextItem *nickItem = addText(p->getNick());
205 QRectF textBoundingRect = nickItem->boundingRect();
206 nickItem->setDefaultTextColor(Qt::white);
207 nickItem->setFont(QFont("Cursive", 12, QFont::Bold));
208 nickItem->setPos(x + nodeSize / 2 - textBoundingRect.width() / 2, y + nodeSize + 5);
209 p->setNickTextItem(nickItem);
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700210 }
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700211
Zhenkai Zhuf474a0a2012-05-30 15:06:29 -0700212}
213
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700214void
215DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor)
216{
217 QGraphicsRectItem *rimItem = p->getRimRectItem();
218 rimItem->setBrush(QBrush(rimColor));
219 QGraphicsRectItem *innerItem = p->getInnerRectItem();
220 innerItem->setBrush(QBrush(Qt::lightGray));
221 QGraphicsTextItem *seqTextItem = p->getSeqTextItem();
222 std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
223 seqTextItem->setPlainText(s.c_str());
224 QRectF textBR = seqTextItem->boundingRect();
225 QRectF innerBR = innerItem->boundingRect();
226 seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2);
227}
228