From eac8972a2809d2aac2ff199e1bc9a69ee449bbe1 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Tue, 28 Sep 2021 10:22:20 +0200 Subject: [PATCH] Calculate `len_selection` depending on modifiers If no modifiers are pressed, a single row has probably been clicked and `selectedRows()` is fast, while a lot of rows might have been deselcted. --- qt/aqt/browser/table/table.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/qt/aqt/browser/table/table.py b/qt/aqt/browser/table/table.py index 436429049..cce83de90 100644 --- a/qt/aqt/browser/table/table.py +++ b/qt/aqt/browser/table/table.py @@ -355,8 +355,17 @@ class Table: def _on_selection_changed( self, selected: QItemSelection, deselected: QItemSelection ) -> None: - self._len_selection += len(selected.indexes()) // self._model.len_columns() - self._len_selection -= len(deselected.indexes()) // self._model.len_columns() + # `selection.indexes()` calls `flags()` for all the selection's indexes, + # whereas `selectedRows()` calls it for the indexes of the resulting selection. + # Both may be slow, so we try to optimise. + if KeyboardModifiersPressed().shift or KeyboardModifiersPressed().control: + # Current selection is modified. The number of added/removed rows is + # usually smaller than the number of rows in the resulting selection. + self._len_selection += len(selected.indexes()) // self._model.len_columns() + self._len_selection -= len(deselected.indexes()) // self._model.len_columns() + else: + # New selection is created. Usually a single row or none at all. + self._len_selection = len(self._view.selectionModel().selectedRows()) self.browser.on_row_changed() def _on_row_state_will_change(self, index: QModelIndex, was_restored: bool) -> None: