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

feat: remove part messages from chat

This commit is contained in:
TriKriSta 2019-05-30 16:30:31 +03:00
parent c2d5b422b3
commit 4c7ecb6024
4 changed files with 67 additions and 2 deletions

View File

@ -688,6 +688,28 @@ void ChatLog::reloadTheme()
} }
} }
void ChatLog::removeFirsts(const int num)
{
if (lines.size() > num) {
lines.erase(lines.begin(), lines.begin()+num);
} else {
lines.clear();
}
for (int i = 0; i < lines.size(); ++i) {
lines[i]->setRow(i);
}
}
void ChatLog::removeLasts(const int num)
{
if (lines.size() > num) {
lines.erase(lines.end()-num, lines.end());
} else {
lines.clear();
}
}
void ChatLog::forceRelayout() void ChatLog::forceRelayout()
{ {
startResizeWorker(); startResizeWorker();

View File

@ -56,6 +56,8 @@ public:
void selectAll(); void selectAll();
void fontChanged(const QFont& font); void fontChanged(const QFont& font);
void reloadTheme(); void reloadTheme();
void removeFirsts(const int num);
void removeLasts(const int num);
QString getSelectedText() const; QString getSelectedText() const;

View File

@ -670,7 +670,13 @@ void GenericChatForm::loadHistoryTo(const QDateTime &time)
{ {
auto end = ChatLogIdx(0); auto end = ChatLogIdx(0);
if (time.isNull()) { if (time.isNull()) {
end = messages.begin()->first; if (messages.size() + 100 >= maxMessages) {
end = ChatLogIdx(messages.rbegin()->first.get() - 100);
chatWidget->removeLasts(100);
removeLastsMessages(100);
} else {
end = messages.begin()->first;
}
} else { } else {
end = firstItemAfterDate(time.date(), chatLog); end = firstItemAfterDate(time.date(), chatLog);
} }
@ -687,7 +693,14 @@ void GenericChatForm::loadHistoryFrom(const QDateTime &time)
{ {
auto begin = ChatLogIdx(0); auto begin = ChatLogIdx(0);
if (time.isNull()) { if (time.isNull()) {
begin = messages.rbegin()->first; if (messages.size() + 100 >= maxMessages) {
begin = ChatLogIdx(messages.rbegin()->first.get() + 100);
chatWidget->removeFirsts(100);
removeFirstsMessages(100);
} else {
begin = messages.rbegin()->first;
}
} else { } else {
begin = firstItemAfterDate(time.date(), chatLog); begin = firstItemAfterDate(time.date(), chatLog);
} }
@ -700,6 +713,24 @@ void GenericChatForm::loadHistoryFrom(const QDateTime &time)
renderMessages(begin, end); renderMessages(begin, end);
} }
void GenericChatForm::removeFirstsMessages(const int num)
{
if (messages.size() > num) {
messages.erase(messages.begin(), std::next(messages.begin(), num));
} else {
messages.clear();
}
}
void GenericChatForm::removeLastsMessages(const int num)
{
if (messages.size() > 100) {
messages.erase(std::next(messages.end(), -100), messages.end());
} else {
messages.clear();
}
}
void GenericChatForm::disableSearchText() void GenericChatForm::disableSearchText()
{ {
@ -993,6 +1024,10 @@ void GenericChatForm::handleSearchResult(SearchResult result, SearchDirection di
void GenericChatForm::renderMessage(ChatLogIdx idx) void GenericChatForm::renderMessage(ChatLogIdx idx)
{ {
if (chatWidget->getLines().size() >= maxMessages) {
chatWidget->removeFirsts(optimalRemove);
removeFirstsMessages(optimalRemove);
}
renderMessages(idx, idx + 1); renderMessages(idx, idx + 1);
} }

View File

@ -138,6 +138,8 @@ private:
void loadHistory(const QDateTime& time, const LoadHistoryDialog::LoadType type); void loadHistory(const QDateTime& time, const LoadHistoryDialog::LoadType type);
void loadHistoryTo(const QDateTime& time); void loadHistoryTo(const QDateTime& time);
void loadHistoryFrom(const QDateTime& time); void loadHistoryFrom(const QDateTime& time);
void removeFirstsMessages(const int num);
void removeLastsMessages(const int num);
protected: protected:
ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message, ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message,
@ -199,6 +201,10 @@ protected:
SearchPos searchPos; SearchPos searchPos;
std::map<ChatLogIdx, ChatMessage::Ptr> messages; std::map<ChatLogIdx, ChatMessage::Ptr> messages;
bool colorizeNames = false; bool colorizeNames = false;
private:
const int maxMessages{300};
const int optimalRemove{50};
}; };
#endif // GENERICCHATFORM_H #endif // GENERICCHATFORM_H