Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 1 | #include <QtGui> |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 2 | #include <vector> |
| 3 | #include <iostream> |
| 4 | #include <assert.h> |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 5 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 6 | #include <memory> |
| 7 | #include "digesttreescene.h" |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 8 | |
| 9 | static const double Pi = 3.14159265358979323846264338327950288419717; |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 10 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 11 | |
| 12 | DigestTreeScene::DisplayUserPtr DigestTreeScene::DisplayUserNullPtr; |
| 13 | |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 14 | DigestTreeScene::DigestTreeScene(QWidget *parent) |
| 15 | : QGraphicsScene(parent) |
| 16 | { |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 17 | previouslyUpdatedUser = DisplayUserNullPtr; |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | void |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 21 | DigestTreeScene::processUpdate(const std::vector<Sync::MissingDataInfo> &v, QString digest) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 22 | { |
| 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 Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 28 | if (it == m_roster.end()) |
| 29 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 30 | 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 Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 36 | else |
| 37 | { |
| 38 | it.value()->setSeq(v[i].high); |
| 39 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 40 | } |
| 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 Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 54 | QGraphicsRectItem *rectItem = p->getInnerRectItem(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 55 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 56 | item->setPlainText(s.c_str()); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 57 | 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 Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | m_rootDigest->setPlainText(digest); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | DigestTreeScene::msgReceived(QString prefix, QString nick) |
| 68 | { |
Zhenkai Zhu | 097bfe7 | 2012-06-05 14:30:17 -0700 | [diff] [blame] | 69 | #ifdef __DEBUG |
| 70 | std::cout << "Finding " << prefix.toStdString() << std::endl; |
| 71 | #endif |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 72 | Roster_iterator it = m_roster.find(prefix); |
| 73 | if (it != m_roster.end()) |
| 74 | { |
| 75 | DisplayUserPtr p = it.value(); |
| 76 | if (nick != p->getNick()) { |
| 77 | p->setNick(nick); |
| 78 | QGraphicsTextItem *nickItem = p->getNickTextItem(); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 79 | QGraphicsRectItem *nickRectItem = p->getNickRectItem(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 80 | nickItem->setPlainText(p->getNick()); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 81 | QRectF rectBR = nickRectItem->boundingRect(); |
| 82 | QRectF nickBR = nickItem->boundingRect(); |
| 83 | nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 84 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 85 | |
| 86 | reDrawNode(p, Qt::red); |
| 87 | |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 88 | if (previouslyUpdatedUser != DisplayUserNullPtr && previouslyUpdatedUser != p) |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 89 | { |
| 90 | reDrawNode(previouslyUpdatedUser, Qt::darkBlue); |
| 91 | } |
| 92 | |
| 93 | previouslyUpdatedUser = p; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 94 | } |
Zhenkai Zhu | 097bfe7 | 2012-06-05 14:30:17 -0700 | [diff] [blame] | 95 | #ifdef __DEBUG |
| 96 | else |
| 97 | { |
Zhenkai Zhu | 59245aa | 2012-09-26 16:07:04 -0700 | [diff] [blame] | 98 | std::cout << "Couldn't find prefix, " << prefix.toStdString() << ": let's check"<< std::endl; |
Zhenkai Zhu | 097bfe7 | 2012-06-05 14:30:17 -0700 | [diff] [blame] | 99 | Roster_iterator ii = m_roster.begin(); |
| 100 | while (ii != m_roster.end()) |
| 101 | { |
| 102 | std::cout <<"Prefix: " << ii.key().toStdString() << std::endl; |
| 103 | ++ii; |
| 104 | } |
| 105 | } |
| 106 | #endif |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 110 | DigestTreeScene::clearAll() |
| 111 | { |
| 112 | clear(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 113 | m_roster.clear(); |
| 114 | } |
| 115 | |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 116 | bool |
| 117 | DigestTreeScene::removeNode(const QString prefix) |
| 118 | { |
| 119 | int removedCount = m_roster.remove(prefix); |
| 120 | return (removedCount > 0); |
| 121 | } |
| 122 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 123 | void |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 124 | DigestTreeScene::plot(QString digest) |
| 125 | { |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 126 | clear(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 127 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 128 | int nodeSize = 40; |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 129 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 130 | int siblingDistance = 100, levelDistance = 100; |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 131 | std::auto_ptr<TreeLayout> layout(new OneLevelTreeLayout()); |
| 132 | layout->setSiblingDistance(siblingDistance); |
| 133 | layout->setLevelDistance(levelDistance); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 134 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 135 | int n = m_roster.size(); |
| 136 | std::vector<TreeLayout::Coordinate> childNodesCo(n); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 137 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 138 | layout->setOneLevelLayout(childNodesCo); |
| 139 | |
| 140 | plotEdge(childNodesCo, nodeSize); |
| 141 | plotNode(childNodesCo, digest, nodeSize); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 142 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 143 | previouslyUpdatedUser = DisplayUserNullPtr; |
| 144 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 148 | DigestTreeScene::plotEdge(const std::vector<TreeLayout::Coordinate> &childNodesCo, int nodeSize) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 149 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 150 | int n = childNodesCo.size(); |
| 151 | for (int i = 0; i < n; i++) { |
| 152 | double x1 = 0.0, y1 = 0.0; |
| 153 | double x2 = childNodesCo[i].x, y2 = childNodesCo[i].y; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 154 | QPointF src(x1 + nodeSize/2, y1 + nodeSize/2); |
| 155 | QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 156 | QLineF line(src, dest); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 157 | double angle = ::acos(line.dx() / line.length()); |
| 158 | |
| 159 | double arrowSize = 10; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 160 | QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length()); |
| 161 | QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize, |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 162 | sin(angle + Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 163 | QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize, |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 164 | sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 165 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 166 | addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black)); |
| 167 | addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black)); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 172 | DigestTreeScene::plotNode(const std::vector<TreeLayout::Coordinate> &childNodesCo, QString digest, int nodeSize) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 173 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 174 | RosterIterator it(m_roster); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 175 | int n = childNodesCo.size(); |
| 176 | int rim = 3; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 177 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 178 | // plot root node |
| 179 | QRectF rootBoundingRect(0, 0, nodeSize, nodeSize); |
| 180 | QRectF rootInnerBoundingRect(rim, rim, nodeSize - rim * 2, nodeSize - rim * 2); |
| 181 | addRect(rootBoundingRect, QPen(Qt::black), QBrush(Qt::darkRed)); |
| 182 | addRect(rootInnerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray)); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 183 | QRectF digestRect(- 5.5 * nodeSize , - nodeSize, 12 * nodeSize, 30); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 184 | addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
Zhenkai Zhu | 27df8d4 | 2012-06-05 12:04:04 -0700 | [diff] [blame] | 185 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 186 | QGraphicsTextItem *digestItem = addText(digest); |
| 187 | QRectF digestBoundingRect = digestItem->boundingRect(); |
Zhenkai Zhu | 27df8d4 | 2012-06-05 12:04:04 -0700 | [diff] [blame] | 188 | digestItem->setDefaultTextColor(Qt::black); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 189 | digestItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 190 | digestItem->setPos(- 4.5 * nodeSize + (12 * nodeSize - digestBoundingRect.width()) / 2, - nodeSize + 5); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 191 | m_rootDigest = digestItem; |
| 192 | |
| 193 | // plot child nodes |
| 194 | for (int i = 0; i < n; i++) |
| 195 | { |
| 196 | if (it.hasNext()) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 197 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 198 | it.next(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 199 | } |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 200 | else |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 201 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 202 | abort(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 203 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 204 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 205 | double x = childNodesCo[i].x; |
| 206 | double y = childNodesCo[i].y; |
| 207 | QRectF boundingRect(x, y, nodeSize, nodeSize); |
| 208 | QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2); |
| 209 | DisplayUserPtr p = it.value(); |
| 210 | QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue)); |
| 211 | p->setRimRectItem(rectItem); |
| 212 | |
| 213 | QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray)); |
| 214 | p->setInnerRectItem(innerRectItem); |
| 215 | |
| 216 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 217 | QGraphicsTextItem *seqItem = addText(s.c_str()); |
| 218 | seqItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
| 219 | QRectF seqBoundingRect = seqItem->boundingRect(); |
| 220 | seqItem->setPos(x + nodeSize / 2 - seqBoundingRect.width() / 2, y + nodeSize / 2 - seqBoundingRect.height() / 2); |
| 221 | p->setSeqTextItem(seqItem); |
| 222 | |
| 223 | QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30); |
| 224 | QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
| 225 | p->setNickRectItem(nickRectItem); |
| 226 | QGraphicsTextItem *nickItem = addText(p->getNick()); |
| 227 | QRectF textBoundingRect = nickItem->boundingRect(); |
| 228 | nickItem->setDefaultTextColor(Qt::white); |
| 229 | nickItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
| 230 | nickItem->setPos(x + nodeSize / 2 - textBoundingRect.width() / 2, y + nodeSize + 5); |
| 231 | p->setNickTextItem(nickItem); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 232 | } |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 233 | |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 236 | void |
| 237 | DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor) |
| 238 | { |
| 239 | QGraphicsRectItem *rimItem = p->getRimRectItem(); |
| 240 | rimItem->setBrush(QBrush(rimColor)); |
| 241 | QGraphicsRectItem *innerItem = p->getInnerRectItem(); |
| 242 | innerItem->setBrush(QBrush(Qt::lightGray)); |
| 243 | QGraphicsTextItem *seqTextItem = p->getSeqTextItem(); |
| 244 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 245 | seqTextItem->setPlainText(s.c_str()); |
| 246 | QRectF textBR = seqTextItem->boundingRect(); |
| 247 | QRectF innerBR = innerItem->boundingRect(); |
| 248 | seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2); |
| 249 | } |
| 250 | |