message format change;
add functions fo chatdialog
diff --git a/chatbuf.proto b/chatbuf.proto
index ca1312c..234c931 100644
--- a/chatbuf.proto
+++ b/chatbuf.proto
@@ -1,13 +1,14 @@
package SyncDemo;
message ChatMessage {
- required string msgData = 1;
+ optional string msgData = 1;
required string to = 2;
required string from = 3;
- required int32 timestamp = 4;
+ optional int32 timestamp = 4;
enum ChatMessageType {
CHAT = 0;
- OTHER = 1;
+ HELLO = 1;
+ OTHER = 2;
}
- optional ChatMessageType type = 5;
+ required ChatMessageType type = 5;
}
diff --git a/chatdialog.cpp b/chatdialog.cpp
index c7d8725..3bc518d 100644
--- a/chatdialog.cpp
+++ b/chatdialog.cpp
@@ -1,9 +1,61 @@
#include <QtGui>
#include "chatdialog.h"
+#include <ctime>
ChatDialog::ChatDialog(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
lineEdit->setFocusPolicy(Qt::StrongFocus);
+
+
+ connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
+}
+
+void
+ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg)
+{
+ if (msg.from().isEmpty || message.msgData().isEmpty())
+ 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());
+ QScrollBar *bar = textEdit->verticalScrollBar();
+ bar->setValue(bar->maximum());
+}
+
+void
+ChatDialog::updateTreeView()
+{
+}
+
+void
+ChatDialog::returnPressed()
+{
+ QString text = lineEdit->text();
+ if (text.isEmpty())
+ return;
+
+ SyncDemo::ChatMessage msg;
+ formChatMessage(text, msg);
+
+ // TODO:
+ // 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);
+ time_t seconds = time(NULL);
+ msg.set_timestamp(seconds);
}
diff --git a/chatdialog.h b/chatdialog.h
index 401df9d..0f65426 100644
--- a/chatdialog.h
+++ b/chatdialog.h
@@ -1,6 +1,7 @@
#ifndef CHATDIALOG_H
#define CHATDIALOG_H
#include "ui_chatdialog.h"
+#include "chatbuf.pb.h"
class ChatDialog : public QDialog, private Ui::ChatDialog
{
@@ -8,5 +9,17 @@
public:
ChatDialog(QWidget *parent = 0);
+
+public slots:
+ void appendMessage(SyncDemo::ChatMessage &msg);
+ void updateTreeView();
+
+private slots:
+ void returnPressed();
+ void formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
+
+private:
+ QString m_nick;
+ QString m_chatroom;
};
#endif