diff --git a/qt/aqt/browser/browser.py b/qt/aqt/browser/browser.py index cc74f4990..2cf2c1b19 100644 --- a/qt/aqt/browser/browser.py +++ b/qt/aqt/browser/browser.py @@ -9,13 +9,7 @@ import aqt import aqt.forms from anki._legacy import deprecated from anki.cards import Card, CardId -from anki.collection import ( - Collection, - Config, - OpChanges, - OpChangesWithCount, - SearchNode, -) +from anki.collection import Collection, Config, OpChanges, SearchNode from anki.consts import * from anki.errors import NotFoundError from anki.lang import without_unicode_isolation @@ -60,7 +54,6 @@ from aqt.utils import ( saveState, showWarning, skip_if_selection_is_empty, - tooltip, tr, ) @@ -627,16 +620,8 @@ class Browser(QMainWindow): return nids = self.table.get_selected_note_ids() - - def after_remove(changes: OpChangesWithCount) -> None: - tooltip(tr.browsing_cards_deleted(count=changes.count)) - # select the next card if there is one - self.focusTo = self.editor.currentField - self.table.to_next_row() - - remove_notes(parent=self, note_ids=nids).success( - after_remove - ).run_in_background() + self.table.to_row_of_unselected_note() + remove_notes(parent=self, note_ids=nids).run_in_background() # legacy diff --git a/qt/aqt/browser/table/model.py b/qt/aqt/browser/table/model.py index 22f64b0a4..5cc81f027 100644 --- a/qt/aqt/browser/table/model.py +++ b/qt/aqt/browser/table/model.py @@ -184,6 +184,11 @@ class DataModel(QAbstractTableModel): def get_note_ids(self, indices: List[QModelIndex]) -> Sequence[NoteId]: return self._state.get_note_ids(self.get_items(indices)) + def get_note_id(self, index: QModelIndex) -> Optional[NoteId]: + if nid_list := self._state.get_note_ids([self.get_item(index)]): + return nid_list[0] + return None + # Get row numbers from items def get_item_row(self, item: ItemId) -> Optional[int]: diff --git a/qt/aqt/browser/table/table.py b/qt/aqt/browser/table/table.py index 9a3b33ea2..4d2e1b6f4 100644 --- a/qt/aqt/browser/table/table.py +++ b/qt/aqt/browser/table/table.py @@ -207,6 +207,25 @@ class Table: def to_last_row(self) -> None: self._move_current_to_row(self._model.len_rows() - 1) + def to_row_of_unselected_note(self) -> None: + """Select and set focus to a row whose note is not selected, + starting with the nearest row below, then above the focused row. + If that's not possible, clear selection. + """ + nids = self.get_selected_note_ids() + for row in range(self._current().row(), self.len()): + nid = self._model.get_note_id(self._model.index(row, 0)) + if nid is not None and nid not in nids: + self._move_current_to_row(row) + return + for row in range(self._current().row() - 1, -1, -1): + nid = self._model.get_note_id(self._model.index(row, 0)) + if nid is not None and nid not in nids: + self._move_current_to_row(row) + return + self.clear_selection() + self.clear_current() + def clear_current(self) -> None: self._view.selectionModel().setCurrentIndex( QModelIndex(), QItemSelectionModel.NoUpdate