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,1).firstCursorPosition().insertHtml(msgAction->getMessage());
chatTextTable->cellAt(row,2).firstCursorPosition().insertText(msgAction->getDate()); chatTextTable->cellAt(row,2).firstCursorPosition().insertText(msgAction->getDate());
msgAction->setTextCursor(cur); msgAction->setup(cur, this);
messages.append(msgAction); messages.append(msgAction);
} }

View File

@ -21,13 +21,14 @@
#include <QTextCursor> #include <QTextCursor>
class FileTransferInstance; class FileTransferInstance;
class QTextEdit;
class ChatAction : public QObject class ChatAction : public QObject
{ {
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 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 getName();
virtual QString getMessage() = 0; virtual QString getMessage() = 0;

View File

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

View File

@ -26,7 +26,7 @@ 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 QString getMessage();
virtual void setTextCursor(QTextCursor cursor) final; virtual void setup(QTextCursor cursor, QTextEdit* textEdit) override;
private slots: private slots:
void updateHtml(); void updateHtml();
@ -34,6 +34,7 @@ private slots:
private: private:
FileTransferInstance *w; FileTransferInstance *w;
QTextCursor cur; QTextCursor cur;
QTextEdit* edit;
}; };
#endif // FILETRANSFERACTION_H #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 // 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 // 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); MessageAction(const QString &author, const QString &message, const QString &date, const bool &me);
virtual ~MessageAction(){;} virtual ~MessageAction(){;}
virtual QString getMessage(); virtual QString getMessage();
virtual void setTextCursor(QTextCursor cursor) final; virtual void setup(QTextCursor cursor, QTextEdit*) override;
private: private:
QString message; 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>"); 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 // 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 // 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: 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 setTextCursor(QTextCursor cursor) final; virtual void setup(QTextCursor cursor, QTextEdit*) override;
virtual QString getName() {return QString();} virtual QString getName() {return QString();}
virtual QString getMessage(); virtual QString getMessage();