159 lines
4.6 KiB
C++
159 lines
4.6 KiB
C++
#include "batchconvertpage.h"
|
|
#include <QVBoxLayout>
|
|
#include <QHBoxLayout>
|
|
#include <QLabel>
|
|
#include <QPushButton>
|
|
#include <QGroupBox>
|
|
#include <QFile>
|
|
#include <QCoreApplication>
|
|
|
|
BatchConvertPage::BatchConvertPage(QWidget *parent)
|
|
: QWidget(parent)
|
|
{
|
|
initUI();
|
|
}
|
|
|
|
BatchConvertPage::~BatchConvertPage()
|
|
{
|
|
}
|
|
|
|
void BatchConvertPage::initUI()
|
|
{
|
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
|
layout->setContentsMargins(30, 30, 30, 30);
|
|
layout->setSpacing(20);
|
|
|
|
QGroupBox *funcBox = new QGroupBox("转码功能");
|
|
QVBoxLayout *funcLayout = new QVBoxLayout(funcBox);
|
|
funcLayout->setSpacing(10);
|
|
|
|
QLabel *funcLabel = new QLabel("请输入需要转码的功能名称(每行一个):");
|
|
funcLayout->addWidget(funcLabel);
|
|
|
|
m_funcEdit = new QTextEdit;
|
|
m_funcEdit->setPlaceholderText("");
|
|
m_funcEdit->setMinimumHeight(120);
|
|
funcLayout->addWidget(m_funcEdit);
|
|
|
|
layout->addWidget(funcBox);
|
|
|
|
QHBoxLayout *btnLayout = new QHBoxLayout;
|
|
QPushButton *startBtn = new QPushButton("开始转换");
|
|
startBtn->setStyleSheet("background-color: #1abc9c; color: white; padding: 10px 30px; font-size: 14px; border: none; border-radius: 4px;");
|
|
connect(startBtn, &QPushButton::clicked, this, &BatchConvertPage::onStartConvert);
|
|
QPushButton *clearBtn = new QPushButton("清空");
|
|
connect(clearBtn, &QPushButton::clicked, this, &BatchConvertPage::onClearLog);
|
|
btnLayout->addStretch();
|
|
btnLayout->addWidget(startBtn);
|
|
btnLayout->addWidget(clearBtn);
|
|
layout->addLayout(btnLayout);
|
|
|
|
m_logEdit = new QTextEdit;
|
|
m_logEdit->setReadOnly(true);
|
|
m_logEdit->setStyleSheet("background-color: #2c3e50; color: #ecf0f1;");
|
|
m_logEdit->append("转码工具已准备就绪...");
|
|
layout->addWidget(m_logEdit);
|
|
}
|
|
|
|
bool BatchConvertPage::saveToCustJson(const QStringList &funcList)
|
|
{
|
|
QString jsonPath = QCoreApplication::applicationDirPath() + "/uf2touft3/cust.json";
|
|
|
|
QFile file(jsonPath);
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
m_logEdit->append("[ERROR] 无法打开文件: " + jsonPath);
|
|
return false;
|
|
}
|
|
|
|
QByteArray data = file.readAll();
|
|
file.close();
|
|
|
|
QString content = QString::fromUtf8(data);
|
|
|
|
int dirStart = content.indexOf("\"dir\":[");
|
|
if (dirStart < 0) {
|
|
dirStart = content.indexOf("\"dir\" : [");
|
|
}
|
|
if (dirStart < 0) {
|
|
m_logEdit->append("[ERROR] 未找到 \"dir\" 节点");
|
|
return false;
|
|
}
|
|
|
|
int arrayStart = content.indexOf('[', dirStart);
|
|
int bracketCount = 1;
|
|
int pos = arrayStart + 1;
|
|
while (pos < content.length() && bracketCount > 0) {
|
|
if (content[pos] == '[') bracketCount++;
|
|
else if (content[pos] == ']') bracketCount--;
|
|
pos++;
|
|
}
|
|
int arrayEnd = pos;
|
|
|
|
QString newDirArray = "[";
|
|
for (int i = 0; i < funcList.size(); ++i) {
|
|
if (i > 0) newDirArray += ",";
|
|
newDirArray += "\n \"" + funcList[i] + ".service_design\"";
|
|
}
|
|
newDirArray += "\n ]";
|
|
|
|
content.replace(arrayStart, arrayEnd - arrayStart, newDirArray);
|
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
|
m_logEdit->append("[ERROR] 无法写入文件: " + jsonPath);
|
|
return false;
|
|
}
|
|
|
|
file.write(content.toUtf8());
|
|
file.close();
|
|
|
|
return true;
|
|
}
|
|
|
|
void BatchConvertPage::onStartConvert()
|
|
{
|
|
QString funcText = m_funcEdit->toPlainText().trimmed();
|
|
if (funcText.isEmpty()) {
|
|
m_logEdit->append("请输入需要转码的功能名称!");
|
|
return;
|
|
}
|
|
|
|
QStringList funcs = funcText.split('\n', Qt::SkipEmptyParts);
|
|
QStringList trimmedFuncs;
|
|
for (const QString &func : funcs) {
|
|
QString trimmed = func.trimmed();
|
|
if (!trimmed.isEmpty()) {
|
|
trimmedFuncs.append(trimmed);
|
|
}
|
|
}
|
|
|
|
if (trimmedFuncs.isEmpty()) {
|
|
m_logEdit->append("请输入有效的功能名称!");
|
|
return;
|
|
}
|
|
|
|
m_logEdit->append("========================================");
|
|
m_logEdit->append("正在写入 cust.json ...");
|
|
m_logEdit->append("功能列表:");
|
|
|
|
for (const QString &func : trimmedFuncs) {
|
|
m_logEdit->append(" - " + func + ".service_design");
|
|
}
|
|
|
|
if (saveToCustJson(trimmedFuncs)) {
|
|
m_logEdit->append("[OK] cust.json 写入成功!");
|
|
} else {
|
|
m_logEdit->append("[FAIL] cust.json 写入失败!");
|
|
m_logEdit->append("========================================");
|
|
return;
|
|
}
|
|
|
|
m_logEdit->append("========================================");
|
|
emit startConvert();
|
|
}
|
|
|
|
void BatchConvertPage::onClearLog()
|
|
{
|
|
m_logEdit->clear();
|
|
m_logEdit->append("转码工具已准备就绪...");
|
|
}
|