Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat: edit position chat after load history
Browse files Browse the repository at this point in the history
  • Loading branch information
TriKriSta committed Jul 22, 2019
1 parent 2a9648d commit c2d5b42
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
14 changes: 10 additions & 4 deletions src/chatlog/chatlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ void ChatLog::forceRelayout()
startResizeWorker();
}

void ChatLog::checkVisibility(bool causedByScroll)
void ChatLog::checkVisibility(bool causedWheelEvent)
{
if (lines.empty())
return;
Expand Down Expand Up @@ -734,10 +734,10 @@ void ChatLog::checkVisibility(bool causedByScroll)
emit firstVisibleLineChanged(visibleLines.at(0));
}

if (causedByScroll) {
if (causedWheelEvent) {
if (lowerBound != lines.cend() && lowerBound->get()->row == 0) {
emit loadHistoryLower();
} else if (upperBound != lines.cend() && upperBound->get()->row >= lines.size() - 10) {
} else if (upperBound == lines.cend()) {
emit loadHistoryUpper();
}
}
Expand All @@ -746,7 +746,7 @@ void ChatLog::checkVisibility(bool causedByScroll)
void ChatLog::scrollContentsBy(int dx, int dy)
{
QGraphicsView::scrollContentsBy(dx, dy);
checkVisibility(true);
checkVisibility();
}

void ChatLog::resizeEvent(QResizeEvent* ev)
Expand Down Expand Up @@ -942,6 +942,12 @@ void ChatLog::focusOutEvent(QFocusEvent* ev)
}
}

void ChatLog::wheelEvent(QWheelEvent *event)
{
QGraphicsView::wheelEvent(event);
checkVisibility(true);
}

void ChatLog::retranslateUi()
{
copyAction->setText(tr("Copy"));
Expand Down
3 changes: 2 additions & 1 deletion src/chatlog/chatlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private slots:

void reposition(int start, int end, qreal deltaY);
void updateSceneRect();
void checkVisibility(bool causedByScroll = false);
void checkVisibility(bool causedWheelEvent = false);
void scrollToBottom();
void startResizeWorker();

Expand All @@ -110,6 +110,7 @@ private slots:
virtual void showEvent(QShowEvent*) final override;
virtual void focusInEvent(QFocusEvent* ev) final override;
virtual void focusOutEvent(QFocusEvent* ev) final override;
virtual void wheelEvent(QWheelEvent *event) final override;

void updateMultiSelectionRect();
void updateTypingNotification();
Expand Down
60 changes: 40 additions & 20 deletions src/widget/form/genericchatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,16 +657,47 @@ void GenericChatForm::loadHistory(const QDateTime &time, const LoadHistoryDialog
chatWidget->clear();
messages.clear();

auto begin = firstItemAfterDate(time.date(), chatLog);
auto end = ChatLogIdx(begin.get() + 1);
if (type == LoadHistoryDialog::from) {
loadHistoryFrom(time);
auto msg = messages.cbegin()->second;
chatWidget->scrollToLine(msg);
} else {
loadHistoryTo(time);
}
}

void GenericChatForm::loadHistoryTo(const QDateTime &time)
{
auto end = ChatLogIdx(0);
if (time.isNull()) {
end = messages.begin()->first;
} else {
end = firstItemAfterDate(time.date(), chatLog);
}

auto begin = ChatLogIdx(0);
if (end.get() > 100) {
begin = ChatLogIdx(end.get() - 100);
}

renderMessages(begin, end);
}

if (type == LoadHistoryDialog::from) {
loadHistoryUpper();
void GenericChatForm::loadHistoryFrom(const QDateTime &time)
{
auto begin = ChatLogIdx(0);
if (time.isNull()) {
begin = messages.rbegin()->first;
} else {
loadHistoryLower();
begin = firstItemAfterDate(time.date(), chatLog);
}

int add = 100;
if (begin.get() + 100 > chatLog.getNextIdx().get()) {
add = chatLog.getNextIdx().get() - (begin.get() + 100);
}
auto end = ChatLogIdx(begin.get() + add);
renderMessages(begin, end);
}


Expand Down Expand Up @@ -1023,25 +1054,14 @@ void GenericChatForm::goToCurrentDate()

void GenericChatForm::loadHistoryLower()
{
auto end = messages.begin()->first;
auto begin = ChatLogIdx(0);
if (end.get() > 100) {
begin = ChatLogIdx(end.get() - 100);
}

renderMessages(begin, end);
loadHistoryTo(QDateTime());
}

void GenericChatForm::loadHistoryUpper()
{
auto begin = messages.rbegin()->first;

int add = 100;
if (begin.get() + 100 > chatLog.getNextIdx().get()) {
add = chatLog.getNextIdx().get() - (begin.get() + 100);
}
auto end = ChatLogIdx(begin.get() + add);
renderMessages(begin, end);
auto msg = messages.crbegin()->second;
loadHistoryFrom(QDateTime());
chatWidget->scrollToLine(msg);
}

void GenericChatForm::updateShowDateInfo(const ChatLine::Ptr& line)
Expand Down
2 changes: 2 additions & 0 deletions src/widget/form/genericchatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ protected slots:
void addSystemDateMessage(const QDate& date);
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);

protected:
ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message,
Expand Down

0 comments on commit c2d5b42

Please sign in to comment.