Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 9 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 10 | */ |
| 11 | |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 12 | #include "digest-tree-scene.h" |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 13 | |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 14 | #include <QtGui> |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 15 | |
| 16 | #ifndef Q_MOC_RUN |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 17 | #include <vector> |
| 18 | #include <iostream> |
| 19 | #include <assert.h> |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 20 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 21 | #include <memory> |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 22 | #endif |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 23 | |
| 24 | static const double Pi = 3.14159265358979323846264338327950288419717; |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 25 | |
Zhenkai Zhu | 7f52e1b | 2012-10-09 11:45:36 -0700 | [diff] [blame] | 26 | //DisplayUserPtr DisplayUserNullPtr; |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 27 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 28 | DigestTreeScene::DigestTreeScene(QWidget *parent) |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 29 | : QGraphicsScene(parent) |
| 30 | { |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 31 | previouslyUpdatedUser = DisplayUserNullPtr; |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 35 | DigestTreeScene::processUpdate(const std::vector<Sync::MissingDataInfo> &v, QString digest) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 36 | { |
| 37 | int n = v.size(); |
| 38 | bool rePlot = false; |
| 39 | for (int i = 0; i < n; i++) |
| 40 | { |
Yingdi Yu | 5a5ff20 | 2014-03-17 10:03:02 -0700 | [diff] [blame^] | 41 | QString routablePrefix(v[i].prefix.c_str()); |
| 42 | QString prefix = trimRoutablePrefix(routablePrefix); |
| 43 | |
| 44 | Roster_iterator it = m_roster.find(prefix); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 45 | if (it == m_roster.end()) |
| 46 | { |
Yingdi Yu | 5a5ff20 | 2014-03-17 10:03:02 -0700 | [diff] [blame^] | 47 | // std::cout << "processUpdate v[" << i << "]: " << prefix.toStdString() << std::endl; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 48 | rePlot = true; |
| 49 | DisplayUserPtr p(new DisplayUser()); |
Zhenkai Zhu | c61dbc2 | 2012-10-08 11:36:37 -0700 | [diff] [blame] | 50 | time_t tempTime = time(NULL) - FRESHNESS + 1; |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 51 | p->setReceived(tempTime); |
Yingdi Yu | 5a5ff20 | 2014-03-17 10:03:02 -0700 | [diff] [blame^] | 52 | p->setPrefix(prefix); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 53 | p->setSeq(v[i].high); |
| 54 | m_roster.insert(p->getPrefix(), p); |
| 55 | } |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 56 | else |
| 57 | { |
| 58 | it.value()->setSeq(v[i].high); |
| 59 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | if (rePlot) |
| 63 | { |
| 64 | plot(digest); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 65 | QTimer::singleShot(2100, this, SLOT(emitReplot())); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | { |
| 69 | for (int i = 0; i < n; i++) |
| 70 | { |
Yingdi Yu | 5a5ff20 | 2014-03-17 10:03:02 -0700 | [diff] [blame^] | 71 | QString routablePrefix(v[i].prefix.c_str()); |
| 72 | QString prefix = trimRoutablePrefix(routablePrefix); |
| 73 | |
| 74 | Roster_iterator it = m_roster.find(prefix); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 75 | if (it != m_roster.end()) { |
| 76 | DisplayUserPtr p = it.value(); |
| 77 | QGraphicsTextItem *item = p->getSeqTextItem(); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 78 | QGraphicsRectItem *rectItem = p->getInnerRectItem(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 79 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 80 | item->setPlainText(s.c_str()); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 81 | QRectF textBR = item->boundingRect(); |
| 82 | QRectF rectBR = rectItem->boundingRect(); |
| 83 | 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] | 84 | } |
| 85 | } |
| 86 | m_rootDigest->setPlainText(digest); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 91 | DigestTreeScene::emitReplot() |
| 92 | { |
| 93 | emit replot(); |
| 94 | } |
| 95 | |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 96 | QStringList |
Zhenkai Zhu | 6082ede | 2012-09-27 17:28:46 -0700 | [diff] [blame] | 97 | DigestTreeScene::getRosterList() |
| 98 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 99 | QStringList rosterList; |
| 100 | RosterIterator it(m_roster); |
| 101 | while(it.hasNext()) |
| 102 | { |
| 103 | it.next(); |
| 104 | DisplayUserPtr p = it.value(); |
| 105 | if (p != DisplayUserNullPtr) |
| 106 | { |
| 107 | rosterList << "- " + p->getNick(); |
| 108 | } |
| 109 | } |
| 110 | return rosterList; |
Zhenkai Zhu | 6082ede | 2012-09-27 17:28:46 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void |
Yingdi Yu | 5a5ff20 | 2014-03-17 10:03:02 -0700 | [diff] [blame^] | 114 | DigestTreeScene::msgReceived(QString routablePrefix, QString nick) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 115 | { |
Yingdi Yu | 5a5ff20 | 2014-03-17 10:03:02 -0700 | [diff] [blame^] | 116 | QString prefix = trimRoutablePrefix(routablePrefix); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 117 | Roster_iterator it = m_roster.find(prefix); |
Yingdi Yu | 5a5ff20 | 2014-03-17 10:03:02 -0700 | [diff] [blame^] | 118 | // std::cout << "msgReceived prefix: " << prefix.toStdString() << std::endl; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 119 | if (it != m_roster.end()) |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 120 | { |
Yingdi Yu | 5a5ff20 | 2014-03-17 10:03:02 -0700 | [diff] [blame^] | 121 | // std::cout << "Updating for prefix = " << prefix.toStdString() << " nick = " << nick.toStdString() << std::endl; |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 122 | DisplayUserPtr p = it.value(); |
| 123 | p->setReceived(time(NULL)); |
| 124 | if (nick != p->getNick()) |
| 125 | { |
Yingdi Yu | 5a5ff20 | 2014-03-17 10:03:02 -0700 | [diff] [blame^] | 126 | // std::cout << "old nick = " << p->getNick().toStdString() << std::endl; |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 127 | p->setNick(nick); |
| 128 | QGraphicsTextItem *nickItem = p->getNickTextItem(); |
| 129 | QGraphicsRectItem *nickRectItem = p->getNickRectItem(); |
| 130 | nickItem->setPlainText(p->getNick()); |
| 131 | QRectF rectBR = nickRectItem->boundingRect(); |
| 132 | QRectF nickBR = nickItem->boundingRect(); |
| 133 | nickItem->setPos(rectBR.x() + (rectBR.width() - nickBR.width())/2, rectBR.y() + 5); |
| 134 | emit rosterChanged(QStringList()); |
| 135 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 136 | |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 137 | reDrawNode(p, Qt::red); |
| 138 | |
| 139 | if (previouslyUpdatedUser != DisplayUserNullPtr && previouslyUpdatedUser != p) |
| 140 | { |
| 141 | reDrawNode(previouslyUpdatedUser, Qt::darkBlue); |
| 142 | } |
| 143 | |
| 144 | previouslyUpdatedUser = p; |
| 145 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 149 | DigestTreeScene::clearAll() |
| 150 | { |
| 151 | clear(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 152 | m_roster.clear(); |
| 153 | } |
| 154 | |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 155 | bool |
| 156 | DigestTreeScene::removeNode(const QString prefix) |
| 157 | { |
| 158 | int removedCount = m_roster.remove(prefix); |
| 159 | return (removedCount > 0); |
| 160 | } |
| 161 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 162 | void |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 163 | DigestTreeScene::plot(QString digest) |
| 164 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 165 | #ifdef _DEBUG |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 166 | std::cout << "Plotting at time: " << time(NULL) << std::endl; |
| 167 | #endif |
Zhenkai Zhu | 6fcdee4 | 2012-05-30 17:02:49 -0700 | [diff] [blame] | 168 | clear(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 169 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 170 | int nodeSize = 40; |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 171 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 172 | int siblingDistance = 100, levelDistance = 100; |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 173 | std::auto_ptr<TreeLayout> layout(new OneLevelTreeLayout()); |
| 174 | layout->setSiblingDistance(siblingDistance); |
| 175 | layout->setLevelDistance(levelDistance); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 176 | |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 177 | // do some cleaning, get rid of stale member info |
| 178 | Roster_iterator it = m_roster.begin(); |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 179 | QStringList staleUserList; |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 180 | while (it != m_roster.end()) |
| 181 | { |
| 182 | DisplayUserPtr p = it.value(); |
| 183 | if (p != DisplayUserNullPtr) |
| 184 | { |
| 185 | time_t now = time(NULL); |
Zhenkai Zhu | c61dbc2 | 2012-10-08 11:36:37 -0700 | [diff] [blame] | 186 | if (now - p->getReceived() >= FRESHNESS) |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 187 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 188 | #ifdef _DEBUG |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 189 | std::cout << "Removing user: " << p->getNick().toStdString() << std::endl; |
| 190 | std::cout << "now - last = " << now - p->getReceived() << std::endl; |
| 191 | #endif |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 192 | staleUserList << p->getNick(); |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 193 | p = DisplayUserNullPtr; |
| 194 | it = m_roster.erase(it); |
| 195 | } |
| 196 | else |
| 197 | { |
Zhenkai Zhu | 716fe85 | 2012-10-08 18:27:55 -0700 | [diff] [blame] | 198 | if (!m_currentPrefix.startsWith("/private/local") && p->getPrefix().startsWith("/private/local")) |
| 199 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 200 | #ifdef _DEBUG |
Zhenkai Zhu | 716fe85 | 2012-10-08 18:27:55 -0700 | [diff] [blame] | 201 | std::cout << "erasing: " << p->getPrefix().toStdString() << std::endl; |
| 202 | #endif |
| 203 | staleUserList << p->getNick(); |
| 204 | p = DisplayUserNullPtr; |
| 205 | it = m_roster.erase(it); |
| 206 | continue; |
| 207 | } |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 208 | ++it; |
| 209 | } |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | it = m_roster.erase(it); |
| 214 | } |
| 215 | } |
| 216 | |
Zhenkai Zhu | 6082ede | 2012-09-27 17:28:46 -0700 | [diff] [blame] | 217 | // for simpicity here, whenever we replot, we also redo the roster list |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 218 | emit rosterChanged(staleUserList); |
Zhenkai Zhu | 6082ede | 2012-09-27 17:28:46 -0700 | [diff] [blame] | 219 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 220 | int n = m_roster.size(); |
Zhenkai Zhu | 716fe85 | 2012-10-08 18:27:55 -0700 | [diff] [blame] | 221 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 222 | std::vector<TreeLayout::Coordinate> childNodesCo(n); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 223 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 224 | layout->setOneLevelLayout(childNodesCo); |
| 225 | |
| 226 | plotEdge(childNodesCo, nodeSize); |
| 227 | plotNode(childNodesCo, digest, nodeSize); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 228 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 229 | previouslyUpdatedUser = DisplayUserNullPtr; |
| 230 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 234 | DigestTreeScene::plotEdge(const std::vector<TreeLayout::Coordinate> &childNodesCo, int nodeSize) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 235 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 236 | int n = childNodesCo.size(); |
| 237 | for (int i = 0; i < n; i++) { |
| 238 | double x1 = 0.0, y1 = 0.0; |
| 239 | double x2 = childNodesCo[i].x, y2 = childNodesCo[i].y; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 240 | QPointF src(x1 + nodeSize/2, y1 + nodeSize/2); |
| 241 | QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 242 | QLineF line(src, dest); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 243 | double angle = ::acos(line.dx() / line.length()); |
| 244 | |
| 245 | double arrowSize = 10; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 246 | QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length()); |
| 247 | QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize, |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 248 | sin(angle + Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 249 | QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize, |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 250 | sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 251 | |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 252 | addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black)); |
| 253 | addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 258 | DigestTreeScene::plotNode(const std::vector<TreeLayout::Coordinate> &childNodesCo, QString digest, int nodeSize) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 259 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 260 | RosterIterator it(m_roster); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 261 | int n = childNodesCo.size(); |
| 262 | int rim = 3; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 263 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 264 | // plot root node |
| 265 | QRectF rootBoundingRect(0, 0, nodeSize, nodeSize); |
| 266 | QRectF rootInnerBoundingRect(rim, rim, nodeSize - rim * 2, nodeSize - rim * 2); |
| 267 | addRect(rootBoundingRect, QPen(Qt::black), QBrush(Qt::darkRed)); |
| 268 | addRect(rootInnerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray)); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 269 | QRectF digestRect(- 5.5 * nodeSize , - nodeSize, 12 * nodeSize, 30); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 270 | addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
Zhenkai Zhu | 27df8d4 | 2012-06-05 12:04:04 -0700 | [diff] [blame] | 271 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 272 | QGraphicsTextItem *digestItem = addText(digest); |
| 273 | QRectF digestBoundingRect = digestItem->boundingRect(); |
Zhenkai Zhu | 27df8d4 | 2012-06-05 12:04:04 -0700 | [diff] [blame] | 274 | digestItem->setDefaultTextColor(Qt::black); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 275 | digestItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 276 | digestItem->setPos(- 4.5 * nodeSize + (12 * nodeSize - digestBoundingRect.width()) / 2, - nodeSize + 5); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 277 | m_rootDigest = digestItem; |
| 278 | |
| 279 | // plot child nodes |
| 280 | for (int i = 0; i < n; i++) |
| 281 | { |
| 282 | if (it.hasNext()) |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 283 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 284 | it.next(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 285 | } |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 286 | else |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 287 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 288 | abort(); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 289 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 290 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 291 | double x = childNodesCo[i].x; |
| 292 | double y = childNodesCo[i].y; |
| 293 | QRectF boundingRect(x, y, nodeSize, nodeSize); |
| 294 | QRectF innerBoundingRect(x + rim, y + rim, nodeSize - rim * 2, nodeSize - rim * 2); |
| 295 | DisplayUserPtr p = it.value(); |
| 296 | QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue)); |
| 297 | p->setRimRectItem(rectItem); |
| 298 | |
| 299 | QGraphicsRectItem *innerRectItem = addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray)); |
| 300 | p->setInnerRectItem(innerRectItem); |
| 301 | |
| 302 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 303 | QGraphicsTextItem *seqItem = addText(s.c_str()); |
| 304 | seqItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
| 305 | QRectF seqBoundingRect = seqItem->boundingRect(); |
| 306 | seqItem->setPos(x + nodeSize / 2 - seqBoundingRect.width() / 2, y + nodeSize / 2 - seqBoundingRect.height() / 2); |
| 307 | p->setSeqTextItem(seqItem); |
| 308 | |
| 309 | QRectF textRect(x - nodeSize / 2, y + nodeSize, 2 * nodeSize, 30); |
| 310 | QGraphicsRectItem *nickRectItem = addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan)); |
| 311 | p->setNickRectItem(nickRectItem); |
| 312 | QGraphicsTextItem *nickItem = addText(p->getNick()); |
| 313 | QRectF textBoundingRect = nickItem->boundingRect(); |
| 314 | nickItem->setDefaultTextColor(Qt::white); |
| 315 | nickItem->setFont(QFont("Cursive", 12, QFont::Bold)); |
| 316 | nickItem->setPos(x + nodeSize / 2 - textBoundingRect.width() / 2, y + nodeSize + 5); |
| 317 | p->setNickTextItem(nickItem); |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 318 | } |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 319 | |
Zhenkai Zhu | f474a0a | 2012-05-30 15:06:29 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 322 | void |
| 323 | DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor) |
| 324 | { |
| 325 | QGraphicsRectItem *rimItem = p->getRimRectItem(); |
| 326 | rimItem->setBrush(QBrush(rimColor)); |
| 327 | QGraphicsRectItem *innerItem = p->getInnerRectItem(); |
| 328 | innerItem->setBrush(QBrush(Qt::lightGray)); |
| 329 | QGraphicsTextItem *seqTextItem = p->getSeqTextItem(); |
| 330 | std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq()); |
| 331 | seqTextItem->setPlainText(s.c_str()); |
| 332 | QRectF textBR = seqTextItem->boundingRect(); |
| 333 | QRectF innerBR = innerItem->boundingRect(); |
| 334 | seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2); |
| 335 | } |
| 336 | |
Yingdi Yu | 5a5ff20 | 2014-03-17 10:03:02 -0700 | [diff] [blame^] | 337 | QString |
| 338 | DigestTreeScene::trimRoutablePrefix(QString prefix) |
| 339 | { |
| 340 | bool encaped = false; |
| 341 | ndn::Name prefixName(prefix.toStdString()); |
| 342 | |
| 343 | ndn::Name::const_iterator it = prefixName.begin(); |
| 344 | ndn::Name::const_iterator end = prefixName.end(); |
| 345 | size_t offset = 0; |
| 346 | |
| 347 | for(; it != end; it++, offset++) |
| 348 | { |
| 349 | if(it->toEscapedString() == "%F0.") |
| 350 | { |
| 351 | encaped = true; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if(!encaped) |
| 357 | return prefix; |
| 358 | else |
| 359 | { |
| 360 | return QString(prefixName.getSubName(offset+1).toUri().c_str()); |
| 361 | } |
| 362 | } |
| 363 | |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 364 | #if WAF |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 365 | #include "digest-tree-scene.moc" |
| 366 | #include "digest-tree-scene.cpp.moc" |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 367 | #endif |