Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 1 | #include <QtGui> |
| 2 | #include "chatdialog.h" |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 3 | #include "settingdialog.h" |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 4 | #include <ctime> |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 5 | #include <iostream> |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame^] | 6 | #include <QTimer> |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 7 | |
| 8 | ChatDialog::ChatDialog(QWidget *parent) |
| 9 | : QDialog(parent) |
| 10 | { |
| 11 | setupUi(this); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame^] | 12 | |
| 13 | readSettings(); |
| 14 | updateLabels(); |
| 15 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 16 | lineEdit->setFocusPolicy(Qt::StrongFocus); |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 17 | DigestTreeScene *scene = new DigestTreeScene(); |
| 18 | |
| 19 | treeViewer->setScene(scene); |
| 20 | scene->plot(); |
| 21 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 22 | connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 23 | connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed())); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void |
| 27 | ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg) |
| 28 | { |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 29 | |
| 30 | if (msg.type() != SyncDemo::ChatMessage::CHAT) { |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 31 | return; |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | if (!msg.has_data()) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if (msg.from().empty() || msg.data().empty()) { |
| 39 | return; |
| 40 | } |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 41 | |
| 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 Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 47 | 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 Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 50 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 51 | bar->setValue(bar->maximum()); |
| 52 | } |
| 53 | |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 54 | void |
| 55 | ChatDialog::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 Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame^] | 63 | void |
| 64 | ChatDialog::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 | |
| 75 | void |
| 76 | ChatDialog::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 | |
| 84 | void |
| 85 | ChatDialog::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 Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 93 | void |
| 94 | ChatDialog::returnPressed() |
| 95 | { |
| 96 | QString text = lineEdit->text(); |
| 97 | if (text.isEmpty()) |
| 98 | return; |
| 99 | |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 100 | lineEdit->clear(); |
| 101 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 102 | SyncDemo::ChatMessage msg; |
| 103 | formChatMessage(text, msg); |
| 104 | |
| 105 | // TODO: |
| 106 | // send message |
| 107 | appendMessage(msg); |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 108 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 112 | ChatDialog::buttonPressed() |
| 113 | { |
| 114 | SettingDialog dialog(this, m_nick, m_chatroom, m_prefix); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame^] | 115 | connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString))); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 116 | dialog.exec(); |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 117 | } |
Zhenkai Zhu | e08afe0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 118 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame^] | 119 | void |
| 120 | ChatDialog::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 | } |