mirror of
https://github.com/KenanZhu/AutoLibrary.git
synced 2026-06-18 23:43:02 +08:00
refactor(style): 统一项目代码风格,整理导入顺序、间距规范与方法排列
- GUI 模块统一 QtCore → QtGui → QtWidgets 导入排列,各类独占一行按字母排序 - 统一类间两空行、类内方法间一空行、函数间一空行的间距规范 - 统一方法排列顺序:__init__ → setupUi → connectSignals → public → Slot → private - 统一 _widgets 中 ConditionRowFrame/ActionStepFrame 方法命名(populate* / toScript / updateValueWidget) - LibTimeSelector 迁入 operators/abs 抽象层 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -47,7 +47,6 @@ class ConditionalBlock(QGroupBox):
|
||||
self.connectSignals()
|
||||
self.addInitialConditionRow()
|
||||
|
||||
|
||||
def setupUi(
|
||||
self
|
||||
):
|
||||
@@ -103,7 +102,6 @@ class ConditionalBlock(QGroupBox):
|
||||
mainLayout.addWidget(self.addActionBtn)
|
||||
self.setUpdatesEnabled(True)
|
||||
|
||||
|
||||
def connectSignals(
|
||||
self
|
||||
):
|
||||
@@ -112,7 +110,6 @@ class ConditionalBlock(QGroupBox):
|
||||
self.addCondBtn.clicked.connect(self.addConditionRow)
|
||||
self.addActionBtn.clicked.connect(self.addActionStep)
|
||||
|
||||
|
||||
def addInitialConditionRow(
|
||||
self
|
||||
):
|
||||
@@ -124,7 +121,6 @@ class ConditionalBlock(QGroupBox):
|
||||
self._conditionRows.append(row)
|
||||
self.condRowsLayout.addWidget(row)
|
||||
|
||||
|
||||
def addConditionRow(
|
||||
self
|
||||
):
|
||||
@@ -137,7 +133,6 @@ class ConditionalBlock(QGroupBox):
|
||||
self._conditionRows.append(row)
|
||||
self.condRowsLayout.addWidget(row)
|
||||
|
||||
|
||||
def removeConditionRow(
|
||||
self,
|
||||
row: ConditionRowFrame
|
||||
@@ -149,7 +144,6 @@ class ConditionalBlock(QGroupBox):
|
||||
row.hide()
|
||||
row.deleteLater()
|
||||
|
||||
|
||||
def addActionStep(
|
||||
self
|
||||
):
|
||||
@@ -159,7 +153,6 @@ class ConditionalBlock(QGroupBox):
|
||||
self._actionWidgets.append(step)
|
||||
self.actionsLayout.addWidget(step)
|
||||
|
||||
|
||||
def removeActionStep(
|
||||
self,
|
||||
step: ActionStepFrame
|
||||
@@ -171,48 +164,31 @@ class ConditionalBlock(QGroupBox):
|
||||
step.hide()
|
||||
step.deleteLater()
|
||||
|
||||
@Slot(int)
|
||||
def onTypeChanged(
|
||||
self,
|
||||
_idx
|
||||
):
|
||||
|
||||
isCond = self.typeCombo.currentData() in ("IF", "ELSE IF")
|
||||
self.conditionWidget.setVisible(isCond)
|
||||
self.actionLabel.setText(
|
||||
"执行步骤:" if isCond else "ELSE 执行步骤:"
|
||||
)
|
||||
|
||||
|
||||
def getBlockType(
|
||||
self
|
||||
) -> str:
|
||||
|
||||
return self.typeCombo.currentData()
|
||||
|
||||
|
||||
def getConditionRows(
|
||||
self
|
||||
):
|
||||
|
||||
return list(self._conditionRows)
|
||||
|
||||
|
||||
def getActionSteps(
|
||||
self
|
||||
):
|
||||
|
||||
return list(self._actionWidgets)
|
||||
|
||||
|
||||
def countActionSteps(
|
||||
self
|
||||
) -> int:
|
||||
|
||||
return len(self._actionWidgets)
|
||||
|
||||
|
||||
def toScriptLines(
|
||||
def toScript(
|
||||
self
|
||||
) -> list:
|
||||
"""
|
||||
@@ -223,7 +199,7 @@ class ConditionalBlock(QGroupBox):
|
||||
lines = []
|
||||
if blockType in ("IF", "ELSE IF"):
|
||||
condTexts = [
|
||||
r.toConditionText() for r in self._conditionRows if r.toConditionText()
|
||||
r.toScript() for r in self._conditionRows if r.toScript()
|
||||
]
|
||||
if not condTexts:
|
||||
condTexts = ["true"]
|
||||
@@ -245,12 +221,11 @@ class ConditionalBlock(QGroupBox):
|
||||
else:
|
||||
lines.append("else")
|
||||
for step in self._actionWidgets:
|
||||
scriptLine = step.toScriptLine()
|
||||
scriptLine = step.toScript()
|
||||
if scriptLine:
|
||||
lines.append(scriptLine)
|
||||
return lines
|
||||
|
||||
|
||||
def refreshVarCombos(
|
||||
self
|
||||
):
|
||||
@@ -260,7 +235,6 @@ class ConditionalBlock(QGroupBox):
|
||||
for step in self._actionWidgets:
|
||||
step.refreshVarCombos()
|
||||
|
||||
|
||||
def setPrevBlockType(
|
||||
self,
|
||||
prevType: str | None
|
||||
@@ -278,3 +252,15 @@ class ConditionalBlock(QGroupBox):
|
||||
item.setEnabled(shouldEnable)
|
||||
if prevType == "ELSE" and self.typeCombo.currentData() in ("ELSE IF", "ELSE"):
|
||||
self.typeCombo.setCurrentIndex(0)
|
||||
|
||||
@Slot(int)
|
||||
def onTypeChanged(
|
||||
self,
|
||||
_idx
|
||||
):
|
||||
|
||||
isCond = self.typeCombo.currentData() in ("IF", "ELSE IF")
|
||||
self.conditionWidget.setVisible(isCond)
|
||||
self.actionLabel.setText(
|
||||
"执行步骤:" if isCond else "ELSE 执行步骤:"
|
||||
)
|
||||
|
||||
@@ -42,7 +42,6 @@ class ALAutoScriptOrchDialog(QDialog):
|
||||
self.addBlock()
|
||||
self.scrollLayout.addStretch()
|
||||
|
||||
|
||||
def setupUi(
|
||||
self
|
||||
):
|
||||
@@ -70,7 +69,6 @@ class ALAutoScriptOrchDialog(QDialog):
|
||||
self.btnBox.button(QDialogButtonBox.StandardButton.Cancel).setText("取消")
|
||||
mainLayout.addWidget(self.btnBox)
|
||||
|
||||
|
||||
def connectSignals(
|
||||
self
|
||||
):
|
||||
@@ -79,8 +77,7 @@ class ALAutoScriptOrchDialog(QDialog):
|
||||
self.btnBox.rejected.connect(self.reject)
|
||||
self.addBlockBtn.clicked.connect(self.addBlock)
|
||||
|
||||
|
||||
def _updateBlockTypeRestrictions(
|
||||
def updateBlockTypeRestrictions(
|
||||
self
|
||||
):
|
||||
|
||||
@@ -89,7 +86,6 @@ class ALAutoScriptOrchDialog(QDialog):
|
||||
block.setPrevBlockType(prevType)
|
||||
prevType = block.getBlockType()
|
||||
|
||||
|
||||
def addBlock(
|
||||
self
|
||||
):
|
||||
@@ -98,10 +94,10 @@ class ALAutoScriptOrchDialog(QDialog):
|
||||
len(self._blocks), self._varMgr, parent=self
|
||||
)
|
||||
block.deleteBlockBtn.clicked.connect(lambda: self.removeBlock(block))
|
||||
block.typeCombo.currentIndexChanged.connect(self._updateBlockTypeRestrictions)
|
||||
block.typeCombo.currentIndexChanged.connect(self.updateBlockTypeRestrictions)
|
||||
block.addActionStep()
|
||||
self._blocks.append(block)
|
||||
self._updateBlockTypeRestrictions()
|
||||
self.updateBlockTypeRestrictions()
|
||||
if self.scrollLayout.count() > 0:
|
||||
lastItem = self.scrollLayout.itemAt(
|
||||
self.scrollLayout.count() - 1
|
||||
@@ -113,7 +109,6 @@ class ALAutoScriptOrchDialog(QDialog):
|
||||
return
|
||||
self.scrollLayout.addWidget(block)
|
||||
|
||||
|
||||
def removeBlock(
|
||||
self,
|
||||
block: ConditionalBlock
|
||||
@@ -135,8 +130,7 @@ class ALAutoScriptOrchDialog(QDialog):
|
||||
else:
|
||||
blk.typeCombo.setEnabled(True)
|
||||
blk.refreshVarCombos()
|
||||
self._updateBlockTypeRestrictions()
|
||||
|
||||
self.updateBlockTypeRestrictions()
|
||||
|
||||
def getScript(
|
||||
self
|
||||
@@ -151,7 +145,7 @@ class ALAutoScriptOrchDialog(QDialog):
|
||||
blockType = block.getBlockType()
|
||||
if blockType == "IF" and prevType is not None:
|
||||
parts.append("end")
|
||||
lines = block.toScriptLines()
|
||||
lines = block.toScript()
|
||||
parts.extend(lines)
|
||||
prevType = blockType
|
||||
if self._blocks and self._blocks[0].getBlockType() == "IF":
|
||||
|
||||
@@ -95,181 +95,6 @@ DATE_OFFSET_UNITS = [
|
||||
]
|
||||
|
||||
|
||||
class VariableManager(QObject):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
parent = None
|
||||
):
|
||||
|
||||
super().__init__(parent)
|
||||
self._vars = []
|
||||
self._nameMap = {}
|
||||
|
||||
self._initPresetVars()
|
||||
|
||||
|
||||
def _initPresetVars(
|
||||
self
|
||||
):
|
||||
|
||||
for p in PRESET_VARIABLES:
|
||||
entry = {"name": p["name"], "type": p["type"], "display": p["display"]}
|
||||
self._vars.append(entry)
|
||||
self._nameMap[p["name"]] = entry
|
||||
|
||||
|
||||
def getInfoByName(
|
||||
self,
|
||||
name: str
|
||||
):
|
||||
|
||||
return self._nameMap.get(name.upper().strip())
|
||||
|
||||
|
||||
def populateCombo(
|
||||
self,
|
||||
combo: QComboBox
|
||||
):
|
||||
|
||||
currentData = combo.currentData()
|
||||
combo.blockSignals(True)
|
||||
combo.clear()
|
||||
for entry in self._vars:
|
||||
combo.addItem(
|
||||
entry["display"],
|
||||
(entry["name"], entry["type"])
|
||||
)
|
||||
if currentData:
|
||||
for i in range(combo.count()):
|
||||
d = combo.itemData(i)
|
||||
if d and d[0] == currentData[0]:
|
||||
combo.setCurrentIndex(i)
|
||||
break
|
||||
combo.blockSignals(False)
|
||||
|
||||
|
||||
def findExactNameEntry(
|
||||
self,
|
||||
combo: QComboBox,
|
||||
name: str
|
||||
) -> int:
|
||||
|
||||
name = name.upper().strip()
|
||||
for i in range(combo.count()):
|
||||
d = combo.itemData(i)
|
||||
if d and len(d) >= 1 and d[0].upper().strip() == name:
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
||||
def makeValueWidget(
|
||||
var_type: str,
|
||||
parent: QWidget = None
|
||||
) -> QWidget:
|
||||
|
||||
if var_type == "Int":
|
||||
w = QSpinBox(parent)
|
||||
w.setRange(-999999, 999999)
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(100)
|
||||
return w
|
||||
if var_type == "Float":
|
||||
w = QDoubleSpinBox(parent)
|
||||
w.setRange(-999999.0, 999999.0)
|
||||
w.setDecimals(2)
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(100)
|
||||
return w
|
||||
if var_type == "String":
|
||||
w = QLineEdit(parent)
|
||||
w.setPlaceholderText("输入值")
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(120)
|
||||
return w
|
||||
if var_type == "Boolean":
|
||||
w = QComboBox(parent)
|
||||
w.addItem("是 (true)", "true")
|
||||
w.addItem("否 (false)", "false")
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(100)
|
||||
return w
|
||||
if var_type == "Date":
|
||||
return _DateInputContainer(parent)
|
||||
if var_type == "Time":
|
||||
return _TimeInputContainer(parent)
|
||||
w = QLineEdit(parent)
|
||||
w.setPlaceholderText("输入值")
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(120)
|
||||
return w
|
||||
|
||||
|
||||
def makeOffsetWidget(
|
||||
var_type: str,
|
||||
parent: QWidget = None
|
||||
) -> QWidget:
|
||||
|
||||
if var_type == "Int":
|
||||
w = QSpinBox(parent)
|
||||
w.setRange(-999999, 999999)
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(100)
|
||||
return w
|
||||
if var_type == "Float":
|
||||
w = QDoubleSpinBox(parent)
|
||||
w.setRange(-999999.0, 999999.0)
|
||||
w.setDecimals(2)
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(100)
|
||||
return w
|
||||
if var_type == "Date":
|
||||
return _DateOffsetContainer(parent)
|
||||
if var_type == "Time":
|
||||
return _TimeOffsetContainer(parent)
|
||||
w = QLabel("(不支持该操作)", parent)
|
||||
w.setFixedHeight(25)
|
||||
return w
|
||||
|
||||
|
||||
def makeVarRefCombo(
|
||||
parent: QWidget = None
|
||||
) -> QComboBox:
|
||||
|
||||
cb = QComboBox(parent)
|
||||
cb.setFixedHeight(25)
|
||||
cb.setMinimumWidth(120)
|
||||
cb.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
|
||||
return cb
|
||||
|
||||
|
||||
def makeComboWidget(
|
||||
items,
|
||||
min_width: int = 80,
|
||||
parent: QWidget = None
|
||||
) -> QComboBox:
|
||||
|
||||
cb = QComboBox(parent)
|
||||
for display, data in items:
|
||||
cb.addItem(display, data)
|
||||
cb.setFixedHeight(25)
|
||||
cb.setMinimumWidth(min_width)
|
||||
return cb
|
||||
|
||||
|
||||
def makeLabel(
|
||||
text: str,
|
||||
parent: QWidget = None,
|
||||
width: int = None
|
||||
) -> QLabel:
|
||||
|
||||
lbl = QLabel(text, parent)
|
||||
lbl.setFixedHeight(25)
|
||||
if width:
|
||||
lbl.setFixedWidth(width)
|
||||
return lbl
|
||||
|
||||
|
||||
class _DateInputContainer(QWidget):
|
||||
|
||||
def __init__(
|
||||
@@ -281,7 +106,6 @@ class _DateInputContainer(QWidget):
|
||||
self._dynamicItems = {} # index -> raw expression, for one-way parsed items
|
||||
self.setupUi()
|
||||
|
||||
|
||||
def setupUi(
|
||||
self
|
||||
):
|
||||
@@ -327,7 +151,6 @@ class _DateInputContainer(QWidget):
|
||||
return self._relCombo.currentText()
|
||||
return self._dateEdit.date().toString("yyyy-MM-dd")
|
||||
|
||||
|
||||
def setValue(
|
||||
self,
|
||||
expr: str
|
||||
@@ -392,14 +215,12 @@ class _TimeInputContainer(QWidget):
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
layout.addWidget(self._timeEdit)
|
||||
|
||||
|
||||
def getValue(
|
||||
self
|
||||
) -> str:
|
||||
|
||||
return self._timeEdit.time().toString("HH:mm")
|
||||
|
||||
|
||||
def setValue(
|
||||
self,
|
||||
expr: str
|
||||
@@ -443,14 +264,12 @@ class _DateOffsetContainer(QWidget):
|
||||
layout.addWidget(self._unitCombo)
|
||||
layout.addStretch()
|
||||
|
||||
|
||||
def getValue(
|
||||
self
|
||||
) -> str:
|
||||
|
||||
return str(self.getOffsetDays())
|
||||
|
||||
|
||||
def setValue(
|
||||
self,
|
||||
expr: str
|
||||
@@ -462,7 +281,6 @@ class _DateOffsetContainer(QWidget):
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
||||
def getOffsetDays(
|
||||
self
|
||||
) -> int:
|
||||
@@ -477,7 +295,6 @@ class _DateOffsetContainer(QWidget):
|
||||
return val * 365
|
||||
return val
|
||||
|
||||
|
||||
def getRawValue(
|
||||
self
|
||||
) -> str:
|
||||
@@ -502,14 +319,12 @@ class _TimeOffsetContainer(QWidget):
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
layout.addWidget(self._spinBox)
|
||||
|
||||
|
||||
def getValue(
|
||||
self
|
||||
) -> str:
|
||||
|
||||
return str(self.getOffsetHours())
|
||||
|
||||
|
||||
def setValue(
|
||||
self,
|
||||
expr: str
|
||||
@@ -521,14 +336,12 @@ class _TimeOffsetContainer(QWidget):
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
||||
def getOffsetHours(
|
||||
self
|
||||
) -> int:
|
||||
|
||||
return self._spinBox.value()
|
||||
|
||||
|
||||
def getRawValue(
|
||||
self
|
||||
) -> str:
|
||||
@@ -536,6 +349,172 @@ class _TimeOffsetContainer(QWidget):
|
||||
return str(self._spinBox.value())
|
||||
|
||||
|
||||
class VariableManager(QObject):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
parent = None
|
||||
):
|
||||
|
||||
super().__init__(parent)
|
||||
self._vars = []
|
||||
self._nameMap = {}
|
||||
|
||||
self._initPresetVars()
|
||||
|
||||
def _initPresetVars(
|
||||
self
|
||||
):
|
||||
|
||||
for p in PRESET_VARIABLES:
|
||||
entry = {"name": p["name"], "type": p["type"], "display": p["display"]}
|
||||
self._vars.append(entry)
|
||||
self._nameMap[p["name"]] = entry
|
||||
|
||||
def getInfoByName(
|
||||
self,
|
||||
name: str
|
||||
):
|
||||
|
||||
return self._nameMap.get(name.upper().strip())
|
||||
|
||||
def populateCombo(
|
||||
self,
|
||||
combo: QComboBox
|
||||
):
|
||||
|
||||
currentData = combo.currentData()
|
||||
combo.blockSignals(True)
|
||||
combo.clear()
|
||||
for entry in self._vars:
|
||||
combo.addItem(
|
||||
entry["display"],
|
||||
(entry["name"], entry["type"])
|
||||
)
|
||||
if currentData:
|
||||
for i in range(combo.count()):
|
||||
d = combo.itemData(i)
|
||||
if d and d[0] == currentData[0]:
|
||||
combo.setCurrentIndex(i)
|
||||
break
|
||||
combo.blockSignals(False)
|
||||
|
||||
def findExactNameEntry(
|
||||
self,
|
||||
combo: QComboBox,
|
||||
name: str
|
||||
) -> int:
|
||||
|
||||
name = name.upper().strip()
|
||||
for i in range(combo.count()):
|
||||
d = combo.itemData(i)
|
||||
if d and len(d) >= 1 and d[0].upper().strip() == name:
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
||||
def makeValueWidget(
|
||||
var_type: str,
|
||||
parent: QWidget = None
|
||||
) -> QWidget:
|
||||
|
||||
if var_type == "Int":
|
||||
w = QSpinBox(parent)
|
||||
w.setRange(-999999, 999999)
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(100)
|
||||
return w
|
||||
if var_type == "Float":
|
||||
w = QDoubleSpinBox(parent)
|
||||
w.setRange(-999999.0, 999999.0)
|
||||
w.setDecimals(2)
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(100)
|
||||
return w
|
||||
if var_type == "String":
|
||||
w = QLineEdit(parent)
|
||||
w.setPlaceholderText("输入值")
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(120)
|
||||
return w
|
||||
if var_type == "Boolean":
|
||||
w = QComboBox(parent)
|
||||
w.addItem("是 (true)", "true")
|
||||
w.addItem("否 (false)", "false")
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(100)
|
||||
return w
|
||||
if var_type == "Date":
|
||||
return _DateInputContainer(parent)
|
||||
if var_type == "Time":
|
||||
return _TimeInputContainer(parent)
|
||||
w = QLineEdit(parent)
|
||||
w.setPlaceholderText("输入值")
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(120)
|
||||
return w
|
||||
|
||||
def makeOffsetWidget(
|
||||
var_type: str,
|
||||
parent: QWidget = None
|
||||
) -> QWidget:
|
||||
|
||||
if var_type == "Int":
|
||||
w = QSpinBox(parent)
|
||||
w.setRange(-999999, 999999)
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(100)
|
||||
return w
|
||||
if var_type == "Float":
|
||||
w = QDoubleSpinBox(parent)
|
||||
w.setRange(-999999.0, 999999.0)
|
||||
w.setDecimals(2)
|
||||
w.setFixedHeight(25)
|
||||
w.setMinimumWidth(100)
|
||||
return w
|
||||
if var_type == "Date":
|
||||
return _DateOffsetContainer(parent)
|
||||
if var_type == "Time":
|
||||
return _TimeOffsetContainer(parent)
|
||||
w = QLabel("(不支持该操作)", parent)
|
||||
w.setFixedHeight(25)
|
||||
return w
|
||||
|
||||
def makeVarRefCombo(
|
||||
parent: QWidget = None
|
||||
) -> QComboBox:
|
||||
|
||||
cb = QComboBox(parent)
|
||||
cb.setFixedHeight(25)
|
||||
cb.setMinimumWidth(120)
|
||||
cb.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
|
||||
return cb
|
||||
|
||||
def makeComboWidget(
|
||||
items,
|
||||
min_width: int = 80,
|
||||
parent: QWidget = None
|
||||
) -> QComboBox:
|
||||
|
||||
cb = QComboBox(parent)
|
||||
for display, data in items:
|
||||
cb.addItem(display, data)
|
||||
cb.setFixedHeight(25)
|
||||
cb.setMinimumWidth(min_width)
|
||||
return cb
|
||||
|
||||
def makeLabel(
|
||||
text: str,
|
||||
parent: QWidget = None,
|
||||
width: int = None
|
||||
) -> QLabel:
|
||||
|
||||
lbl = QLabel(text, parent)
|
||||
lbl.setFixedHeight(25)
|
||||
if width:
|
||||
lbl.setFixedWidth(width)
|
||||
return lbl
|
||||
|
||||
def getValueFromWidget(
|
||||
w: QWidget
|
||||
) -> str:
|
||||
@@ -556,8 +535,7 @@ def getValueFromWidget(
|
||||
return w.text()
|
||||
return ""
|
||||
|
||||
|
||||
def setWidgetValue(
|
||||
def setValueToWidget(
|
||||
w: QWidget,
|
||||
var_type: str,
|
||||
expr: str
|
||||
@@ -619,7 +597,6 @@ def setWidgetValue(
|
||||
inner = inner[1:-1].replace('\\"', '"')
|
||||
w.setText(inner)
|
||||
|
||||
|
||||
def encodeValueStr(
|
||||
raw_value: str,
|
||||
var_type: str
|
||||
@@ -632,7 +609,7 @@ def encodeValueStr(
|
||||
"""
|
||||
|
||||
if var_type in ("Date", "Time"):
|
||||
return _encodeDateOrTime(str(raw_value), var_type)
|
||||
return encodeDateOrTime(str(raw_value), var_type)
|
||||
if isinstance(raw_value, bool):
|
||||
return "true" if raw_value else "false"
|
||||
s = str(raw_value)
|
||||
@@ -648,8 +625,7 @@ def encodeValueStr(
|
||||
return f'"{escaped}"'
|
||||
return s
|
||||
|
||||
|
||||
def _encodeDateOrTime(
|
||||
def encodeDateOrTime(
|
||||
raw_value: str,
|
||||
var_type: str
|
||||
) -> str:
|
||||
@@ -707,7 +683,6 @@ def _encodeDateOrTime(
|
||||
return s
|
||||
return f'"{s}"'
|
||||
|
||||
|
||||
def stripOuterParens(
|
||||
s: str
|
||||
) -> str:
|
||||
@@ -725,12 +700,10 @@ def stripOuterParens(
|
||||
return s[1:-1].strip()
|
||||
return s
|
||||
|
||||
|
||||
# Pre-compiled patterns for detecting arithmetic expressions (A + B / A - B)
|
||||
_RE_ARITH_SPACED = re.compile(r'^(.+?)\s+([+-])\s+(.+)$')
|
||||
_RE_ARITH_NOSPACE = re.compile(r'^([A-Za-z_]\w*)([+-])(\d+|[A-Za-z_]\w*)$')
|
||||
|
||||
|
||||
def isArithExpr(
|
||||
expr: str
|
||||
) -> bool:
|
||||
@@ -741,7 +714,6 @@ def isArithExpr(
|
||||
s = expr.strip()
|
||||
return bool(_RE_ARITH_SPACED.match(s) or _RE_ARITH_NOSPACE.match(s))
|
||||
|
||||
|
||||
def isVarReference(
|
||||
expr: str
|
||||
) -> bool:
|
||||
@@ -764,27 +736,7 @@ def isVarReference(
|
||||
return False
|
||||
return bool(re.match(r"^[A-Z_][A-Z0-9_]*$", up))
|
||||
|
||||
|
||||
def findOperatorIn(
|
||||
text: str,
|
||||
operators: list
|
||||
) -> tuple[int, str] | None:
|
||||
|
||||
for op in operators:
|
||||
op_upper = op.upper()
|
||||
start = 0
|
||||
while True:
|
||||
idx = text.upper().find(op_upper, start)
|
||||
if idx < 0:
|
||||
break
|
||||
if _isInsideLiteral(text, idx):
|
||||
start = idx + 1
|
||||
continue
|
||||
return (idx, op)
|
||||
return None
|
||||
|
||||
|
||||
def _isInsideLiteral(
|
||||
def isInsideLiteral(
|
||||
text: str,
|
||||
pos: int
|
||||
) -> bool:
|
||||
@@ -799,3 +751,21 @@ def _isInsideLiteral(
|
||||
elif ch == '"' and not in_single:
|
||||
in_double = not in_double
|
||||
return in_single or in_double
|
||||
|
||||
def findOperatorIn(
|
||||
text: str,
|
||||
operators: list
|
||||
) -> tuple[int, str] | None:
|
||||
|
||||
for op in operators:
|
||||
op_upper = op.upper()
|
||||
start = 0
|
||||
while True:
|
||||
idx = text.upper().find(op_upper, start)
|
||||
if idx < 0:
|
||||
break
|
||||
if isInsideLiteral(text, idx):
|
||||
start = idx + 1
|
||||
continue
|
||||
return (idx, op)
|
||||
return None
|
||||
|
||||
@@ -58,7 +58,6 @@ class ConditionRowFrame(QFrame):
|
||||
self.setupUi()
|
||||
self.connectSignals()
|
||||
|
||||
|
||||
def setupUi(
|
||||
self
|
||||
):
|
||||
@@ -90,15 +89,7 @@ class ConditionRowFrame(QFrame):
|
||||
layout.addWidget(self._compTypeCombo)
|
||||
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)
|
||||
self.initLiteralStack()
|
||||
self.rhsVarCombo = makeVarRefCombo(self)
|
||||
self.rhsStack.addWidget(self.rhsVarCombo)
|
||||
self.rhsStack.setCurrentIndex(0)
|
||||
@@ -113,7 +104,6 @@ class ConditionRowFrame(QFrame):
|
||||
layout.addStretch()
|
||||
self.setUpdatesEnabled(True)
|
||||
|
||||
|
||||
def populateLeftVarCombo(
|
||||
self
|
||||
):
|
||||
@@ -136,13 +126,25 @@ class ConditionRowFrame(QFrame):
|
||||
self.leftVarCombo.setCurrentIndex(ci)
|
||||
break
|
||||
|
||||
|
||||
def populateRhsVarCombo(
|
||||
def populateRHSVarCombo(
|
||||
self
|
||||
):
|
||||
|
||||
self._varMgr.populateCombo(self.rhsVarCombo)
|
||||
|
||||
def initLiteralStack(
|
||||
self
|
||||
):
|
||||
|
||||
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)
|
||||
|
||||
def connectSignals(
|
||||
self
|
||||
@@ -151,58 +153,22 @@ class ConditionRowFrame(QFrame):
|
||||
self.leftVarCombo.currentIndexChanged.connect(self.onLeftVarChanged)
|
||||
self._compTypeCombo.currentIndexChanged.connect(self.onCompTypeChanged)
|
||||
|
||||
@Slot(int)
|
||||
def onLeftVarChanged(
|
||||
self,
|
||||
idx
|
||||
):
|
||||
|
||||
self._rawRhsExpr = ""
|
||||
if idx < 0:
|
||||
return
|
||||
data = self.leftVarCombo.itemData(idx)
|
||||
if not data:
|
||||
return
|
||||
name, vartype = data
|
||||
isBool = name in ("true", "false")
|
||||
self._isBoolMode = isBool
|
||||
self.opCombo.setVisible(not isBool)
|
||||
self._compTypeCombo.setVisible(not isBool)
|
||||
self.rhsStack.setVisible(not isBool)
|
||||
if not isBool:
|
||||
self.updateRhsLiteralWidget(vartype)
|
||||
|
||||
|
||||
def updateRhsLiteralWidget(
|
||||
self,
|
||||
vartype: str
|
||||
):
|
||||
|
||||
if vartype not in self.literalWidgets:
|
||||
vartype = "String"
|
||||
self.literalStack.setCurrentWidget(self.literalWidgets[vartype])
|
||||
|
||||
@Slot(int)
|
||||
def onCompTypeChanged(
|
||||
self,
|
||||
idx
|
||||
):
|
||||
|
||||
self._rawRhsExpr = ""
|
||||
isVar = (self._compTypeCombo.currentData() == "variable")
|
||||
self.rhsStack.setCurrentIndex(1 if isVar else 0)
|
||||
if isVar:
|
||||
self.populateRhsVarCombo()
|
||||
|
||||
|
||||
def getLogic(
|
||||
self
|
||||
) -> str:
|
||||
|
||||
return self.logicCombo.currentData() if self.logicCombo else ""
|
||||
|
||||
def updateRHSLiteralWidget(
|
||||
self,
|
||||
vartype: str
|
||||
):
|
||||
|
||||
def toConditionText(
|
||||
if vartype not in self._literalWidgets:
|
||||
vartype = "String"
|
||||
self.literalStack.setCurrentWidget(self._literalWidgets[vartype])
|
||||
|
||||
def toScript(
|
||||
self
|
||||
) -> str:
|
||||
|
||||
@@ -230,20 +196,53 @@ 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)
|
||||
return f"{name} {opSym} {encoded}"
|
||||
return ""
|
||||
|
||||
|
||||
def refreshVarCombos(
|
||||
self
|
||||
):
|
||||
|
||||
self.populateLeftVarCombo()
|
||||
self.populateRhsVarCombo()
|
||||
self.populateRHSVarCombo()
|
||||
|
||||
@Slot(int)
|
||||
def onLeftVarChanged(
|
||||
self,
|
||||
idx
|
||||
):
|
||||
|
||||
self._rawRhsExpr = ""
|
||||
if idx < 0:
|
||||
return
|
||||
data = self.leftVarCombo.itemData(idx)
|
||||
if not data:
|
||||
return
|
||||
name, vartype = data
|
||||
isBool = name in ("true", "false")
|
||||
self._isBoolMode = isBool
|
||||
self.opCombo.setVisible(not isBool)
|
||||
self._compTypeCombo.setVisible(not isBool)
|
||||
self.rhsStack.setVisible(not isBool)
|
||||
if not isBool:
|
||||
self.updateRHSLiteralWidget(vartype)
|
||||
|
||||
@Slot(int)
|
||||
def onCompTypeChanged(
|
||||
self,
|
||||
idx
|
||||
):
|
||||
|
||||
self._rawRhsExpr = ""
|
||||
isVar = (self._compTypeCombo.currentData() == "variable")
|
||||
self.rhsStack.setCurrentIndex(1 if isVar else 0)
|
||||
if isVar:
|
||||
self.populateRHSVarCombo()
|
||||
|
||||
|
||||
class ActionStepFrame(QFrame):
|
||||
|
||||
@@ -262,7 +261,6 @@ class ActionStepFrame(QFrame):
|
||||
self.setupUi()
|
||||
self.connectSignals()
|
||||
|
||||
|
||||
def setupUi(
|
||||
self
|
||||
):
|
||||
@@ -280,7 +278,7 @@ class ActionStepFrame(QFrame):
|
||||
self.targetCombo = QComboBox(self)
|
||||
self.targetCombo.setFixedHeight(25)
|
||||
self.targetCombo.setMinimumWidth(120)
|
||||
self.buildTargetCombo()
|
||||
self.populateTargetCombo()
|
||||
layout.addWidget(self.targetCombo)
|
||||
layout.addWidget(makeLabel("为", self))
|
||||
self.valueSrcCombo = makeComboWidget([
|
||||
@@ -301,8 +299,7 @@ class ActionStepFrame(QFrame):
|
||||
layout.addWidget(self.deleteBtn)
|
||||
self.setUpdatesEnabled(True)
|
||||
|
||||
|
||||
def buildTargetCombo(
|
||||
def populateTargetCombo(
|
||||
self
|
||||
):
|
||||
|
||||
@@ -319,7 +316,6 @@ class ActionStepFrame(QFrame):
|
||||
)
|
||||
self.targetCombo.blockSignals(False)
|
||||
|
||||
|
||||
def initValueStacks(
|
||||
self
|
||||
):
|
||||
@@ -338,7 +334,6 @@ class ActionStepFrame(QFrame):
|
||||
self._offsetWidgets[vt] = lbl
|
||||
self.valueStack.addWidget(lbl)
|
||||
|
||||
|
||||
def connectSignals(
|
||||
self
|
||||
):
|
||||
@@ -347,32 +342,14 @@ class ActionStepFrame(QFrame):
|
||||
self.targetCombo.currentIndexChanged.connect(self.onTargetChanged)
|
||||
self.valueSrcCombo.currentIndexChanged.connect(self.onValueSrcChanged)
|
||||
|
||||
@Slot(int)
|
||||
def onTargetChanged(
|
||||
self,
|
||||
idx
|
||||
):
|
||||
def getTargetName(
|
||||
self
|
||||
) -> str:
|
||||
|
||||
if idx < 0:
|
||||
return
|
||||
data = self.targetCombo.itemData(idx)
|
||||
if not data:
|
||||
return
|
||||
_, vartype = data
|
||||
self._currentTargetType = vartype
|
||||
self.updateRHSWidget()
|
||||
self.onValueSrcChanged(self.valueSrcCombo.currentIndex())
|
||||
data = self.targetCombo.currentData()
|
||||
return data[0] if data else ""
|
||||
|
||||
@Slot(int)
|
||||
def onOpTypeChanged(
|
||||
self,
|
||||
idx
|
||||
):
|
||||
|
||||
self.updateRHSWidget()
|
||||
|
||||
|
||||
def updateRHSWidget(
|
||||
def updateValueWidget(
|
||||
self
|
||||
):
|
||||
|
||||
@@ -386,30 +363,7 @@ class ActionStepFrame(QFrame):
|
||||
else:
|
||||
self.valueStack.setCurrentWidget(self._literalWidgets.get("String"))
|
||||
|
||||
@Slot(int)
|
||||
def onValueSrcChanged(
|
||||
self,
|
||||
idx
|
||||
):
|
||||
|
||||
isVar = (self.valueSrcCombo.currentData() == "variable")
|
||||
self.valueStack.setVisible(not isVar)
|
||||
self.existingVarCombo.setVisible(isVar)
|
||||
if isVar:
|
||||
self._varMgr.populateCombo(self.existingVarCombo)
|
||||
else:
|
||||
self.updateRHSWidget()
|
||||
|
||||
|
||||
def getTargetName(
|
||||
self
|
||||
) -> str:
|
||||
|
||||
data = self.targetCombo.currentData()
|
||||
return data[0] if data else ""
|
||||
|
||||
|
||||
def toScriptLine(
|
||||
def toScript(
|
||||
self
|
||||
) -> str:
|
||||
"""
|
||||
@@ -422,7 +376,7 @@ class ActionStepFrame(QFrame):
|
||||
return " -- pass"
|
||||
if not target:
|
||||
return ""
|
||||
rawVal = self._getValueRaw()
|
||||
rawVal = self.getValueRaw()
|
||||
vartype = self._currentTargetType
|
||||
if op == "set":
|
||||
encoded = encodeValueStr(rawVal, vartype)
|
||||
@@ -445,8 +399,7 @@ class ActionStepFrame(QFrame):
|
||||
return f" {target} = {target} - {rawVal}"
|
||||
return ""
|
||||
|
||||
|
||||
def _getValueRaw(
|
||||
def getValueRaw(
|
||||
self
|
||||
) -> str:
|
||||
|
||||
@@ -458,13 +411,12 @@ class ActionStepFrame(QFrame):
|
||||
return getValueFromWidget(w)
|
||||
return ""
|
||||
|
||||
|
||||
def refreshVarCombos(
|
||||
self
|
||||
):
|
||||
|
||||
currentData = self.targetCombo.currentData()
|
||||
self.buildTargetCombo()
|
||||
self.populateTargetCombo()
|
||||
if currentData:
|
||||
for i in range(self.targetCombo.count()):
|
||||
d = self.targetCombo.itemData(i)
|
||||
@@ -472,3 +424,41 @@ class ActionStepFrame(QFrame):
|
||||
self.targetCombo.setCurrentIndex(i)
|
||||
break
|
||||
self._varMgr.populateCombo(self.existingVarCombo)
|
||||
|
||||
@Slot(int)
|
||||
def onTargetChanged(
|
||||
self,
|
||||
idx
|
||||
):
|
||||
|
||||
if idx < 0:
|
||||
return
|
||||
data = self.targetCombo.itemData(idx)
|
||||
if not data:
|
||||
return
|
||||
_, vartype = data
|
||||
self._currentTargetType = vartype
|
||||
self.updateValueWidget()
|
||||
self.onValueSrcChanged(self.valueSrcCombo.currentIndex())
|
||||
|
||||
@Slot(int)
|
||||
def onOpTypeChanged(
|
||||
self,
|
||||
idx
|
||||
):
|
||||
|
||||
self.updateValueWidget()
|
||||
|
||||
@Slot(int)
|
||||
def onValueSrcChanged(
|
||||
self,
|
||||
idx
|
||||
):
|
||||
|
||||
isVar = (self.valueSrcCombo.currentData() == "variable")
|
||||
self.valueStack.setVisible(not isVar)
|
||||
self.existingVarCombo.setVisible(isVar)
|
||||
if isVar:
|
||||
self._varMgr.populateCombo(self.existingVarCombo)
|
||||
else:
|
||||
self.updateValueWidget()
|
||||
|
||||
Reference in New Issue
Block a user