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; |
| 39 | testDraw(this); |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 43 | DigestTreeScene::processUpdate(std::vector<Sync::MissingDataInfo> &v, QString digest) |
| 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 |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 84 | DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor) |
| 85 | { |
| 86 | QGraphicsRectItem *rimItem = p->getRimRectItem(); |
| 87 | rimItem->setBrush(QBrush(rimColor)); |
| 88 | QGraphicsRectItem *innerItem = p->getInnerRectItem(); |
| 89 | innerItem->setBrush(QBrush(Qt::lightGray)); |
| 90 | QGraphicsTextItem *seqTextItem = p->getSeqTextItem(); |
| 91 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 92 | seqTextItem->setPlainText(s.c_str()); |
| 93 | QRectF textBR = seqTextItem->boundingRect(); |
| 94 | QRectF innerBR = innerItem->boundingRect(); |
| 95 | seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2); |
| 96 | } |
| 97 | |
| 98 | void |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 99 | DigestTreeScene::msgReceived(QString prefix, QString nick) |
| 100 | { |
| 101 | Roster_iterator it = m_roster.find(prefix); |
| 102 | if (it != m_roster.end()) |
| 103 | { |
| 104 | DisplayUserPtr p = it.value(); |
| 105 | if (nick != p->getNick()) { |
| 106 | p->setNick(nick); |
| 107 | QGraphicsTextItem *nickItem = p->getNickTextItem(); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 108 | QGraphicsRectItem *nickRectItem = p->getNickRectItem(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 109 | nickItem->setPlainText(p->getNick()); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 110 | QRectF rectBR = nickRectItem->boundingRect(); |
| 111 | QRectF nickBR = nickItem->boundingRect(); |
| 112 | nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 113 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 114 | |
| 115 | reDrawNode(p, Qt::red); |
| 116 | |
| 117 | if (previouslyUpdatedUser != DisplayUserNullPtr) |
| 118 | { |
| 119 | reDrawNode(previouslyUpdatedUser, Qt::darkBlue); |
| 120 | } |
| 121 | |
| 122 | previouslyUpdatedUser = p; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | DigestTreeScene::plot(QString digest) |
| 128 | { |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 129 | clear(); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 130 | m_graph.clear(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 131 | int n = m_roster.size(); |
| 132 | ogdf::node root = m_graph.newNode(); |
| 133 | int rootIndex = root->index(); |
| 134 | for (int i = 0; i < n; i++) { |
| 135 | ogdf::node leaf = m_graph.newNode(); |
| 136 | m_graph.newEdge(root, leaf); |
| 137 | } |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 138 | ogdf::GraphAttributes GA(m_graph); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 139 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 140 | int nodeSize = 40; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 141 | int siblingDistance = 100, levelDistance = 100; |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 142 | ogdf::TreeLayout layout; |
| 143 | layout.siblingDistance(siblingDistance); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 144 | layout.levelDistance(levelDistance); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 145 | layout.callSortByPositions(GA, m_graph); |
| 146 | |
| 147 | int width = GA.boundingBox().width(); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 148 | int height = GA.boundingBox().height(); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 149 | //setSceneRect(QRect(- (width + nodeSize) / 2, - 50, width + nodeSize, height + nodeSize)); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 150 | GA.setAllWidth(nodeSize); |
| 151 | GA.setAllHeight(nodeSize); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 152 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 153 | plotEdge(GA); |
| 154 | plotNode(GA, rootIndex, digest); |
| 155 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 156 | previouslyUpdatedUser = DisplayUserNullPtr; |
| 157 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void |
| 161 | DigestTreeScene::plotEdge(ogdf::GraphAttributes &GA) |
| 162 | { |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 163 | ogdf::edge e; |
| 164 | forall_edges(e, m_graph) { |
| 165 | ogdf::node source = e->source(); |
| 166 | ogdf::node target = e->target(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 167 | int nodeSize = GA.width(target); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 168 | int x1 = GA.x(source), y1 = -GA.y(source); |
| 169 | int x2 = GA.x(target), y2 = -GA.y(target); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 170 | QPointF src(x1 + nodeSize/2, y1 + nodeSize/2); |
| 171 | QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 172 | QLineF line(src, dest); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 173 | double angle = ::acos(line.dx() / line.length()); |
| 174 | |
| 175 | double arrowSize = 10; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 176 | QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length()); |
| 177 | QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize, |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 178 | sin(angle + Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 179 | QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize, |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 180 | sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 181 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 182 | addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black)); |
| 183 | addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black)); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | DigestTreeScene::plotNode(ogdf::GraphAttributes &GA, int rootIndex, QString digest) |
| 189 | { |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 190 | ogdf::node n; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 191 | RosterIterator it(m_roster); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 192 | forall_nodes(n, m_graph) { |
| 193 | double x = GA.x(n); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 194 | double y = -GA.y(n); |
| 195 | double w = GA.width(n); |
| 196 | double h = GA.height(n); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 197 | int rim = 3; |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 198 | QRectF boundingRect(x, y, w, h); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 199 | QRectF innerBoundingRect(x + rim, y + rim, w - rim * 2, h - rim * 2); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 200 | |
| 201 | if (n->index() == rootIndex) |
| 202 | { |
| 203 | addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkRed)); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 204 | addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray)); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 205 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 206 | QRectF digestRect(x - w, y - h, 3 * w, 30); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 207 | addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
| 208 | QGraphicsTextItem *digestItem = addText(digest); |
| 209 | QRectF digestBoundingRect = digestItem->boundingRect(); |
| 210 | digestItem->setDefaultTextColor(Qt::white); |
| 211 | digestItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 212 | digestItem->setPos(x - w + (3 * w - digestBoundingRect.width()) / 2, y - h + 5); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 213 | m_rootDigest = digestItem; |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | if (it.hasNext()) |
| 218 | { |
| 219 | it.next(); |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | abort(); |
| 224 | } |
| 225 | DisplayUserPtr p = it.value(); |
| 226 | QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue)); |
| 227 | p->setRimRectItem(rectItem); |
| 228 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 229 | QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray)); |
| 230 | p->setInnerRectItem(innerRectItem); |
| 231 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 232 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 233 | QGraphicsTextItem *seqItem = addText(s.c_str()); |
| 234 | seqItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
| 235 | QRectF seqBoundingRect = seqItem->boundingRect(); |
| 236 | seqItem->setPos(x + w / 2 - seqBoundingRect.width() / 2, y + h / 2 - seqBoundingRect.height() / 2); |
| 237 | p->setSeqTextItem(seqItem); |
| 238 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 239 | QRectF textRect(x - w / 2, y + h, 2 * w, 30); |
| 240 | QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
| 241 | p->setNickRectItem(nickRectItem); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 242 | QGraphicsTextItem *nickItem = addText(p->getNick()); |
| 243 | QRectF textBoundingRect = nickItem->boundingRect(); |
| 244 | nickItem->setDefaultTextColor(Qt::white); |
| 245 | nickItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 246 | nickItem->setPos(x + w / 2 - textBoundingRect.width() / 2, y + h + 5); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 247 | p->setNickTextItem(nickItem); |
| 248 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 249 | |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 250 | } |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 251 | } |
| 252 | |