mirror of
https://github.com/KenanZhu/AutoLibrary.git
synced 2026-06-17 23:13:03 +08:00
feat(gui): 编排窗口支持算术表达式解析与回显
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -560,6 +560,8 @@ def encodeValueStr(
|
|||||||
var_type: str
|
var_type: str
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
||||||
|
if isArithExpr(raw_value):
|
||||||
|
return raw_value
|
||||||
if var_type == "Time":
|
if var_type == "Time":
|
||||||
if raw_value.startswith("+") or raw_value.startswith("-"):
|
if raw_value.startswith("+") or raw_value.startswith("-"):
|
||||||
return raw_value
|
return raw_value
|
||||||
@@ -609,6 +611,20 @@ def stripOuterParens(
|
|||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
# Pre-compiled pattern for detecting arithmetic expressions like "A + B" / "C - D"
|
||||||
|
_RE_ARITH_EXPR = re.compile(r'^.+?\s+[+-]\s+.+$')
|
||||||
|
|
||||||
|
|
||||||
|
def isArithExpr(
|
||||||
|
expr: str
|
||||||
|
) -> bool:
|
||||||
|
"""
|
||||||
|
Return True if expr looks like a two-operand arithmetic expression (A ± B).
|
||||||
|
"""
|
||||||
|
|
||||||
|
return bool(_RE_ARITH_EXPR.match(expr.strip()))
|
||||||
|
|
||||||
|
|
||||||
def isVarReference(
|
def isVarReference(
|
||||||
expr: str
|
expr: str
|
||||||
) -> bool:
|
) -> bool:
|
||||||
@@ -623,6 +639,8 @@ def isVarReference(
|
|||||||
return False
|
return False
|
||||||
if re.match(r"^[+-]?\d", s):
|
if re.match(r"^[+-]?\d", s):
|
||||||
return False
|
return False
|
||||||
|
if isArithExpr(s):
|
||||||
|
return False
|
||||||
return bool(re.match(r"^[A-Z_][A-Z0-9_]*$", up))
|
return bool(re.match(r"^[A-Z_][A-Z0-9_]*$", up))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -57,9 +57,11 @@ class ScriptOrchObserver(ParsingObserver):
|
|||||||
if kind == K_SET:
|
if kind == K_SET:
|
||||||
self._current_actions.append((target, value, "set"))
|
self._current_actions.append((target, value, "set"))
|
||||||
elif kind == K_ADD:
|
elif kind == K_ADD:
|
||||||
self._current_actions.append((target, f"+{value}", "add"))
|
prefixed = value if value.startswith("-") else f"+{value}"
|
||||||
|
self._current_actions.append((target, prefixed, "add"))
|
||||||
else:
|
else:
|
||||||
self._current_actions.append((target, f"-{value}", "sub"))
|
prefixed = value if value.startswith("-") else f"-{value}"
|
||||||
|
self._current_actions.append((target, prefixed, "sub"))
|
||||||
elif kind == K_ENDIF:
|
elif kind == K_ENDIF:
|
||||||
self._flushCurrentBlock()
|
self._flushCurrentBlock()
|
||||||
self._current_type = None
|
self._current_type = None
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
"""
|
"""
|
||||||
Widget components for the AutoScript orchestration dialog.
|
Widget components for the AutoScript orchestration dialog.
|
||||||
"""
|
"""
|
||||||
|
import re
|
||||||
|
|
||||||
from PySide6.QtCore import Slot
|
from PySide6.QtCore import Slot
|
||||||
from PySide6.QtWidgets import (
|
from PySide6.QtWidgets import (
|
||||||
QComboBox,
|
QComboBox,
|
||||||
@@ -21,6 +23,7 @@ from gui.ALAutoScriptOrchDialog._helpers import (
|
|||||||
VAR_TYPE_ORDER,
|
VAR_TYPE_ORDER,
|
||||||
encodeValueStr,
|
encodeValueStr,
|
||||||
getValueFromWidget,
|
getValueFromWidget,
|
||||||
|
isArithExpr,
|
||||||
isVarReference,
|
isVarReference,
|
||||||
makeComboWidget,
|
makeComboWidget,
|
||||||
makeLabel,
|
makeLabel,
|
||||||
@@ -46,6 +49,7 @@ class ConditionRowFrame(QFrame):
|
|||||||
self._blockIndex = parentBlockIndex
|
self._blockIndex = parentBlockIndex
|
||||||
self._isFirst = isFirst
|
self._isFirst = isFirst
|
||||||
self._isBoolMode = False
|
self._isBoolMode = False
|
||||||
|
self._rawRhsExpr = ""
|
||||||
|
|
||||||
self.setupUi()
|
self.setupUi()
|
||||||
self.connectSignals()
|
self.connectSignals()
|
||||||
@@ -133,6 +137,7 @@ class ConditionRowFrame(QFrame):
|
|||||||
idx
|
idx
|
||||||
):
|
):
|
||||||
|
|
||||||
|
self._rawRhsExpr = ""
|
||||||
if idx < 0:
|
if idx < 0:
|
||||||
return
|
return
|
||||||
data = self.leftVarCombo.itemData(idx)
|
data = self.leftVarCombo.itemData(idx)
|
||||||
@@ -157,6 +162,7 @@ class ConditionRowFrame(QFrame):
|
|||||||
idx
|
idx
|
||||||
):
|
):
|
||||||
|
|
||||||
|
self._rawRhsExpr = ""
|
||||||
isVar = (self._compTypeCombo.currentData() == "variable")
|
isVar = (self._compTypeCombo.currentData() == "variable")
|
||||||
self.rhsStack.setCurrentIndex(1 if isVar else 0)
|
self.rhsStack.setCurrentIndex(1 if isVar else 0)
|
||||||
if isVar:
|
if isVar:
|
||||||
@@ -179,6 +185,8 @@ class ConditionRowFrame(QFrame):
|
|||||||
return ""
|
return ""
|
||||||
name, vartype = data
|
name, vartype = data
|
||||||
opSym = self.opCombo.currentData()
|
opSym = self.opCombo.currentData()
|
||||||
|
if self._rawRhsExpr:
|
||||||
|
return f"{name} {opSym} {self._rawRhsExpr}"
|
||||||
isVarRef = (self._compTypeCombo.currentData() == "variable")
|
isVarRef = (self._compTypeCombo.currentData() == "variable")
|
||||||
if isVarRef:
|
if isVarRef:
|
||||||
rd = self.rhsVarCombo.currentData()
|
rd = self.rhsVarCombo.currentData()
|
||||||
@@ -204,21 +212,31 @@ class ConditionRowFrame(QFrame):
|
|||||||
valueExpr: str
|
valueExpr: str
|
||||||
):
|
):
|
||||||
|
|
||||||
for ci in range(self.leftVarCombo.count()):
|
self._rawRhsExpr = ""
|
||||||
d = self.leftVarCombo.itemData(ci)
|
self.leftVarCombo.blockSignals(True)
|
||||||
if d and d[0] == operandName:
|
self.opCombo.blockSignals(True)
|
||||||
self.leftVarCombo.setCurrentIndex(ci)
|
self._compTypeCombo.blockSignals(True)
|
||||||
break
|
try:
|
||||||
if opSym:
|
for ci in range(self.leftVarCombo.count()):
|
||||||
for oi in range(self.opCombo.count()):
|
d = self.leftVarCombo.itemData(ci)
|
||||||
if self.opCombo.itemData(oi) == opSym:
|
if d and d[0] == operandName:
|
||||||
self.opCombo.setCurrentIndex(oi)
|
self.leftVarCombo.setCurrentIndex(ci)
|
||||||
break
|
break
|
||||||
|
if opSym:
|
||||||
|
for oi in range(self.opCombo.count()):
|
||||||
|
if self.opCombo.itemData(oi) == opSym:
|
||||||
|
self.opCombo.setCurrentIndex(oi)
|
||||||
|
break
|
||||||
|
finally:
|
||||||
|
self.leftVarCombo.blockSignals(False)
|
||||||
|
self.opCombo.blockSignals(False)
|
||||||
|
self._compTypeCombo.blockSignals(False)
|
||||||
|
data = self.leftVarCombo.currentData()
|
||||||
|
vartype = data[1] if data else "String"
|
||||||
|
self.updateRhsLiteralWidget(vartype)
|
||||||
if not valueExpr:
|
if not valueExpr:
|
||||||
return
|
return
|
||||||
up = valueExpr.strip().upper()
|
up = valueExpr.strip().upper()
|
||||||
data = self.leftVarCombo.currentData()
|
|
||||||
vartype = data[1] if data else "String"
|
|
||||||
if isVarReference(valueExpr) or self._isKnownVar(up):
|
if isVarReference(valueExpr) or self._isKnownVar(up):
|
||||||
self._compTypeCombo.setCurrentIndex(1)
|
self._compTypeCombo.setCurrentIndex(1)
|
||||||
self.populateRhsVarCombo()
|
self.populateRhsVarCombo()
|
||||||
@@ -228,12 +246,45 @@ class ConditionRowFrame(QFrame):
|
|||||||
else:
|
else:
|
||||||
self.rhsVarCombo.addItem(up, (up, "String"))
|
self.rhsVarCombo.addItem(up, (up, "String"))
|
||||||
self.rhsVarCombo.setCurrentIndex(self.rhsVarCombo.count() - 1)
|
self.rhsVarCombo.setCurrentIndex(self.rhsVarCombo.count() - 1)
|
||||||
|
elif isArithExpr(valueExpr):
|
||||||
|
self._tryLoadCondArithExpr(valueExpr, vartype)
|
||||||
else:
|
else:
|
||||||
self._compTypeCombo.setCurrentIndex(0)
|
self._compTypeCombo.setCurrentIndex(0)
|
||||||
w = self.literalWidgets.get(vartype)
|
w = self.literalWidgets.get(vartype)
|
||||||
if w:
|
if w:
|
||||||
setWidgetValue(w, vartype, valueExpr)
|
setWidgetValue(w, vartype, valueExpr)
|
||||||
|
|
||||||
|
def _tryLoadCondArithExpr(
|
||||||
|
self,
|
||||||
|
expr: str,
|
||||||
|
vartype: str
|
||||||
|
):
|
||||||
|
"""Try to decompose a condition RHS arithmetic expression into UI state."""
|
||||||
|
|
||||||
|
s = expr.strip()
|
||||||
|
m = re.match(r'^(.+?)\s+([+-])\s+(.+)$', s)
|
||||||
|
if not m:
|
||||||
|
self._rawRhsExpr = s
|
||||||
|
return
|
||||||
|
left = m.group(1).strip()
|
||||||
|
op = m.group(2).strip()
|
||||||
|
right = m.group(3).strip()
|
||||||
|
left_up = left.upper()
|
||||||
|
|
||||||
|
if vartype == "Date" and left_up == "CURRENT_DATE":
|
||||||
|
try:
|
||||||
|
n = int(right)
|
||||||
|
offset = n if op == "+" else -n
|
||||||
|
if offset in (-2, -1, 0, 1, 2):
|
||||||
|
self._compTypeCombo.setCurrentIndex(0)
|
||||||
|
w = self.literalWidgets.get("Date")
|
||||||
|
if w and hasattr(w, "setValue"):
|
||||||
|
w.setValue(s)
|
||||||
|
return
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
self._rawRhsExpr = s
|
||||||
|
|
||||||
|
|
||||||
def _isKnownVar(
|
def _isKnownVar(
|
||||||
self,
|
self,
|
||||||
@@ -426,12 +477,9 @@ class ActionStepFrame(QFrame):
|
|||||||
rawVal = self._getValueRaw()
|
rawVal = self._getValueRaw()
|
||||||
if op == "set":
|
if op == "set":
|
||||||
vartype = self._currentTargetType
|
vartype = self._currentTargetType
|
||||||
|
if isArithExpr(rawVal):
|
||||||
|
return f" SET {target} = {rawVal}"
|
||||||
encoded = encodeValueStr(rawVal, vartype)
|
encoded = encodeValueStr(rawVal, vartype)
|
||||||
if vartype == "Time":
|
|
||||||
if rawVal.startswith("+"):
|
|
||||||
return f" {target} .ADD. {rawVal[1:]}"
|
|
||||||
if rawVal.startswith("-"):
|
|
||||||
return f" {target} .SUB. {rawVal[1:]}"
|
|
||||||
return f" SET {target} = {encoded}"
|
return f" SET {target} = {encoded}"
|
||||||
elif op == "add":
|
elif op == "add":
|
||||||
vartype = self._currentTargetType
|
vartype = self._currentTargetType
|
||||||
@@ -500,6 +548,12 @@ class ActionStepFrame(QFrame):
|
|||||||
s = expr.strip()
|
s = expr.strip()
|
||||||
if not s:
|
if not s:
|
||||||
return
|
return
|
||||||
|
op = self.opTypeCombo.currentData()
|
||||||
|
if op in ("add", "sub") and s.startswith("-"):
|
||||||
|
s = s[1:]
|
||||||
|
self.opTypeCombo.setCurrentIndex(
|
||||||
|
2 if op == "add" else 1
|
||||||
|
)
|
||||||
up = s.upper()
|
up = s.upper()
|
||||||
if isVarReference(s):
|
if isVarReference(s):
|
||||||
self.valueSrcCombo.setCurrentIndex(1)
|
self.valueSrcCombo.setCurrentIndex(1)
|
||||||
@@ -510,12 +564,81 @@ class ActionStepFrame(QFrame):
|
|||||||
else:
|
else:
|
||||||
self.existingVarCombo.addItem(up, (up, "String"))
|
self.existingVarCombo.addItem(up, (up, "String"))
|
||||||
self.existingVarCombo.setCurrentIndex(self.existingVarCombo.count() - 1)
|
self.existingVarCombo.setCurrentIndex(self.existingVarCombo.count() - 1)
|
||||||
|
elif isArithExpr(s):
|
||||||
|
self._tryLoadArithExpr(s)
|
||||||
else:
|
else:
|
||||||
self.valueSrcCombo.setCurrentIndex(0)
|
self.valueSrcCombo.setCurrentIndex(0)
|
||||||
w = self.valueStack.currentWidget()
|
w = self.valueStack.currentWidget()
|
||||||
if w:
|
if w:
|
||||||
setWidgetValue(w, self._currentTargetType, expr)
|
setWidgetValue(w, self._currentTargetType, expr)
|
||||||
|
|
||||||
|
def _tryLoadArithExpr(
|
||||||
|
self,
|
||||||
|
expr: str
|
||||||
|
):
|
||||||
|
"""Try to decompose an arithmetic expression into UI state."""
|
||||||
|
|
||||||
|
s = expr.strip()
|
||||||
|
m = re.match(r'^(.+?)\s+([+-])\s+(.+)$', s)
|
||||||
|
if not m:
|
||||||
|
self._storeAsCustomExpr(s)
|
||||||
|
return
|
||||||
|
left = m.group(1).strip()
|
||||||
|
op = m.group(2).strip()
|
||||||
|
right = m.group(3).strip()
|
||||||
|
left_up = left.upper()
|
||||||
|
|
||||||
|
# CURRENT_DATE ± N for Date targets — try relative combo for ±0/1/2,
|
||||||
|
# otherwise store as custom expression to preserve relative semantics
|
||||||
|
if self._currentTargetType == "Date" and left_up == "CURRENT_DATE":
|
||||||
|
try:
|
||||||
|
n = int(right)
|
||||||
|
offset = n if op == "+" else -n
|
||||||
|
if offset in (-2, -1, 0, 1, 2):
|
||||||
|
w = self._literalWidgets.get("Date")
|
||||||
|
if w and hasattr(w, "setValue"):
|
||||||
|
w.setValue(s)
|
||||||
|
self.valueSrcCombo.setCurrentIndex(0)
|
||||||
|
return
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
self._storeAsCustomExpr(s)
|
||||||
|
return
|
||||||
|
|
||||||
|
# CURRENT_TIME ± N for Time targets — map to add/sub with offset
|
||||||
|
if self._currentTargetType == "Time" and left_up == "CURRENT_TIME":
|
||||||
|
try:
|
||||||
|
hours = int(right)
|
||||||
|
if op == "-":
|
||||||
|
hours = -hours
|
||||||
|
self.opTypeCombo.setCurrentIndex(
|
||||||
|
1 if hours >= 0 else 2
|
||||||
|
)
|
||||||
|
self.valueSrcCombo.setCurrentIndex(0)
|
||||||
|
w = self._offsetWidgets.get("Time")
|
||||||
|
if w and hasattr(w, "setValue"):
|
||||||
|
w.setValue(str(abs(hours)))
|
||||||
|
return
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self._storeAsCustomExpr(s)
|
||||||
|
|
||||||
|
def _storeAsCustomExpr(
|
||||||
|
self,
|
||||||
|
expr: str
|
||||||
|
):
|
||||||
|
"""Store a raw expression in the variable combo when it can't be decomposed."""
|
||||||
|
|
||||||
|
self.valueSrcCombo.setCurrentIndex(1)
|
||||||
|
self._varMgr.populateCombo(self.existingVarCombo)
|
||||||
|
found = self._varMgr.findExactNameEntry(self.existingVarCombo, expr)
|
||||||
|
if found < 0:
|
||||||
|
self.existingVarCombo.addItem(expr, (expr, self._currentTargetType))
|
||||||
|
self.existingVarCombo.setCurrentIndex(self.existingVarCombo.count() - 1)
|
||||||
|
else:
|
||||||
|
self.existingVarCombo.setCurrentIndex(found)
|
||||||
|
|
||||||
|
|
||||||
def refreshVarCombos(
|
def refreshVarCombos(
|
||||||
self
|
self
|
||||||
|
|||||||
Reference in New Issue
Block a user