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