Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -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 | |
| 12 | #ifndef CHRONOS_DIGEST_TREE_SCENE_HPP |
| 13 | #define CHRONOS_DIGEST_TREE_SCENE_HPP |
| 14 | |
| 15 | #include <QtGui/QGraphicsScene> |
| 16 | #include <QColor> |
| 17 | #include <QMap> |
| 18 | |
| 19 | #ifndef Q_MOC_RUN |
| 20 | #include "tree-layout.hpp" |
| 21 | #include <sync-seq-no.h> |
| 22 | #include <sync-logic.h> |
| 23 | #include <ctime> |
| 24 | #include <vector> |
| 25 | #include <boost/shared_ptr.hpp> |
| 26 | #endif |
| 27 | |
| 28 | const int FRESHNESS = 60; |
| 29 | |
| 30 | class QGraphicsTextItem; |
| 31 | |
| 32 | namespace chronos { |
| 33 | |
| 34 | class User; |
| 35 | class DisplayUser; |
| 36 | typedef boost::shared_ptr<DisplayUser> DisplayUserPtr; |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame^] | 37 | typedef QMap<QString, DisplayUserPtr> Roster; |
| 38 | typedef QMap<QString, DisplayUserPtr>::iterator Roster_iterator; |
| 39 | typedef QMapIterator<QString, DisplayUserPtr> RosterIterator; |
| 40 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 41 | static DisplayUserPtr DisplayUserNullPtr; |
| 42 | |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame^] | 43 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 44 | class DigestTreeScene : public QGraphicsScene |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 48 | public: |
| 49 | DigestTreeScene(QWidget *parent = 0); |
| 50 | |
| 51 | void |
| 52 | processUpdate(const std::vector<Sync::MissingDataInfo>& v, QString digest); |
| 53 | |
| 54 | void |
| 55 | msgReceived(QString prefix, QString nick); |
| 56 | |
| 57 | void |
| 58 | clearAll(); |
| 59 | |
| 60 | bool |
| 61 | removeNode(const QString prefix); |
| 62 | |
| 63 | void |
| 64 | plot(QString digest); |
| 65 | |
| 66 | QStringList |
| 67 | getRosterList(); |
| 68 | |
| 69 | void |
| 70 | setCurrentPrefix(QString prefix) |
| 71 | { |
| 72 | m_currentPrefix = prefix; |
| 73 | } |
| 74 | |
| 75 | QMap<QString, DisplayUserPtr> getRosterFull() |
| 76 | { |
| 77 | return m_roster; |
| 78 | } |
| 79 | |
| 80 | signals: |
| 81 | void |
| 82 | replot(); |
| 83 | |
| 84 | void |
| 85 | rosterChanged(QStringList); |
| 86 | |
| 87 | private slots: |
| 88 | void |
| 89 | emitReplot(); |
| 90 | |
| 91 | private: |
| 92 | void |
| 93 | plotEdge(const std::vector<chronos::TreeLayout::Coordinate>& v, int nodeSize); |
| 94 | |
| 95 | void |
| 96 | plotNode(const std::vector<chronos::TreeLayout::Coordinate>& v, QString digest, int nodeSize); |
| 97 | |
| 98 | void |
| 99 | reDrawNode(DisplayUserPtr p, QColor rimColor); |
| 100 | |
| 101 | QString |
| 102 | trimRoutablePrefix(QString prefix); |
| 103 | |
| 104 | private: |
| 105 | Roster m_roster; |
| 106 | QGraphicsTextItem* m_rootDigest; |
| 107 | DisplayUserPtr previouslyUpdatedUser; |
| 108 | QString m_currentPrefix; |
| 109 | }; |
| 110 | |
| 111 | class User |
| 112 | { |
| 113 | public: |
| 114 | User() |
| 115 | :m_received(::time(NULL)) |
| 116 | { |
| 117 | } |
| 118 | |
| 119 | User(QString n, QString p, QString c) |
| 120 | : m_nick(n) |
| 121 | , m_prefix(p) |
| 122 | , m_chatroom(c) |
| 123 | , m_received(::time(NULL)) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | setNick(QString nick) |
| 129 | { |
| 130 | m_nick = nick; |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | setPrefix(QString prefix) |
| 135 | { |
| 136 | m_prefix = prefix; |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | setChatroom(QString chatroom) |
| 141 | { |
| 142 | m_chatroom = chatroom; |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | setSeq(Sync::SeqNo seq) |
| 147 | { |
| 148 | m_seq = seq; |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | setReceived(time_t t) |
| 153 | { |
| 154 | m_received = t; |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | setOriginPrefix(QString originPrefix) |
| 159 | { |
| 160 | m_originPrefix = originPrefix; |
| 161 | } |
| 162 | |
| 163 | QString |
| 164 | getNick() |
| 165 | { |
| 166 | return m_nick; |
| 167 | } |
| 168 | |
| 169 | QString getPrefix() |
| 170 | { |
| 171 | return m_prefix; |
| 172 | } |
| 173 | |
| 174 | QString getChatroom() |
| 175 | { |
| 176 | return m_chatroom; |
| 177 | } |
| 178 | |
| 179 | QString getOriginPrefix() |
| 180 | { |
| 181 | return m_originPrefix; |
| 182 | } |
| 183 | |
| 184 | Sync::SeqNo |
| 185 | getSeqNo() |
| 186 | { |
| 187 | return m_seq; |
| 188 | } |
| 189 | |
| 190 | time_t getReceived() |
| 191 | { |
| 192 | return m_received; |
| 193 | } |
| 194 | |
| 195 | private: |
| 196 | QString m_nick; |
| 197 | QString m_prefix; |
| 198 | QString m_chatroom; |
| 199 | QString m_originPrefix; |
| 200 | Sync::SeqNo m_seq; |
| 201 | time_t m_received; |
| 202 | }; |
| 203 | |
| 204 | class DisplayUser : public User |
| 205 | { |
| 206 | public: |
| 207 | DisplayUser() |
| 208 | : m_seqTextItem(NULL) |
| 209 | , m_nickTextItem(NULL) |
| 210 | , m_rimRectItem(NULL) |
| 211 | { |
| 212 | } |
| 213 | |
| 214 | DisplayUser(QString n, QString p , QString c) |
| 215 | : User(n, p, c) |
| 216 | , m_seqTextItem(NULL) |
| 217 | , m_nickTextItem(NULL) |
| 218 | , m_rimRectItem(NULL) |
| 219 | { |
| 220 | } |
| 221 | |
| 222 | QGraphicsTextItem* |
| 223 | getSeqTextItem() |
| 224 | { |
| 225 | return m_seqTextItem; |
| 226 | } |
| 227 | |
| 228 | QGraphicsTextItem* |
| 229 | getNickTextItem() |
| 230 | { |
| 231 | return m_nickTextItem; |
| 232 | } |
| 233 | |
| 234 | QGraphicsRectItem* |
| 235 | getRimRectItem() |
| 236 | { |
| 237 | return m_rimRectItem; |
| 238 | } |
| 239 | |
| 240 | QGraphicsRectItem* |
| 241 | getInnerRectItem() |
| 242 | { |
| 243 | return m_innerRectItem; |
| 244 | } |
| 245 | |
| 246 | QGraphicsRectItem* |
| 247 | getNickRectItem() |
| 248 | { |
| 249 | return m_nickRectItem; |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | setSeqTextItem(QGraphicsTextItem* item) |
| 254 | { |
| 255 | m_seqTextItem = item; |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | setNickTextItem(QGraphicsTextItem* item) |
| 260 | { |
| 261 | m_nickTextItem = item; |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | setRimRectItem(QGraphicsRectItem* item) |
| 266 | { |
| 267 | m_rimRectItem = item; |
| 268 | } |
| 269 | |
| 270 | void |
| 271 | setInnerRectItem(QGraphicsRectItem* item) |
| 272 | { |
| 273 | m_innerRectItem = item; |
| 274 | } |
| 275 | |
| 276 | void |
| 277 | setNickRectItem(QGraphicsRectItem* item) |
| 278 | { |
| 279 | m_nickRectItem = item; |
| 280 | } |
| 281 | |
| 282 | private: |
| 283 | QGraphicsTextItem* m_seqTextItem; |
| 284 | QGraphicsTextItem* m_nickTextItem; |
| 285 | QGraphicsRectItem* m_rimRectItem; |
| 286 | QGraphicsRectItem* m_innerRectItem; |
| 287 | QGraphicsRectItem* m_nickRectItem; |
| 288 | }; |
| 289 | |
| 290 | } // namespace chronos |
| 291 | |
| 292 | #endif // CHRONOS_DIGEST_TREE_SCENE_HPP |