blob: 9fbe15eaee4f8db1da07ca4679b27866636fb451 [file] [log] [blame]
Zhenkai Zhufd52ab72012-05-29 17:34:35 -07001#include <QtGui>
2#include "chatdialog.h"
Zhenkai Zhuc71da772012-05-30 21:25:23 -07003#include <ctime>
Zhenkai Zhu2c55b382012-05-31 13:27:24 -07004#include <iostream>
Zhenkai Zhufd52ab72012-05-29 17:34:35 -07005
6ChatDialog::ChatDialog(QWidget *parent)
7 : QDialog(parent)
8{
9 setupUi(this);
10 lineEdit->setFocusPolicy(Qt::StrongFocus);
Zhenkai Zhuc71da772012-05-30 21:25:23 -070011
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070012 // for test only
13 m_nick = "Tester";
14 m_chatroom = "Test";
Zhenkai Zhuc71da772012-05-30 21:25:23 -070015
16 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
17}
18
19void
20ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg)
21{
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070022
23 if (msg.type() != SyncDemo::ChatMessage::CHAT) {
Zhenkai Zhuc71da772012-05-30 21:25:23 -070024 return;
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070025 }
26
27 if (!msg.has_data()) {
28 return;
29 }
30
31 if (msg.from().empty() || msg.data().empty()) {
32 return;
33 }
Zhenkai Zhuc71da772012-05-30 21:25:23 -070034
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 Zhu2c55b382012-05-31 13:27:24 -070040 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 Zhuc71da772012-05-30 21:25:23 -070043 QScrollBar *bar = textEdit->verticalScrollBar();
44 bar->setValue(bar->maximum());
45}
46
47void
48ChatDialog::updateTreeView()
49{
50}
51
52void
53ChatDialog::returnPressed()
54{
55 QString text = lineEdit->text();
56 if (text.isEmpty())
57 return;
58
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070059 lineEdit->clear();
60
Zhenkai Zhuc71da772012-05-30 21:25:23 -070061 SyncDemo::ChatMessage msg;
62 formChatMessage(text, msg);
63
64 // TODO:
65 // send message
66 appendMessage(msg);
67 updateTreeView();
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070068
Zhenkai Zhuc71da772012-05-30 21:25:23 -070069}
70
71void
72ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070073 msg.set_from(m_nick.toStdString());
74 msg.set_to(m_chatroom.toStdString());
75 msg.set_data(text.toStdString());
Zhenkai Zhuc71da772012-05-30 21:25:23 -070076 time_t seconds = time(NULL);
77 msg.set_timestamp(seconds);
Zhenkai Zhufd52ab72012-05-29 17:34:35 -070078}