Switch to new row before deleting notes

The table now properly deselects deleted rows, but that takes effort and
it's more convenient to have a selected row after deleting.
This commit is contained in:
RumovZ 2021-09-25 17:28:38 +02:00
parent f54f15cd44
commit 18f0d026b5
3 changed files with 27 additions and 18 deletions

View file

@ -9,13 +9,7 @@ import aqt
import aqt.forms import aqt.forms
from anki._legacy import deprecated from anki._legacy import deprecated
from anki.cards import Card, CardId from anki.cards import Card, CardId
from anki.collection import ( from anki.collection import Collection, Config, OpChanges, SearchNode
Collection,
Config,
OpChanges,
OpChangesWithCount,
SearchNode,
)
from anki.consts import * from anki.consts import *
from anki.errors import NotFoundError from anki.errors import NotFoundError
from anki.lang import without_unicode_isolation from anki.lang import without_unicode_isolation
@ -60,7 +54,6 @@ from aqt.utils import (
saveState, saveState,
showWarning, showWarning,
skip_if_selection_is_empty, skip_if_selection_is_empty,
tooltip,
tr, tr,
) )
@ -627,16 +620,8 @@ class Browser(QMainWindow):
return return
nids = self.table.get_selected_note_ids() nids = self.table.get_selected_note_ids()
self.table.to_row_of_unselected_note()
def after_remove(changes: OpChangesWithCount) -> None: remove_notes(parent=self, note_ids=nids).run_in_background()
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()
# legacy # legacy

View file

@ -184,6 +184,11 @@ class DataModel(QAbstractTableModel):
def get_note_ids(self, indices: List[QModelIndex]) -> Sequence[NoteId]: def get_note_ids(self, indices: List[QModelIndex]) -> Sequence[NoteId]:
return self._state.get_note_ids(self.get_items(indices)) 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 # Get row numbers from items
def get_item_row(self, item: ItemId) -> Optional[int]: def get_item_row(self, item: ItemId) -> Optional[int]:

View file

@ -207,6 +207,25 @@ class Table:
def to_last_row(self) -> None: def to_last_row(self) -> None:
self._move_current_to_row(self._model.len_rows() - 1) 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: def clear_current(self) -> None:
self._view.selectionModel().setCurrentIndex( self._view.selectionModel().setCurrentIndex(
QModelIndex(), QItemSelectionModel.NoUpdate QModelIndex(), QItemSelectionModel.NoUpdate