Prefer selectionModel().reset() over .clear()

The latter triggers `selectionChanged()` unreliably, probably due to the
aggregation of chronologically close events, causing problems in
tracking `_len_selection`. `reset()` never emits signals.
This commit is contained in:
RumovZ 2021-09-28 10:19:33 +02:00
parent f89811870e
commit 287854d014

View File

@ -132,10 +132,12 @@ class Table:
def select_single_card(self, card_id: CardId) -> None: def select_single_card(self, card_id: CardId) -> None:
"""Try to set the selection to the item corresponding to the given card.""" """Try to set the selection to the item corresponding to the given card."""
self.clear_selection() self._reset_selection()
if (row := self._model.get_card_row(card_id)) is not None: if (row := self._model.get_card_row(card_id)) is not None:
self._view.selectRow(row) self._view.selectRow(row)
self._scroll_to_row(row, scroll_even_if_visible=True) self._scroll_to_row(row, scroll_even_if_visible=True)
else:
self.browser.on_row_changed()
# Reset # Reset
@ -223,8 +225,8 @@ class Table:
if nid is not None and nid not in nids: if nid is not None and nid not in nids:
self._move_current_to_row(row) self._move_current_to_row(row)
return return
self.clear_selection() self._reset_selection()
self.clear_current() self.browser.on_row_changed()
def clear_current(self) -> None: def clear_current(self) -> None:
self._view.selectionModel().setCurrentIndex( self._view.selectionModel().setCurrentIndex(
@ -248,6 +250,11 @@ class Table:
) )
self._view.selectionModel().setCurrentIndex(index, QItemSelectionModel.NoUpdate) self._view.selectionModel().setCurrentIndex(index, QItemSelectionModel.NoUpdate)
def _reset_selection(self) -> None:
"""Remove selection and focus without emitting signals."""
self._view.selectionModel().reset()
self._len_selection = 0
def _select_rows(self, rows: List[int]) -> None: def _select_rows(self, rows: List[int]) -> None:
selection = QItemSelection() selection = QItemSelection()
for row in rows: for row in rows:
@ -470,7 +477,7 @@ class Table:
"""Restore the saved selection and current element as far as possible and scroll to the """Restore the saved selection and current element as far as possible and scroll to the
new current element. Clear the saved selection. new current element. Clear the saved selection.
""" """
self.clear_selection() self._reset_selection()
if not self._model.is_empty(): if not self._model.is_empty():
rows, current = new_selected_and_current() rows, current = new_selected_and_current()
rows = self._qualify_selected_rows(rows, current) rows = self._qualify_selected_rows(rows, current)