Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 1 | #include "digesttreescene.h" |
| 2 | #include <QtGui> |
| 3 | #include "ogdf/basic/Array.h" |
| 4 | #include "ogdf/basic/Graph_d.h" |
| 5 | #include "ogdf/tree/TreeLayout.h" |
| 6 | #include <vector> |
| 7 | #include <iostream> |
| 8 | #include <assert.h> |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 9 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 10 | |
| 11 | static const double Pi = 3.14159265358979323846264338327950288419717; |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 12 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 13 | static void |
| 14 | testDraw(DigestTreeScene * scene) |
| 15 | { |
| 16 | std::string prefix[5] = {"/ndn/1", "/ndn/2", "/ndn/3", "/ndn/4", "/ndn/5"}; |
| 17 | std::string nick[5] = {"tom", "jerry", "jason", "michael", "hurry"}; |
| 18 | std::vector<Sync::MissingDataInfo> v; |
| 19 | for (int i = 0; i < 5; i++) |
| 20 | { |
| 21 | Sync::MissingDataInfo mdi = {prefix[i], Sync::SeqNo(0), Sync::SeqNo(i * (2 << i) )}; |
| 22 | v.push_back(mdi); |
| 23 | } |
| 24 | |
| 25 | scene->processUpdate(v, "12341234@!#%!@"); |
| 26 | |
| 27 | for (int i = 0; i < 5; i++) |
| 28 | { |
| 29 | scene-> msgReceived(prefix[i].c_str(), nick[i].c_str()); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | DigestTreeScene::DisplayUserPtr DigestTreeScene::DisplayUserNullPtr; |
| 34 | |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 35 | DigestTreeScene::DigestTreeScene(QWidget *parent) |
| 36 | : QGraphicsScene(parent) |
| 37 | { |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 38 | previouslyUpdatedUser = DisplayUserNullPtr; |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame^] | 39 | // testDraw(this); |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame^] | 43 | DigestTreeScene::processUpdate(const std::vector<Sync::MissingDataInfo> &v, QString digest) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 44 | { |
| 45 | int n = v.size(); |
| 46 | bool rePlot = false; |
| 47 | for (int i = 0; i < n; i++) |
| 48 | { |
| 49 | Roster_iterator it = m_roster.find(v[i].prefix.c_str()); |
| 50 | if (it == m_roster.end()) { |
| 51 | rePlot = true; |
| 52 | DisplayUserPtr p(new DisplayUser()); |
| 53 | p->setPrefix(v[i].prefix.c_str()); |
| 54 | p->setSeq(v[i].high); |
| 55 | m_roster.insert(p->getPrefix(), p); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (rePlot) |
| 60 | { |
| 61 | plot(digest); |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | for (int i = 0; i < n; i++) |
| 66 | { |
| 67 | Roster_iterator it = m_roster.find(v[i].prefix.c_str()); |
| 68 | if (it != m_roster.end()) { |
| 69 | DisplayUserPtr p = it.value(); |
| 70 | QGraphicsTextItem *item = p->getSeqTextItem(); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 71 | QGraphicsRectItem *rectItem = p->getInnerRectItem(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 72 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 73 | item->setPlainText(s.c_str()); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 74 | QRectF textBR = item->boundingRect(); |
| 75 | QRectF rectBR = rectItem->boundingRect(); |
| 76 | 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] | 77 | } |
| 78 | } |
| 79 | m_rootDigest->setPlainText(digest); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | DigestTreeScene::msgReceived(QString prefix, QString nick) |
| 85 | { |
| 86 | Roster_iterator it = m_roster.find(prefix); |
| 87 | if (it != m_roster.end()) |
| 88 | { |
| 89 | DisplayUserPtr p = it.value(); |
| 90 | if (nick != p->getNick()) { |
| 91 | p->setNick(nick); |
| 92 | QGraphicsTextItem *nickItem = p->getNickTextItem(); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 93 | QGraphicsRectItem *nickRectItem = p->getNickRectItem(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 94 | nickItem->setPlainText(p->getNick()); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 95 | QRectF rectBR = nickRectItem->boundingRect(); |
| 96 | QRectF nickBR = nickItem->boundingRect(); |
| 97 | nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 98 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 99 | |
| 100 | reDrawNode(p, Qt::red); |
| 101 | |
| 102 | if (previouslyUpdatedUser != DisplayUserNullPtr) |
| 103 | { |
| 104 | reDrawNode(previouslyUpdatedUser, Qt::darkBlue); |
| 105 | } |
| 106 | |
| 107 | previouslyUpdatedUser = p; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | void |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame^] | 112 | DigestTreeScene::clearAll() |
| 113 | { |
| 114 | clear(); |
| 115 | m_graph.clear(); |
| 116 | m_roster.clear(); |
| 117 | } |
| 118 | |
| 119 | void |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 120 | DigestTreeScene::plot(QString digest) |
| 121 | { |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 122 | clear(); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 123 | m_graph.clear(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 124 | int n = m_roster.size(); |
| 125 | ogdf::node root = m_graph.newNode(); |
| 126 | int rootIndex = root->index(); |
| 127 | for (int i = 0; i < n; i++) { |
| 128 | ogdf::node leaf = m_graph.newNode(); |
| 129 | m_graph.newEdge(root, leaf); |
| 130 | } |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 131 | ogdf::GraphAttributes GA(m_graph); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 132 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 133 | int nodeSize = 40; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 134 | int siblingDistance = 100, levelDistance = 100; |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 135 | ogdf::TreeLayout layout; |
| 136 | layout.siblingDistance(siblingDistance); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 137 | layout.levelDistance(levelDistance); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 138 | layout.callSortByPositions(GA, m_graph); |
| 139 | |
| 140 | int width = GA.boundingBox().width(); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 141 | int height = GA.boundingBox().height(); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 142 | //setSceneRect(QRect(- (width + nodeSize) / 2, - 50, width + nodeSize, height + nodeSize)); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 143 | GA.setAllWidth(nodeSize); |
| 144 | GA.setAllHeight(nodeSize); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 145 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 146 | plotEdge(GA); |
| 147 | plotNode(GA, rootIndex, digest); |
| 148 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 149 | previouslyUpdatedUser = DisplayUserNullPtr; |
| 150 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void |
| 154 | DigestTreeScene::plotEdge(ogdf::GraphAttributes &GA) |
| 155 | { |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 156 | ogdf::edge e; |
| 157 | forall_edges(e, m_graph) { |
| 158 | ogdf::node source = e->source(); |
| 159 | ogdf::node target = e->target(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 160 | int nodeSize = GA.width(target); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 161 | int x1 = GA.x(source), y1 = -GA.y(source); |
| 162 | int x2 = GA.x(target), y2 = -GA.y(target); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 163 | QPointF src(x1 + nodeSize/2, y1 + nodeSize/2); |
| 164 | QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 165 | QLineF line(src, dest); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 166 | double angle = ::acos(line.dx() / line.length()); |
| 167 | |
| 168 | double arrowSize = 10; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 169 | QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length()); |
| 170 | QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize, |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 171 | sin(angle + Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 172 | QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize, |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 173 | sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 174 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 175 | addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black)); |
| 176 | addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black)); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | DigestTreeScene::plotNode(ogdf::GraphAttributes &GA, int rootIndex, QString digest) |
| 182 | { |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 183 | ogdf::node n; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 184 | RosterIterator it(m_roster); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 185 | forall_nodes(n, m_graph) { |
| 186 | double x = GA.x(n); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 187 | double y = -GA.y(n); |
| 188 | double w = GA.width(n); |
| 189 | double h = GA.height(n); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 190 | int rim = 3; |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 191 | QRectF boundingRect(x, y, w, h); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 192 | QRectF innerBoundingRect(x + rim, y + rim, w - rim * 2, h - rim * 2); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 193 | |
| 194 | if (n->index() == rootIndex) |
| 195 | { |
| 196 | addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkRed)); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 197 | addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray)); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 198 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 199 | QRectF digestRect(x - w, y - h, 3 * w, 30); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 200 | addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
| 201 | QGraphicsTextItem *digestItem = addText(digest); |
| 202 | QRectF digestBoundingRect = digestItem->boundingRect(); |
| 203 | digestItem->setDefaultTextColor(Qt::white); |
| 204 | digestItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 205 | digestItem->setPos(x - w + (3 * w - digestBoundingRect.width()) / 2, y - h + 5); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 206 | m_rootDigest = digestItem; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | if (it.hasNext()) |
| 211 | { |
| 212 | it.next(); |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | abort(); |
| 217 | } |
| 218 | DisplayUserPtr p = it.value(); |
| 219 | QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue)); |
| 220 | p->setRimRectItem(rectItem); |
| 221 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 222 | QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray)); |
| 223 | p->setInnerRectItem(innerRectItem); |
| 224 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 225 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 226 | QGraphicsTextItem *seqItem = addText(s.c_str()); |
| 227 | seqItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
| 228 | QRectF seqBoundingRect = seqItem->boundingRect(); |
| 229 | seqItem->setPos(x + w / 2 - seqBoundingRect.width() / 2, y + h / 2 - seqBoundingRect.height() / 2); |
| 230 | p->setSeqTextItem(seqItem); |
| 231 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 232 | QRectF textRect(x - w / 2, y + h, 2 * w, 30); |
| 233 | QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
| 234 | p->setNickRectItem(nickRectItem); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 235 | QGraphicsTextItem *nickItem = addText(p->getNick()); |
| 236 | QRectF textBoundingRect = nickItem->boundingRect(); |
| 237 | nickItem->setDefaultTextColor(Qt::white); |
| 238 | nickItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 239 | nickItem->setPos(x + w / 2 - textBoundingRect.width() / 2, y + h + 5); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 240 | p->setNickTextItem(nickItem); |
| 241 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 242 | |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 243 | } |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame^] | 246 | void |
| 247 | DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor) |
| 248 | { |
| 249 | QGraphicsRectItem *rimItem = p->getRimRectItem(); |
| 250 | rimItem->setBrush(QBrush(rimColor)); |
| 251 | QGraphicsRectItem *innerItem = p->getInnerRectItem(); |
| 252 | innerItem->setBrush(QBrush(Qt::lightGray)); |
| 253 | QGraphicsTextItem *seqTextItem = p->getSeqTextItem(); |
| 254 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 255 | seqTextItem->setPlainText(s.c_str()); |
| 256 | QRectF textBR = seqTextItem->boundingRect(); |
| 257 | QRectF innerBR = innerItem->boundingRect(); |
| 258 | seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2); |
| 259 | } |
| 260 | |