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