1
0
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:
apprb 2014-10-25 01:25:28 +09:00
parent 57a995117f
commit 8300f42e02
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
3 changed files with 23 additions and 15 deletions

View File

@ -22,6 +22,7 @@
#include <QAbstractTextDocumentLayout> #include <QAbstractTextDocumentLayout>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <algorithm>
ChatAreaWidget::ChatAreaWidget(QWidget *parent) ChatAreaWidget::ChatAreaWidget(QWidget *parent)
: QTextBrowser(parent) : QTextBrowser(parent)
@ -96,14 +97,14 @@ void ChatAreaWidget::onAnchorClicked(const QUrl &url)
QDesktopServices::openUrl(url); QDesktopServices::openUrl(url);
} }
void ChatAreaWidget::insertMessage(ChatAction *msgAction) void ChatAreaWidget::insertMessage(ChatAction *msgAction, QTextCursor::MoveOperation pos)
{ {
if (msgAction == nullptr) if (msgAction == nullptr)
return; return;
checkSlider(); checkSlider();
QTextTable *chatTextTable = getMsgTable(); QTextTable *chatTextTable = getMsgTable(pos);
QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition(); QTextCursor cur = chatTextTable->cellAt(0, 2).firstCursorPosition();
cur.clearSelection(); cur.clearSelection();
cur.setKeepPositionOnInsert(true); cur.setKeepPositionOnInsert(true);
@ -115,7 +116,10 @@ void ChatAreaWidget::insertMessage(ChatAction *msgAction)
msgAction->setup(cur, this); msgAction->setup(cur, this);
messages.append(msgAction); if (msgAction->isInteractive())
messages.append(msgAction);
else
delete msgAction;
} }
int ChatAreaWidget::getNumberOfMessages() int ChatAreaWidget::getNumberOfMessages()
@ -136,7 +140,7 @@ void ChatAreaWidget::checkSlider()
lockSliderToBottom = scroll && scroll->value() == scroll->maximum(); lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
} }
QTextTable *ChatAreaWidget::getMsgTable() QTextTable *ChatAreaWidget::getMsgTable(QTextCursor::MoveOperation pos)
{ {
if (tableFrmt == nullptr) if (tableFrmt == nullptr)
{ {
@ -151,7 +155,7 @@ QTextTable *ChatAreaWidget::getMsgTable()
} }
QTextCursor tc = textCursor(); QTextCursor tc = textCursor();
tc.movePosition(QTextCursor::End); tc.movePosition(pos);
QTextTable *chatTextTable = tc.insertTable(1, 5, *tableFrmt); QTextTable *chatTextTable = tc.insertTable(1, 5, *tableFrmt);
@ -189,3 +193,13 @@ void ChatAreaWidget::clearChatArea()
insertMessage(message); insertMessage(message);
} }
} }
void ChatAreaWidget::insertMessagesTop(QList<ChatAction*> &list)
{
std::reverse(list.begin(), list.end());
for (ChatAction* it : list)
{
insertMessage(it, QTextCursor::Start);
}
}

View File

@ -29,8 +29,8 @@ class ChatAreaWidget : public QTextBrowser
public: public:
explicit ChatAreaWidget(QWidget *parent = 0); explicit ChatAreaWidget(QWidget *parent = 0);
virtual ~ChatAreaWidget(); virtual ~ChatAreaWidget();
void insertMessage(ChatAction *msgAction); void insertMessage(ChatAction *msgAction, QTextCursor::MoveOperation pos = QTextCursor::End);
QList<ChatAction*>& getMesages() {return messages;} void insertMessagesTop(QList<ChatAction *> &list);
int nameColWidth() {return nameWidth;} int nameColWidth() {return nameWidth;}
void setNameColWidth(int w); void setNameColWidth(int w);
@ -51,7 +51,7 @@ private slots:
private: private:
void checkSlider(); void checkSlider();
QTextTable* getMsgTable(); QTextTable* getMsgTable(QTextCursor::MoveOperation pos = QTextCursor::End);
QTextTableFormat* tableFrmt; QTextTableFormat* tableFrmt;
QList<ChatAction*> messages; QList<ChatAction*> messages;

View File

@ -588,18 +588,12 @@ void ChatForm::onLoadHistory()
} }
previousName = storedPrevName; previousName = storedPrevName;
for (ChatAction *ca : chatWidget->getMesages())
historyMessages.append(ca);
int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value(); int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value();
chatWidget->getMesages().clear();
chatWidget->clear();
if (earliestMessage != nullptr) if (earliestMessage != nullptr)
*earliestMessage = fromTime; *earliestMessage = fromTime;
for (ChatAction *ca : historyMessages) chatWidget->insertMessagesTop(historyMessages);
chatWidget->insertMessage(ca);
savedSliderPos = chatWidget->verticalScrollBar()->maximum() - savedSliderPos; savedSliderPos = chatWidget->verticalScrollBar()->maximum() - savedSliderPos;
chatWidget->verticalScrollBar()->setValue(savedSliderPos); chatWidget->verticalScrollBar()->setValue(savedSliderPos);