blob: 6fd93dfd21eea60d479dcaba05c84124342456ce [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 Zhu3472e3f2012-06-02 00:44:42 -07006#include <QTimer>
Zhenkai Zhufd52ab72012-05-29 17:34:35 -07007
8ChatDialog::ChatDialog(QWidget *parent)
9 : QDialog(parent)
10{
11 setupUi(this);
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070012
13 readSettings();
14 updateLabels();
15
Zhenkai Zhufd52ab72012-05-29 17:34:35 -070016 lineEdit->setFocusPolicy(Qt::StrongFocus);
Zhenkai Zhu9ec8f412012-06-01 15:44:36 -070017 DigestTreeScene *scene = new DigestTreeScene();
18
19 treeViewer->setScene(scene);
Zhenkai Zhue5660932012-06-04 15:25:20 -070020 QRectF rect = scene->itemsBoundingRect();
21 scene->setSceneRect(rect);
Zhenkai Zhu56a88592012-06-04 09:42:53 -070022 //scene->plot();
Zhenkai Zhu9ec8f412012-06-01 15:44:36 -070023
Zhenkai Zhuc71da772012-05-30 21:25:23 -070024 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -070025 connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
Zhenkai Zhuc71da772012-05-30 21:25:23 -070026}
27
28void
29ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg)
30{
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070031
32 if (msg.type() != SyncDemo::ChatMessage::CHAT) {
Zhenkai Zhuc71da772012-05-30 21:25:23 -070033 return;
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070034 }
35
36 if (!msg.has_data()) {
37 return;
38 }
39
40 if (msg.from().empty() || msg.data().empty()) {
41 return;
42 }
Zhenkai Zhuc71da772012-05-30 21:25:23 -070043
44 QTextCursor cursor(textEdit->textCursor());
45 cursor.movePosition(QTextCursor::End);
46 QTextTableFormat tableFormat;
47 tableFormat.setBorder(0);
48 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070049 QString from = QString("<%1>: ").arg(msg.from().c_str());
50 table->cellAt(0, 0).firstCursorPosition().insertText(from);
51 table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str());
Zhenkai Zhuc71da772012-05-30 21:25:23 -070052 QScrollBar *bar = textEdit->verticalScrollBar();
53 bar->setValue(bar->maximum());
54}
55
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -070056void
57ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu56a88592012-06-04 09:42:53 -070058 msg.set_from(m_user.getNick().toStdString());
59 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -070060 msg.set_data(text.toStdString());
61 time_t seconds = time(NULL);
62 msg.set_timestamp(seconds);
Zhenkai Zhuc71da772012-05-30 21:25:23 -070063}
64
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070065void
66ChatDialog::readSettings()
67{
68 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu56a88592012-06-04 09:42:53 -070069 QString nick = s.value("nick", "").toString();
70 QString chatroom = s.value("chatroom", "").toString();
71 QString prefix = s.value("prefix", "").toString();
72 if (nick == "" || chatroom == "" || prefix == "") {
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070073 QTimer::singleShot(500, this, SLOT(buttonPressed()));
74 }
Zhenkai Zhu56a88592012-06-04 09:42:53 -070075 else {
76 m_user.setNick(nick);
77 m_user.setChatroom(chatroom);
78 m_user.setPrefix(prefix);
79 }
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070080}
81
82void
83ChatDialog::writeSettings()
84{
85 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu56a88592012-06-04 09:42:53 -070086 s.setValue("nick", m_user.getNick());
87 s.setValue("chatroom", m_user.getChatroom());
88 s.setValue("prefix", m_user.getPrefix());
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070089}
90
91void
92ChatDialog::updateLabels()
93{
Zhenkai Zhu56a88592012-06-04 09:42:53 -070094 QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom());
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070095 infoLabel->setText(settingDisp);
Zhenkai Zhu56a88592012-06-04 09:42:53 -070096 QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070097 prefixLabel->setText(prefixDisp);
98}
99
Zhenkai Zhuc71da772012-05-30 21:25:23 -0700100void
101ChatDialog::returnPressed()
102{
103 QString text = lineEdit->text();
104 if (text.isEmpty())
105 return;
106
Zhenkai Zhu2c55b382012-05-31 13:27:24 -0700107 lineEdit->clear();
108
Zhenkai Zhuc71da772012-05-30 21:25:23 -0700109 SyncDemo::ChatMessage msg;
110 formChatMessage(text, msg);
111
112 // TODO:
113 // send message
114 appendMessage(msg);
Zhenkai Zhu2c55b382012-05-31 13:27:24 -0700115
Zhenkai Zhuc71da772012-05-30 21:25:23 -0700116}
117
118void
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -0700119ChatDialog::buttonPressed()
120{
Zhenkai Zhu56a88592012-06-04 09:42:53 -0700121 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix());
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -0700122 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -0700123 dialog.exec();
Zhenkai Zhufd52ab72012-05-29 17:34:35 -0700124}
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700125
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -0700126void
127ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix)
128{
129 bool needWrite = false;
Zhenkai Zhu56a88592012-06-04 09:42:53 -0700130 if (!nick.isEmpty() && nick != m_user.getNick()) {
131 m_user.setNick(nick);
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -0700132 needWrite = true;
133 }
Zhenkai Zhu56a88592012-06-04 09:42:53 -0700134 if (!prefix.isEmpty() && prefix != m_user.getPrefix()) {
135 m_user.setPrefix(prefix);
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -0700136 needWrite = true;
137 // TODO: set the previous prefix as left?
138 }
Zhenkai Zhu56a88592012-06-04 09:42:53 -0700139 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
140 m_user.setChatroom(chatroom);
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -0700141 needWrite = true;
142 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
143 }
144 if (needWrite) {
145 writeSettings();
146 updateLabels();
147 }
148}
Zhenkai Zhue5660932012-06-04 15:25:20 -0700149
150void
151ChatDialog::resizeEvent(QResizeEvent *e)
152{
153 fitView();
154}
155
156void
157ChatDialog::showEvent(QShowEvent *e)
158{
159 fitView();
160}
161
162void
163ChatDialog::fitView()
164{
165 treeViewer->fitInView(treeViewer->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
166}