From af9eb6d6b118ff2575b891b38a73dc0776960d58 Mon Sep 17 00:00:00 2001 From: hollow-owl <71401395+hollow-owl@users.noreply.github.com> Date: Tue, 17 Aug 2021 00:00:41 -0500 Subject: [PATCH] Select entry above deleted entry * Fix #6304 - Don't cause the list view to scroll to the top when deleting any entry in the list. --- src/gui/DatabaseWidget.cpp | 21 ++++++++++++++++++--- src/gui/entry/EntryView.cpp | 5 +++++ src/gui/entry/EntryView.h | 1 + 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 58f6f2f8d..86dbea417 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -462,8 +462,16 @@ void DatabaseWidget::deleteSelectedEntries() void DatabaseWidget::deleteEntries(QList selectedEntries, bool confirm) { + if (selectedEntries.isEmpty()) { + return; + } + + // Find the index above the first entry for selection after deletion + auto index = m_entryView->indexFromEntry(selectedEntries.first()); + index = m_entryView->indexAbove(index); + // Confirm entry removal before moving forward - auto* recycleBin = m_db->metadata()->recycleBin(); + auto recycleBin = m_db->metadata()->recycleBin(); bool permanent = (recycleBin && recycleBin->findEntryByUuid(selectedEntries.first()->uuid())) || !m_db->metadata()->recycleBinEnabled(); @@ -475,8 +483,15 @@ void DatabaseWidget::deleteEntries(QList selectedEntries, bool confirm) refreshSearch(); - m_entryView->setFirstEntryActive(); - auto* currentEntry = currentSelectedEntry(); + // Select the row above the deleted entries + if (index.isValid()) { + m_entryView->setCurrentIndex(index); + } else { + m_entryView->setFirstEntryActive(); + } + + // Update the preview widget + auto currentEntry = currentSelectedEntry(); if (currentEntry) { m_previewView->setEntry(currentEntry); } else { diff --git a/src/gui/entry/EntryView.cpp b/src/gui/entry/EntryView.cpp index 1e1995581..fa1deb6d5 100644 --- a/src/gui/entry/EntryView.cpp +++ b/src/gui/entry/EntryView.cpp @@ -289,6 +289,11 @@ Entry* EntryView::entryFromIndex(const QModelIndex& index) } } +QModelIndex EntryView::indexFromEntry(Entry* entry) +{ + return m_sortModel->mapFromSource(m_model->indexFromEntry(entry)); +} + int EntryView::currentEntryIndex() { QModelIndexList list = selectionModel()->selectedRows(); diff --git a/src/gui/entry/EntryView.h b/src/gui/entry/EntryView.h index 4608087fd..c61fa6884 100644 --- a/src/gui/entry/EntryView.h +++ b/src/gui/entry/EntryView.h @@ -39,6 +39,7 @@ public: Entry* currentEntry(); void setCurrentEntry(Entry* entry); Entry* entryFromIndex(const QModelIndex& index); + QModelIndex indexFromEntry(Entry* entry); int currentEntryIndex(); bool inSearchMode(); bool isSorted();