blob: ca1b3ae2bd1ad1c0b97ab7f6a5c7a46d0d1854a8 [file] [log] [blame]
Zhenkai Zhufd52ab72012-05-29 17:34:35 -07001#include <QtGui>
2#include "chatdialog.h"
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -07003#include "settingdialog.h"
Zhenkai Zhuc71da772012-05-30 21:25:23 -07004#include <ctime>
Zhenkai Zhu2c55b382012-05-31 13:27:24 -07005#include <iostream>
Zhenkai Zhufd52ab72012-05-29 17:34:35 -07006
7ChatDialog::ChatDialog(QWidget *parent)
8 : QDialog(parent)
9{
10 setupUi(this);
11 lineEdit->setFocusPolicy(Qt::StrongFocus);
Zhenkai Zhuc71da772012-05-30 21:25:23 -070012
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070013 // for test only
14 m_nick = "Tester";
15 m_chatroom = "Test";
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -070016 m_prefix = "/ndn/ucla.edu/cs/tester";
17
18 QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_nick).arg(m_chatroom);
19 infoLabel->setText(settingDisp);
20 QString prefixDisp = QString("<Prefix: %1>").arg(m_prefix);
21 prefixLabel->setText(prefixDisp);
Zhenkai Zhuc71da772012-05-30 21:25:23 -070022
Zhenkai Zhu9ec8f412012-06-01 15:44:36 -070023 DigestTreeScene *scene = new DigestTreeScene();
24
25 treeViewer->setScene(scene);
26 scene->plot();
27
Zhenkai Zhuc71da772012-05-30 21:25:23 -070028 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -070029 connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
Zhenkai Zhuc71da772012-05-30 21:25:23 -070030}
31
32void
33ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg)
34{
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070035
36 if (msg.type() != SyncDemo::ChatMessage::CHAT) {
Zhenkai Zhuc71da772012-05-30 21:25:23 -070037 return;
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070038 }
39
40 if (!msg.has_data()) {
41 return;
42 }
43
44 if (msg.from().empty() || msg.data().empty()) {
45 return;
46 }
Zhenkai Zhuc71da772012-05-30 21:25:23 -070047
48 QTextCursor cursor(textEdit->textCursor());
49 cursor.movePosition(QTextCursor::End);
50 QTextTableFormat tableFormat;
51 tableFormat.setBorder(0);
52 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070053 QString from = QString("<%1>: ").arg(msg.from().c_str());
54 table->cellAt(0, 0).firstCursorPosition().insertText(from);
55 table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str());
Zhenkai Zhuc71da772012-05-30 21:25:23 -070056 QScrollBar *bar = textEdit->verticalScrollBar();
57 bar->setValue(bar->maximum());
58}
59
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -070060void
61ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
62 msg.set_from(m_nick.toStdString());
63 msg.set_to(m_chatroom.toStdString());
64 msg.set_data(text.toStdString());
65 time_t seconds = time(NULL);
66 msg.set_timestamp(seconds);
Zhenkai Zhuc71da772012-05-30 21:25:23 -070067}
68
69void
70ChatDialog::returnPressed()
71{
72 QString text = lineEdit->text();
73 if (text.isEmpty())
74 return;
75
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070076 lineEdit->clear();
77
Zhenkai Zhuc71da772012-05-30 21:25:23 -070078 SyncDemo::ChatMessage msg;
79 formChatMessage(text, msg);
80
81 // TODO:
82 // send message
83 appendMessage(msg);
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070084
Zhenkai Zhuc71da772012-05-30 21:25:23 -070085}
86
87void
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -070088ChatDialog::buttonPressed()
89{
90 SettingDialog dialog(this, m_nick, m_chatroom, m_prefix);
91 dialog.exec();
92 setButton->setFocusPolicy(Qt::NoFocus);
Zhenkai Zhufd52ab72012-05-29 17:34:35 -070093}
Zhenkai Zhu80af0e02012-05-31 15:49:07 -070094