Merge pull request #1181 from RumovZ/flag-mod

Move flags into separate module
This commit is contained in:
Damien Elmes 2021-05-20 18:48:33 +10:00 committed by GitHub
commit ebae455668
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 67 deletions

View File

@ -18,6 +18,7 @@ from anki.utils import ids2str, isMac
from aqt import AnkiQt, gui_hooks
from aqt.editor import Editor
from aqt.exporting import ExportDialog
from aqt.flags import load_flags
from aqt.operations.card import set_card_deck, set_card_flag
from aqt.operations.collection import redo, undo
from aqt.operations.note import remove_notes
@ -42,7 +43,6 @@ from aqt.utils import (
current_top_level_widget,
ensure_editor_saved,
getTag,
load_flags,
no_arg_trigger,
openHelp,
qtMenuShortcutWorkaround,

View File

@ -19,6 +19,7 @@ from aqt.browser.sidebar.model import SidebarModel
from aqt.browser.sidebar.searchbar import SidebarSearchBar
from aqt.browser.sidebar.toolbar import SidebarTool, SidebarToolbar
from aqt.clayout import CardLayout
from aqt.flags import load_flags
from aqt.models import Models
from aqt.operations import QueryOp
from aqt.operations.deck import (
@ -35,14 +36,7 @@ from aqt.operations.tag import (
)
from aqt.qt import *
from aqt.theme import ColoredIcon, theme_manager
from aqt.utils import (
KeyboardModifiersPressed,
askUser,
getOnlyText,
load_flags,
showWarning,
tr,
)
from aqt.utils import KeyboardModifiersPressed, askUser, getOnlyText, showWarning, tr
class SidebarStage(Enum):

68
qt/aqt/flags.py Normal file
View File

@ -0,0 +1,68 @@
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from __future__ import annotations
from dataclasses import dataclass
from typing import Dict, List, cast
from anki import Collection
from anki.collection import SearchNode
from aqt import colors
from aqt.theme import ColoredIcon
from aqt.utils import tr
@dataclass
class Flag:
"""A container class for flag related data.
index -- The integer by which the flag is represented internally (1-7).
label -- The text by which the flag is described in the GUI.
icon -- The icon by which the flag is represented in the GUI.
search_node -- The node to build a search string for finding cards with the flag.
action -- The name of the action to assign the flag in the browser form.
"""
index: int
label: str
icon: ColoredIcon
search_node: SearchNode
action: str
def load_flags(col: Collection) -> List[Flag]:
"""Return a list of all flags, reloading labels from the config."""
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),
"actionBlue_Flag",
),
]

View File

@ -35,6 +35,7 @@ from anki.scheduler.v3 import Scheduler as V3Scheduler
from anki.tags import MARKED_TAG
from anki.utils import stripHTML
from aqt import AnkiQt, gui_hooks
from aqt.flags import load_flags
from aqt.operations.card import set_card_flag
from aqt.operations.note import remove_notes
from aqt.operations.scheduling import (
@ -51,14 +52,7 @@ from aqt.qt import *
from aqt.sound import av_player, play_clicked_audio, record_audio
from aqt.theme import theme_manager
from aqt.toolbar import BottomBar
from aqt.utils import (
askUserDialog,
downArrow,
load_flags,
qtMenuShortcutWorkaround,
tooltip,
tr,
)
from aqt.utils import askUserDialog, downArrow, qtMenuShortcutWorkaround, tooltip, tr
from aqt.webview import AnkiWebView

View File

@ -6,14 +6,12 @@ import os
import re
import subprocess
import sys
from dataclasses import dataclass
from enum import Enum
from functools import wraps
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
List,
Literal,
Optional,
@ -37,12 +35,10 @@ 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 ColoredIcon, theme_manager
from aqt.theme import theme_manager
if TYPE_CHECKING:
TextFormat = Union[Literal["plain", "rich"]]
@ -1027,51 +1023,6 @@ def no_arg_trigger(func: Callable) -> Callable:
return pyqtSlot()(func) # type: ignore
@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),
"actionBlue_Flag",
),
]
class KeyboardModifiersPressed:
"Util for type-safe checks of currently-pressed modifier keys."