mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Reducing memory consumption: no reason to store all displayed ChatActions in memory
This commit is contained in:
parent
57a995117f
commit
8300f42e02
|
@ -22,6 +22,7 @@
|
|||
#include <QAbstractTextDocumentLayout>
|
||||
#include <QCoreApplication>
|
||||
#include <QDebug>
|
||||
#include <algorithm>
|
||||
|
||||
ChatAreaWidget::ChatAreaWidget(QWidget *parent)
|
||||
: QTextBrowser(parent)
|
||||
|
@ -96,14 +97,14 @@ void ChatAreaWidget::onAnchorClicked(const QUrl &url)
|
|||
QDesktopServices::openUrl(url);
|
||||
}
|
||||
|
||||
void ChatAreaWidget::insertMessage(ChatAction *msgAction)
|
||||
void ChatAreaWidget::insertMessage(ChatAction *msgAction, QTextCursor::MoveOperation pos)
|
||||
{
|
||||
if (msgAction == nullptr)
|
||||
return;
|
||||
|
||||
checkSlider();
|
||||
|
||||
QTextTable *chatTextTable = getMsgTable();
|
||||
QTextTable *chatTextTable = getMsgTable(pos);
|
||||
QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition();
|
||||
cur.clearSelection();
|
||||
cur.setKeepPositionOnInsert(true);
|
||||
|
@ -115,7 +116,10 @@ void ChatAreaWidget::insertMessage(ChatAction *msgAction)
|
|||
|
||||
msgAction->setup(cur, this);
|
||||
|
||||
messages.append(msgAction);
|
||||
if (msgAction->isInteractive())
|
||||
messages.append(msgAction);
|
||||
else
|
||||
delete msgAction;
|
||||
}
|
||||
|
||||
int ChatAreaWidget::getNumberOfMessages()
|
||||
|
@ -136,7 +140,7 @@ void ChatAreaWidget::checkSlider()
|
|||
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
|
||||
}
|
||||
|
||||
QTextTable *ChatAreaWidget::getMsgTable()
|
||||
QTextTable *ChatAreaWidget::getMsgTable(QTextCursor::MoveOperation pos)
|
||||
{
|
||||
if (tableFrmt == nullptr)
|
||||
{
|
||||
|
@ -151,7 +155,7 @@ QTextTable *ChatAreaWidget::getMsgTable()
|
|||
}
|
||||
|
||||
QTextCursor tc = textCursor();
|
||||
tc.movePosition(QTextCursor::End);
|
||||
tc.movePosition(pos);
|
||||
|
||||
QTextTable *chatTextTable = tc.insertTable(1, 5, *tableFrmt);
|
||||
|
||||
|
@ -189,3 +193,13 @@ void ChatAreaWidget::clearChatArea()
|
|||
insertMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
void ChatAreaWidget::insertMessagesTop(QList<ChatAction*> &list)
|
||||
{
|
||||
std::reverse(list.begin(), list.end());
|
||||
|
||||
for (ChatAction* it : list)
|
||||
{
|
||||
insertMessage(it, QTextCursor::Start);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,8 +29,8 @@ class ChatAreaWidget : public QTextBrowser
|
|||
public:
|
||||
explicit ChatAreaWidget(QWidget *parent = 0);
|
||||
virtual ~ChatAreaWidget();
|
||||
void insertMessage(ChatAction *msgAction);
|
||||
QList<ChatAction*>& getMesages() {return messages;}
|
||||
void insertMessage(ChatAction *msgAction, QTextCursor::MoveOperation pos = QTextCursor::End);
|
||||
void insertMessagesTop(QList<ChatAction *> &list);
|
||||
|
||||
int nameColWidth() {return nameWidth;}
|
||||
void setNameColWidth(int w);
|
||||
|
@ -51,7 +51,7 @@ private slots:
|
|||
|
||||
private:
|
||||
void checkSlider();
|
||||
QTextTable* getMsgTable();
|
||||
QTextTable* getMsgTable(QTextCursor::MoveOperation pos = QTextCursor::End);
|
||||
|
||||
QTextTableFormat* tableFrmt;
|
||||
QList<ChatAction*> messages;
|
||||
|
|
|
@ -588,18 +588,12 @@ void ChatForm::onLoadHistory()
|
|||
}
|
||||
previousName = storedPrevName;
|
||||
|
||||
for (ChatAction *ca : chatWidget->getMesages())
|
||||
historyMessages.append(ca);
|
||||
|
||||
int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value();
|
||||
|
||||
chatWidget->getMesages().clear();
|
||||
chatWidget->clear();
|
||||
if (earliestMessage != nullptr)
|
||||
*earliestMessage = fromTime;
|
||||
|
||||
for (ChatAction *ca : historyMessages)
|
||||
chatWidget->insertMessage(ca);
|
||||
chatWidget->insertMessagesTop(historyMessages);
|
||||
|
||||
savedSliderPos = chatWidget->verticalScrollBar()->maximum() - savedSliderPos;
|
||||
chatWidget->verticalScrollBar()->setValue(savedSliderPos);
|
||||
|
|
Loading…
Reference in New Issue
Block a user