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 | |
| 16 | connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
| 17 | } |
| 18 | |
| 19 | void |
| 20 | ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg) |
| 21 | { |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame^] | 22 | |
| 23 | if (msg.type() != SyncDemo::ChatMessage::CHAT) { |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 24 | return; |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame^] | 25 | } |
| 26 | |
| 27 | if (!msg.has_data()) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | if (msg.from().empty() || msg.data().empty()) { |
| 32 | return; |
| 33 | } |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 34 | |
| 35 | QTextCursor cursor(textEdit->textCursor()); |
| 36 | cursor.movePosition(QTextCursor::End); |
| 37 | QTextTableFormat tableFormat; |
| 38 | tableFormat.setBorder(0); |
| 39 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame^] | 40 | QString from = QString("<%1>: ").arg(msg.from().c_str()); |
| 41 | table->cellAt(0, 0).firstCursorPosition().insertText(from); |
| 42 | table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str()); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 43 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 44 | bar->setValue(bar->maximum()); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | ChatDialog::updateTreeView() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | ChatDialog::returnPressed() |
| 54 | { |
| 55 | QString text = lineEdit->text(); |
| 56 | if (text.isEmpty()) |
| 57 | return; |
| 58 | |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame^] | 59 | lineEdit->clear(); |
| 60 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 61 | SyncDemo::ChatMessage msg; |
| 62 | formChatMessage(text, msg); |
| 63 | |
| 64 | // TODO: |
| 65 | // send message |
| 66 | appendMessage(msg); |
| 67 | updateTreeView(); |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame^] | 68 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
| 72 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame^] | 73 | msg.set_from(m_nick.toStdString()); |
| 74 | msg.set_to(m_chatroom.toStdString()); |
| 75 | msg.set_data(text.toStdString()); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 76 | time_t seconds = time(NULL); |
| 77 | msg.set_timestamp(seconds); |
Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 78 | } |