跳转至

新增功能选项卡

本程序所有功能都以「标签页」形式呈现。新增一个功能需要三步:创建页面类、登记到选项卡表、接入核心服务。

1. 创建页面模块

ui/ 下新建一个文件,文件名用功能的中文拼音(与现有约定一致,如 qiudao.pyjifen.py)。定义一个继承自 QWidget 的页面类:

# ui/xin_gongneng.py
from PySide6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLabel
from PySide6.QtCore import QCoreApplication


class XinGongneng(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.fs = None            # 函数字典(可选,主窗口创建标签时传入)
        layout = QVBoxLayout(self)
        self.label = QLabel(QCoreApplication.translate("XinGongneng", "新功能"))
        layout.addWidget(self.label)
        btn = QPushButton(QCoreApplication.translate("XinGongneng", "计算"))
        btn.clicked.connect(self.compute)
        layout.addWidget(btn)

    def compute(self):
        # 解析输入(统一输入标准,支持 $ LaTeX 前缀、自定义函数、集合/区间)
        from functions.derivative import derivative
        res = derivative("x**2", "x")          # 返回 SymPy 表达式
        # 渲染结果到 QGraphicsView
        from core.render import setGraphicsView
        setGraphicsView("", str(res), self.graphics_view)

Tip

页面类构造函数第二参数惯例为 fs(函数字典 / 缓存区),主窗口创建标签页时传入;若不使用可省略。计算请尽量调用 functions/ 中的纯函数,保持界面与逻辑解耦。

2. 登记到选项卡表

编辑 ui/__init__.py,在 _submodules_tab_registrytabs_dict 三处同步添加(索引自增、保持唯一):

_submodules = [..., "xin_gongneng"]
_tab_registry = [..., ("xin_gongneng", "XinGongneng")]
tabs_dict = {..., "新功能": 23}

tabs_list 是懒加载代理,无需改动。run.py 启动时会自动把新页加入标签页栏。

3. 接入核心服务

  • 输入解析:使用 core.sympify.sympify(expr, fs, is_simplify=..., is_rationalize=...),以获得统一的输入标准($ LaTeX 前缀、自定义函数、集合 / 区间等)。
  • 结果渲染:使用 core.render.setGraphicsView(name, latex_str, graphics_view) 将结果渲染为 SVG 并显示于 QGraphicsView;切换主题时由 core.render.refreshGraphicsView() 自动重绘。
  • 主题 / 语言:通过 core.settings.current_theme() / apply_language() 读取与切换,无需自行管理。
  • 国际化:所有用户可见字符串用 QCoreApplication.translate("Context", "文本") 包裹,并在 i18n/*.ts 中补充翻译(详见《国际化》)。

常见陷阱

  • 必须在 run.py 设置好 QCoreApplication 的组织 / 应用名之后,core.settings 才能正确解析设置路径(否则主题 / 语言无法跨重启持久化)。
  • 不要在 functions/import PySide6;计算层应保持纯函数、可独立测试。