From 4beb42f615861b0a484232760d6decd4d21a6b80 Mon Sep 17 00:00:00 2001 From: david-ajax Date: Sun, 3 Aug 2025 03:57:21 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=B8=83=E5=B1=80=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E5=B9=B6=E6=94=B9=E8=BF=9B=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compositions.py | 68 +++++++++++++++++++++++++++++++++++++-------- main.py | 54 ++++++++++++++++++++++++++++++++--- nucleon/陈情表.toml | 2 +- reactor.py | 20 ++++++++----- styles.tcss | 5 ++-- 5 files changed, 123 insertions(+), 26 deletions(-) diff --git a/compositions.py b/compositions.py index 9edfa84..009ab5a 100644 --- a/compositions.py +++ b/compositions.py @@ -1,16 +1,29 @@ from textual.app import App, ComposeResult from textual.events import Event -from textual.widgets import Header, Footer, ListView, ListItem, Label, Static, Button -from textual.containers import Container, Horizontal +from textual.widgets import Collapsible, Header, Footer, Markdown, ListView, ListItem, Label, Static, Button +from textual.containers import Container, Horizontal, Center from textual.screen import Screen +from textual.widget import Widget import pathlib from typing import Tuple, Dict import particles as pt +import uuid +from functools import wraps class Composition(): - def __init__(self, screen: Screen, atom: Tuple[pt.Electron, pt.Nucleon, Dict]): + def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]): self.screen = screen - self.atom = atom + self.atom = atom + from reactor import Reactor + self.reactor: Reactor = reactor + self.reg = dict() + def regid(self, id_): + self.reg[id_] = id_ + str(uuid.uuid4()) + return self.reg[id_] + def getid(self, id_): + if id_ not in self.reg.keys(): + return "None" + return self.reg[id_] def compose(self): yield Label("示例标签", id="testlabel") yield Button("示例按钮", id="testbtn") @@ -23,18 +36,50 @@ class Placeholder(Composition): self.screen = screen def compose(self): yield Label("示例标签", id="testlabel") - yield Button("示例按钮", id="testbtn") + yield Button("示例按钮", id="testbtn", classes="choice") def handler(self, event): print(event.button.id) self.screen.query_one("#testlabel", Label).update("hi") class Recognition(Composition): - def __init__(self, screen: Screen, atom: Tuple[pt.Electron, pt.Nucleon, Dict]): - super().__init__(screen, atom) + def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]): + super().__init__(screen, reactor, atom) + def compose(self): + with Center(): + yield Static(f"[dim]{self.atom[1]['translation']}[/]") + yield Label(f"") + with Center(): + yield Label(f"[b][b]{self.atom[1]['content']}[/][/]", id=self.regid("sentence")) # 致敬传奇去重串 uuid + #with Collapsible(title="附加信息", collapsed=True): + for i in self.atom[2]["testdata"]["additional_inf"]: + if self.atom[1][i]: + print(type(self.atom[1][i])) + print(self.atom[1][i]) + if isinstance(self.atom[1][i], list): + for j in self.atom[1][i]: + print(999) + yield Markdown(f"### {self.atom[2]["keydata"][i]}: {j}") + continue + if isinstance(self.atom[1][i], Dict): + t = "" + for j, k in self.atom[1][i].items(): + t += f"> **{j}**: {k} \n" + yield Markdown(t, id=self.regid("tran")) + with Center(): + yield Button("我已知晓", id=self.regid("ok")) + def handler(self, event): + if event.button.id == self.getid("ok"): + print(1) + """"""#assessment = self.reactor.report(self.reactor.current_atom, -1) + return 0 + +class BasicEvaluation(Composition): + def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]): + super().__init__(screen, reactor, atom) def compose(self): yield Label(self.atom[1]["content"], id="sentence") for i in self.atom[2]["testdata"]["additional_inf"]: - yield Label("[" + self.atom[2]["keydata"][i] + "] " + str(self.atom[1][i]), id=f"label_{i}") + yield Label(f"{self.atom[2]["keydata"][i]}: {self.atom[1][i]}", id=f"label_{i}") yield Button("我已知晓", id="ok") def handler(self, event): if event.button.id == "ok": @@ -42,7 +87,7 @@ class Recognition(Composition): class FillBlank(Composition): def __init__(self, screen: Screen, atom: Tuple[pt.Electron, pt.Nucleon, Dict]): - super().__init__(screen, atom) + """"""#super().__init__(screen, atom) def compose(self): yield Label(self.atom[1]["content"], id="sentence") for i in self.atom[2]["testdata"]["additional_inf"]: @@ -57,7 +102,8 @@ registry = { "sample": Composition, "recognition": Recognition, "fill_blank_test": FillBlank, - "draw_card_test": Composition, + "draw_card_test": BasicEvaluation, + "basic_evaluation": BasicEvaluation, } @@ -66,7 +112,7 @@ registry = { class TestScreen(Screen): def __init__(self): super().__init__(name=None, id=None, classes=None) - self.comp = Recognition(self, pt.Atom.advanced_placeholder()) + self.comp = Recognition(self, None, pt.Atom.advanced_placeholder()) def compose(self) -> ComposeResult: yield Header(show_clock=True) yield from self.comp.compose() diff --git a/main.py b/main.py index c5e96b1..3cd1417 100644 --- a/main.py +++ b/main.py @@ -48,10 +48,12 @@ class MemScreen(Screen): self.stage += self.reactor.set_round_templated(self.stage) #print(self.reactor.procession) self.reactor.forward() - self.compo:compo.Composition = compo.Placeholder(self) + #self.compo:compo.Composition = compo.Placeholder(self) + self.compo = next(self.reactor.current_appar) + def compose(self) -> ComposeResult: - self.compo = next(self.reactor.current_appar) + print(self.compo) yield Header(show_clock=True) with Container(id="main_container"): yield from self.compo.compose() @@ -67,7 +69,7 @@ class MemScreen(Screen): pass def on_button_pressed(self, event): - self.compo.handler(event) + ret = self.compo.handler(event) """feedback_label = self.query_one("#feedback", Static) if type(event) == str: btnid = event @@ -76,7 +78,51 @@ class MemScreen(Screen): btnid = str(btnid) quality = int(btnid.replace('q', '')) assessment = self.reactor.report(self.reactor.current_atom, quality) - if assessment == 1: + """ + # 遵循 perror 返回值规则 + if ret == 0: # 成功 + try: + self.compo = next(self.reactor.current_appar) + self.refresh_ui() + except StopIteration: + nxt = self.reactor.forward(1) + print(2) + self.compo = next(self.reactor.current_appar) + print(self.compo) + #print("next", nxt, self.reactor.current_atom) + if nxt == -1: + if self.reactor.round_set == 0: + if self.stage == 4: + if config.get("save"): + self.reactor.save() + #self._show_finished_screen("今日目标已完成") + else: + self.reactor.set_round_templated(self.stage) + self.reactor.forward(1) + #self._update_ui() + self.stage += 1 + return + return + else: + self.refresh_ui() + return + if ret == 1: # 不允许前进 + self.refresh_ui() + return + + def refresh_ui(self): + area = self.query_one("#main_container") + self.call_later(self.recompose) + #print(area.children) + #for child in list(area.children): + # child.remove() # 致敬传奇组件树 DOM + #print(1,list(self.compo.compose())) + #area.mount(*list(self.compo.compose())) + + def report(self, quality): + assessment = self.reactor.report(self.reactor.current_atom, quality) + return assessment + """if assessment == 1: # 需要复习 feedback_label.update(f"评分为 {quality}, 已经加入至复习, 请重复记忆") else: diff --git a/nucleon/陈情表.toml b/nucleon/陈情表.toml index f385a37..29bf48d 100644 --- a/nucleon/陈情表.toml +++ b/nucleon/陈情表.toml @@ -7,7 +7,7 @@ translation = "语句翻译" # 测试项目元数据 ["testdata"] # 记忆时显示的额外信息 -additional_inf = ["translation", "note"] +additional_inf = ["translation","keyword_note", "note"] # 填空测试 fill_blank_test = ["translation"] # 选择题测试 diff --git a/reactor.py b/reactor.py index a27461f..f112fd0 100644 --- a/reactor.py +++ b/reactor.py @@ -4,10 +4,10 @@ import pathlib import auxiliary as aux import compositions as comps import random - +#from pprint import pprint as print # debug class Apparatus(): """反应器对象, 决策一个原子的不同记忆方式, 并反馈到布局""" - def __init__(self, screen, atom): + def __init__(self, screen, reactor, atom): self.electron: pt.Electron = atom[0] self.nucleon: pt.Nucleon = atom[1] self.positron: dict = atom[2] @@ -15,12 +15,12 @@ class Apparatus(): self.procession: typing.List[comps.Composition] = list() if self.positron["is_new_activation"] == 1: self.positron["is_new_activation"] = 0 - self.procession.append(comps.registry["recognition"](screen, atom)) + self.procession.append(comps.registry["recognition"](screen, reactor, atom)) return for i in self.positron["testdata"].keys(): if i == "additional_inf": continue - self.procession.append(comps.registry[i](screen, atom)) + self.procession.append(comps.registry[i](screen, reactor, atom)) # self.procession.reverse() random.shuffle(self.procession) @@ -76,6 +76,7 @@ class Reactor(): self.current_atom: typing.Tuple[pt.Electron, pt.Nucleon, dict] self.round_set = 0 self.current_atom = pt.Atom.placeholder() + #print(self.atoms_new) def set_round(self, title, procession): self.round_set = 1 @@ -90,11 +91,10 @@ class Reactor(): 2: "新记忆模式", 3: "总复习模式" } - processions = { 1: self.atoms_review, 2: self.atoms_new, - 3: list(set(self.atoms_new + self.atoms_review)) + 3: (self.atoms_new + self.atoms_review) } ret = 1 if stage == 1 and len(processions[1]) == 0: @@ -104,6 +104,12 @@ class Reactor(): return ret def forward(self, step = 1): + """ + 返回值规则: + 1: 重定向至 failed + -1: 此轮已完成 + 0: 下一个记忆单元 + """ if self.index + step >= len(self.procession): if len(self.failed) > 0: self.procession = self.failed @@ -118,7 +124,7 @@ class Reactor(): return -1 # 此轮已完成 self.index += step self.current_atom = self.procession[self.index] - self.current_appar = Apparatus(self.screen, self.current_atom).iterator() + self.current_appar = Apparatus(self.screen, self, self.current_atom).iterator() return 0 def save(self): diff --git a/styles.tcss b/styles.tcss index 8e7c265..56b6784 100644 --- a/styles.tcss +++ b/styles.tcss @@ -1,11 +1,10 @@ - Screen { align: center middle; } #main_container { align: center middle; - width: 80%; + width: 95%; height: auto; border: thick $primary-lighten-2; padding: 2; @@ -52,4 +51,4 @@ Button { margin-bottom: 0; align-horizontal: center; width: 40%; -} \ No newline at end of file +}