1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

fix(chatlog): update precise selection when chatlog content changes

Fix #5769. Precise selections (within one chatline) need to be invalidated when
the line they are apart of is removed from the chatlog.
This commit is contained in:
Anthony Bilinski 2019-09-28 17:01:48 -07:00
parent 08b2c848e2
commit b95bac238d
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
2 changed files with 104 additions and 49 deletions

View File

@ -457,7 +457,7 @@ void ChatLog::insertChatlinesOnTop(const QList<ChatLine::Ptr>& newLines)
lines = combLines; lines = combLines;
moveSelectionRectDownIfMulti(newLines.size()); moveSelectionRectDownIfSelected(newLines.size());
scene->setItemIndexMethod(oldIndexMeth); scene->setItemIndexMethod(oldIndexMeth);
@ -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) void ChatLog::removeFirsts(const int num)
{ {
if (lines.size() > num) { if (lines.size() > num) {
@ -785,7 +740,7 @@ void ChatLog::removeFirsts(const int num)
lines[i]->setRow(i); lines[i]->setRow(i);
} }
moveSelectionRectUpIfMulti(num); moveSelectionRectUpIfSelected(num);
} }
void ChatLog::removeLasts(const int num) void ChatLog::removeLasts(const int num)
@ -1100,3 +1055,99 @@ bool ChatLog::isActiveFileTransfer(ChatLine::Ptr l)
return false; 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();
}
}

View File

@ -58,8 +58,6 @@ public:
void selectAll(); void selectAll();
void fontChanged(const QFont& font); void fontChanged(const QFont& font);
void reloadTheme(); void reloadTheme();
void moveSelectionRectUpIfMulti(int offset);
void moveSelectionRectDownIfMulti(int offset);
void removeFirsts(const int num); void removeFirsts(const int num);
void removeLasts(const int num); void removeLasts(const int num);
void setScroll(const bool scroll); void setScroll(const bool scroll);
@ -130,6 +128,12 @@ private:
void retranslateUi(); void retranslateUi();
bool isActiveFileTransfer(ChatLine::Ptr l); bool isActiveFileTransfer(ChatLine::Ptr l);
void handleMultiClickEvent(); 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: private:
enum class SelectionMode enum class SelectionMode