Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 1 | #include <QtGui> |
| 2 | #include "chatdialog.h" |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame^] | 3 | #include <ctime> |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 4 | |
| 5 | ChatDialog::ChatDialog(QWidget *parent) |
| 6 | : QDialog(parent) |
| 7 | { |
| 8 | setupUi(this); |
| 9 | lineEdit->setFocusPolicy(Qt::StrongFocus); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame^] | 10 | |
| 11 | |
| 12 | connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
| 13 | } |
| 14 | |
| 15 | void |
| 16 | ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg) |
| 17 | { |
| 18 | if (msg.from().isEmpty || message.msgData().isEmpty()) |
| 19 | return; |
| 20 | |
| 21 | QTextCursor cursor(textEdit->textCursor()); |
| 22 | cursor.movePosition(QTextCursor::End); |
| 23 | QTextTableFormat tableFormat; |
| 24 | tableFormat.setBorder(0); |
| 25 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
| 26 | table->cellAt(0, 0).firstCursorPosition().insertText("<" + msg.from() + ">: "); |
| 27 | table->cellAt(0, 1).firstCursorPosition().insertText(msg.msgData()); |
| 28 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 29 | bar->setValue(bar->maximum()); |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | ChatDialog::updateTreeView() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | ChatDialog::returnPressed() |
| 39 | { |
| 40 | QString text = lineEdit->text(); |
| 41 | if (text.isEmpty()) |
| 42 | return; |
| 43 | |
| 44 | SyncDemo::ChatMessage msg; |
| 45 | formChatMessage(text, msg); |
| 46 | |
| 47 | // TODO: |
| 48 | // send message |
| 49 | appendMessage(msg); |
| 50 | updateTreeView(); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
| 56 | msg.set_from(m_nick); |
| 57 | msg.set_to(m_chatroom); |
| 58 | msg.set_msgData(text); |
| 59 | time_t seconds = time(NULL); |
| 60 | msg.set_timestamp(seconds); |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 61 | } |