From 600a304ab812c8fc1a1f7f4041b5959acc4c2b7b Mon Sep 17 00:00:00 2001 From: KenanZhu <3471685733@qq.com> Date: Mon, 18 May 2026 16:01:16 +0800 Subject: [PATCH] =?UTF-8?q?style(gui):=20=E8=A7=84=E8=8C=83=E7=BC=96?= =?UTF-8?q?=E6=8E=92=E5=AF=B9=E8=AF=9D=E6=A1=86=E5=B1=9E=E6=80=A7=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E5=B9=B6=E6=B6=88=E9=99=A4=E5=86=97=E4=BD=99=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gui/ALAutoScriptOrchDialog/_blocks.py | 22 ++--- src/gui/ALAutoScriptOrchDialog/_dialog.py | 32 +++---- src/gui/ALAutoScriptOrchDialog/_helpers.py | 98 ++++++++------------ src/gui/ALAutoScriptOrchDialog/_widgets.py | 100 ++++++++++----------- 4 files changed, 112 insertions(+), 140 deletions(-) diff --git a/src/gui/ALAutoScriptOrchDialog/_blocks.py b/src/gui/ALAutoScriptOrchDialog/_blocks.py index 4422e80..e0e3625 100644 --- a/src/gui/ALAutoScriptOrchDialog/_blocks.py +++ b/src/gui/ALAutoScriptOrchDialog/_blocks.py @@ -79,9 +79,9 @@ class ConditionalBlock(QGroupBox): condLayout.setContentsMargins(4, 4, 4, 4) condLayout.setSpacing(6) - self._condRowsLayout = QVBoxLayout() - self._condRowsLayout.setSpacing(4) - condLayout.addLayout(self._condRowsLayout) + self.condRowsLayout = QVBoxLayout() + self.condRowsLayout.setSpacing(4) + condLayout.addLayout(self.condRowsLayout) self.addCondBtn = QPushButton("+ 添加条件", self.conditionWidget) self.addCondBtn.setFixedHeight(25) condLayout.addWidget(self.addCondBtn) @@ -89,9 +89,9 @@ class ConditionalBlock(QGroupBox): self.actionLabel = QLabel("执行步骤:", self) self.actionLabel.setFixedHeight(25) mainLayout.addWidget(self.actionLabel) - self._actionsLayout = QVBoxLayout() - self._actionsLayout.setSpacing(4) - mainLayout.addLayout(self._actionsLayout) + self.actionsLayout = QVBoxLayout() + self.actionsLayout.setSpacing(4) + mainLayout.addLayout(self.actionsLayout) self.addActionBtn = QPushButton("+ 添加执行步骤", self) self.addActionBtn.setFixedHeight(25) mainLayout.addWidget(self.addActionBtn) @@ -116,7 +116,7 @@ class ConditionalBlock(QGroupBox): isFirst=True, parent=self ) self._conditionRows.append(row) - self._condRowsLayout.addWidget(row) + self.condRowsLayout.addWidget(row) def addConditionRow( @@ -129,7 +129,7 @@ class ConditionalBlock(QGroupBox): ) row.deleteBtn.clicked.connect(lambda: self.removeConditionRow(row)) self._conditionRows.append(row) - self._condRowsLayout.addWidget(row) + self.condRowsLayout.addWidget(row) def removeConditionRow( @@ -139,7 +139,7 @@ class ConditionalBlock(QGroupBox): if row in self._conditionRows and len(self._conditionRows) > 1: self._conditionRows.remove(row) - self._condRowsLayout.removeWidget(row) + self.condRowsLayout.removeWidget(row) row.hide() row.deleteLater() @@ -151,7 +151,7 @@ class ConditionalBlock(QGroupBox): step = ActionStepFrame(self._varMgr, self.blockIndex, parent=self) step.deleteBtn.clicked.connect(lambda: self.removeActionStep(step)) self._actionWidgets.append(step) - self._actionsLayout.addWidget(step) + self.actionsLayout.addWidget(step) def removeActionStep( @@ -161,7 +161,7 @@ class ConditionalBlock(QGroupBox): if step in self._actionWidgets: self._actionWidgets.remove(step) - self._actionsLayout.removeWidget(step) + self.actionsLayout.removeWidget(step) step.hide() step.deleteLater() diff --git a/src/gui/ALAutoScriptOrchDialog/_dialog.py b/src/gui/ALAutoScriptOrchDialog/_dialog.py index 21f838a..c5fedad 100644 --- a/src/gui/ALAutoScriptOrchDialog/_dialog.py +++ b/src/gui/ALAutoScriptOrchDialog/_dialog.py @@ -46,7 +46,7 @@ class ALAutoScriptOrchDialog(QDialog): self.loadFromScript(existingScript) else: self.addBlock() - self._scrollLayout.addStretch() + self.scrollLayout.addStretch() def setupUi( @@ -61,8 +61,8 @@ class ALAutoScriptOrchDialog(QDialog): scroll.setWidgetResizable(True) scroll.setFrameShape(QFrame.NoFrame) scrollContent = QWidget() - self._scrollLayout = QVBoxLayout(scrollContent) - self._scrollLayout.setSpacing(5) + self.scrollLayout = QVBoxLayout(scrollContent) + self.scrollLayout.setSpacing(5) scroll.setWidget(scrollContent) mainLayout.addWidget(scroll) self.addBlockBtn = QPushButton("+ 添加判断块") @@ -108,16 +108,16 @@ class ALAutoScriptOrchDialog(QDialog): block.addActionStep() self._blocks.append(block) self._updateBlockTypeRestrictions() - if self._scrollLayout.count() > 0: - lastItem = self._scrollLayout.itemAt( - self._scrollLayout.count() - 1 + if self.scrollLayout.count() > 0: + lastItem = self.scrollLayout.itemAt( + self.scrollLayout.count() - 1 ) if lastItem and lastItem.spacerItem(): - self._scrollLayout.insertWidget( - self._scrollLayout.count() - 1, block + self.scrollLayout.insertWidget( + self.scrollLayout.count() - 1, block ) return - self._scrollLayout.addWidget(block) + self.scrollLayout.addWidget(block) def removeBlock( @@ -130,7 +130,7 @@ class ALAutoScriptOrchDialog(QDialog): return if block in self._blocks: self._blocks.remove(block) - self._scrollLayout.removeWidget(block) + self.scrollLayout.removeWidget(block) block.hide() block.deleteLater() for i, blk in enumerate(self._blocks): @@ -200,8 +200,8 @@ class ALAutoScriptOrchDialog(QDialog): typeIdxMap = {"IF": 0, "ELSE IF": 1, "ELSE": 2} parsedBlocks = parseBlocks(script) self._blocks.clear() - while self._scrollLayout.count(): - item = self._scrollLayout.takeAt(0) + while self.scrollLayout.count(): + item = self.scrollLayout.takeAt(0) if item.widget(): item.widget().deleteLater() try: @@ -222,8 +222,8 @@ class ALAutoScriptOrchDialog(QDialog): self._parseConditions(block, condition) except Exception: self._blocks.clear() - while self._scrollLayout.count(): - item = self._scrollLayout.takeAt(0) + while self.scrollLayout.count(): + item = self.scrollLayout.takeAt(0) if item.widget(): item.widget().deleteLater() self._updateBlockTypeRestrictions() @@ -255,7 +255,7 @@ class ALAutoScriptOrchDialog(QDialog): allLogics.append(".AND.") allSubConds.append(ap) for row in list(block._conditionRows): - block._condRowsLayout.removeWidget(row) + block.condRowsLayout.removeWidget(row) row.hide() row.deleteLater() block._conditionRows.clear() @@ -278,7 +278,7 @@ class ALAutoScriptOrchDialog(QDialog): row.logicCombo.setCurrentIndex(li) break block._conditionRows.append(row) - block._condRowsLayout.addWidget(row) + block.condRowsLayout.addWidget(row) subUp = subCond.upper() if subUp in (".TRUE.", ".FALSE."): row.loadFromParts(subUp, "", "") diff --git a/src/gui/ALAutoScriptOrchDialog/_helpers.py b/src/gui/ALAutoScriptOrchDialog/_helpers.py index cd8261f..220c7e8 100644 --- a/src/gui/ALAutoScriptOrchDialog/_helpers.py +++ b/src/gui/ALAutoScriptOrchDialog/_helpers.py @@ -3,7 +3,11 @@ Helper utilities and constants for the AutoScript orchestration dialog. """ import re -from PySide6.QtCore import QObject, QDate, QTime +from PySide6.QtCore import ( + QObject, + QDate, + QTime +) from PySide6.QtWidgets import ( QComboBox, QDateEdit, @@ -18,7 +22,14 @@ from PySide6.QtWidgets import ( QWidget, ) -from autoscript import ALL_VARIABLES +from autoscript import ( + ALL_VARIABLES, + splitTopLevel +) +from autoscript.ASOperator import ( + ARITH_TYPES, + COMPARISON_OPERATORS +) VAR_TYPE_ORDER = [ @@ -40,14 +51,19 @@ PRESET_VARIABLES = [ PRESET_NAMES = { p["name"] for p in PRESET_VARIABLES } -COMPARE_OPERATORS = sorted([ - ("等于", ".EQ."), - ("不等于", ".NEQ."), - ("大于", ".BGT."), - ("小于", ".BLT."), - ("大于等于", ".BGE."), - ("小于等于", ".BLE."), -], key=lambda x: len(x[1]), reverse=True) +# Operator display names (UI-specific), symbols derived from engine +_COMPARE_DISPLAY_MAP = { + ".EQ.": "等于", + ".NEQ.": "不等于", + ".BGT.": "大于", + ".BLT.": "小于", + ".BGE.": "大于等于", + ".BLE.": "小于等于", +} +COMPARE_OPERATORS = sorted( + [(name, op) for op, name in _COMPARE_DISPLAY_MAP.items() if op in COMPARISON_OPERATORS], + key=lambda x: len(x[1]), reverse=True +) LOGIC_OPERATORS = [ ("并且 (.AND.)", ".AND."), ("或者 (.OR.)", ".OR."), @@ -57,12 +73,6 @@ ACTION_TYPES = [ ("增加", "add"), ("减少", "sub"), ] -ARITH_TYPES = { - "Date", - "Time", - "Int", - "Float" -} DATE_RELATIVE_OPTIONS = [ ("前天", "day_before_yesterday"), ("昨天", "yesterday"), @@ -310,21 +320,17 @@ class _DateInputContainer(QWidget): ): s = expr.strip().upper() - if s == "CURRENT_DATE - 2": + _RELATIVE_MAP = { + "CURRENT_DATE": 0, "TODAY": 0, + "CURRENT_DATE + 1": 1, "TOMORROW": 1, + "CURRENT_DATE + 2": 2, + "CURRENT_DATE - 1": 3, + "CURRENT_DATE - 2": 4, + } + idx = _RELATIVE_MAP.get(s) + if idx is not None: self._modeCombo.setCurrentIndex(0) - self._relCombo.setCurrentIndex(4) - elif s == "CURRENT_DATE - 1": - self._modeCombo.setCurrentIndex(0) - self._relCombo.setCurrentIndex(3) - elif s in ("CURRENT_DATE", "TODAY"): - self._modeCombo.setCurrentIndex(0) - self._relCombo.setCurrentIndex(0) - elif s == "CURRENT_DATE + 1" or s == "TOMORROW": - self._modeCombo.setCurrentIndex(0) - self._relCombo.setCurrentIndex(1) - elif s == "CURRENT_DATE + 2": - self._modeCombo.setCurrentIndex(0) - self._relCombo.setCurrentIndex(2) + self._relCombo.setCurrentIndex(idx) elif s.startswith("DATE("): self._modeCombo.setCurrentIndex(1) m = re.match(r"DATE\((\d{4}-\d{2}-\d{2})\)", s) @@ -585,38 +591,6 @@ def encodeValueStr( return raw_value -def splitTopLevel( - text: str, - delimiter: str -) -> list: - - parts = [] - depth = 0 - buf = "" - i = 0 - textUpper = text.upper() - delimUpper = delimiter.upper() - dlen = len(delimUpper) - while i < len(text): - if text[i] == "(": - depth += 1 - buf += text[i] - elif text[i] == ")": - depth -= 1 - buf += text[i] - elif depth == 0 and textUpper[i:i + dlen] == delimUpper: - parts.append(buf) - buf = "" - i += dlen - continue - else: - buf += text[i] - i += 1 - if buf.strip(): - parts.append(buf) - return parts - - def stripOuterParens( s: str ) -> str: diff --git a/src/gui/ALAutoScriptOrchDialog/_widgets.py b/src/gui/ALAutoScriptOrchDialog/_widgets.py index d0fced6..800e07a 100644 --- a/src/gui/ALAutoScriptOrchDialog/_widgets.py +++ b/src/gui/ALAutoScriptOrchDialog/_widgets.py @@ -80,21 +80,21 @@ class ConditionRowFrame(QFrame): ("变量", "variable"), ], min_width=70, parent=self) layout.addWidget(self._compTypeCombo) - self._rhsStack = QStackedWidget(self) - self._rhsStack.setFixedHeight(25) - self._literalStack = QStackedWidget(self) - self._literalStack.setFixedHeight(25) - self._literalWidgets = {} + self.rhsStack = QStackedWidget(self) + self.rhsStack.setFixedHeight(25) + self.literalStack = QStackedWidget(self) + self.literalStack.setFixedHeight(25) + self.literalWidgets = {} for vt in VAR_TYPE_ORDER: - w = makeValueWidget(vt, self._literalStack) - self._literalWidgets[vt] = w - self._literalStack.addWidget(w) - self._literalStack.setCurrentWidget(self._literalWidgets.get("String")) - self._rhsStack.addWidget(self._literalStack) + w = makeValueWidget(vt, self.literalStack) + self.literalWidgets[vt] = w + self.literalStack.addWidget(w) + self.literalStack.setCurrentWidget(self.literalWidgets.get("String")) + self.rhsStack.addWidget(self.literalStack) self.rhsVarCombo = makeVarRefCombo(self) - self._rhsStack.addWidget(self.rhsVarCombo) - self._rhsStack.setCurrentIndex(0) - layout.addWidget(self._rhsStack) + self.rhsStack.addWidget(self.rhsVarCombo) + self.rhsStack.setCurrentIndex(0) + layout.addWidget(self.rhsStack) if not self._isFirst: self.deleteBtn = QPushButton("×", self) self.deleteBtn.setFixedSize(25, 25) @@ -127,7 +127,6 @@ class ConditionRowFrame(QFrame): self.leftVarCombo.currentIndexChanged.connect(self.onLeftVarChanged) self._compTypeCombo.currentIndexChanged.connect(self.onCompTypeChanged) - @Slot(int) def onLeftVarChanged( self, @@ -148,10 +147,9 @@ class ConditionRowFrame(QFrame): vartype: str ): - if vartype not in self._literalWidgets: + if vartype not in self.literalWidgets: vartype = "String" - self._literalStack.setCurrentWidget(self._literalWidgets[vartype]) - + self.literalStack.setCurrentWidget(self.literalWidgets[vartype]) @Slot(int) def onCompTypeChanged( @@ -160,7 +158,7 @@ class ConditionRowFrame(QFrame): ): isVar = (self._compTypeCombo.currentData() == "variable") - self._rhsStack.setCurrentIndex(1 if isVar else 0) + self.rhsStack.setCurrentIndex(1 if isVar else 0) if isVar: self.populateRhsVarCombo() @@ -191,7 +189,7 @@ class ConditionRowFrame(QFrame): if rhsText: return f"{name} {opSym} {rhsText}" return "" - w = self._literalWidgets.get(vartype) + w = self.literalWidgets.get(vartype) if w: rawVal = getValueFromWidget(w) encoded = encodeValueStr(rawVal, vartype) @@ -232,7 +230,7 @@ class ConditionRowFrame(QFrame): self.rhsVarCombo.setCurrentIndex(self.rhsVarCombo.count() - 1) else: self._compTypeCombo.setCurrentIndex(0) - w = self._literalWidgets.get(vartype) + w = self.literalWidgets.get(vartype) if w: setWidgetValue(w, vartype, valueExpr) @@ -291,15 +289,15 @@ class ActionStepFrame(QFrame): self.buildTargetCombo() layout.addWidget(self.targetCombo) layout.addWidget(makeLabel("为", self)) - self._valueSrcCombo = makeComboWidget([ + self.valueSrcCombo = makeComboWidget([ ("特定值", "literal"), ("变量", "variable"), ], min_width=70, parent=self) - layout.addWidget(self._valueSrcCombo) - self._valueStack = QStackedWidget(self) - self._valueStack.setFixedHeight(25) + layout.addWidget(self.valueSrcCombo) + self.valueStack = QStackedWidget(self) + self.valueStack.setFixedHeight(25) self.initValueStacks() - layout.addWidget(self._valueStack) + layout.addWidget(self.valueStack) self.existingVarCombo = makeVarRefCombo(self) self.existingVarCombo.setVisible(False) layout.addWidget(self.existingVarCombo) @@ -335,16 +333,16 @@ class ActionStepFrame(QFrame): self._literalWidgets = {} self._offsetWidgets = {} for vt in VAR_TYPE_ORDER: - self._literalWidgets[vt] = makeValueWidget(vt, self._valueStack) - self._valueStack.addWidget(self._literalWidgets[vt]) + self._literalWidgets[vt] = makeValueWidget(vt, self.valueStack) + self.valueStack.addWidget(self._literalWidgets[vt]) if vt in ARITH_TYPES: - self._offsetWidgets[vt] = makeOffsetWidget(vt, self._valueStack) - self._valueStack.addWidget(self._offsetWidgets[vt]) + self._offsetWidgets[vt] = makeOffsetWidget(vt, self.valueStack) + self.valueStack.addWidget(self._offsetWidgets[vt]) else: - lbl = QLabel("(不支持该操作)", self._valueStack) + lbl = QLabel("(不支持该操作)", self.valueStack) lbl.setFixedHeight(25) self._offsetWidgets[vt] = lbl - self._valueStack.addWidget(lbl) + self.valueStack.addWidget(lbl) def connectSignals( @@ -353,7 +351,7 @@ class ActionStepFrame(QFrame): self.opTypeCombo.currentIndexChanged.connect(self.onOpTypeChanged) self.targetCombo.currentIndexChanged.connect(self.onTargetChanged) - self._valueSrcCombo.currentIndexChanged.connect(self.onValueSrcChanged) + self.valueSrcCombo.currentIndexChanged.connect(self.onValueSrcChanged) @Slot(int) def onTargetChanged( @@ -369,7 +367,7 @@ class ActionStepFrame(QFrame): _, vartype = data self._currentTargetType = vartype self.updateRHSWidget() - self.onValueSrcChanged(self._valueSrcCombo.currentIndex()) + self.onValueSrcChanged(self.valueSrcCombo.currentIndex()) @Slot(int) def onOpTypeChanged( @@ -388,11 +386,11 @@ class ActionStepFrame(QFrame): isArith = (op in ("add", "sub")) actualType = self._currentTargetType if isArith and actualType in self._offsetWidgets: - self._valueStack.setCurrentWidget(self._offsetWidgets[actualType]) + self.valueStack.setCurrentWidget(self._offsetWidgets[actualType]) elif actualType in self._literalWidgets: - self._valueStack.setCurrentWidget(self._literalWidgets[actualType]) + self.valueStack.setCurrentWidget(self._literalWidgets[actualType]) else: - self._valueStack.setCurrentWidget(self._literalWidgets.get("String")) + self.valueStack.setCurrentWidget(self._literalWidgets.get("String")) @Slot(int) def onValueSrcChanged( @@ -400,8 +398,8 @@ class ActionStepFrame(QFrame): idx ): - isVar = (self._valueSrcCombo.currentData() == "variable") - self._valueStack.setVisible(not isVar) + isVar = (self.valueSrcCombo.currentData() == "variable") + self.valueStack.setVisible(not isVar) self.existingVarCombo.setVisible(isVar) if isVar: self._varMgr.populateCombo(self.existingVarCombo) @@ -437,20 +435,20 @@ class ActionStepFrame(QFrame): return f" SET {target} = {encoded}" elif op == "add": vartype = self._currentTargetType - if vartype == "Date" and hasattr(self._valueStack.currentWidget(), "getOffsetDays"): - days = self._valueStack.currentWidget().getOffsetDays() + if vartype == "Date" and hasattr(self.valueStack.currentWidget(), "getOffsetDays"): + days = self.valueStack.currentWidget().getOffsetDays() return f" {target} .ADD. {days}" - if vartype == "Time" and hasattr(self._valueStack.currentWidget(), "getOffsetHours"): - hours = self._valueStack.currentWidget().getOffsetHours() + if vartype == "Time" and hasattr(self.valueStack.currentWidget(), "getOffsetHours"): + hours = self.valueStack.currentWidget().getOffsetHours() return f" {target} .ADD. {hours}" return f" {target} .ADD. {rawVal}" elif op == "sub": vartype = self._currentTargetType - if vartype == "Date" and hasattr(self._valueStack.currentWidget(), "getOffsetDays"): - days = self._valueStack.currentWidget().getOffsetDays() + if vartype == "Date" and hasattr(self.valueStack.currentWidget(), "getOffsetDays"): + days = self.valueStack.currentWidget().getOffsetDays() return f" {target} .SUB. {days}" - if vartype == "Time" and hasattr(self._valueStack.currentWidget(), "getOffsetHours"): - hours = self._valueStack.currentWidget().getOffsetHours() + if vartype == "Time" and hasattr(self.valueStack.currentWidget(), "getOffsetHours"): + hours = self.valueStack.currentWidget().getOffsetHours() return f" {target} .SUB. {hours}" return f" {target} .SUB. {rawVal}" return "" @@ -460,9 +458,9 @@ class ActionStepFrame(QFrame): self ) -> str: - if self._valueSrcCombo.currentData() == "variable": + if self.valueSrcCombo.currentData() == "variable": return self.existingVarCombo.currentText().strip() - w = self._valueStack.currentWidget() + w = self.valueStack.currentWidget() if w: return getValueFromWidget(w) return "" @@ -504,7 +502,7 @@ class ActionStepFrame(QFrame): return up = s.upper() if isVarReference(s): - self._valueSrcCombo.setCurrentIndex(1) + self.valueSrcCombo.setCurrentIndex(1) self._varMgr.populateCombo(self.existingVarCombo) idx = self._varMgr.findExactNameEntry(self.existingVarCombo, up) if idx >= 0: @@ -513,8 +511,8 @@ class ActionStepFrame(QFrame): self.existingVarCombo.addItem(up, (up, "String")) self.existingVarCombo.setCurrentIndex(self.existingVarCombo.count() - 1) else: - self._valueSrcCombo.setCurrentIndex(0) - w = self._valueStack.currentWidget() + self.valueSrcCombo.setCurrentIndex(0) + w = self.valueStack.currentWidget() if w: setWidgetValue(w, self._currentTargetType, expr)