diff --git a/src/chatlog/chatlog.cpp b/src/chatlog/chatlog.cpp index a6bb68c71..215750df6 100644 --- a/src/chatlog/chatlog.cpp +++ b/src/chatlog/chatlog.cpp @@ -399,6 +399,34 @@ void ChatLog::insertChatlineOnTop(ChatLine::Ptr l) checkVisibility(); } +void ChatLog::insertChatlineOnTop(const QList& newLines) +{ + if(newLines.isEmpty()) + return; + + //move all lines down by n + int n = newLines.size(); + for(ChatLine::Ptr l : lines) + l->setRowIndex(l->getRowIndex() + n); + + //add the new line + for(ChatLine::Ptr l : newLines) + { + l->addToScene(scene); + l->setRowIndex(--n); + lines.prepend(l); + } + + //full refresh is required + layout(0, lines.size(), useableWidth()); + updateSceneRect(); + + if(stickToBtm) + scrollToBottom(); + + checkVisibility(); +} + bool ChatLog::stickToBottom() { return verticalScrollBar()->value() == verticalScrollBar()->maximum(); @@ -521,7 +549,7 @@ void ChatLog::checkVisibility() visibleLines = newVisibleLines; - // assure order + // enforce order std::sort(visibleLines.begin(), visibleLines.end(), [](const ChatLine::Ptr lhs, const ChatLine::Ptr rhs) { return lhs->getRowIndex() < rhs->getRowIndex(); diff --git a/src/chatlog/chatlog.h b/src/chatlog/chatlog.h index 2df5f60ea..a75da65e9 100644 --- a/src/chatlog/chatlog.h +++ b/src/chatlog/chatlog.h @@ -39,6 +39,7 @@ public: void insertChatlineAtBottom(ChatLine::Ptr l); void insertChatlineOnTop(ChatLine::Ptr l); + void insertChatlineOnTop(const QList& newLines); void clearSelection(); void clear(); void copySelectedText() const; diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 21c591429..c80b9d01c 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -704,7 +704,7 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered) ToxID storedPrevId = previousId; ToxID prevId; - QList historyMessages; + QList historyMessages; QDate lastDate(1,0,0); for (const auto &it : msgs) @@ -758,8 +758,7 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered) earliestMessage = since; - for(ChatMessage::Ptr m : historyMessages) - chatWidget->insertChatlineOnTop(m); + chatWidget->insertChatlineOnTop(historyMessages); savedSliderPos = chatWidget->verticalScrollBar()->maximum() - savedSliderPos; chatWidget->verticalScrollBar()->setValue(savedSliderPos);