From 5d44a1729ad06f77d687ab4589e5101cdbad816c Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Tue, 27 Aug 2024 00:18:13 +0300 Subject: [PATCH] disable custom field edit if ordering is not set to custom field --- config.py | 42 ++++++++++++++++-------------------------- settings_dialog.py | 9 +++++++-- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/config.py b/config.py index 7799b77..58fcf07 100644 --- a/config.py +++ b/config.py @@ -1,8 +1,8 @@ # Copyright: Ren Tatsumoto # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html - +import enum import sys -from typing import Any, Callable +from typing import Any, Callable, Final from anki.cards import Card @@ -41,38 +41,28 @@ def key(card: Card): return key -class OrderingChoices: - _choices = { - "Due": due_key, - "Interval length": lambda card: card.ivl, - "Card ID": lambda card: card.id, - "Deck ID": lambda card: card.did, - "Sort Field": sort_field_key, - "Sort Field (numeric)": generic_numeric_key(sort_field_key), - "Custom field": custom_field_key, - "Custom field (numeric)": generic_numeric_key(custom_field_key), - } - - def __getitem__(self, key) -> Callable[[Card], Any]: - return self._choices[key] - - @classmethod - def names(cls): - return cls._choices.keys() +ORDERING_CHOICES: Final[dict[str, Callable[[Card], Any]]] = { + "Due": due_key, + "Interval length": lambda card: card.ivl, + "Card ID": lambda card: card.id, + "Deck ID": lambda card: card.did, + "Sort Field": sort_field_key, + "Sort Field (numeric)": generic_numeric_key(sort_field_key), + "Custom field": custom_field_key, + "Custom field (numeric)": generic_numeric_key(custom_field_key), +} class Config(AddonConfigManager): - _ordering_choices = OrderingChoices() - def __init__(self, default: bool = False) -> None: super().__init__(default) - if self["ordering"] not in self._ordering_choices.names(): + if self["ordering"] not in ORDERING_CHOICES: print(f"Wrong ordering: {self['ordering']}") - self["ordering"] = next(name for name in OrderingChoices.names()) + self["ordering"] = next(name for name in ORDERING_CHOICES) @property - def ord_key(self): - return self._ordering_choices[self["ordering"]] + def ord_key(self) -> Callable[[Card], Any]: + return ORDERING_CHOICES[self["ordering"]] @classmethod def default(cls): diff --git a/settings_dialog.py b/settings_dialog.py index cf6cd46..419c63c 100644 --- a/settings_dialog.py +++ b/settings_dialog.py @@ -15,7 +15,7 @@ from .ajt_common.monospace_line_edit import MonoSpaceLineEdit from .ajt_common.multiple_choice_selector import MultipleChoiceSelector from .ajt_common.widget_placement import place_widgets_in_grid -from .config import ACTION_NAME, Config, OrderingChoices, config +from .config import ACTION_NAME, ORDERING_CHOICES, Config, config ###################################################################### # UI Layout @@ -178,7 +178,7 @@ def __init__(self): restoreGeom(self, self.name) def populate_widgets(self): - self._ordering_combo_box.addItems(OrderingChoices.names()) + self._ordering_combo_box.addItems(ORDERING_CHOICES.keys()) self._limit_to_fields.set_texts(dict.fromkeys(gather_all_field_names())) def load_config_values(self, cfg: Config): @@ -196,6 +196,11 @@ def connect_ui_elements(self): qconnect(self._bottom_box.accepted, self.accept) qconnect(self._bottom_box.rejected, self.reject) qconnect(self._reset_button.clicked, functools.partial(self.load_config_values, Config.default())) + qconnect(self._ordering_combo_box.currentIndexChanged, self._set_custom_field_active_status) + + def _set_custom_field_active_status(self): + current_ordering = self._ordering_combo_box.currentText() + self._custom_sort_field_edit.setEnabled(current_ordering.lower().startswith("custom field")) def accept(self): config["field_separator"] = self._field_separator_edit.text()