Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 1 | #include <QtGui> |
| 2 | #include "chatdialog.h" |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 3 | #include "settingdialog.h" |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 4 | #include <ctime> |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 5 | #include <iostream> |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 6 | #include <QTimer> |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 7 | #include <QMetaType> |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 8 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 9 | #define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/sync-demo" |
| 10 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 11 | void |
| 12 | ChatDialog::testDraw() |
| 13 | { |
| 14 | std::string prefix[5] = {"/ndn/1", "/ndn/2", "/ndn/3", "/ndn/4", "/ndn/5"}; |
| 15 | std::string nick[5] = {"tom", "jerry", "jason", "michael", "hurry"}; |
| 16 | std::vector<Sync::MissingDataInfo> v; |
| 17 | for (int i = 0; i < 5; i++) |
| 18 | { |
| 19 | Sync::MissingDataInfo mdi = {prefix[i], Sync::SeqNo(0), Sync::SeqNo(i * (2 << i) )}; |
| 20 | v.push_back(mdi); |
| 21 | } |
| 22 | |
| 23 | m_scene->processUpdate(v, "12341234@!#%!@"); |
| 24 | |
| 25 | for (int i = 0; i < 5; i++) |
| 26 | { |
| 27 | m_scene-> msgReceived(prefix[i].c_str(), nick[i].c_str()); |
| 28 | } |
| 29 | |
| 30 | fitView(); |
| 31 | } |
| 32 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 33 | ChatDialog::ChatDialog(QWidget *parent) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 34 | : QDialog(parent), m_sock(NULL) |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 35 | { |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 36 | // have to register this, otherwise |
| 37 | // the signal-slot system won't recognize this type |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 38 | qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>"); |
| 39 | qRegisterMetaType<size_t>("size_t"); |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 40 | setupUi(this); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 41 | m_session = time(NULL); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 42 | |
| 43 | readSettings(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 44 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 45 | updateLabels(); |
| 46 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 47 | lineEdit->setFocusPolicy(Qt::StrongFocus); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 48 | m_scene = new DigestTreeScene(this); |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 49 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 50 | treeViewer->setScene(m_scene); |
| 51 | m_scene->plot("Empty"); |
| 52 | QRectF rect = m_scene->itemsBoundingRect(); |
| 53 | m_scene->setSceneRect(rect); |
| 54 | |
| 55 | // create sync socket |
| 56 | if(!m_user.getChatroom().isEmpty()) { |
| 57 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 58 | syncPrefix += "/"; |
| 59 | syncPrefix += m_user.getChatroom().toStdString(); |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 60 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemove, this, _1)); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 61 | } |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 62 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 63 | connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 64 | connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed())); |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 65 | connect(this, SIGNAL(dataReceived(QString, const char *, size_t)), this, SLOT(processData(QString, const char *, size_t))); |
| 66 | connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>))); |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 67 | //testDraw(); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 70 | ChatDialog::~ChatDialog() |
| 71 | { |
| 72 | if (m_sock != NULL) |
| 73 | { |
| 74 | delete m_sock; |
| 75 | m_sock = NULL; |
| 76 | } |
| 77 | } |
| 78 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 79 | void |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 80 | ChatDialog::appendMessage(const SyncDemo::ChatMessage msg) |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 81 | { |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 82 | |
| 83 | if (msg.type() != SyncDemo::ChatMessage::CHAT) { |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 84 | return; |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if (!msg.has_data()) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | if (msg.from().empty() || msg.data().empty()) { |
| 92 | return; |
| 93 | } |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 94 | |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 95 | std::cout << "<<<< Received Message: " << msg.data() << std::endl; |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 96 | QTextCursor cursor(textEdit->textCursor()); |
| 97 | cursor.movePosition(QTextCursor::End); |
| 98 | QTextTableFormat tableFormat; |
| 99 | tableFormat.setBorder(0); |
| 100 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 101 | QString from = QString("<%1>: ").arg(msg.from().c_str()); |
| 102 | table->cellAt(0, 0).firstCursorPosition().insertText(from); |
| 103 | table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str()); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 104 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 105 | bar->setValue(bar->maximum()); |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 106 | std::cout << "<<<<, Message appended " << std::endl; |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 109 | void |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 110 | ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock) |
| 111 | { |
| 112 | emit treeUpdated(v); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 117 | { |
| 118 | if (v.empty()) |
| 119 | { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | // reflect the changes on digest tree |
| 124 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 125 | |
| 126 | int n = v.size(); |
| 127 | int totalMissingPackets = 0; |
| 128 | for (int i = 0; i < n; i++) |
| 129 | { |
| 130 | totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1; |
| 131 | } |
| 132 | |
| 133 | if (totalMissingPackets < 10) { |
| 134 | for (int i = 0; i < n; i++) |
| 135 | { |
| 136 | for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq) |
| 137 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 138 | m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | // too bad; too many missing packets |
| 145 | // we may just join a new chatroom |
| 146 | // or some network patition just healed |
| 147 | // we don't try to fetch any data in this case (for now) |
| 148 | } |
| 149 | |
| 150 | // adjust the view |
| 151 | fitView(); |
| 152 | |
| 153 | } |
| 154 | |
| 155 | void |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 156 | ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len) |
| 157 | { |
| 158 | emit dataReceived(name.c_str(), buf, len); |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | ChatDialog::processData(QString name, const char *buf, size_t len) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 163 | { |
| 164 | SyncDemo::ChatMessage msg; |
| 165 | if (!msg.ParseFromArray(buf, len)) |
| 166 | { |
| 167 | std::cerr << "Errrrr.. Can not parse msg at "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 168 | abort(); |
| 169 | } |
| 170 | |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 171 | // display msg received from network |
| 172 | // we have to do so; this function is called by ccnd thread |
| 173 | // so if we call appendMsg directly |
| 174 | // Qt crash as "QObject: Cannot create children for a parent that is in a different thread" |
| 175 | // the "cannonical" way to is use signal-slot |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 176 | appendMessage(msg); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 177 | |
| 178 | // update the tree view |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 179 | std::string prefix = name.toStdString().substr(0, name.toStdString().find_last_of('/')); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 180 | m_scene->msgReceived(prefix.c_str(), msg.from().c_str()); |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | ChatDialog::processRemove(std::string prefix) |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 189 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 190 | msg.set_from(m_user.getNick().toStdString()); |
| 191 | msg.set_to(m_user.getChatroom().toStdString()); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 192 | msg.set_data(text.toStdString()); |
| 193 | time_t seconds = time(NULL); |
| 194 | msg.set_timestamp(seconds); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 195 | msg.set_type(SyncDemo::ChatMessage::CHAT); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 198 | bool |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 199 | ChatDialog::readSettings() |
| 200 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 201 | #ifndef __DEBUG |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 202 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 203 | QString nick = s.value("nick", "").toString(); |
| 204 | QString chatroom = s.value("chatroom", "").toString(); |
| 205 | QString prefix = s.value("prefix", "").toString(); |
| 206 | if (nick == "" || chatroom == "" || prefix == "") { |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 207 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 208 | return false; |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 209 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 210 | else { |
| 211 | m_user.setNick(nick); |
| 212 | m_user.setChatroom(chatroom); |
| 213 | m_user.setPrefix(prefix); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 214 | return true; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 215 | } |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 216 | #else |
| 217 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
| 218 | return false; |
| 219 | #endif |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void |
| 223 | ChatDialog::writeSettings() |
| 224 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 225 | #ifndef __DEBUG |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 226 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 227 | s.setValue("nick", m_user.getNick()); |
| 228 | s.setValue("chatroom", m_user.getChatroom()); |
| 229 | s.setValue("prefix", m_user.getPrefix()); |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 230 | #endif |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void |
| 234 | ChatDialog::updateLabels() |
| 235 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 236 | QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 237 | infoLabel->setText(settingDisp); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 238 | QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 239 | prefixLabel->setText(prefixDisp); |
| 240 | } |
| 241 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 242 | void |
| 243 | ChatDialog::returnPressed() |
| 244 | { |
| 245 | QString text = lineEdit->text(); |
| 246 | if (text.isEmpty()) |
| 247 | return; |
| 248 | |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 249 | lineEdit->clear(); |
| 250 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 251 | SyncDemo::ChatMessage msg; |
| 252 | formChatMessage(text, msg); |
| 253 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 254 | appendMessage(msg); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 255 | |
| 256 | // send msg |
| 257 | size_t size = msg.ByteSize(); |
| 258 | char *buf = new char[size]; |
| 259 | msg.SerializeToArray(buf, size); |
| 260 | if (!msg.IsInitialized()) |
| 261 | { |
| 262 | std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 263 | abort(); |
| 264 | } |
| 265 | m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, 60); |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 266 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 270 | ChatDialog::buttonPressed() |
| 271 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 272 | SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 273 | connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString))); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 274 | dialog.exec(); |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 275 | } |
Zhenkai Zhu | e08afe0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 276 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 277 | void |
| 278 | ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix) |
| 279 | { |
| 280 | bool needWrite = false; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 281 | if (!nick.isEmpty() && nick != m_user.getNick()) { |
| 282 | m_user.setNick(nick); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 283 | needWrite = true; |
| 284 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 285 | if (!prefix.isEmpty() && prefix != m_user.getPrefix()) { |
| 286 | m_user.setPrefix(prefix); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 287 | needWrite = true; |
| 288 | // TODO: set the previous prefix as left? |
| 289 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 290 | if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) { |
| 291 | m_user.setChatroom(chatroom); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 292 | needWrite = true; |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 293 | |
| 294 | m_scene->clearAll(); |
| 295 | m_scene->plot("Empty"); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 296 | // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 297 | if (m_sock != NULL) |
| 298 | { |
| 299 | delete m_sock; |
| 300 | m_sock = NULL; |
| 301 | } |
| 302 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 303 | syncPrefix += "/"; |
| 304 | syncPrefix += m_user.getChatroom().toStdString(); |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 305 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemove, this, _1)); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 306 | |
| 307 | fitView(); |
| 308 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 309 | } |
| 310 | if (needWrite) { |
| 311 | writeSettings(); |
| 312 | updateLabels(); |
| 313 | } |
| 314 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 315 | |
| 316 | void |
| 317 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 318 | { |
| 319 | fitView(); |
| 320 | } |
| 321 | |
| 322 | void |
| 323 | ChatDialog::showEvent(QShowEvent *e) |
| 324 | { |
| 325 | fitView(); |
| 326 | } |
| 327 | |
| 328 | void |
| 329 | ChatDialog::fitView() |
| 330 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 331 | QRectF rect = m_scene->itemsBoundingRect(); |
| 332 | m_scene->setSceneRect(rect); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 333 | treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 334 | } |