Make SidebarItem._is_extended a property

This commit is contained in:
RumovZ 2021-03-03 11:43:31 +01:00
parent e2940de4a4
commit 61e61376a2

View file

@ -110,7 +110,7 @@ class SidebarItem:
self.children: List["SidebarItem"] = [] self.children: List["SidebarItem"] = []
self.tooltip: Optional[str] = None self.tooltip: Optional[str] = None
self._parent_item: Optional["SidebarItem"] = None self._parent_item: Optional["SidebarItem"] = None
self._is_expanded = expanded self._expanded = expanded
self._row_in_parent: Optional[int] = None self._row_in_parent: Optional[int] = None
self._search_matches_self = False self._search_matches_self = False
self._search_matches_child = False self._search_matches_child = False
@ -138,10 +138,20 @@ class SidebarItem:
self.add_child(item) self.add_child(item)
return item return item
def is_expanded(self, searching: bool) -> bool: @property
def expanded(self) -> bool:
return self._expanded
@expanded.setter
def expanded(self, expanded: bool) -> None:
if self.expanded != expanded:
self._expanded = expanded
if self.on_expanded:
self.on_expanded(expanded)
def show_expanded(self, searching: bool) -> bool:
if not searching: if not searching:
return self._is_expanded return self.expanded
else:
if self._search_matches_child: if self._search_matches_child:
return True return True
# if search matches top level, expand children one level # if search matches top level, expand children one level
@ -496,7 +506,7 @@ class SidebarTreeView(QTreeView):
continue continue
self._expand_where_necessary(model, idx, searching) self._expand_where_necessary(model, idx, searching)
if item := model.item_for_index(idx): if item := model.item_for_index(idx):
if item.is_expanded(searching): if item.show_expanded(searching):
self.setExpanded(idx, True) self.setExpanded(idx, True)
def update_search( def update_search(
@ -639,19 +649,14 @@ class SidebarTreeView(QTreeView):
def _on_expansion(self, idx: QModelIndex) -> None: def _on_expansion(self, idx: QModelIndex) -> None:
if self.current_search: if self.current_search:
return return
self._on_expand_or_collapse(idx, True) if item := self.model().item_for_index(idx):
item.expanded = True
def _on_collapse(self, idx: QModelIndex) -> None: def _on_collapse(self, idx: QModelIndex) -> None:
if self.current_search: if self.current_search:
return return
self._on_expand_or_collapse(idx, False) if item := self.model().item_for_index(idx):
item.expanded = False
def _on_expand_or_collapse(self, idx: QModelIndex, expanded: bool) -> None:
item = self.model().item_for_index(idx)
if item and item._is_expanded != expanded:
item._is_expanded = expanded
if item.on_expanded:
item.on_expanded(expanded)
# Tree building # Tree building
########################### ###########################