Handle search on event level

Instead of assigning each sidebar item a lambda, add a field for search
representation and handle searching in event handler.
This commit is contained in:
RumovZ 2021-02-25 19:57:12 +01:00
parent 47e1e62967
commit 172133299b

View file

@ -81,6 +81,7 @@ class SidebarItem:
name: str, name: str,
icon: Union[str, ColoredIcon], icon: Union[str, ColoredIcon],
on_click: Callable[[], None] = None, on_click: Callable[[], None] = None,
search_node: Optional[SearchNode] = None,
on_expanded: Callable[[bool], None] = None, on_expanded: Callable[[bool], None] = None,
expanded: bool = False, expanded: bool = False,
item_type: SidebarItemType = SidebarItemType.CUSTOM, item_type: SidebarItemType = SidebarItemType.CUSTOM,
@ -95,6 +96,7 @@ class SidebarItem:
self.item_type = item_type self.item_type = item_type
self.id = id self.id = id
self.on_click = on_click self.on_click = on_click
self.search_node = search_node
self.on_expanded = on_expanded self.on_expanded = on_expanded
self.children: List["SidebarItem"] = [] self.children: List["SidebarItem"] = []
self.tooltip: Optional[str] = None self.tooltip: Optional[str] = None
@ -113,7 +115,7 @@ class SidebarItem:
name: Union[str, TR.V], name: Union[str, TR.V],
icon: Union[str, ColoredIcon], icon: Union[str, ColoredIcon],
type: SidebarItemType, type: SidebarItemType,
on_click: Callable[[], None], search_node: Optional[SearchNode],
) -> SidebarItem: ) -> SidebarItem:
"Add child sidebar item, and return it." "Add child sidebar item, and return it."
if not isinstance(name, str): if not isinstance(name, str):
@ -121,7 +123,7 @@ class SidebarItem:
item = SidebarItem( item = SidebarItem(
name=name, name=name,
icon=icon, icon=icon,
on_click=on_click, search_node=search_node,
item_type=type, item_type=type,
) )
self.add_child(item) self.add_child(item)
@ -575,6 +577,8 @@ class SidebarTreeView(QTreeView):
if item := self.model().item_for_index(idx): if item := self.model().item_for_index(idx):
if item.on_click: if item.on_click:
item.on_click() item.on_click()
elif self.tool == SidebarTool.SEARCH and (search := item.search_node):
self.update_search(search)
def _on_expansion(self, idx: QModelIndex) -> None: def _on_expansion(self, idx: QModelIndex) -> None:
if self.current_search: if self.current_search:
@ -651,9 +655,6 @@ class SidebarTreeView(QTreeView):
return top return top
def _filter_func(self, *terms: Union[str, SearchNode]) -> Callable:
return lambda: self.update_search(*terms)
# Tree: Saved Searches # Tree: Saved Searches
########################### ###########################
@ -678,7 +679,7 @@ class SidebarTreeView(QTreeView):
item = SidebarItem( item = SidebarItem(
name, name,
icon, icon,
self._filter_func(filt), search_node=SearchNode(parsable_text=filt),
item_type=SidebarItemType.SAVED_SEARCH, item_type=SidebarItemType.SAVED_SEARCH,
) )
root.add_child(item) root.add_child(item)
@ -696,47 +697,42 @@ class SidebarTreeView(QTreeView):
type=SidebarItemType.TODAY_ROOT, type=SidebarItemType.TODAY_ROOT,
) )
type = SidebarItemType.TODAY type = SidebarItemType.TODAY
search = self._filter_func
root.add_simple( root.add_simple(
name=TR.BROWSING_SIDEBAR_DUE_TODAY, name=TR.BROWSING_SIDEBAR_DUE_TODAY,
icon=icon, icon=icon,
type=type, type=type,
on_click=search(SearchNode(due_on_day=0)), search_node=SearchNode(due_on_day=0),
) )
root.add_simple( root.add_simple(
name=TR.BROWSING_ADDED_TODAY, name=TR.BROWSING_ADDED_TODAY,
icon=icon, icon=icon,
type=type, type=type,
on_click=search(SearchNode(added_in_days=1)), search_node=SearchNode(added_in_days=1),
) )
root.add_simple( root.add_simple(
name=TR.BROWSING_EDITED_TODAY, name=TR.BROWSING_EDITED_TODAY,
icon=icon, icon=icon,
type=type, type=type,
on_click=search(SearchNode(edited_in_days=1)), search_node=SearchNode(edited_in_days=1),
) )
root.add_simple( root.add_simple(
name=TR.BROWSING_STUDIED_TODAY, name=TR.BROWSING_STUDIED_TODAY,
icon=icon, icon=icon,
type=type, type=type,
on_click=search(SearchNode(rated=SearchNode.Rated(days=1))), search_node=SearchNode(rated=SearchNode.Rated(days=1)),
) )
root.add_simple( root.add_simple(
name=TR.BROWSING_AGAIN_TODAY, name=TR.BROWSING_AGAIN_TODAY,
icon=icon, icon=icon,
type=type, type=type,
on_click=search( search_node=SearchNode(rated=SearchNode.Rated(days=1, rating=SearchNode.RATING_AGAIN)),
SearchNode(
rated=SearchNode.Rated(days=1, rating=SearchNode.RATING_AGAIN)
)
),
) )
root.add_simple( root.add_simple(
name=TR.BROWSING_SIDEBAR_OVERDUE, name=TR.BROWSING_SIDEBAR_OVERDUE,
icon=icon, icon=icon,
type=type, type=type,
on_click=search( search_node=self.col.group_searches(
SearchNode(card_state=SearchNode.CARD_STATE_DUE), SearchNode(card_state=SearchNode.CARD_STATE_DUE),
SearchNode(negated=SearchNode(due_on_day=0)), SearchNode(negated=SearchNode(due_on_day=0)),
), ),
@ -755,38 +751,37 @@ class SidebarTreeView(QTreeView):
type=SidebarItemType.CARD_STATE_ROOT, type=SidebarItemType.CARD_STATE_ROOT,
) )
type = SidebarItemType.CARD_STATE type = SidebarItemType.CARD_STATE
search = self._filter_func
root.add_simple( root.add_simple(
TR.ACTIONS_NEW, TR.ACTIONS_NEW,
icon=icon.with_color(colors.NEW_COUNT), icon=icon.with_color(colors.NEW_COUNT),
type=type, type=type,
on_click=search(SearchNode(card_state=SearchNode.CARD_STATE_NEW)), search_node=SearchNode(card_state=SearchNode.CARD_STATE_NEW),
) )
root.add_simple( root.add_simple(
name=TR.SCHEDULING_LEARNING, name=TR.SCHEDULING_LEARNING,
icon=icon.with_color(colors.LEARN_COUNT), icon=icon.with_color(colors.LEARN_COUNT),
type=type, type=type,
on_click=search(SearchNode(card_state=SearchNode.CARD_STATE_LEARN)), search_node=SearchNode(card_state=SearchNode.CARD_STATE_LEARN),
) )
root.add_simple( root.add_simple(
name=TR.SCHEDULING_REVIEW, name=TR.SCHEDULING_REVIEW,
icon=icon.with_color(colors.REVIEW_COUNT), icon=icon.with_color(colors.REVIEW_COUNT),
type=type, type=type,
on_click=search(SearchNode(card_state=SearchNode.CARD_STATE_REVIEW)), search_node=SearchNode(card_state=SearchNode.CARD_STATE_REVIEW),
) )
root.add_simple( root.add_simple(
name=TR.BROWSING_SUSPENDED, name=TR.BROWSING_SUSPENDED,
icon=icon.with_color(colors.SUSPENDED_FG), icon=icon.with_color(colors.SUSPENDED_FG),
type=type, type=type,
on_click=search(SearchNode(card_state=SearchNode.CARD_STATE_SUSPENDED)), search_node=SearchNode(card_state=SearchNode.CARD_STATE_SUSPENDED),
) )
root.add_simple( root.add_simple(
name=TR.BROWSING_BURIED, name=TR.BROWSING_BURIED,
icon=icon.with_color(colors.BURIED_FG), icon=icon.with_color(colors.BURIED_FG),
type=type, type=type,
on_click=search(SearchNode(card_state=SearchNode.CARD_STATE_BURIED)), search_node=SearchNode(card_state=SearchNode.CARD_STATE_BURIED),
) )
# Tree: Flags # Tree: Flags
@ -794,7 +789,6 @@ class SidebarTreeView(QTreeView):
def _flags_tree(self, root: SidebarItem) -> None: def _flags_tree(self, root: SidebarItem) -> None:
icon = ColoredIcon(path=":/icons/flag.svg", color=colors.DISABLED) icon = ColoredIcon(path=":/icons/flag.svg", color=colors.DISABLED)
search = self._filter_func
root = self._section_root( root = self._section_root(
root=root, root=root,
name=TR.BROWSING_SIDEBAR_FLAGS, name=TR.BROWSING_SIDEBAR_FLAGS,
@ -802,38 +796,38 @@ class SidebarTreeView(QTreeView):
collapse_key=Config.Bool.COLLAPSE_FLAGS, collapse_key=Config.Bool.COLLAPSE_FLAGS,
type=SidebarItemType.FLAG_ROOT, type=SidebarItemType.FLAG_ROOT,
) )
root.on_click = search(SearchNode(flag=SearchNode.FLAG_ANY)) root.search_node = SearchNode(flag=SearchNode.FLAG_ANY)
type = SidebarItemType.FLAG type = SidebarItemType.FLAG
root.add_simple( root.add_simple(
TR.ACTIONS_RED_FLAG, TR.ACTIONS_RED_FLAG,
icon=icon.with_color(colors.FLAG1_FG), icon=icon.with_color(colors.FLAG1_FG),
type=type, type=type,
on_click=search(SearchNode(flag=SearchNode.FLAG_RED)), search_node=SearchNode(flag=SearchNode.FLAG_RED),
) )
root.add_simple( root.add_simple(
TR.ACTIONS_ORANGE_FLAG, TR.ACTIONS_ORANGE_FLAG,
icon=icon.with_color(colors.FLAG2_FG), icon=icon.with_color(colors.FLAG2_FG),
type=type, type=type,
on_click=search(SearchNode(flag=SearchNode.FLAG_ORANGE)), search_node=SearchNode(flag=SearchNode.FLAG_ORANGE),
) )
root.add_simple( root.add_simple(
TR.ACTIONS_GREEN_FLAG, TR.ACTIONS_GREEN_FLAG,
icon=icon.with_color(colors.FLAG3_FG), icon=icon.with_color(colors.FLAG3_FG),
type=type, type=type,
on_click=search(SearchNode(flag=SearchNode.FLAG_GREEN)), search_node=SearchNode(flag=SearchNode.FLAG_GREEN),
) )
root.add_simple( root.add_simple(
TR.ACTIONS_BLUE_FLAG, TR.ACTIONS_BLUE_FLAG,
icon=icon.with_color(colors.FLAG4_FG), icon=icon.with_color(colors.FLAG4_FG),
type=type, type=type,
on_click=search(SearchNode(flag=SearchNode.FLAG_BLUE)), search_node=SearchNode(flag=SearchNode.FLAG_BLUE),
) )
root.add_simple( root.add_simple(
TR.BROWSING_NO_FLAG, TR.BROWSING_NO_FLAG,
icon=icon.with_color(colors.DISABLED), icon=icon.with_color(colors.DISABLED),
type=type, type=type,
on_click=search(SearchNode(flag=SearchNode.FLAG_NONE)), search_node=SearchNode(flag=SearchNode.FLAG_NONE),
) )
# Tree: Tags # Tree: Tags
@ -854,11 +848,11 @@ class SidebarTreeView(QTreeView):
) )
item = SidebarItem( item = SidebarItem(
node.name, name=node.name,
icon, icon=icon,
self._filter_func(SearchNode(tag=head + node.name)), search_node=SearchNode(tag=head + node.name),
toggle_expand(), on_expanded=toggle_expand(),
node.expanded, expanded=node.expanded,
item_type=SidebarItemType.TAG, item_type=SidebarItemType.TAG,
full_name=head + node.name, full_name=head + node.name,
) )
@ -874,12 +868,12 @@ class SidebarTreeView(QTreeView):
collapse_key=Config.Bool.COLLAPSE_TAGS, collapse_key=Config.Bool.COLLAPSE_TAGS,
type=SidebarItemType.TAG_ROOT, type=SidebarItemType.TAG_ROOT,
) )
root.on_click = self._filter_func(SearchNode(negated=SearchNode(tag="none"))) root.search_node = SearchNode(negated=SearchNode(tag="none"))
root.add_simple( root.add_simple(
name=tr(TR.BROWSING_SIDEBAR_UNTAGGED), name=tr(TR.BROWSING_SIDEBAR_UNTAGGED),
icon=icon, icon=icon,
type=SidebarItemType.TAG_NONE, type=SidebarItemType.TAG_NONE,
on_click=self._filter_func(SearchNode(tag="none")), search_node=SearchNode(tag="none"),
) )
render(root, tree.children) render(root, tree.children)
@ -900,11 +894,11 @@ class SidebarTreeView(QTreeView):
return lambda _: self.mw.col.decks.collapseBrowser(did) return lambda _: self.mw.col.decks.collapseBrowser(did)
item = SidebarItem( item = SidebarItem(
node.name, name=node.name,
icon, icon=icon,
self._filter_func(SearchNode(deck=head + node.name)), search_node=SearchNode(deck=head + node.name),
toggle_expand(), on_expanded=toggle_expand(),
not node.collapsed, expanded=not node.collapsed,
item_type=SidebarItemType.DECK, item_type=SidebarItemType.DECK,
id=node.deck_id, id=node.deck_id,
full_name=head + node.name, full_name=head + node.name,
@ -921,12 +915,12 @@ class SidebarTreeView(QTreeView):
collapse_key=Config.Bool.COLLAPSE_DECKS, collapse_key=Config.Bool.COLLAPSE_DECKS,
type=SidebarItemType.DECK_ROOT, type=SidebarItemType.DECK_ROOT,
) )
root.on_click = self._filter_func(SearchNode(deck="*")) root.search_node = SearchNode(deck="*")
current = root.add_simple( current = root.add_simple(
name=tr(TR.BROWSING_CURRENT_DECK), name=tr(TR.BROWSING_CURRENT_DECK),
icon=icon, icon=icon,
type=SidebarItemType.DECK, type=SidebarItemType.DECK,
on_click=self._filter_func(SearchNode(deck="current")), search_node=SearchNode(deck="current"),
) )
current.id = self.mw.col.decks.selected() current.id = self.mw.col.decks.selected()
@ -949,7 +943,7 @@ class SidebarTreeView(QTreeView):
item = SidebarItem( item = SidebarItem(
nt["name"], nt["name"],
icon, icon,
self._filter_func(SearchNode(note=nt["name"])), search_node=SearchNode(note=nt["name"]),
item_type=SidebarItemType.NOTETYPE, item_type=SidebarItemType.NOTETYPE,
id=nt["id"], id=nt["id"],
) )
@ -958,7 +952,7 @@ class SidebarTreeView(QTreeView):
child = SidebarItem( child = SidebarItem(
tmpl["name"], tmpl["name"],
icon, icon,
self._filter_func( search_node=self.col.group_searches(
SearchNode(note=nt["name"]), SearchNode(template=c) SearchNode(note=nt["name"]), SearchNode(template=c)
), ),
item_type=SidebarItemType.NOTETYPE_TEMPLATE, item_type=SidebarItemType.NOTETYPE_TEMPLATE,