Skip to content

Commit

Permalink
disable custom field edit if ordering is not set to custom field
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsumoto-ren committed Aug 26, 2024
1 parent 25e1055 commit 5d44a17
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
42 changes: 16 additions & 26 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
# 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

Expand Down Expand Up @@ -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):
Expand Down
9 changes: 7 additions & 2 deletions settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand Down

0 comments on commit 5d44a17

Please sign in to comment.