mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
ChatAction refactoring
This commit is contained in:
parent
f7a855aa20
commit
977649f686
|
@ -124,10 +124,11 @@ int HistoryKeeper::addChatEntry(const QString& chat, const QString& message, con
|
||||||
{
|
{
|
||||||
int chat_id = getChatID(chat, ctSingle).first;
|
int chat_id = getChatID(chat, ctSingle).first;
|
||||||
int sender_id = getAliasID(sender);
|
int sender_id = getAliasID(sender);
|
||||||
|
bool status = sender != Core::getInstance()->getSelfId().publicKey;
|
||||||
|
|
||||||
db->exec(QString("INSERT INTO history (timestamp, chat_id, sender, message)") +
|
db->exec(QString("INSERT INTO history (timestamp, chat_id, sender, sent_ok, message)") +
|
||||||
QString("VALUES (%1, %2, %3, '%4');")
|
QString("VALUES (%1, %2, %3, %4, '%5');")
|
||||||
.arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(wrapMessage(message)));
|
.arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(status).arg(wrapMessage(message)));
|
||||||
|
|
||||||
messageID++;
|
messageID++;
|
||||||
return messageID;
|
return messageID;
|
||||||
|
|
|
@ -102,6 +102,10 @@ void ChatAreaWidget::insertMessage(ChatActionPtr msgAction, QTextCursor::MoveOpe
|
||||||
checkSlider();
|
checkSlider();
|
||||||
|
|
||||||
QTextTable *chatTextTable = getMsgTable(pos);
|
QTextTable *chatTextTable = getMsgTable(pos);
|
||||||
|
msgAction->assignPlace(chatTextTable, this);
|
||||||
|
msgAction->dispaly();
|
||||||
|
|
||||||
|
/*
|
||||||
QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition();
|
QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition();
|
||||||
cur.clearSelection();
|
cur.clearSelection();
|
||||||
cur.setKeepPositionOnInsert(true);
|
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, 2).firstCursorPosition().insertHtml(msgAction->getMessage());
|
||||||
chatTextTable->cellAt(0, 4).firstCursorPosition().setBlockFormat(dateFormat);
|
chatTextTable->cellAt(0, 4).firstCursorPosition().setBlockFormat(dateFormat);
|
||||||
chatTextTable->cellAt(0, 4).firstCursorPosition().insertHtml(msgAction->getDate());
|
chatTextTable->cellAt(0, 4).firstCursorPosition().insertHtml(msgAction->getDate());
|
||||||
|
|
||||||
msgAction->setup(cur, this);
|
msgAction->setup(cur, this);
|
||||||
|
*/
|
||||||
|
|
||||||
if (msgAction->isInteractive())
|
if (msgAction->isInteractive())
|
||||||
messages.append(msgAction);
|
messages.append(msgAction);
|
||||||
|
|
|
@ -121,6 +121,7 @@ void ChatForm::onSendTriggered()
|
||||||
qDebug() << "db id:" << id;
|
qDebug() << "db id:" << id;
|
||||||
|
|
||||||
addSelfMessage(msg, isAction, timestamp);
|
addSelfMessage(msg, isAction, timestamp);
|
||||||
|
|
||||||
int rec;
|
int rec;
|
||||||
if (isAction)
|
if (isAction)
|
||||||
rec = Core::getInstance()->sendAction(f->getFriendID(), msg);
|
rec = Core::getInstance()->sendAction(f->getFriendID(), msg);
|
||||||
|
|
|
@ -149,6 +149,8 @@ GenericChatForm::GenericChatForm(QWidget *parent) :
|
||||||
chatWidget->document()->setDefaultStyleSheet(Style::getStylesheet(":ui/chatArea/innerStyle.css"));
|
chatWidget->document()->setDefaultStyleSheet(Style::getStylesheet(":ui/chatArea/innerStyle.css"));
|
||||||
chatWidget->setStyleSheet(Style::getStylesheet(":/ui/chatArea/chatArea.css"));
|
chatWidget->setStyleSheet(Style::getStylesheet(":/ui/chatArea/chatArea.css"));
|
||||||
headWidget->setStyleSheet(Style::getStylesheet(":/ui/chatArea/chatHead.css"));
|
headWidget->setStyleSheet(Style::getStylesheet(":/ui/chatArea/chatHead.css"));
|
||||||
|
|
||||||
|
ChatAction::setupFormat();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GenericChatForm::isEmpty()
|
bool GenericChatForm::isEmpty()
|
||||||
|
|
|
@ -24,9 +24,10 @@ class ActionAction : public MessageAction
|
||||||
public:
|
public:
|
||||||
ActionAction(const QString &author, QString message, const QString& date, const bool&);
|
ActionAction(const QString &author, QString message, const QString& date, const bool&);
|
||||||
virtual ~ActionAction(){;}
|
virtual ~ActionAction(){;}
|
||||||
|
|
||||||
|
protected:
|
||||||
virtual QString getMessage();
|
virtual QString getMessage();
|
||||||
virtual QString getName();
|
virtual QString getName();
|
||||||
virtual void setup(QTextCursor, QTextEdit*) override {;}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString message;
|
QString message;
|
||||||
|
|
|
@ -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("<div class=%1>%2</div>").arg("alert_name").arg(toHtmlChars(name));
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
QString AlertAction::getMessage()
|
QString AlertAction::getMessage()
|
||||||
{
|
{
|
||||||
return MessageAction::getMessage("alert");
|
return MessageAction::getMessage("alert");
|
||||||
|
|
|
@ -24,9 +24,9 @@ class AlertAction : public MessageAction
|
||||||
public:
|
public:
|
||||||
AlertAction(const QString &author, const QString &message, const QString& date);
|
AlertAction(const QString &author, const QString &message, const QString& date);
|
||||||
virtual ~AlertAction(){;}
|
virtual ~AlertAction(){;}
|
||||||
|
|
||||||
|
protected:
|
||||||
virtual QString getMessage();
|
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:
|
private:
|
||||||
QString message;
|
QString message;
|
||||||
|
|
|
@ -17,6 +17,11 @@
|
||||||
#include "chataction.h"
|
#include "chataction.h"
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QBuffer>
|
#include <QBuffer>
|
||||||
|
#include <QTextTable>
|
||||||
|
#include <QScrollBar>
|
||||||
|
#include <QTextEdit>
|
||||||
|
|
||||||
|
QTextBlockFormat ChatAction::nameFormat, ChatAction::dateFormat;
|
||||||
|
|
||||||
QString ChatAction::toHtmlChars(const QString &str)
|
QString ChatAction::toHtmlChars(const QString &str)
|
||||||
{
|
{
|
||||||
|
@ -53,3 +58,59 @@ QString ChatAction::getDate()
|
||||||
else
|
else
|
||||||
return QString("<div class=date>" + toHtmlChars(date) + "</div>");
|
return QString("<div class=date>" + toHtmlChars(date) + "</div>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
|
@ -23,26 +23,42 @@
|
||||||
|
|
||||||
class FileTransferInstance;
|
class FileTransferInstance;
|
||||||
class QTextEdit;
|
class QTextEdit;
|
||||||
|
class QTextTable;
|
||||||
|
|
||||||
class ChatAction : public QObject
|
class ChatAction : public QObject
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
ChatAction(const bool &me, const QString &author, const QString &date) : isMe(me), name(author), date(date) {;}
|
ChatAction(const bool &me, const QString &author, const QString &date) : isMe(me), name(author), date(date) {;}
|
||||||
virtual ~ChatAction(){;}
|
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 getName();
|
||||||
virtual QString getMessage() = 0;
|
virtual QString getMessage() = 0;
|
||||||
virtual QString getDate();
|
virtual QString getDate();
|
||||||
virtual bool isInteractive(){return false;}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
QString toHtmlChars(const QString &str);
|
QString toHtmlChars(const QString &str);
|
||||||
QString QImage2base64(const QImage &img);
|
QString QImage2base64(const QImage &img);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool isMe;
|
bool isMe;
|
||||||
QString name, date;
|
QString name, date;
|
||||||
|
|
||||||
|
QTextTable *textTable;
|
||||||
|
QTextEdit *textEdit;
|
||||||
|
QTextCursor cur;
|
||||||
|
|
||||||
|
static QTextBlockFormat nameFormat, dateFormat;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef QSharedPointer<ChatAction> ChatActionPtr;
|
typedef QSharedPointer<ChatAction> ChatActionPtr;
|
||||||
|
|
|
@ -22,11 +22,10 @@
|
||||||
|
|
||||||
FileTransferAction::FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me)
|
FileTransferAction::FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me)
|
||||||
: ChatAction(me, author, date)
|
: ChatAction(me, author, date)
|
||||||
, edit(nullptr)
|
|
||||||
{
|
{
|
||||||
w = widget;
|
w = widget;
|
||||||
|
|
||||||
connect(w, &FileTransferInstance::stateUpdated, this, &FileTransferAction::updateHtml);
|
connect(w, &FileTransferInstance::stateUpdated, this, &FileTransferAction::updateContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
FileTransferAction::~FileTransferAction()
|
FileTransferAction::~FileTransferAction()
|
||||||
|
@ -43,6 +42,7 @@ QString FileTransferAction::getMessage()
|
||||||
return widgetHtml;
|
return widgetHtml;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
void FileTransferAction::setup(QTextCursor cursor, QTextEdit *textEdit)
|
void FileTransferAction::setup(QTextCursor cursor, QTextEdit *textEdit)
|
||||||
{
|
{
|
||||||
cur = cursor;
|
cur = cursor;
|
||||||
|
@ -53,7 +53,8 @@ void FileTransferAction::setup(QTextCursor cursor, QTextEdit *textEdit)
|
||||||
|
|
||||||
edit = textEdit;
|
edit = textEdit;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
/*
|
||||||
void FileTransferAction::updateHtml()
|
void FileTransferAction::updateHtml()
|
||||||
{
|
{
|
||||||
if (cur.isNull() || !edit)
|
if (cur.isNull() || !edit)
|
||||||
|
@ -75,7 +76,7 @@ void FileTransferAction::updateHtml()
|
||||||
// restore old slider value
|
// restore old slider value
|
||||||
edit->verticalScrollBar()->setValue(vSliderVal);
|
edit->verticalScrollBar()->setValue(vSliderVal);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
bool FileTransferAction::isInteractive()
|
bool FileTransferAction::isInteractive()
|
||||||
{
|
{
|
||||||
if (w->getState() == FileTransferInstance::TransfState::tsCanceled
|
if (w->getState() == FileTransferInstance::TransfState::tsCanceled
|
||||||
|
|
|
@ -25,17 +25,13 @@ class FileTransferAction : public ChatAction
|
||||||
public:
|
public:
|
||||||
FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me);
|
FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me);
|
||||||
virtual ~FileTransferAction();
|
virtual ~FileTransferAction();
|
||||||
virtual QString getMessage();
|
|
||||||
virtual void setup(QTextCursor cursor, QTextEdit* textEdit) override;
|
|
||||||
virtual bool isInteractive();
|
virtual bool isInteractive();
|
||||||
|
|
||||||
private slots:
|
protected:
|
||||||
void updateHtml();
|
virtual QString getMessage();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FileTransferInstance *w;
|
FileTransferInstance *w;
|
||||||
QTextCursor cur;
|
|
||||||
QTextEdit* edit;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FILETRANSFERACTION_H
|
#endif // FILETRANSFERACTION_H
|
||||||
|
|
|
@ -17,11 +17,13 @@
|
||||||
#include "messageaction.h"
|
#include "messageaction.h"
|
||||||
#include "src/misc/smileypack.h"
|
#include "src/misc/smileypack.h"
|
||||||
#include "src/misc/settings.h"
|
#include "src/misc/settings.h"
|
||||||
|
#include <QTextTable>
|
||||||
|
|
||||||
MessageAction::MessageAction(const QString &author, const QString &message, const QString &date, const bool &me) :
|
MessageAction::MessageAction(const QString &author, const QString &message, const QString &date, const bool &me) :
|
||||||
ChatAction(me, author, date),
|
ChatAction(me, author, date),
|
||||||
message(message)
|
message(message)
|
||||||
{
|
{
|
||||||
|
isProcessed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString MessageAction::getMessage(QString div)
|
QString MessageAction::getMessage(QString div)
|
||||||
|
@ -71,3 +73,17 @@ QString MessageAction::getMessage()
|
||||||
else
|
else
|
||||||
return getMessage("message");
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -24,12 +24,18 @@ class MessageAction : public ChatAction
|
||||||
public:
|
public:
|
||||||
MessageAction(const QString &author, const QString &message, const QString &date, const bool &me);
|
MessageAction(const QString &author, const QString &message, const QString &date, const bool &me);
|
||||||
virtual ~MessageAction(){;}
|
virtual ~MessageAction(){;}
|
||||||
|
virtual void featureUpdate();
|
||||||
|
void markAsSent();
|
||||||
|
|
||||||
|
protected:
|
||||||
virtual QString getMessage();
|
virtual QString getMessage();
|
||||||
virtual QString getMessage(QString div);
|
virtual QString getMessage(QString div);
|
||||||
virtual void setup(QTextCursor, QTextEdit*) override {;}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QString message;
|
QString message;
|
||||||
|
bool isProcessed;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef QSharedPointer<MessageAction> MessageActionPtr;
|
||||||
|
|
||||||
#endif // MESSAGEACTION_H
|
#endif // MESSAGEACTION_H
|
||||||
|
|
|
@ -24,8 +24,8 @@ class SystemMessageAction : public ChatAction
|
||||||
public:
|
public:
|
||||||
SystemMessageAction(const QString &message, const QString& type, const QString &date);
|
SystemMessageAction(const QString &message, const QString& type, const QString &date);
|
||||||
virtual ~SystemMessageAction(){;}
|
virtual ~SystemMessageAction(){;}
|
||||||
virtual void setup(QTextCursor, QTextEdit*) override {;}
|
|
||||||
|
|
||||||
|
protected:
|
||||||
virtual QString getName() {return QString();}
|
virtual QString getName() {return QString();}
|
||||||
virtual QString getMessage();
|
virtual QString getMessage();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user