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 | { |
| 98 | std::cout << "Couldn't find prefix, let's check"<< std::endl; |
| 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 | |
| 116 | void |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 117 | DigestTreeScene::plot(QString digest) |
| 118 | { |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 119 | clear(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 120 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 121 | int nodeSize = 40; |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 122 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 123 | int siblingDistance = 100, levelDistance = 100; |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 124 | std::auto_ptr<TreeLayout> layout(new OneLevelTreeLayout()); |
| 125 | layout->setSiblingDistance(siblingDistance); |
| 126 | layout->setLevelDistance(levelDistance); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 127 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 128 | int n = m_roster.size(); |
| 129 | std::vector<TreeLayout::Coordinate> childNodesCo(n); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 130 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 131 | layout->setOneLevelLayout(childNodesCo); |
| 132 | |
| 133 | plotEdge(childNodesCo, nodeSize); |
| 134 | plotNode(childNodesCo, digest, nodeSize); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 135 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 136 | previouslyUpdatedUser = DisplayUserNullPtr; |
| 137 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 141 | DigestTreeScene::plotEdge(const std::vector<TreeLayout::Coordinate> &childNodesCo, int nodeSize) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 142 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 143 | int n = childNodesCo.size(); |
| 144 | for (int i = 0; i < n; i++) { |
| 145 | double x1 = 0.0, y1 = 0.0; |
| 146 | double x2 = childNodesCo[i].x, y2 = childNodesCo[i].y; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 147 | QPointF src(x1 + nodeSize/2, y1 + nodeSize/2); |
| 148 | QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 149 | QLineF line(src, dest); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 150 | double angle = ::acos(line.dx() / line.length()); |
| 151 | |
| 152 | double arrowSize = 10; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 153 | QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length()); |
| 154 | QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize, |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 155 | sin(angle + Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 156 | QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize, |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 157 | sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 158 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 159 | addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black)); |
| 160 | addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black)); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 165 | DigestTreeScene::plotNode(const std::vector<TreeLayout::Coordinate> &childNodesCo, QString digest, int nodeSize) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 166 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 167 | RosterIterator it(m_roster); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 168 | int n = childNodesCo.size(); |
| 169 | int rim = 3; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 170 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 171 | // plot root node |
| 172 | QRectF rootBoundingRect(0, 0, nodeSize, nodeSize); |
| 173 | QRectF rootInnerBoundingRect(rim, rim, nodeSize - rim * 2, nodeSize - rim * 2); |
| 174 | addRect(rootBoundingRect, QPen(Qt::black), QBrush(Qt::darkRed)); |
| 175 | addRect(rootInnerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray)); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 176 | QRectF digestRect(- 5.5 * nodeSize , - nodeSize, 12 * nodeSize, 30); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 177 | addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
Zhenkai Zhu | 27df8d4 | 2012-06-05 12:04:04 -0700 | [diff] [blame] | 178 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 179 | QGraphicsTextItem *digestItem = addText(digest); |
| 180 | QRectF digestBoundingRect = digestItem->boundingRect(); |
Zhenkai Zhu | 27df8d4 | 2012-06-05 12:04:04 -0700 | [diff] [blame] | 181 | digestItem->setDefaultTextColor(Qt::black); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 182 | digestItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 183 | digestItem->setPos(- 4.5 * nodeSize + (12 * nodeSize - digestBoundingRect.width()) / 2, - nodeSize + 5); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 184 | m_rootDigest = digestItem; |
| 185 | |
| 186 | // plot child nodes |
| 187 | for (int i = 0; i < n; i++) |
| 188 | { |
| 189 | if (it.hasNext()) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 190 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 191 | it.next(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 192 | } |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 193 | else |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 194 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 195 | abort(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 196 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 197 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 198 | double x = childNodesCo[i].x; |
| 199 | double y = childNodesCo[i].y; |
| 200 | QRectF boundingRect(x, y, nodeSize, nodeSize); |
| 201 | QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2); |
| 202 | DisplayUserPtr p = it.value(); |
| 203 | QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue)); |
| 204 | p->setRimRectItem(rectItem); |
| 205 | |
| 206 | QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray)); |
| 207 | p->setInnerRectItem(innerRectItem); |
| 208 | |
| 209 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 210 | QGraphicsTextItem *seqItem = addText(s.c_str()); |
| 211 | seqItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
| 212 | QRectF seqBoundingRect = seqItem->boundingRect(); |
| 213 | seqItem->setPos(x + nodeSize / 2 - seqBoundingRect.width() / 2, y + nodeSize / 2 - seqBoundingRect.height() / 2); |
| 214 | p->setSeqTextItem(seqItem); |
| 215 | |
| 216 | QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30); |
| 217 | QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
| 218 | p->setNickRectItem(nickRectItem); |
| 219 | QGraphicsTextItem *nickItem = addText(p->getNick()); |
| 220 | QRectF textBoundingRect = nickItem->boundingRect(); |
| 221 | nickItem->setDefaultTextColor(Qt::white); |
| 222 | nickItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
| 223 | nickItem->setPos(x + nodeSize / 2 - textBoundingRect.width() / 2, y + nodeSize + 5); |
| 224 | p->setNickTextItem(nickItem); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 225 | } |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 226 | |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 229 | void |
| 230 | DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor) |
| 231 | { |
| 232 | QGraphicsRectItem *rimItem = p->getRimRectItem(); |
| 233 | rimItem->setBrush(QBrush(rimColor)); |
| 234 | QGraphicsRectItem *innerItem = p->getInnerRectItem(); |
| 235 | innerItem->setBrush(QBrush(Qt::lightGray)); |
| 236 | QGraphicsTextItem *seqTextItem = p->getSeqTextItem(); |
| 237 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 238 | seqTextItem->setPlainText(s.c_str()); |
| 239 | QRectF textBR = seqTextItem->boundingRect(); |
| 240 | QRectF innerBR = innerItem->boundingRect(); |
| 241 | seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2); |
| 242 | } |
| 243 | |