Scroll to first sidebar search match

This commit is contained in:
abdo 2021-06-27 11:55:10 +03:00
parent 1b15069b24
commit 33e6662dda

View File

@ -215,23 +215,35 @@ class SidebarTreeView(QTreeView):
# start from a collapsed state, as it's faster # start from a collapsed state, as it's faster
self.collapseAll() self.collapseAll()
self.setColumnHidden(0, not self.model().search(text)) 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( def _expand_where_necessary(
self, self,
model: SidebarModel, model: SidebarModel,
parent: Optional[QModelIndex] = None, parent: Optional[QModelIndex] = None,
searching: bool = False, searching: bool = False,
) -> None: scroll_to_first_match: bool = False,
) -> bool:
parent = parent or QModelIndex() parent = parent or QModelIndex()
for row in range(model.rowCount(parent)): for row in range(model.rowCount(parent)):
idx = model.index(row, 0, parent) idx = model.index(row, 0, parent)
if not idx.isValid(): if not idx.isValid():
continue 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 := model.item_for_index(idx):
if item.show_expanded(searching): if item.show_expanded(searching):
self.setExpanded(idx, True) 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( def update_search(
self, self,