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