From 5fa68c885a6eafaf82dbff28d430e489a2b2e355 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Wed, 19 May 2021 19:17:43 +0200 Subject: [PATCH] Use dataclass instead of tuple for loaded flags --- qt/aqt/browser/browser.py | 31 +++++++++++-------------------- qt/aqt/browser/sidebar/tree.py | 10 +++++----- qt/aqt/utils.py | 24 +++++++++++++++++++----- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/qt/aqt/browser/browser.py b/qt/aqt/browser/browser.py index 3d9449842..4b68ca289 100644 --- a/qt/aqt/browser/browser.py +++ b/qt/aqt/browser/browser.py @@ -167,16 +167,14 @@ class Browser(QMainWindow): qconnect(f.action_set_due_date.triggered, self.set_due_date) qconnect(f.action_forget.triggered, self.forget_cards) qconnect(f.actionToggle_Suspend.triggered, self.suspend_selected_cards) - qconnect(f.actionRed_Flag.triggered, lambda: self.set_flag_of_selected_cards(1)) - qconnect( - f.actionOrange_Flag.triggered, lambda: self.set_flag_of_selected_cards(2) - ) - qconnect( - f.actionGreen_Flag.triggered, lambda: self.set_flag_of_selected_cards(3) - ) - qconnect( - f.actionBlue_Flag.triggered, lambda: self.set_flag_of_selected_cards(4) - ) + + def set_flag_func(desired_flag: int) -> Callable: + return lambda: self.set_flag_of_selected_cards(desired_flag) + + for flag in load_flags(self.col): + qconnect( + getattr(self.form, flag.action).triggered, set_flag_func(flag.index) + ) self._update_flag_labels() qconnect(f.actionExport.triggered, self._on_export_notes) # jumps @@ -713,21 +711,14 @@ where id in %s""" flag = self.card and self.card.user_flag() flag = flag or 0 - flagActions = [ - self.form.actionRed_Flag, - self.form.actionOrange_Flag, - self.form.actionGreen_Flag, - self.form.actionBlue_Flag, - ] - - for c, act in enumerate(flagActions): - act.setChecked(flag == c + 1) + for f in load_flags(self.col): + getattr(self.form, f.action).setChecked(flag == f.index) qtMenuShortcutWorkaround(self.form.menuFlag) def _update_flag_labels(self) -> None: 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: if checked: diff --git a/qt/aqt/browser/sidebar/tree.py b/qt/aqt/browser/sidebar/tree.py index f29e3cc5e..a20403d2e 100644 --- a/qt/aqt/browser/sidebar/tree.py +++ b/qt/aqt/browser/sidebar/tree.py @@ -609,14 +609,14 @@ class SidebarTreeView(QTreeView): ) 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( SidebarItem( - name=flag[0], - icon=flag[1], - search_node=flag[2], + name=flag.label, + icon=flag.icon, + search_node=flag.search_node, item_type=SidebarItemType.FLAG, - id=index + 1, + id=flag.index, ) ) diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 4e1bdfc22..3407b2733 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -6,6 +6,7 @@ import os import re import subprocess import sys +from dataclasses import dataclass from enum import Enum from functools import wraps from typing import ( @@ -1026,30 +1027,43 @@ def no_arg_trigger(func: Callable) -> Callable: 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", {})) icon = ColoredIcon(path=":/icons/flag.svg", color=colors.DISABLED) return [ - ( + Flag( + 1, labels["1"] if "1" in labels else tr.actions_red_flag(), icon.with_color(colors.FLAG1_FG), SearchNode(flag=SearchNode.FLAG_RED), "actionRed_Flag", ), - ( + Flag( + 2, labels["2"] if "2" in labels else tr.actions_orange_flag(), icon.with_color(colors.FLAG2_FG), SearchNode(flag=SearchNode.FLAG_ORANGE), "actionOrange_Flag", ), - ( + Flag( + 3, labels["3"] if "3" in labels else tr.actions_green_flag(), icon.with_color(colors.FLAG3_FG), SearchNode(flag=SearchNode.FLAG_GREEN), "actionGreen_Flag", ), - ( + Flag( + 4, labels["4"] if "4" in labels else tr.actions_blue_flag(), icon.with_color(colors.FLAG4_FG), SearchNode(flag=SearchNode.FLAG_BLUE),