Make sidebar flags renamable

This commit is contained in:
RumovZ 2021-05-18 22:08:36 +02:00
parent 363548e8a2
commit 9816227b5c
3 changed files with 66 additions and 29 deletions

View File

@ -40,6 +40,7 @@ class SidebarItemType(Enum):
def is_editable(self) -> bool:
return self in (
SidebarItemType.FLAG,
SidebarItemType.SAVED_SEARCH,
SidebarItemType.DECK,
SidebarItemType.TAG,

View File

@ -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),
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.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),
)
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
###########################

View File

@ -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."