From 977649f6869021604458af8dd847678453463b8a Mon Sep 17 00:00:00 2001 From: apprb Date: Mon, 10 Nov 2014 01:07:15 +0900 Subject: [PATCH] ChatAction refactoring --- src/historykeeper.cpp | 7 ++- src/widget/chatareawidget.cpp | 6 +- src/widget/form/chatform.cpp | 1 + src/widget/form/genericchatform.cpp | 2 + src/widget/tool/chatactions/actionaction.h | 3 +- src/widget/tool/chatactions/alertaction.cpp | 20 ------ src/widget/tool/chatactions/alertaction.h | 4 +- src/widget/tool/chatactions/chataction.cpp | 61 +++++++++++++++++++ src/widget/tool/chatactions/chataction.h | 22 ++++++- .../tool/chatactions/filetransferaction.cpp | 9 +-- .../tool/chatactions/filetransferaction.h | 8 +-- src/widget/tool/chatactions/messageaction.cpp | 16 +++++ src/widget/tool/chatactions/messageaction.h | 8 ++- .../tool/chatactions/systemmessageaction.h | 2 +- 14 files changed, 127 insertions(+), 42 deletions(-) diff --git a/src/historykeeper.cpp b/src/historykeeper.cpp index 64e674974..d617e28f8 100644 --- a/src/historykeeper.cpp +++ b/src/historykeeper.cpp @@ -124,10 +124,11 @@ int HistoryKeeper::addChatEntry(const QString& chat, const QString& message, con { int chat_id = getChatID(chat, ctSingle).first; int sender_id = getAliasID(sender); + bool status = sender != Core::getInstance()->getSelfId().publicKey; - db->exec(QString("INSERT INTO history (timestamp, chat_id, sender, message)") + - QString("VALUES (%1, %2, %3, '%4');") - .arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(wrapMessage(message))); + db->exec(QString("INSERT INTO history (timestamp, chat_id, sender, sent_ok, message)") + + QString("VALUES (%1, %2, %3, %4, '%5');") + .arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(status).arg(wrapMessage(message))); messageID++; return messageID; diff --git a/src/widget/chatareawidget.cpp b/src/widget/chatareawidget.cpp index 142d0fb4b..393a01c0e 100644 --- a/src/widget/chatareawidget.cpp +++ b/src/widget/chatareawidget.cpp @@ -102,6 +102,10 @@ void ChatAreaWidget::insertMessage(ChatActionPtr msgAction, QTextCursor::MoveOpe checkSlider(); QTextTable *chatTextTable = getMsgTable(pos); + msgAction->assignPlace(chatTextTable, this); + msgAction->dispaly(); + + /* QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition(); cur.clearSelection(); cur.setKeepPositionOnInsert(true); @@ -110,8 +114,8 @@ void ChatAreaWidget::insertMessage(ChatActionPtr msgAction, QTextCursor::MoveOpe chatTextTable->cellAt(0, 2).firstCursorPosition().insertHtml(msgAction->getMessage()); chatTextTable->cellAt(0, 4).firstCursorPosition().setBlockFormat(dateFormat); chatTextTable->cellAt(0, 4).firstCursorPosition().insertHtml(msgAction->getDate()); - msgAction->setup(cur, this); + */ if (msgAction->isInteractive()) messages.append(msgAction); diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 0f224a2e3..1015bd6d4 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -121,6 +121,7 @@ void ChatForm::onSendTriggered() qDebug() << "db id:" << id; addSelfMessage(msg, isAction, timestamp); + int rec; if (isAction) rec = Core::getInstance()->sendAction(f->getFriendID(), msg); diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index 187f61367..e6228245c 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -149,6 +149,8 @@ GenericChatForm::GenericChatForm(QWidget *parent) : chatWidget->document()->setDefaultStyleSheet(Style::getStylesheet(":ui/chatArea/innerStyle.css")); chatWidget->setStyleSheet(Style::getStylesheet(":/ui/chatArea/chatArea.css")); headWidget->setStyleSheet(Style::getStylesheet(":/ui/chatArea/chatHead.css")); + + ChatAction::setupFormat(); } bool GenericChatForm::isEmpty() diff --git a/src/widget/tool/chatactions/actionaction.h b/src/widget/tool/chatactions/actionaction.h index 484af272b..6f5318aac 100644 --- a/src/widget/tool/chatactions/actionaction.h +++ b/src/widget/tool/chatactions/actionaction.h @@ -24,9 +24,10 @@ class ActionAction : public MessageAction public: ActionAction(const QString &author, QString message, const QString& date, const bool&); virtual ~ActionAction(){;} + +protected: virtual QString getMessage(); virtual QString getName(); - virtual void setup(QTextCursor, QTextEdit*) override {;} private: QString message; diff --git a/src/widget/tool/chatactions/alertaction.cpp b/src/widget/tool/chatactions/alertaction.cpp index b62c1896e..23179340d 100644 --- a/src/widget/tool/chatactions/alertaction.cpp +++ b/src/widget/tool/chatactions/alertaction.cpp @@ -21,26 +21,6 @@ AlertAction::AlertAction(const QString &author, const QString &message, const QS { } -void AlertAction::setup(QTextCursor cursor, QTextEdit *) -{ - // When this function is called, we're supposed to only update ourselve when needed - // Nobody should ask us to do anything with our content, we're on our own - // Except we never udpate on our own, so we can safely free our resources - - (void) cursor; - message.clear(); - message.squeeze(); - name.clear(); - name.squeeze(); - date.clear(); - date.squeeze(); -} -/* -QString AlertAction::getName() -{ - return QString("
%2
").arg("alert_name").arg(toHtmlChars(name)); -} -*/ QString AlertAction::getMessage() { return MessageAction::getMessage("alert"); diff --git a/src/widget/tool/chatactions/alertaction.h b/src/widget/tool/chatactions/alertaction.h index 64e00e6bf..d37f355bd 100644 --- a/src/widget/tool/chatactions/alertaction.h +++ b/src/widget/tool/chatactions/alertaction.h @@ -24,9 +24,9 @@ class AlertAction : public MessageAction public: AlertAction(const QString &author, const QString &message, const QString& date); virtual ~AlertAction(){;} + +protected: virtual QString getMessage(); - //virtual QString getName(); only do the message for now; preferably would do the whole row - virtual void setup(QTextCursor cursor, QTextEdit*) override; private: QString message; diff --git a/src/widget/tool/chatactions/chataction.cpp b/src/widget/tool/chatactions/chataction.cpp index 6978711a6..a2aa3e140 100644 --- a/src/widget/tool/chatactions/chataction.cpp +++ b/src/widget/tool/chatactions/chataction.cpp @@ -17,6 +17,11 @@ #include "chataction.h" #include #include +#include +#include +#include + +QTextBlockFormat ChatAction::nameFormat, ChatAction::dateFormat; QString ChatAction::toHtmlChars(const QString &str) { @@ -53,3 +58,59 @@ QString ChatAction::getDate() else return QString("
" + toHtmlChars(date) + "
"); } + +void ChatAction::assignPlace(QTextTable *position, QTextEdit *te) +{ + textTable = position; + cur = position->cellAt(0, 2).firstCursorPosition(); + cur.clearSelection(); + cur.setKeepPositionOnInsert(true); + textEdit = te; +} + +void ChatAction::dispaly() +{ + textTable->cellAt(0, 0).firstCursorPosition().setBlockFormat(nameFormat); + textTable->cellAt(0, 0).firstCursorPosition().insertHtml(getName()); + textTable->cellAt(0, 2).firstCursorPosition().insertHtml(getMessage()); + textTable->cellAt(0, 4).firstCursorPosition().setBlockFormat(dateFormat); + textTable->cellAt(0, 4).firstCursorPosition().insertHtml(getDate()); + + cur.setKeepPositionOnInsert(true); + int end=cur.selectionEnd(); + cur.setPosition(cur.position()); + cur.setPosition(end, QTextCursor::KeepAnchor); + + featureUpdate(); +} + +void ChatAction::setupFormat() +{ + nameFormat.setAlignment(Qt::AlignRight); + nameFormat.setNonBreakableLines(true); + dateFormat.setAlignment(Qt::AlignLeft); + dateFormat.setNonBreakableLines(true); +} + +void ChatAction::updateContent() +{ + if (cur.isNull() || !textEdit) + return; + + int vSliderVal = textEdit->verticalScrollBar()->value(); + + // update content + int pos = cur.selectionStart(); + cur.removeSelectedText(); + cur.setKeepPositionOnInsert(false); + cur.insertHtml(getMessage()); + cur.setKeepPositionOnInsert(true); + int end = cur.position(); + cur.setPosition(pos); + cur.setPosition(end, QTextCursor::KeepAnchor); + + // restore old slider value + textEdit->verticalScrollBar()->setValue(vSliderVal); + + featureUpdate(); +} diff --git a/src/widget/tool/chatactions/chataction.h b/src/widget/tool/chatactions/chataction.h index d7da9fa92..b007ecb60 100644 --- a/src/widget/tool/chatactions/chataction.h +++ b/src/widget/tool/chatactions/chataction.h @@ -23,26 +23,42 @@ class FileTransferInstance; class QTextEdit; +class QTextTable; class ChatAction : public QObject { + Q_OBJECT public: ChatAction(const bool &me, const QString &author, const QString &date) : isMe(me), name(author), date(date) {;} virtual ~ChatAction(){;} - virtual void setup(QTextCursor cursor, QTextEdit* textEdit) = 0; ///< Call once, and then you MUST let the object update itself + void assignPlace(QTextTable *position, QTextEdit* te); + virtual void dispaly(); + virtual bool isInteractive(){return false;} + virtual void featureUpdate() {;} + + static void setupFormat(); + +public slots: + void updateContent(); + +protected: virtual QString getName(); virtual QString getMessage() = 0; virtual QString getDate(); - virtual bool isInteractive(){return false;} -protected: QString toHtmlChars(const QString &str); QString QImage2base64(const QImage &img); protected: bool isMe; QString name, date; + + QTextTable *textTable; + QTextEdit *textEdit; + QTextCursor cur; + + static QTextBlockFormat nameFormat, dateFormat; }; typedef QSharedPointer ChatActionPtr; diff --git a/src/widget/tool/chatactions/filetransferaction.cpp b/src/widget/tool/chatactions/filetransferaction.cpp index 01910813b..103e72f36 100644 --- a/src/widget/tool/chatactions/filetransferaction.cpp +++ b/src/widget/tool/chatactions/filetransferaction.cpp @@ -22,11 +22,10 @@ FileTransferAction::FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me) : ChatAction(me, author, date) - , edit(nullptr) { w = widget; - connect(w, &FileTransferInstance::stateUpdated, this, &FileTransferAction::updateHtml); + connect(w, &FileTransferInstance::stateUpdated, this, &FileTransferAction::updateContent); } FileTransferAction::~FileTransferAction() @@ -43,6 +42,7 @@ QString FileTransferAction::getMessage() return widgetHtml; } +/* void FileTransferAction::setup(QTextCursor cursor, QTextEdit *textEdit) { cur = cursor; @@ -53,7 +53,8 @@ void FileTransferAction::setup(QTextCursor cursor, QTextEdit *textEdit) edit = textEdit; } - +*/ +/* void FileTransferAction::updateHtml() { if (cur.isNull() || !edit) @@ -75,7 +76,7 @@ void FileTransferAction::updateHtml() // restore old slider value edit->verticalScrollBar()->setValue(vSliderVal); } - +*/ bool FileTransferAction::isInteractive() { if (w->getState() == FileTransferInstance::TransfState::tsCanceled diff --git a/src/widget/tool/chatactions/filetransferaction.h b/src/widget/tool/chatactions/filetransferaction.h index 816975ee2..696d4a5c0 100644 --- a/src/widget/tool/chatactions/filetransferaction.h +++ b/src/widget/tool/chatactions/filetransferaction.h @@ -25,17 +25,13 @@ class FileTransferAction : public ChatAction public: FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me); virtual ~FileTransferAction(); - virtual QString getMessage(); - virtual void setup(QTextCursor cursor, QTextEdit* textEdit) override; virtual bool isInteractive(); -private slots: - void updateHtml(); +protected: + virtual QString getMessage(); private: FileTransferInstance *w; - QTextCursor cur; - QTextEdit* edit; }; #endif // FILETRANSFERACTION_H diff --git a/src/widget/tool/chatactions/messageaction.cpp b/src/widget/tool/chatactions/messageaction.cpp index 499299463..0b309b8be 100644 --- a/src/widget/tool/chatactions/messageaction.cpp +++ b/src/widget/tool/chatactions/messageaction.cpp @@ -17,11 +17,13 @@ #include "messageaction.h" #include "src/misc/smileypack.h" #include "src/misc/settings.h" +#include MessageAction::MessageAction(const QString &author, const QString &message, const QString &date, const bool &me) : ChatAction(me, author, date), message(message) { + isProcessed = false; } QString MessageAction::getMessage(QString div) @@ -71,3 +73,17 @@ QString MessageAction::getMessage() else return getMessage("message"); } + +void MessageAction::featureUpdate() +{ + QTextTableCell cell = textTable->cellAt(0,3); + QTextTableCellFormat format; + if (!isProcessed) + format.setBackground(QColor(Qt::red)); + cell.setFormat(format); +} + +void MessageAction::markAsSent() +{ + isProcessed = true; +} diff --git a/src/widget/tool/chatactions/messageaction.h b/src/widget/tool/chatactions/messageaction.h index d0e8f4066..c1a3d59df 100644 --- a/src/widget/tool/chatactions/messageaction.h +++ b/src/widget/tool/chatactions/messageaction.h @@ -24,12 +24,18 @@ class MessageAction : public ChatAction public: MessageAction(const QString &author, const QString &message, const QString &date, const bool &me); virtual ~MessageAction(){;} + virtual void featureUpdate(); + void markAsSent(); + +protected: virtual QString getMessage(); virtual QString getMessage(QString div); - virtual void setup(QTextCursor, QTextEdit*) override {;} protected: QString message; + bool isProcessed; }; +typedef QSharedPointer MessageActionPtr; + #endif // MESSAGEACTION_H diff --git a/src/widget/tool/chatactions/systemmessageaction.h b/src/widget/tool/chatactions/systemmessageaction.h index 1358f7c88..99740cedc 100644 --- a/src/widget/tool/chatactions/systemmessageaction.h +++ b/src/widget/tool/chatactions/systemmessageaction.h @@ -24,8 +24,8 @@ class SystemMessageAction : public ChatAction public: SystemMessageAction(const QString &message, const QString& type, const QString &date); virtual ~SystemMessageAction(){;} - virtual void setup(QTextCursor, QTextEdit*) override {;} +protected: virtual QString getName() {return QString();} virtual QString getMessage();