From 33e6662dda7af19e5a311beadb6fe9732053fcdc Mon Sep 17 00:00:00 2001 From: abdo Date: Sun, 27 Jun 2021 11:55:10 +0300 Subject: [PATCH 1/2] Scroll to first sidebar search match --- qt/aqt/browser/sidebar/tree.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/qt/aqt/browser/sidebar/tree.py b/qt/aqt/browser/sidebar/tree.py index 97ab95a23..f10b7aa8e 100644 --- a/qt/aqt/browser/sidebar/tree.py +++ b/qt/aqt/browser/sidebar/tree.py @@ -215,23 +215,35 @@ class SidebarTreeView(QTreeView): # start from a collapsed state, as it's faster self.collapseAll() self.setColumnHidden(0, not self.model().search(text)) - self._expand_where_necessary(self.model(), searching=True) + self._expand_where_necessary( + self.model(), searching=True, scroll_to_first_match=True + ) def _expand_where_necessary( self, model: SidebarModel, parent: Optional[QModelIndex] = None, searching: bool = False, - ) -> None: + scroll_to_first_match: bool = False, + ) -> bool: parent = parent or QModelIndex() for row in range(model.rowCount(parent)): idx = model.index(row, 0, parent) if not idx.isValid(): continue - self._expand_where_necessary(model, idx, searching) + scroll_to_first_match = self._expand_where_necessary( + model, idx, searching, scroll_to_first_match + ) if item := model.item_for_index(idx): if item.show_expanded(searching): self.setExpanded(idx, True) + if item.is_highlighted() and scroll_to_first_match: + self.selectionModel().setCurrentIndex( + idx, QItemSelectionModel.SelectCurrent + ) + self.scrollTo(idx) + scroll_to_first_match = False + return scroll_to_first_match def update_search( self, From ecabf35350efa260c75e473e66a2c60f6e7cca65 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 29 Jun 2021 11:40:59 +1000 Subject: [PATCH 2/2] use inner function instead of return value in _expand_where_necessary() --- qt/aqt/browser/sidebar/tree.py | 42 +++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/qt/aqt/browser/sidebar/tree.py b/qt/aqt/browser/sidebar/tree.py index f10b7aa8e..37fb75ff5 100644 --- a/qt/aqt/browser/sidebar/tree.py +++ b/qt/aqt/browser/sidebar/tree.py @@ -225,25 +225,29 @@ class SidebarTreeView(QTreeView): parent: Optional[QModelIndex] = None, searching: bool = False, scroll_to_first_match: bool = False, - ) -> bool: - parent = parent or QModelIndex() - for row in range(model.rowCount(parent)): - idx = model.index(row, 0, parent) - if not idx.isValid(): - continue - scroll_to_first_match = self._expand_where_necessary( - model, idx, searching, scroll_to_first_match - ) - if item := model.item_for_index(idx): - if item.show_expanded(searching): - self.setExpanded(idx, True) - if item.is_highlighted() and scroll_to_first_match: - self.selectionModel().setCurrentIndex( - idx, QItemSelectionModel.SelectCurrent - ) - self.scrollTo(idx) - scroll_to_first_match = False - return scroll_to_first_match + ) -> None: + def expand_node(parent: QModelIndex) -> None: + nonlocal scroll_to_first_match + + for row in range(model.rowCount(parent)): + idx = model.index(row, 0, parent) + if not idx.isValid(): + continue + + # descend into children first + expand_node(idx) + + if item := model.item_for_index(idx): + if item.show_expanded(searching): + self.setExpanded(idx, True) + if item.is_highlighted() and scroll_to_first_match: + self.selectionModel().setCurrentIndex( + idx, QItemSelectionModel.SelectCurrent + ) + self.scrollTo(idx) + scroll_to_first_match = False + + expand_node(parent or QModelIndex()) def update_search( self,