blob: 0163df90660115a404a19c6ad4078956c2ee67db [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
Zhenkai Zhu9ec8f412012-06-01 15:44:36 -070016 DigestTreeScene *scene = new DigestTreeScene();
17
18 treeViewer->setScene(scene);
19 scene->plot();
20
Zhenkai Zhuc71da772012-05-30 21:25:23 -070021 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
22}
23
24void
25ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg)
26{
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070027
28 if (msg.type() != SyncDemo::ChatMessage::CHAT) {
Zhenkai Zhuc71da772012-05-30 21:25:23 -070029 return;
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070030 }
31
32 if (!msg.has_data()) {
33 return;
34 }
35
36 if (msg.from().empty() || msg.data().empty()) {
37 return;
38 }
Zhenkai Zhuc71da772012-05-30 21:25:23 -070039
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 Zhu2c55b382012-05-31 13:27:24 -070045 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 Zhuc71da772012-05-30 21:25:23 -070048 QScrollBar *bar = textEdit->verticalScrollBar();
49 bar->setValue(bar->maximum());
50}
51
52void
53ChatDialog::updateTreeView()
54{
55}
56
57void
58ChatDialog::returnPressed()
59{
60 QString text = lineEdit->text();
61 if (text.isEmpty())
62 return;
63
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070064 lineEdit->clear();
65
Zhenkai Zhuc71da772012-05-30 21:25:23 -070066 SyncDemo::ChatMessage msg;
67 formChatMessage(text, msg);
68
69 // TODO:
70 // send message
71 appendMessage(msg);
72 updateTreeView();
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070073
Zhenkai Zhuc71da772012-05-30 21:25:23 -070074}
75
76void
77ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070078 msg.set_from(m_nick.toStdString());
79 msg.set_to(m_chatroom.toStdString());
80 msg.set_data(text.toStdString());
Zhenkai Zhuc71da772012-05-30 21:25:23 -070081 time_t seconds = time(NULL);
82 msg.set_timestamp(seconds);
Zhenkai Zhufd52ab72012-05-29 17:34:35 -070083}
Zhenkai Zhu80af0e02012-05-31 15:49:07 -070084