From 9816227b5caa86d7edbcaec7b9697aefbfead2a7 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Tue, 18 May 2021 22:08:36 +0200 Subject: [PATCH] Make sidebar flags renamable --- qt/aqt/browser/sidebar/item.py | 1 + qt/aqt/browser/sidebar/tree.py | 61 ++++++++++++++++++---------------- qt/aqt/utils.py | 33 +++++++++++++++++- 3 files changed, 66 insertions(+), 29 deletions(-) diff --git a/qt/aqt/browser/sidebar/item.py b/qt/aqt/browser/sidebar/item.py index 4c38eece2..d38e8a7c4 100644 --- a/qt/aqt/browser/sidebar/item.py +++ b/qt/aqt/browser/sidebar/item.py @@ -40,6 +40,7 @@ class SidebarItemType(Enum): def is_editable(self) -> bool: return self in ( + SidebarItemType.FLAG, SidebarItemType.SAVED_SEARCH, SidebarItemType.DECK, SidebarItemType.TAG, diff --git a/qt/aqt/browser/sidebar/tree.py b/qt/aqt/browser/sidebar/tree.py index 407faadef..573979938 100644 --- a/qt/aqt/browser/sidebar/tree.py +++ b/qt/aqt/browser/sidebar/tree.py @@ -35,7 +35,14 @@ from aqt.operations.tag import ( ) from aqt.qt import * from aqt.theme import ColoredIcon, theme_manager -from aqt.utils import KeyboardModifiersPressed, askUser, getOnlyText, showWarning, tr +from aqt.utils import ( + KeyboardModifiersPressed, + askUser, + getOnlyText, + load_flags, + showWarning, + tr, +) class SidebarStage(Enum): @@ -361,6 +368,8 @@ class SidebarTreeView(QTreeView): self.rename_saved_search(item, new_name) elif item.item_type == SidebarItemType.TAG: self.rename_tag(item, new_name) + elif item.item_type == SidebarItemType.FLAG: + self.rename_flag(item, new_name) # renaming may be asynchronous so always return False return False @@ -600,35 +609,21 @@ class SidebarTreeView(QTreeView): ) root.search_node = SearchNode(flag=SearchNode.FLAG_ANY) - type = SidebarItemType.FLAG - root.add_simple( - tr.actions_red_flag(), - icon=icon.with_color(colors.FLAG1_FG), - type=type, - search_node=SearchNode(flag=SearchNode.FLAG_RED), - ) - root.add_simple( - tr.actions_orange_flag(), - icon=icon.with_color(colors.FLAG2_FG), - type=type, - search_node=SearchNode(flag=SearchNode.FLAG_ORANGE), - ) - root.add_simple( - tr.actions_green_flag(), - icon=icon.with_color(colors.FLAG3_FG), - type=type, - search_node=SearchNode(flag=SearchNode.FLAG_GREEN), - ) - root.add_simple( - tr.actions_blue_flag(), - icon=icon.with_color(colors.FLAG4_FG), - type=type, - search_node=SearchNode(flag=SearchNode.FLAG_BLUE), - ) + for index, flag in enumerate(load_flags(self.col)): + root.add_child( + SidebarItem( + name=flag[0], + icon=flag[1], + search_node=flag[2], + item_type=SidebarItemType.FLAG, + id=index + 1, + ) + ) + root.add_simple( tr.browsing_no_flag(), - icon=icon.with_color(colors.DISABLED), - type=type, + icon=icon, + type=SidebarItemType.FLAG, search_node=SearchNode(flag=SearchNode.FLAG_NONE), ) @@ -872,6 +867,16 @@ class SidebarTreeView(QTreeView): lambda: set_children_expanded(False), ) + # Flags + ########################### + + def rename_flag(self, item: SidebarItem, new_name: str) -> None: + labels = self.col.get_config("flagLabels", {}) + labels[str(item.id)] = new_name + self.col.set_config("flagLabels", labels) + item.name = new_name + self.refresh() + # Decks ########################### diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 7533e4566..7a96d8c6c 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -12,6 +12,7 @@ from typing import ( TYPE_CHECKING, Any, Callable, + Dict, List, Literal, Optional, @@ -35,10 +36,12 @@ from PyQt5.QtWidgets import ( import aqt from anki import Collection +from anki.collection import SearchNode from anki.lang import TR, tr_legacyglobal # pylint: disable=unused-import from anki.utils import invalidFilename, isMac, isWin, noBundledLibs, versionWithBuild +from aqt import colors from aqt.qt import * -from aqt.theme import theme_manager +from aqt.theme import ColoredIcon, theme_manager if TYPE_CHECKING: TextFormat = Union[Literal["plain", "rich"]] @@ -1023,6 +1026,34 @@ def no_arg_trigger(func: Callable) -> Callable: return pyqtSlot()(func) # type: ignore +def load_flags(col: Collection) -> List[Tuple[str, ColoredIcon, SearchNode]]: + labels = cast(Dict[str, str], col.get_config("flagLabels", {})) + icon = ColoredIcon(path=":/icons/flag.svg", color=colors.DISABLED) + + return [ + ( + labels["1"] if "1" in labels else tr.actions_red_flag(), + icon.with_color(colors.FLAG1_FG), + SearchNode(flag=SearchNode.FLAG_RED), + ), + ( + labels["2"] if "2" in labels else tr.actions_orange_flag(), + icon.with_color(colors.FLAG2_FG), + SearchNode(flag=SearchNode.FLAG_ORANGE), + ), + ( + labels["3"] if "3" in labels else tr.actions_green_flag(), + icon.with_color(colors.FLAG3_FG), + SearchNode(flag=SearchNode.FLAG_GREEN), + ), + ( + labels["4"] if "4" in labels else tr.actions_blue_flag(), + icon.with_color(colors.FLAG4_FG), + SearchNode(flag=SearchNode.FLAG_BLUE), + ), + ] + + class KeyboardModifiersPressed: "Util for type-safe checks of currently-pressed modifier keys."