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