Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 1 | #include <QtGui> |
| 2 | #include "chatdialog.h" |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 3 | #include "settingdialog.h" |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 4 | #include <ctime> |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 5 | #include <iostream> |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 6 | #include <QTimer> |
Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 7 | |
| 8 | ChatDialog::ChatDialog(QWidget *parent) |
| 9 | : QDialog(parent) |
| 10 | { |
| 11 | setupUi(this); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 12 | |
| 13 | readSettings(); |
| 14 | updateLabels(); |
| 15 | |
Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 16 | lineEdit->setFocusPolicy(Qt::StrongFocus); |
Zhenkai Zhu | 9ec8f41 | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 17 | DigestTreeScene *scene = new DigestTreeScene(); |
| 18 | |
| 19 | treeViewer->setScene(scene); |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 20 | QRectF rect = scene->itemsBoundingRect(); |
| 21 | scene->setSceneRect(rect); |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 22 | //scene->plot(); |
Zhenkai Zhu | 9ec8f41 | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 23 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 24 | connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 25 | connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed())); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void |
| 29 | ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg) |
| 30 | { |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 31 | |
| 32 | if (msg.type() != SyncDemo::ChatMessage::CHAT) { |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 33 | return; |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | if (!msg.has_data()) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | if (msg.from().empty() || msg.data().empty()) { |
| 41 | return; |
| 42 | } |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 43 | |
| 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 Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 49 | 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 Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 52 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 53 | bar->setValue(bar->maximum()); |
| 54 | } |
| 55 | |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 56 | void |
| 57 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 58 | msg.set_from(m_user.getNick().toStdString()); |
| 59 | msg.set_to(m_user.getChatroom().toStdString()); |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 60 | msg.set_data(text.toStdString()); |
| 61 | time_t seconds = time(NULL); |
| 62 | msg.set_timestamp(seconds); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 65 | void |
| 66 | ChatDialog::readSettings() |
| 67 | { |
| 68 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 69 | 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 Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 73 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
| 74 | } |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 75 | else { |
| 76 | m_user.setNick(nick); |
| 77 | m_user.setChatroom(chatroom); |
| 78 | m_user.setPrefix(prefix); |
| 79 | } |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
| 83 | ChatDialog::writeSettings() |
| 84 | { |
| 85 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 86 | s.setValue("nick", m_user.getNick()); |
| 87 | s.setValue("chatroom", m_user.getChatroom()); |
| 88 | s.setValue("prefix", m_user.getPrefix()); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void |
| 92 | ChatDialog::updateLabels() |
| 93 | { |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 94 | QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom()); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 95 | infoLabel->setText(settingDisp); |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 96 | QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix()); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 97 | prefixLabel->setText(prefixDisp); |
| 98 | } |
| 99 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 100 | void |
| 101 | ChatDialog::returnPressed() |
| 102 | { |
| 103 | QString text = lineEdit->text(); |
| 104 | if (text.isEmpty()) |
| 105 | return; |
| 106 | |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 107 | lineEdit->clear(); |
| 108 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 109 | SyncDemo::ChatMessage msg; |
| 110 | formChatMessage(text, msg); |
| 111 | |
| 112 | // TODO: |
| 113 | // send message |
| 114 | appendMessage(msg); |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 115 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 119 | ChatDialog::buttonPressed() |
| 120 | { |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 121 | SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix()); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 122 | connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString))); |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 123 | dialog.exec(); |
Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 124 | } |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 125 | |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 126 | void |
| 127 | ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix) |
| 128 | { |
| 129 | bool needWrite = false; |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 130 | if (!nick.isEmpty() && nick != m_user.getNick()) { |
| 131 | m_user.setNick(nick); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 132 | needWrite = true; |
| 133 | } |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 134 | if (!prefix.isEmpty() && prefix != m_user.getPrefix()) { |
| 135 | m_user.setPrefix(prefix); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 136 | needWrite = true; |
| 137 | // TODO: set the previous prefix as left? |
| 138 | } |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 139 | if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) { |
| 140 | m_user.setChatroom(chatroom); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 141 | 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 Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame^] | 149 | |
| 150 | void |
| 151 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 152 | { |
| 153 | fitView(); |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | ChatDialog::showEvent(QShowEvent *e) |
| 158 | { |
| 159 | fitView(); |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | ChatDialog::fitView() |
| 164 | { |
| 165 | treeViewer->fitInView(treeViewer->scene()->itemsBoundingRect(), Qt::KeepAspectRatio); |
| 166 | } |