Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 1 | #include <QtGui> |
| 2 | #include "chatdialog.h" |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame^] | 3 | #include "settingdialog.h" |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 4 | #include <ctime> |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 5 | #include <iostream> |
Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 6 | |
| 7 | ChatDialog::ChatDialog(QWidget *parent) |
| 8 | : QDialog(parent) |
| 9 | { |
| 10 | setupUi(this); |
| 11 | lineEdit->setFocusPolicy(Qt::StrongFocus); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 12 | |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 13 | // for test only |
| 14 | m_nick = "Tester"; |
| 15 | m_chatroom = "Test"; |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame^] | 16 | m_prefix = "/ndn/ucla.edu/cs/tester"; |
| 17 | |
| 18 | QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_nick).arg(m_chatroom); |
| 19 | infoLabel->setText(settingDisp); |
| 20 | QString prefixDisp = QString("<Prefix: %1>").arg(m_prefix); |
| 21 | prefixLabel->setText(prefixDisp); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 22 | |
Zhenkai Zhu | 9ec8f41 | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 23 | DigestTreeScene *scene = new DigestTreeScene(); |
| 24 | |
| 25 | treeViewer->setScene(scene); |
| 26 | scene->plot(); |
| 27 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 28 | connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame^] | 29 | connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed())); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void |
| 33 | ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg) |
| 34 | { |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 35 | |
| 36 | if (msg.type() != SyncDemo::ChatMessage::CHAT) { |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 37 | return; |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | if (!msg.has_data()) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | if (msg.from().empty() || msg.data().empty()) { |
| 45 | return; |
| 46 | } |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 47 | |
| 48 | QTextCursor cursor(textEdit->textCursor()); |
| 49 | cursor.movePosition(QTextCursor::End); |
| 50 | QTextTableFormat tableFormat; |
| 51 | tableFormat.setBorder(0); |
| 52 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 53 | QString from = QString("<%1>: ").arg(msg.from().c_str()); |
| 54 | table->cellAt(0, 0).firstCursorPosition().insertText(from); |
| 55 | table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str()); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 56 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 57 | bar->setValue(bar->maximum()); |
| 58 | } |
| 59 | |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame^] | 60 | void |
| 61 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
| 62 | msg.set_from(m_nick.toStdString()); |
| 63 | msg.set_to(m_chatroom.toStdString()); |
| 64 | msg.set_data(text.toStdString()); |
| 65 | time_t seconds = time(NULL); |
| 66 | msg.set_timestamp(seconds); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void |
| 70 | ChatDialog::returnPressed() |
| 71 | { |
| 72 | QString text = lineEdit->text(); |
| 73 | if (text.isEmpty()) |
| 74 | return; |
| 75 | |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 76 | lineEdit->clear(); |
| 77 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 78 | SyncDemo::ChatMessage msg; |
| 79 | formChatMessage(text, msg); |
| 80 | |
| 81 | // TODO: |
| 82 | // send message |
| 83 | appendMessage(msg); |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 84 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame^] | 88 | ChatDialog::buttonPressed() |
| 89 | { |
| 90 | SettingDialog dialog(this, m_nick, m_chatroom, m_prefix); |
| 91 | dialog.exec(); |
| 92 | setButton->setFocusPolicy(Qt::NoFocus); |
Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 93 | } |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 94 | |