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: def is_editable(self) -> bool:
return self in ( return self in (
SidebarItemType.FLAG,
SidebarItemType.SAVED_SEARCH, SidebarItemType.SAVED_SEARCH,
SidebarItemType.DECK, SidebarItemType.DECK,
SidebarItemType.TAG, SidebarItemType.TAG,

View File

@ -35,7 +35,14 @@ from aqt.operations.tag import (
) )
from aqt.qt import * from aqt.qt import *
from aqt.theme import ColoredIcon, theme_manager 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): class SidebarStage(Enum):
@ -361,6 +368,8 @@ class SidebarTreeView(QTreeView):
self.rename_saved_search(item, new_name) self.rename_saved_search(item, new_name)
elif item.item_type == SidebarItemType.TAG: elif item.item_type == SidebarItemType.TAG:
self.rename_tag(item, new_name) 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 # renaming may be asynchronous so always return False
return False return False
@ -600,35 +609,21 @@ class SidebarTreeView(QTreeView):
) )
root.search_node = SearchNode(flag=SearchNode.FLAG_ANY) root.search_node = SearchNode(flag=SearchNode.FLAG_ANY)
type = SidebarItemType.FLAG for index, flag in enumerate(load_flags(self.col)):
root.add_simple( root.add_child(
tr.actions_red_flag(), SidebarItem(
icon=icon.with_color(colors.FLAG1_FG), name=flag[0],
type=type, icon=flag[1],
search_node=SearchNode(flag=SearchNode.FLAG_RED), search_node=flag[2],
) item_type=SidebarItemType.FLAG,
root.add_simple( id=index + 1,
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( root.add_simple(
tr.browsing_no_flag(), tr.browsing_no_flag(),
icon=icon.with_color(colors.DISABLED), icon=icon,
type=type, type=SidebarItemType.FLAG,
search_node=SearchNode(flag=SearchNode.FLAG_NONE), search_node=SearchNode(flag=SearchNode.FLAG_NONE),
) )
@ -872,6 +867,16 @@ class SidebarTreeView(QTreeView):
lambda: set_children_expanded(False), 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 # Decks
########################### ###########################

View File

@ -12,6 +12,7 @@ from typing import (
TYPE_CHECKING, TYPE_CHECKING,
Any, Any,
Callable, Callable,
Dict,
List, List,
Literal, Literal,
Optional, Optional,
@ -35,10 +36,12 @@ from PyQt5.QtWidgets import (
import aqt import aqt
from anki import Collection from anki import Collection
from anki.collection import SearchNode
from anki.lang import TR, tr_legacyglobal # pylint: disable=unused-import from anki.lang import TR, tr_legacyglobal # pylint: disable=unused-import
from anki.utils import invalidFilename, isMac, isWin, noBundledLibs, versionWithBuild from anki.utils import invalidFilename, isMac, isWin, noBundledLibs, versionWithBuild
from aqt import colors
from aqt.qt import * from aqt.qt import *
from aqt.theme import theme_manager from aqt.theme import ColoredIcon, theme_manager
if TYPE_CHECKING: if TYPE_CHECKING:
TextFormat = Union[Literal["plain", "rich"]] TextFormat = Union[Literal["plain", "rich"]]
@ -1023,6 +1026,34 @@ def no_arg_trigger(func: Callable) -> Callable:
return pyqtSlot()(func) # type: ignore 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: class KeyboardModifiersPressed:
"Util for type-safe checks of currently-pressed modifier keys." "Util for type-safe checks of currently-pressed modifier keys."