add type hello in chatbuf;

dialog text display work for local message
This commit is contained in:
Zhenkai Zhu committed 2012-05-31 13:27:24 -07:00
1 parent c71da77ac5
commit 2c55b38800
3 files changed
+30 -13

No files matched your search

+5 -5
View File
@@ -1,14 +1,14 @@
package SyncDemo;
message ChatMessage {
optional string msgData = 1;
required string to = 2;
required string from = 3;
optional int32 timestamp = 4;
required string to = 1;
required string from = 2;
enum ChatMessageType {
CHAT = 0;
HELLO = 1;
OTHER = 2;
}
required ChatMessageType type = 5;
required ChatMessageType type = 3 [default = CHAT];
optional string data = 4;
optional int32 timestamp = 5;
}
+24 -7
View File
@@ -1,6 +1,7 @@
#include <QtGui>
#include "chatdialog.h"
#include <ctime>
#include <iostream>
ChatDialog::ChatDialog(QWidget *parent)
: QDialog(parent)
@@ -8,6 +9,9 @@ ChatDialog::ChatDialog(QWidget *parent)
setupUi(this);
lineEdit->setFocusPolicy(Qt::StrongFocus);
// for test only
m_nick = "Tester";
m_chatroom = "Test";
connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
}
@@ -15,16 +19,27 @@ ChatDialog::ChatDialog(QWidget *parent)
void
ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg)
{
if (msg.from().isEmpty || message.msgData().isEmpty())
if (msg.type() != SyncDemo::ChatMessage::CHAT) {
return;
}
if (!msg.has_data()) {
return;
}
if (msg.from().empty() || msg.data().empty()) {
return;
}
QTextCursor cursor(textEdit->textCursor());
cursor.movePosition(QTextCursor::End);
QTextTableFormat tableFormat;
tableFormat.setBorder(0);
QTextTable *table = cursor.insertTable(1, 2, tableFormat);
table->cellAt(0, 0).firstCursorPosition().insertText("<" + msg.from() + ">: ");
table->cellAt(0, 1).firstCursorPosition().insertText(msg.msgData());
QString from = QString("<%1>: ").arg(msg.from().c_str());
table->cellAt(0, 0).firstCursorPosition().insertText(from);
table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str());
QScrollBar *bar = textEdit->verticalScrollBar();
bar->setValue(bar->maximum());
}
@@ -41,6 +56,8 @@ ChatDialog::returnPressed()
if (text.isEmpty())
return;
lineEdit->clear();
SyncDemo::ChatMessage msg;
formChatMessage(text, msg);
@@ -48,14 +65,14 @@ ChatDialog::returnPressed()
// send message
appendMessage(msg);
updateTreeView();
}
void
ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
msg.set_from(m_nick);
msg.set_to(m_chatroom);
msg.set_msgData(text);
msg.set_from(m_nick.toStdString());
msg.set_to(m_chatroom.toStdString());
msg.set_data(text.toStdString());
time_t seconds = time(NULL);
msg.set_timestamp(seconds);
}
+1 -1
View File
@@ -11,7 +11,7 @@ public:
ChatDialog(QWidget *parent = 0);
public slots:
void appendMessage(SyncDemo::ChatMessage &msg);
void appendMessage(const SyncDemo::ChatMessage &msg);
void updateTreeView();
private slots: