1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

ChatAction refactoring

This commit is contained in:
apprb 2014-11-10 01:07:15 +09:00
parent f7a855aa20
commit 977649f686
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
14 changed files with 127 additions and 42 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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()

View File

@ -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;

View File

@ -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()
{
return MessageAction::getMessage("alert");

View File

@ -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;

View File

@ -17,6 +17,11 @@
#include "chataction.h"
#include <QStringList>
#include <QBuffer>
#include <QTextTable>
#include <QScrollBar>
#include <QTextEdit>
QTextBlockFormat ChatAction::nameFormat, ChatAction::dateFormat;
QString ChatAction::toHtmlChars(const QString &str)
{
@ -53,3 +58,59 @@ QString ChatAction::getDate()
else
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();
}

View File

@ -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<ChatAction> ChatActionPtr;

View File

@ -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

View File

@ -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

View File

@ -17,11 +17,13 @@
#include "messageaction.h"
#include "src/misc/smileypack.h"
#include "src/misc/settings.h"
#include <QTextTable>
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;
}

View File

@ -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<MessageAction> MessageActionPtr;
#endif // MESSAGEACTION_H

View File

@ -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();