blob: 3bc518d263e3976b7b395e21ace3d833e946dc80 [file] [log] [blame]
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -07001#include <QtGui>
2#include "chatdialog.h"
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -07003#include <ctime>
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -07004
5ChatDialog::ChatDialog(QWidget *parent)
6 : QDialog(parent)
7{
8 setupUi(this);
9 lineEdit->setFocusPolicy(Qt::StrongFocus);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070010
11
12 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
13}
14
15void
16ChatDialog::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
32void
33ChatDialog::updateTreeView()
34{
35}
36
37void
38ChatDialog::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
54void
55ChatDialog::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 Zhu6d589aa2012-05-29 17:34:35 -070061}