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 precise selection when chatlog content changes
Browse files Browse the repository at this point in the history
Fix #5769. Precise selections (within one chatline) need to be invalidated when
the line they are apart of is removed from the chatlog.
  • Loading branch information
anthonybilinski committed Sep 29, 2019
1 parent 08b2c84 commit b95bac2
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 49 deletions.
145 changes: 98 additions & 47 deletions src/chatlog/chatlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ void ChatLog::insertChatlinesOnTop(const QList<ChatLine::Ptr>& newLines)

lines = combLines;

moveSelectionRectDownIfMulti(newLines.size());
moveSelectionRectDownIfSelected(newLines.size());

scene->setItemIndexMethod(oldIndexMeth);

Expand Down Expand Up @@ -727,51 +727,6 @@ 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 != 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 != 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 @@ -785,7 +740,7 @@ void ChatLog::removeFirsts(const int num)
lines[i]->setRow(i);
}

moveSelectionRectUpIfMulti(num);
moveSelectionRectUpIfSelected(num);
}

void ChatLog::removeLasts(const int num)
Expand Down Expand Up @@ -1100,3 +1055,99 @@ bool ChatLog::isActiveFileTransfer(ChatLine::Ptr l)

return false;
}

/**
* @brief Adjusts the selection based on chatlog changing lines
* @param offset Amount to shift selection rect up by. Must be non-negative.
*/
void ChatLog::moveSelectionRectUpIfSelected(int offset)
{
assert(offset >= 0);
switch (selectionMode)
{
case SelectionMode::None:
return;
case SelectionMode::Precise:
movePreciseSelectionUp(offset);
break;
case SelectionMode::Multi:
moveMultiSelectionUp(offset);
break;
}
}

/**
* @brief Adjusts the selections based on chatlog changing lines
* @param offset removed from the lines indexes. Must be non-negative.
*/
void ChatLog::moveSelectionRectDownIfSelected(int offset)
{
assert(offset >= 0);
switch (selectionMode)
{
case SelectionMode::None:
return;
case SelectionMode::Precise:
movePreciseSelectionDown(offset);
break;
case SelectionMode::Multi:
moveMultiSelectionDown(offset);
break;
}
}

void ChatLog::movePreciseSelectionDown(int offset)
{
assert(selFirstRow == selLastRow && selFirstRow == selClickedRow);
const int lastLine = lines.size() - 1;
if (selClickedRow + offset > lastLine) {
clearSelection();
} else {
const int newRow = selClickedRow + offset;
selClickedRow = newRow;
selLastRow = newRow;
selFirstRow = newRow;
emit selectionChanged();
}
}

void ChatLog::movePreciseSelectionUp(int offset)
{
assert(selFirstRow == selLastRow && selFirstRow == selClickedRow);
if (selClickedRow < offset) {
clearSelection();
} else {
const int newRow = selClickedRow - offset;
selClickedRow = newRow;
selLastRow = newRow;
selFirstRow = newRow;
emit selectionChanged();
}
}

void ChatLog::moveMultiSelectionUp(int offset)
{
if (selLastRow < offset) { // entire selection now out of bounds
clearSelection();
} else {
selLastRow -= offset;
selClickedRow = std::max(0, selClickedRow - offset);
selFirstRow = std::max(0, selFirstRow - offset);
updateMultiSelectionRect();
emit selectionChanged();
}
}

void ChatLog::moveMultiSelectionDown(int offset)
{
const int lastLine = lines.size() - 1;
if (selFirstRow + offset > lastLine) { // entire selection now out of bounds
clearSelection();
} else {
selFirstRow += offset;
selClickedRow = std::min(lastLine, selClickedRow + offset);
selLastRow = std::min(lastLine, selLastRow + offset);
updateMultiSelectionRect();
emit selectionChanged();
}
}
8 changes: 6 additions & 2 deletions src/chatlog/chatlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ 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 Expand Up @@ -130,6 +128,12 @@ private slots:
void retranslateUi();
bool isActiveFileTransfer(ChatLine::Ptr l);
void handleMultiClickEvent();
void moveSelectionRectUpIfSelected(int offset);
void moveSelectionRectDownIfSelected(int offset);
void movePreciseSelectionDown(int offset);
void movePreciseSelectionUp(int offset);
void moveMultiSelectionUp(int offset);
void moveMultiSelectionDown(int offset);

private:
enum class SelectionMode
Expand Down

0 comments on commit b95bac2

Please sign in to comment.