mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
parent
a56b09b987
commit
c96248d67f
1 changed files with 29 additions and 1 deletions
|
@ -223,6 +223,21 @@ class FilterModel(QSortFilterProxyModel):
|
||||||
return None
|
return None
|
||||||
return self.mapToSource(idx).internalPointer()
|
return self.mapToSource(idx).internalPointer()
|
||||||
|
|
||||||
|
def _anyParentMatches(self, item: SidebarItem) -> bool:
|
||||||
|
if not item.parentItem:
|
||||||
|
return False
|
||||||
|
if self.parent().current_search.lower() in item.parentItem.name.lower():
|
||||||
|
return True
|
||||||
|
return self._anyParentMatches(item.parentItem)
|
||||||
|
|
||||||
|
def filterAcceptsRow(self, row: int, parent: QModelIndex) -> bool:
|
||||||
|
current_search = self.parent().current_search
|
||||||
|
if not current_search:
|
||||||
|
return False
|
||||||
|
current_search = current_search.lower()
|
||||||
|
item = self.sourceModel().index(row, 0, parent).internalPointer()
|
||||||
|
return current_search in item.name.lower() or self._anyParentMatches(item)
|
||||||
|
|
||||||
|
|
||||||
class SidebarSearchBar(QLineEdit):
|
class SidebarSearchBar(QLineEdit):
|
||||||
def __init__(self, sidebar: SidebarTreeView) -> None:
|
def __init__(self, sidebar: SidebarTreeView) -> None:
|
||||||
|
@ -339,7 +354,20 @@ class SidebarTreeView(QTreeView):
|
||||||
# a better way than this?
|
# a better way than this?
|
||||||
self.collapseAll()
|
self.collapseAll()
|
||||||
filter_model.setFilterFixedString(text)
|
filter_model.setFilterFixedString(text)
|
||||||
self.expandAll()
|
self.expandMatches(self.rootIndex())
|
||||||
|
|
||||||
|
def expandMatches(self, parent: QModelIndex) -> bool:
|
||||||
|
"Expand match trees one level."
|
||||||
|
expand = False
|
||||||
|
for i in range(self.model().rowCount(parent)):
|
||||||
|
idx = self.model().index(i, 0, parent)
|
||||||
|
item = self.model().item_for_index(idx)
|
||||||
|
expandChild = self.expandMatches(idx) or (
|
||||||
|
bool(item) and self.current_search.lower() in item.name.lower()
|
||||||
|
)
|
||||||
|
expand |= expandChild
|
||||||
|
self.setExpanded(idx, expandChild)
|
||||||
|
return expand
|
||||||
|
|
||||||
def drawRow(
|
def drawRow(
|
||||||
self, painter: QPainter, options: QStyleOptionViewItem, idx: QModelIndex
|
self, painter: QPainter, options: QStyleOptionViewItem, idx: QModelIndex
|
||||||
|
|
Loading…
Reference in a new issue