diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index 7cfbc5a20..02e572e3f 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -654,6 +654,11 @@ QDateTime GenericChatForm::getTime(const ChatLine::Ptr &chatLine) const return QDateTime(); } +/** + * @brief GenericChatForm::loadHistory load history + * @param time start date + * @param type indicates the direction of loading history + */ void GenericChatForm::loadHistory(const QDateTime &time, const LoadHistoryDialog::LoadType type) { chatWidget->clear(); @@ -669,6 +674,10 @@ void GenericChatForm::loadHistory(const QDateTime &time, const LoadHistoryDialog } } +/** + * @brief GenericChatForm::loadHistoryTo load history before to date "time" or before the first "messages" item + * @param time start date + */ void GenericChatForm::loadHistoryTo(const QDateTime &time) { chatWidget->setScroll(false); @@ -695,7 +704,12 @@ void GenericChatForm::loadHistoryTo(const QDateTime &time) } } -void GenericChatForm::loadHistoryFrom(const QDateTime &time) +/** + * @brief GenericChatForm::loadHistoryFrom load history starting from date "time" or from the last "messages" item + * @param time start date + * @return true if function loaded history else false + */ +bool GenericChatForm::loadHistoryFrom(const QDateTime &time) { chatWidget->setScroll(false); auto begin = ChatLogIdx(0); @@ -709,8 +723,19 @@ void GenericChatForm::loadHistoryFrom(const QDateTime &time) if (begin.get() + DEF_NUM_MSG_TO_LOAD > chatLog.getNextIdx().get()) { add = chatLog.getNextIdx().get() - begin.get(); } + + // The chatLog.getNextIdx() is usually 1 more than the idx on last "messages" item + // so if we have nothing to load, "add" is equal 1 + if (add <= 1) { + chatWidget->setScroll(true); + return false; + } + auto end = ChatLogIdx(begin.get() + add); + renderMessages(begin, end); + + return true; } void GenericChatForm::removeFirstsMessages(const int num) @@ -1147,11 +1172,17 @@ void GenericChatForm::goToCurrentDate() renderMessages(begin, end); } +/** + * @brief GenericChatForm::loadHistoryLower load history after scrolling chatlog before first "messages" item + */ void GenericChatForm::loadHistoryLower() { loadHistoryTo(QDateTime()); } +/** + * @brief GenericChatForm::loadHistoryUpper load history after scrolling chatlog after last "messages" item + */ void GenericChatForm::loadHistoryUpper() { if (messages.empty()) { @@ -1159,8 +1190,9 @@ void GenericChatForm::loadHistoryUpper() } auto msg = messages.crbegin()->second; - loadHistoryFrom(QDateTime()); - chatWidget->scrollToLine(msg); + if (loadHistoryFrom(QDateTime())) { + chatWidget->scrollToLine(msg); + } } void GenericChatForm::updateShowDateInfo(const ChatLine::Ptr& line) diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index 3358de41b..2e1b2df0f 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -137,7 +137,7 @@ private: QDateTime getTime(const ChatLine::Ptr& chatLine) const; void loadHistory(const QDateTime& time, const LoadHistoryDialog::LoadType type); void loadHistoryTo(const QDateTime& time); - void loadHistoryFrom(const QDateTime& time); + bool loadHistoryFrom(const QDateTime& time); void removeFirstsMessages(const int num); void removeLastsMessages(const int num);