From 0fd25aea6eb0a892dc526c9422fd393b951a63c4 Mon Sep 17 00:00:00 2001 From: kk-min <76023265+kk-min@users.noreply.github.com> Date: Fri, 26 Jan 2024 12:52:45 +0800 Subject: [PATCH] refactor: Separate UI from __init__.py --- __init__.py | 62 +++++++++------------------------------------ ui/typing_dialog.py | 42 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 ui/typing_dialog.py diff --git a/__init__.py b/__init__.py index d1a20b3..fb0d4c2 100644 --- a/__init__.py +++ b/__init__.py @@ -1,46 +1,13 @@ from aqt import mw, gui_hooks -from aqt.utils import showInfo, showCritical -from aqt.qt import * +from aqt.qt import Callable, QAction +from .ui.typing_dialog import TypingDialog from .config import Config, run_on_configuration_change -class TypingDialog(QDialog): - def __init__(self, parent, title, ans): - super().__init__(parent) - self.setWindowTitle("Taipingu") - self.setModal(False) - self.layout = QVBoxLayout(self) - self.ans = ans - self.correct = False - self.label = QLabel(self) - self.label.setText(self.ans) - self.label.move(200, 100) - self.label.setStyleSheet("font-size: 24px; text-align: center; width: 300px;") - self.layout.addWidget(self.label) - - self.lineedit = QLineEdit(self) - self.label.move(200, 200) - self.lineedit.setStyleSheet("font-size: 24px; width: 300px;") - self.layout.addWidget(self.lineedit) - self.lineedit.returnPressed.connect(self.check) - - self.show() - - def check(self): - if self.correct: - self.exit() - userInput = self.lineedit.text() - if userInput == self.ans: - self.label.setStyleSheet("font-size: 24px; text-align: center; width: 300px; color: #70C270;") - self.label.setText(self.ans + " - Correct!") - self.correct = True - else: - self.label.setStyleSheet("font-size: 24px; text-align: center; width: 300px; color: #E50000;") - self.label.setText(self.ans + " - Incorrect!") - - def exit(self): - self.close() +# load config object +config = Config() +config.load() # a tiny helper for menu items, since type checking is broken there def checkable(title: str, on_click: Callable[[bool], None]) -> QAction: @@ -48,7 +15,7 @@ def checkable(title: str, on_click: Callable[[bool], None]) -> QAction: action.triggered.connect(on_click) # noqa return action -def typingTest(card): +def typingTest(card) -> None: if not config.get('enable_typing_test'): return keyList = config.get('trigger_fields') @@ -59,28 +26,23 @@ def typingTest(card): break mw.typeWidget = TypingDialog(mw, "Taipingu", ans) -# load config object -config = Config() -config.load() - # create checkbox menu item for Taipingu under 'Tools' def set_enable_typing_test(enabled: bool) -> None: config.enable_typing_test = enabled -menu_enable_typing_test = checkable(title="Enable typing test", on_click=set_enable_typing_test) - def adjust_menu(old, new) -> None: menu_enable_typing_test.setChecked(config.enable_typing_test) +@run_on_configuration_change +def on_config_change() -> None: + config.load() + adjust_menu(None, None) + # add Taipingu menu item to Tools menu menu_taipingu = mw.form.menuTools.addMenu("Taipingu") +menu_enable_typing_test = checkable(title="Enable typing test", on_click=set_enable_typing_test) menu_taipingu.addAction(menu_enable_typing_test) gui_hooks.state_did_change.append(adjust_menu) -@run_on_configuration_change -def on_config_change() -> None: - config.load() - adjust_menu(None, None) - gui_hooks.reviewer_did_show_question.append(typingTest) diff --git a/ui/typing_dialog.py b/ui/typing_dialog.py new file mode 100644 index 0000000..f1bfdcb --- /dev/null +++ b/ui/typing_dialog.py @@ -0,0 +1,42 @@ +from aqt.qt import * + +class TypingDialog(QDialog): + def __init__(self, parent, title, ans): + super().__init__(parent) + self.setWindowTitle("Taipingu") + self.setModal(False) + self.layout = QVBoxLayout(self) + self.ans = ans + self.correct = False + + self.label = QLabel(self) + self.label.setText(self.ans) + self.label.move(200, 100) + self.label.setStyleSheet("font-size: 24px; text-align: center; width: 300px;") + self.layout.addWidget(self.label) + + self.lineedit = QLineEdit(self) + self.label.move(200, 200) + self.lineedit.setStyleSheet("font-size: 24px; width: 300px;") + self.layout.addWidget(self.lineedit) + self.lineedit.returnPressed.connect(self.check) + + self.show() + + def check(self) -> None: + userInput = self.lineedit.text() + + if self.correct and userInput == self.ans: + self.exit() + + if userInput == self.ans: + self.label.setStyleSheet("font-size: 24px; text-align: center; width: 300px; color: #70C270;") + self.label.setText(self.ans + " - Correct!") + self.correct = True + else: + self.label.setStyleSheet("font-size: 24px; text-align: center; width: 300px; color: #E50000;") + self.label.setText(self.ans + " - Incorrect!") + self.correct = False + + def exit(self) -> None: + self.close()