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

Commit

Permalink
fix(chatlog): update multi line selection on chatlog change
Browse files Browse the repository at this point in the history
Fixes crash due to out of bound access. Fixes selection box jumping on history load.

Fix #5769
  • Loading branch information
anthonybilinski committed Aug 30, 2019
1 parent badef48 commit 2bba121
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/chatlog/chatlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#include <QShortcut>
#include <QTimer>

#include <algorithm>
#include <cassert>

/**
* @var ChatLog::repNameAfter
* @brief repetition interval sender name (sec)
Expand Down Expand Up @@ -454,6 +457,8 @@ void ChatLog::insertChatlinesOnTop(const QList<ChatLine::Ptr>& newLines)

lines = combLines;

moveSelectionRectDownIfMulti(newLines.size());

scene->setItemIndexMethod(oldIndexMeth);

// redo layout
Expand Down Expand Up @@ -722,6 +727,51 @@ void ChatLog::reloadTheme()
}
}

/**
* @brief Adjusts the multi line selection rectangle based on chatlog changing lines
* @param offset Amount to shift selection rect up by. Must be non-negative.
*/
void ChatLog::moveSelectionRectUpIfMulti(int offset)
{
assert(offset >= 0);
if (selectionMode != Multi) {
return;
}
if (selLastRow < offset) { // entire selection now out of bounds
clearSelection();
return;
} else {
selLastRow -= offset;
}
selClickedRow = std::max(0, selClickedRow - offset);
selFirstRow = std::max(0, selFirstRow - offset);
updateMultiSelectionRect();
emit selectionChanged();
}

/**
* @brief Adjusts the multi line selection rectangle based on chatlog changing lines
* @param offset Amount to shift selection rect down by. Must be non-negative.
*/
void ChatLog::moveSelectionRectDownIfMulti(int offset)
{
assert(offset >= 0);
if (selectionMode != Multi) {
return;
}
const int lastLine = lines.size() - 1;
if (selFirstRow + offset > lastLine) { // entire selection now out of bounds
clearSelection();
return;
} else {
selFirstRow += offset;
}
selClickedRow = std::min(lastLine, selClickedRow + offset);
selLastRow = std::min(lastLine, selLastRow + offset);
updateMultiSelectionRect();
emit selectionChanged();
}

void ChatLog::removeFirsts(const int num)
{
if (lines.size() > num) {
Expand All @@ -734,6 +784,8 @@ void ChatLog::removeFirsts(const int num)
for (int i = 0; i < lines.size(); ++i) {
lines[i]->setRow(i);
}

moveSelectionRectUpIfMulti(num);
}

void ChatLog::removeLasts(const int num)
Expand Down Expand Up @@ -950,7 +1002,7 @@ void ChatLog::onWorkerTimeout()
verticalScrollBar()->show();

isScroll = true;
emit workerTimeoutFinished();
emit workerTimeoutFinished();
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/chatlog/chatlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class ChatLog : public QGraphicsView
void selectAll();
void fontChanged(const QFont& font);
void reloadTheme();
void moveSelectionRectUpIfMulti(int offset);
void moveSelectionRectDownIfMulti(int offset);
void removeFirsts(const int num);
void removeLasts(const int num);
void setScroll(const bool scroll);
Expand Down

0 comments on commit 2bba121

Please sign in to comment.