257 lines
9.1 KiB
C++
257 lines
9.1 KiB
C++
// eventpropertyeditor.h
|
||
#pragma once
|
||
#include <QDialog>
|
||
#include <QVBoxLayout>
|
||
#include <QListWidget>
|
||
#include <QDialogButtonBox>
|
||
#include <QComboBox>
|
||
#include <QLineEdit>
|
||
#include <QLabel>
|
||
#include <QPushButton>
|
||
#include <QStackedWidget>
|
||
#include <QFormLayout>
|
||
#include <QFileDialog>
|
||
#include "propertyType/configEventData.h"
|
||
|
||
class EventPropertyEditor : public QDialog {
|
||
Q_OBJECT
|
||
|
||
public:
|
||
EventPropertyEditor(const EventList& initialEvents, QWidget* parent = nullptr)
|
||
: QDialog(parent), m_events(initialEvents) {
|
||
setupUI();
|
||
loadEvents();
|
||
}
|
||
|
||
EventList getEvents() const { return m_events; }
|
||
|
||
private slots:
|
||
void onAddEvent() {
|
||
// 简化的事件添加对话框
|
||
QDialog dialog(this);
|
||
dialog.setWindowTitle("添加事件");
|
||
QVBoxLayout* mainLayout = new QVBoxLayout(&dialog);
|
||
|
||
QGridLayout* gridLayout = new QGridLayout();
|
||
mainLayout->addLayout(gridLayout);
|
||
|
||
int row = 0;
|
||
|
||
// 触发器选择
|
||
QComboBox* triggerCombo = new QComboBox();
|
||
triggerCombo->addItems({"click", "doubleClick", "rightClick", "hoverEnter", "hoverLeave"});
|
||
gridLayout->addWidget(new QLabel("触发器:"), row, 0);
|
||
gridLayout->addWidget(triggerCombo, row, 1);
|
||
row++;
|
||
|
||
// 事件类型选择
|
||
QComboBox* typeCombo = new QComboBox();
|
||
typeCombo->addItems({"showPanel", "setVariable", "runScript"});
|
||
gridLayout->addWidget(new QLabel("事件类型:"), row, 0);
|
||
gridLayout->addWidget(typeCombo, row, 1);
|
||
row++;
|
||
|
||
// 动态参数区域
|
||
QStackedWidget* paramStack = new QStackedWidget();
|
||
gridLayout->addWidget(paramStack, row, 0, 1, 2);
|
||
row++;
|
||
|
||
// 创建不同类型的参数页面
|
||
QWidget* showPanelPage = createShowPanelPage();
|
||
QWidget* setVariablePage = createSetVariablePage();
|
||
QWidget* runScriptPage = createRunScriptPage();
|
||
|
||
paramStack->addWidget(showPanelPage);
|
||
paramStack->setCurrentIndex(0);
|
||
paramStack->addWidget(setVariablePage);
|
||
paramStack->addWidget(runScriptPage);
|
||
|
||
// 连接类型变化信号
|
||
connect(typeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||
paramStack, &QStackedWidget::setCurrentIndex);
|
||
|
||
QDialogButtonBox* buttons = new QDialogButtonBox(
|
||
QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
||
mainLayout->addWidget(buttons);
|
||
|
||
connect(buttons, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
|
||
connect(buttons, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
|
||
|
||
if (dialog.exec() == QDialog::Accepted) {
|
||
EventData event;
|
||
event.trigger = triggerCombo->currentText();
|
||
event.type = typeCombo->currentText();
|
||
|
||
// 根据事件类型获取参数
|
||
if (event.type == "showPanel") {
|
||
QLineEdit* panelIdEdit = showPanelPage->findChild<QLineEdit*>("panelIdEdit");
|
||
QComboBox* showCombo = showPanelPage->findChild<QComboBox*>("showCombo");
|
||
|
||
event.parameters["panelId"] = panelIdEdit->text();
|
||
event.parameters["show"] = showCombo->currentText().toLower() == "true";
|
||
event.description = QString("显隐面板: %1").arg(panelIdEdit->text());
|
||
|
||
} else if (event.type == "setVariable") {
|
||
QLineEdit* varNameEdit = setVariablePage->findChild<QLineEdit*>("varNameEdit");
|
||
QLineEdit* varValueEdit = setVariablePage->findChild<QLineEdit*>("varValueEdit");
|
||
|
||
event.parameters["variable"] = varNameEdit->text();
|
||
event.parameters["value"] = varValueEdit->text();
|
||
event.description = QString("设置变量: %1 = %2").arg(varNameEdit->text(), varValueEdit->text());
|
||
|
||
} else if (event.type == "runScript") {
|
||
QLineEdit* scriptPathEdit = runScriptPage->findChild<QLineEdit*>("scriptPathEdit");
|
||
QComboBox* scriptTypeCombo = runScriptPage->findChild<QComboBox*>("scriptTypeCombo");
|
||
|
||
event.parameters["script"] = scriptPathEdit->text();
|
||
event.parameters["type"] = scriptTypeCombo->currentText();
|
||
event.description = QString("执行脚本: %1").arg(scriptPathEdit->text());
|
||
}
|
||
|
||
m_events.addEvent(event);
|
||
updateEventList();
|
||
}
|
||
}
|
||
|
||
void onEditEvent() {
|
||
int row = m_eventList->currentRow();
|
||
if (row < 0 || row >= m_events.events.size()) return;
|
||
|
||
// 简化的编辑:删除后重新添加
|
||
m_events.events.removeAt(row);
|
||
onAddEvent();
|
||
}
|
||
|
||
void onDeleteEvent() {
|
||
int row = m_eventList->currentRow();
|
||
if (row >= 0 && row < m_events.events.size()) {
|
||
m_events.events.removeAt(row);
|
||
updateEventList();
|
||
}
|
||
}
|
||
|
||
private:
|
||
void setupUI() {
|
||
setWindowTitle("事件编辑器");
|
||
resize(500, 400);
|
||
|
||
QVBoxLayout* mainLayout = new QVBoxLayout(this);
|
||
|
||
// 事件列表
|
||
m_eventList = new QListWidget();
|
||
mainLayout->addWidget(m_eventList);
|
||
|
||
// 按钮
|
||
QHBoxLayout* btnLayout = new QHBoxLayout();
|
||
QPushButton* addBtn = new QPushButton("添加");
|
||
QPushButton* editBtn = new QPushButton("编辑");
|
||
QPushButton* deleteBtn = new QPushButton("删除");
|
||
|
||
btnLayout->addWidget(addBtn);
|
||
btnLayout->addWidget(editBtn);
|
||
btnLayout->addWidget(deleteBtn);
|
||
btnLayout->addStretch();
|
||
|
||
mainLayout->addLayout(btnLayout);
|
||
|
||
// 对话框按钮
|
||
QDialogButtonBox* dialogBtns = new QDialogButtonBox(
|
||
QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
||
mainLayout->addWidget(dialogBtns);
|
||
|
||
// 连接信号
|
||
connect(addBtn, &QPushButton::clicked, this, &EventPropertyEditor::onAddEvent);
|
||
connect(editBtn, &QPushButton::clicked, this, &EventPropertyEditor::onEditEvent);
|
||
connect(deleteBtn, &QPushButton::clicked, this, &EventPropertyEditor::onDeleteEvent);
|
||
connect(dialogBtns, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
||
connect(dialogBtns, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
||
}
|
||
|
||
void loadEvents() {
|
||
updateEventList();
|
||
}
|
||
|
||
void updateEventList() {
|
||
m_eventList->clear();
|
||
for (const auto& event : m_events.events) {
|
||
QString text = QString("%1: %2")
|
||
.arg(event.trigger, event.description);
|
||
m_eventList->addItem(text);
|
||
}
|
||
}
|
||
|
||
// 创建"showPanel"类型参数的页面
|
||
QWidget* createShowPanelPage() {
|
||
QWidget* page = new QWidget();
|
||
QFormLayout* layout = new QFormLayout(page);
|
||
|
||
QLineEdit* panelIdEdit = new QLineEdit();
|
||
panelIdEdit->setObjectName("panelIdEdit");
|
||
panelIdEdit->setPlaceholderText("输入面板ID,如: panel1");
|
||
|
||
QComboBox* showCombo = new QComboBox();
|
||
showCombo->setObjectName("showCombo");
|
||
showCombo->addItems({"true", "false"});
|
||
|
||
layout->addRow("面板ID:", panelIdEdit);
|
||
layout->addRow("显示/隐藏:", showCombo);
|
||
|
||
return page;
|
||
}
|
||
|
||
// 创建"setVariable"类型参数的页面
|
||
QWidget* createSetVariablePage() {
|
||
QWidget* page = new QWidget();
|
||
QFormLayout* layout = new QFormLayout(page);
|
||
|
||
QLineEdit* varNameEdit = new QLineEdit();
|
||
varNameEdit->setObjectName("varNameEdit");
|
||
varNameEdit->setPlaceholderText("输入变量名,如: isVisible");
|
||
|
||
QLineEdit* varValueEdit = new QLineEdit();
|
||
varValueEdit->setObjectName("varValueEdit");
|
||
varValueEdit->setPlaceholderText("输入变量值,如: true, false, 123");
|
||
|
||
layout->addRow("变量名:", varNameEdit);
|
||
layout->addRow("变量值:", varValueEdit);
|
||
|
||
return page;
|
||
}
|
||
|
||
// 创建"runScript"类型参数的页面
|
||
QWidget* createRunScriptPage() {
|
||
QWidget* page = new QWidget();
|
||
QFormLayout* layout = new QFormLayout(page);
|
||
|
||
QLineEdit* scriptPathEdit = new QLineEdit();
|
||
scriptPathEdit->setObjectName("scriptPathEdit");
|
||
scriptPathEdit->setPlaceholderText("输入脚本路径,如: scripts/init.js");
|
||
|
||
QPushButton* browseBtn = new QPushButton("浏览...");
|
||
QHBoxLayout* pathLayout = new QHBoxLayout();
|
||
pathLayout->addWidget(scriptPathEdit);
|
||
pathLayout->addWidget(browseBtn);
|
||
|
||
QComboBox* scriptTypeCombo = new QComboBox();
|
||
scriptTypeCombo->setObjectName("scriptTypeCombo");
|
||
scriptTypeCombo->addItems({"js", "py", "lua"});
|
||
|
||
layout->addRow("脚本路径:", pathLayout);
|
||
layout->addRow("脚本类型:", scriptTypeCombo);
|
||
|
||
// 可选:连接浏览按钮
|
||
connect(browseBtn, &QPushButton::clicked, this, [scriptPathEdit]() {
|
||
QString path = QFileDialog::getOpenFileName(nullptr, "选择脚本文件",
|
||
"", "脚本文件 (*.js *.py *.lua)");
|
||
if (!path.isEmpty()) {
|
||
scriptPathEdit->setText(path);
|
||
}
|
||
});
|
||
|
||
return page;
|
||
}
|
||
private:
|
||
EventList m_events;
|
||
QListWidget* m_eventList = nullptr;
|
||
};
|