1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
This commit is contained in:
krepa098 2014-09-27 13:23:00 +02:00
parent f9df1a1503
commit 4d24bd53f2
8 changed files with 26 additions and 11 deletions

View File

@ -118,7 +118,7 @@ void ChatAreaWidget::insertMessage(ChatAction *msgAction)
chatTextTable->cellAt(row,1).firstCursorPosition().insertHtml(msgAction->getMessage());
chatTextTable->cellAt(row,2).firstCursorPosition().insertText(msgAction->getDate());
msgAction->setTextCursor(cur);
msgAction->setup(cur, this);
messages.append(msgAction);
}

View File

@ -21,13 +21,14 @@
#include <QTextCursor>
class FileTransferInstance;
class QTextEdit;
class ChatAction : public QObject
{
public:
ChatAction(const bool &me, const QString &author, const QString &date) : isMe(me), name(author), date(date) {;}
virtual ~ChatAction(){;}
virtual void setTextCursor(QTextCursor cursor){(void)cursor;} ///< Call once, and then you MUST let the object update itself
virtual void setup(QTextCursor cursor, QTextEdit* textEdit) = 0; ///< Call once, and then you MUST let the object update itself
virtual QString getName();
virtual QString getMessage() = 0;

View File

@ -17,8 +17,12 @@
#include "filetransferaction.h"
#include "filetransferinstance.h"
FileTransferAction::FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me) :
ChatAction(me, author, date)
#include <QTextEdit>
#include <QScrollBar>
FileTransferAction::FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me)
: ChatAction(me, author, date)
, edit(nullptr)
{
w = widget;
@ -39,20 +43,26 @@ QString FileTransferAction::getMessage()
return widgetHtml;
}
void FileTransferAction::setTextCursor(QTextCursor cursor)
void FileTransferAction::setup(QTextCursor cursor, QTextEdit *textEdit)
{
cur = cursor;
cur.setKeepPositionOnInsert(true);
int end=cur.selectionEnd();
cur.setPosition(cur.position());
cur.setPosition(end, QTextCursor::KeepAnchor);
edit = textEdit;
}
void FileTransferAction::updateHtml()
{
if (cur.isNull())
if (cur.isNull() || !edit)
return;
// save old slider value
int vSliderVal = edit->verticalScrollBar()->value();
// update content
int pos = cur.selectionStart();
cur.removeSelectedText();
cur.setKeepPositionOnInsert(false);
@ -62,6 +72,9 @@ void FileTransferAction::updateHtml()
cur.setPosition(pos);
cur.setPosition(end, QTextCursor::KeepAnchor);
// restore old slider value
edit->verticalScrollBar()->setValue(vSliderVal);
// Free our ressources if we'll never need to update again
if (w->getState() == FileTransferInstance::TransfState::tsCanceled
|| w->getState() == FileTransferInstance::TransfState::tsFinished)

View File

@ -26,7 +26,7 @@ public:
FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me);
virtual ~FileTransferAction();
virtual QString getMessage();
virtual void setTextCursor(QTextCursor cursor) final;
virtual void setup(QTextCursor cursor, QTextEdit* textEdit) override;
private slots:
void updateHtml();
@ -34,6 +34,7 @@ private slots:
private:
FileTransferInstance *w;
QTextCursor cur;
QTextEdit* edit;
};
#endif // FILETRANSFERACTION_H

View File

@ -23,7 +23,7 @@ MessageAction::MessageAction(const QString &author, const QString &message, cons
{
}
void MessageAction::setTextCursor(QTextCursor cursor)
void MessageAction::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

View File

@ -25,7 +25,7 @@ public:
MessageAction(const QString &author, const QString &message, const QString &date, const bool &me);
virtual ~MessageAction(){;}
virtual QString getMessage();
virtual void setTextCursor(QTextCursor cursor) final;
virtual void setup(QTextCursor cursor, QTextEdit*) override;
private:
QString message;

View File

@ -28,7 +28,7 @@ QString SystemMessageAction::getMessage()
return QString("<table width=100%><tr><td align=center><div class=" + type + ">" + message + "</td><tr></div></table>");
}
void SystemMessageAction::setTextCursor(QTextCursor cursor)
void SystemMessageAction::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

View File

@ -24,7 +24,7 @@ class SystemMessageAction : public ChatAction
public:
SystemMessageAction(const QString &message, const QString& type, const QString &date);
virtual ~SystemMessageAction(){;}
virtual void setTextCursor(QTextCursor cursor) final;
virtual void setup(QTextCursor cursor, QTextEdit*) override;
virtual QString getName() {return QString();}
virtual QString getMessage();