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 | { |
| 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 Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 76 | QGraphicsRectItem *nickRectItem = p->getNickRectItem(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 77 | nickItem->setPlainText(p->getNick()); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 78 | QRectF rectBR = nickRectItem->boundingRect(); |
| 79 | QRectF nickBR = nickItem->boundingRect(); |
| 80 | nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 81 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 82 | |
| 83 | reDrawNode(p, Qt::red); |
| 84 | |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame^] | 85 | if (previouslyUpdatedUser != DisplayUserNullPtr && previouslyUpdatedUser != p) |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 86 | { |
| 87 | reDrawNode(previouslyUpdatedUser, Qt::darkBlue); |
| 88 | } |
| 89 | |
| 90 | previouslyUpdatedUser = p; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | void |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 95 | DigestTreeScene::clearAll() |
| 96 | { |
| 97 | clear(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 98 | m_roster.clear(); |
| 99 | } |
| 100 | |
| 101 | void |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 102 | DigestTreeScene::plot(QString digest) |
| 103 | { |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 104 | clear(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 105 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 106 | int nodeSize = 40; |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 107 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 108 | int siblingDistance = 100, levelDistance = 100; |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 109 | std::auto_ptr<TreeLayout> layout(new OneLevelTreeLayout()); |
| 110 | layout->setSiblingDistance(siblingDistance); |
| 111 | layout->setLevelDistance(levelDistance); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 112 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 113 | int n = m_roster.size(); |
| 114 | std::vector<TreeLayout::Coordinate> childNodesCo(n); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 115 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 116 | layout->setOneLevelLayout(childNodesCo); |
| 117 | |
| 118 | plotEdge(childNodesCo, nodeSize); |
| 119 | plotNode(childNodesCo, digest, nodeSize); |
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 | previouslyUpdatedUser = DisplayUserNullPtr; |
| 122 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 126 | DigestTreeScene::plotEdge(const std::vector<TreeLayout::Coordinate> &childNodesCo, int nodeSize) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 127 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 128 | 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 Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 132 | QPointF src(x1 + nodeSize/2, y1 + nodeSize/2); |
| 133 | QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 134 | QLineF line(src, dest); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 135 | double angle = ::acos(line.dx() / line.length()); |
| 136 | |
| 137 | double arrowSize = 10; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 138 | 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 Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 140 | sin(angle + Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 141 | QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize, |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 142 | sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 143 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 144 | addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black)); |
| 145 | addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black)); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 150 | DigestTreeScene::plotNode(const std::vector<TreeLayout::Coordinate> &childNodesCo, QString digest, int nodeSize) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 151 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 152 | RosterIterator it(m_roster); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 153 | int n = childNodesCo.size(); |
| 154 | int rim = 3; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 155 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 156 | // 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 Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 161 | QRectF digestRect(- 5.5 * nodeSize , - nodeSize, 12 * nodeSize, 30); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 162 | addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
Zhenkai Zhu | 27df8d4 | 2012-06-05 12:04:04 -0700 | [diff] [blame] | 163 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 164 | QGraphicsTextItem *digestItem = addText(digest); |
| 165 | QRectF digestBoundingRect = digestItem->boundingRect(); |
Zhenkai Zhu | 27df8d4 | 2012-06-05 12:04:04 -0700 | [diff] [blame] | 166 | digestItem->setDefaultTextColor(Qt::black); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 167 | digestItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 168 | digestItem->setPos(- 4.5 * nodeSize + (12 * nodeSize - digestBoundingRect.width()) / 2, - nodeSize + 5); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 169 | m_rootDigest = digestItem; |
| 170 | |
| 171 | // plot child nodes |
| 172 | for (int i = 0; i < n; i++) |
| 173 | { |
| 174 | if (it.hasNext()) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 175 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 176 | it.next(); |
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 | else |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 179 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 180 | abort(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 181 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 182 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 183 | 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 Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 210 | } |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 211 | |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 214 | void |
| 215 | DigestTreeScene::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 | |