Use dataclass instead of tuple for loaded flags

This commit is contained in:
RumovZ 2021-05-19 19:17:43 +02:00
parent 30736ddf75
commit 5fa68c885a
3 changed files with 35 additions and 30 deletions

View File

@ -167,16 +167,14 @@ class Browser(QMainWindow):
qconnect(f.action_set_due_date.triggered, self.set_due_date) qconnect(f.action_set_due_date.triggered, self.set_due_date)
qconnect(f.action_forget.triggered, self.forget_cards) qconnect(f.action_forget.triggered, self.forget_cards)
qconnect(f.actionToggle_Suspend.triggered, self.suspend_selected_cards) qconnect(f.actionToggle_Suspend.triggered, self.suspend_selected_cards)
qconnect(f.actionRed_Flag.triggered, lambda: self.set_flag_of_selected_cards(1))
qconnect( def set_flag_func(desired_flag: int) -> Callable:
f.actionOrange_Flag.triggered, lambda: self.set_flag_of_selected_cards(2) return lambda: self.set_flag_of_selected_cards(desired_flag)
)
qconnect( for flag in load_flags(self.col):
f.actionGreen_Flag.triggered, lambda: self.set_flag_of_selected_cards(3) qconnect(
) getattr(self.form, flag.action).triggered, set_flag_func(flag.index)
qconnect( )
f.actionBlue_Flag.triggered, lambda: self.set_flag_of_selected_cards(4)
)
self._update_flag_labels() self._update_flag_labels()
qconnect(f.actionExport.triggered, self._on_export_notes) qconnect(f.actionExport.triggered, self._on_export_notes)
# jumps # jumps
@ -713,21 +711,14 @@ where id in %s"""
flag = self.card and self.card.user_flag() flag = self.card and self.card.user_flag()
flag = flag or 0 flag = flag or 0
flagActions = [ for f in load_flags(self.col):
self.form.actionRed_Flag, getattr(self.form, f.action).setChecked(flag == f.index)
self.form.actionOrange_Flag,
self.form.actionGreen_Flag,
self.form.actionBlue_Flag,
]
for c, act in enumerate(flagActions):
act.setChecked(flag == c + 1)
qtMenuShortcutWorkaround(self.form.menuFlag) qtMenuShortcutWorkaround(self.form.menuFlag)
def _update_flag_labels(self) -> None: def _update_flag_labels(self) -> None:
for flag in load_flags(self.col): for flag in load_flags(self.col):
getattr(self.form, flag[3]).setText(flag[0]) getattr(self.form, flag.action).setText(flag.label)
def toggle_mark_of_selected_notes(self, checked: bool) -> None: def toggle_mark_of_selected_notes(self, checked: bool) -> None:
if checked: if checked:

View File

@ -609,14 +609,14 @@ class SidebarTreeView(QTreeView):
) )
root.search_node = SearchNode(flag=SearchNode.FLAG_ANY) root.search_node = SearchNode(flag=SearchNode.FLAG_ANY)
for index, flag in enumerate(load_flags(self.col)): for flag in load_flags(self.col):
root.add_child( root.add_child(
SidebarItem( SidebarItem(
name=flag[0], name=flag.label,
icon=flag[1], icon=flag.icon,
search_node=flag[2], search_node=flag.search_node,
item_type=SidebarItemType.FLAG, item_type=SidebarItemType.FLAG,
id=index + 1, id=flag.index,
) )
) )

View File

@ -6,6 +6,7 @@ import os
import re import re
import subprocess import subprocess
import sys import sys
from dataclasses import dataclass
from enum import Enum from enum import Enum
from functools import wraps from functools import wraps
from typing import ( from typing import (
@ -1026,30 +1027,43 @@ 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, str]]: @dataclass
class Flag:
index: int
label: str
icon: ColoredIcon
search_node: SearchNode
action: str
def load_flags(col: Collection) -> List[Flag]:
labels = cast(Dict[str, str], col.get_config("flagLabels", {})) labels = cast(Dict[str, str], col.get_config("flagLabels", {}))
icon = ColoredIcon(path=":/icons/flag.svg", color=colors.DISABLED) icon = ColoredIcon(path=":/icons/flag.svg", color=colors.DISABLED)
return [ return [
( Flag(
1,
labels["1"] if "1" in labels else tr.actions_red_flag(), labels["1"] if "1" in labels else tr.actions_red_flag(),
icon.with_color(colors.FLAG1_FG), icon.with_color(colors.FLAG1_FG),
SearchNode(flag=SearchNode.FLAG_RED), SearchNode(flag=SearchNode.FLAG_RED),
"actionRed_Flag", "actionRed_Flag",
), ),
( Flag(
2,
labels["2"] if "2" in labels else tr.actions_orange_flag(), labels["2"] if "2" in labels else tr.actions_orange_flag(),
icon.with_color(colors.FLAG2_FG), icon.with_color(colors.FLAG2_FG),
SearchNode(flag=SearchNode.FLAG_ORANGE), SearchNode(flag=SearchNode.FLAG_ORANGE),
"actionOrange_Flag", "actionOrange_Flag",
), ),
( Flag(
3,
labels["3"] if "3" in labels else tr.actions_green_flag(), labels["3"] if "3" in labels else tr.actions_green_flag(),
icon.with_color(colors.FLAG3_FG), icon.with_color(colors.FLAG3_FG),
SearchNode(flag=SearchNode.FLAG_GREEN), SearchNode(flag=SearchNode.FLAG_GREEN),
"actionGreen_Flag", "actionGreen_Flag",
), ),
( Flag(
4,
labels["4"] if "4" in labels else tr.actions_blue_flag(), labels["4"] if "4" in labels else tr.actions_blue_flag(),
icon.with_color(colors.FLAG4_FG), icon.with_color(colors.FLAG4_FG),
SearchNode(flag=SearchNode.FLAG_BLUE), SearchNode(flag=SearchNode.FLAG_BLUE),