blob: 34f568226ee82c3ccf5e26ceb2dad4238bffd696 [file] [log] [blame]
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -07001#include <QtGui>
2#include "chatdialog.h"
Zhenkai Zhu85845d22012-06-01 23:10:43 -07003#include "settingdialog.h"
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -07004#include <ctime>
Zhenkai Zhub6338822012-05-31 13:27:24 -07005#include <iostream>
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -07006#include <QTimer>
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -07007
8ChatDialog::ChatDialog(QWidget *parent)
9 : QDialog(parent)
10{
11 setupUi(this);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070012
13 readSettings();
14 updateLabels();
15
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070016 lineEdit->setFocusPolicy(Qt::StrongFocus);
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070017 DigestTreeScene *scene = new DigestTreeScene();
18
19 treeViewer->setScene(scene);
20 scene->plot();
21
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070022 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
Zhenkai Zhu85845d22012-06-01 23:10:43 -070023 connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070024}
25
26void
27ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg)
28{
Zhenkai Zhub6338822012-05-31 13:27:24 -070029
30 if (msg.type() != SyncDemo::ChatMessage::CHAT) {
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070031 return;
Zhenkai Zhub6338822012-05-31 13:27:24 -070032 }
33
34 if (!msg.has_data()) {
35 return;
36 }
37
38 if (msg.from().empty() || msg.data().empty()) {
39 return;
40 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070041
42 QTextCursor cursor(textEdit->textCursor());
43 cursor.movePosition(QTextCursor::End);
44 QTextTableFormat tableFormat;
45 tableFormat.setBorder(0);
46 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
Zhenkai Zhub6338822012-05-31 13:27:24 -070047 QString from = QString("<%1>: ").arg(msg.from().c_str());
48 table->cellAt(0, 0).firstCursorPosition().insertText(from);
49 table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str());
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070050 QScrollBar *bar = textEdit->verticalScrollBar();
51 bar->setValue(bar->maximum());
52}
53
Zhenkai Zhu85845d22012-06-01 23:10:43 -070054void
55ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
56 msg.set_from(m_nick.toStdString());
57 msg.set_to(m_chatroom.toStdString());
58 msg.set_data(text.toStdString());
59 time_t seconds = time(NULL);
60 msg.set_timestamp(seconds);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070061}
62
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070063void
64ChatDialog::readSettings()
65{
66 QSettings s(ORGANIZATION, APPLICATION);
67 m_nick = s.value("nick", "").toString();
68 m_chatroom = s.value("chatroom", "").toString();
69 m_prefix = s.value("prefix", "").toString();
70 if (m_nick == "" || m_chatroom == "" || m_prefix == "") {
71 QTimer::singleShot(500, this, SLOT(buttonPressed()));
72 }
73}
74
75void
76ChatDialog::writeSettings()
77{
78 QSettings s(ORGANIZATION, APPLICATION);
79 s.setValue("nick", m_nick);
80 s.setValue("chatroom", m_chatroom);
81 s.setValue("prefix", m_prefix);
82}
83
84void
85ChatDialog::updateLabels()
86{
87 QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_nick).arg(m_chatroom);
88 infoLabel->setText(settingDisp);
89 QString prefixDisp = QString("<Prefix: %1>").arg(m_prefix);
90 prefixLabel->setText(prefixDisp);
91}
92
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070093void
94ChatDialog::returnPressed()
95{
96 QString text = lineEdit->text();
97 if (text.isEmpty())
98 return;
99
Zhenkai Zhub6338822012-05-31 13:27:24 -0700100 lineEdit->clear();
101
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700102 SyncDemo::ChatMessage msg;
103 formChatMessage(text, msg);
104
105 // TODO:
106 // send message
107 appendMessage(msg);
Zhenkai Zhub6338822012-05-31 13:27:24 -0700108
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700109}
110
111void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700112ChatDialog::buttonPressed()
113{
114 SettingDialog dialog(this, m_nick, m_chatroom, m_prefix);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700115 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700116 dialog.exec();
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700117}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700118
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700119void
120ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix)
121{
122 bool needWrite = false;
123 if (!nick.isEmpty() && nick != m_nick) {
124 m_nick = nick;
125 needWrite = true;
126 }
127 if (!prefix.isEmpty() && prefix != m_prefix) {
128 m_prefix = prefix;
129 needWrite = true;
130 // TODO: set the previous prefix as left?
131 }
132 if (!chatroom.isEmpty() && chatroom != m_chatroom) {
133 m_chatroom = chatroom;
134 needWrite = true;
135 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
136 }
137 if (needWrite) {
138 writeSettings();
139 updateLabels();
140 }
141}