增加转码功能函数配置修改自动刷新机制

This commit is contained in:
taocong 2026-06-02 10:39:01 +08:00
parent cc7fc51399
commit 5718503f26
2 changed files with 69 additions and 20 deletions

View File

@ -48,10 +48,20 @@ BatchConvertPage::BatchConvertPage(QWidget *parent)
{ {
initUI(); initUI();
loadFromCustJson(); loadFromCustJson();
m_configWatcher = new QFileSystemWatcher(this);
QString configPath = getConfigFilePath();
if (!configPath.isEmpty() && QFile::exists(configPath)) {
m_configWatcher->addPath(configPath);
connect(m_configWatcher, &QFileSystemWatcher::fileChanged, this, &BatchConvertPage::onConfigFileChanged);
}
} }
BatchConvertPage::~BatchConvertPage() BatchConvertPage::~BatchConvertPage()
{ {
if (m_configWatcher) {
m_configWatcher->removePaths(m_configWatcher->files());
}
} }
void BatchConvertPage::initUI() void BatchConvertPage::initUI()
@ -244,6 +254,9 @@ void BatchConvertPage::onEditRow(int row)
m_funcList[row] = newName; m_funcList[row] = newName;
updateTable(); updateTable();
if (saveToCustJson(m_funcList)) {
loadFromCustJson();
}
} }
void BatchConvertPage::onAddFunction() void BatchConvertPage::onAddFunction()
@ -328,6 +341,9 @@ void BatchConvertPage::onRemoveFunction()
} }
updateTable(); updateTable();
if (saveToCustJson(m_funcList)) {
loadFromCustJson();
}
} }
void BatchConvertPage::onClearTable() void BatchConvertPage::onClearTable()
@ -342,6 +358,9 @@ void BatchConvertPage::onClearTable()
if (reply == QMessageBox::Yes) { if (reply == QMessageBox::Yes) {
m_funcList.clear(); m_funcList.clear();
updateTable(); updateTable();
if (saveToCustJson(m_funcList)) {
loadFromCustJson();
}
} }
} }
@ -351,7 +370,6 @@ bool BatchConvertPage::saveToCustJson(const QStringList &funcList)
QFile file(jsonPath); QFile file(jsonPath);
if (!file.open(QIODevice::ReadOnly)) { if (!file.open(QIODevice::ReadOnly)) {
LogManager::instance()->logError(QString("无法打开文件: %1").arg(jsonPath));
return false; return false;
} }
@ -360,12 +378,19 @@ bool BatchConvertPage::saveToCustJson(const QStringList &funcList)
QString content = QString::fromUtf8(data); QString content = QString::fromUtf8(data);
int dirStart = content.indexOf("\"dir\":["); int convertStart = content.indexOf("\"convert\":");
if (convertStart < 0) {
convertStart = content.indexOf("\"convert\" :");
}
if (convertStart < 0) {
return false;
}
int dirStart = content.indexOf("\"dir\":[", convertStart);
if (dirStart < 0) { if (dirStart < 0) {
dirStart = content.indexOf("\"dir\" : ["); dirStart = content.indexOf("\"dir\" : [", convertStart);
} }
if (dirStart < 0) { if (dirStart < 0) {
LogManager::instance()->logError("未找到 \"dir\" 节点");
return false; return false;
} }
@ -389,7 +414,6 @@ bool BatchConvertPage::saveToCustJson(const QStringList &funcList)
content.replace(arrayStart, arrayEnd - arrayStart, newDirArray); content.replace(arrayStart, arrayEnd - arrayStart, newDirArray);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
LogManager::instance()->logError(QString("无法写入文件: %1").arg(jsonPath));
return false; return false;
} }
@ -404,7 +428,16 @@ void BatchConvertPage::loadFromCustJson()
QString jsonPath = QCoreApplication::applicationDirPath() + "/uf2touft3/cust.json"; QString jsonPath = QCoreApplication::applicationDirPath() + "/uf2touft3/cust.json";
QFile file(jsonPath); QFile file(jsonPath);
if (!file.open(QIODevice::ReadOnly)) { int retryCount = 0;
const int maxRetries = 3;
const int retryDelayMs = 100;
while (!file.open(QIODevice::ReadOnly) && retryCount < maxRetries) {
retryCount++;
QThread::msleep(retryDelayMs);
}
if (!file.isOpen()) {
return; return;
} }
@ -421,7 +454,16 @@ void BatchConvertPage::loadFromCustJson()
return; return;
} }
int arrayStart = content.indexOf('[', convertStart); int dirStart = content.indexOf("\"dir\":[", convertStart);
if (dirStart < 0) {
dirStart = content.indexOf("\"dir\" : [", convertStart);
}
if (dirStart < 0) {
return;
}
int arrayStart = content.indexOf('[', dirStart);
int bracketCount = 1; int bracketCount = 1;
int pos = arrayStart + 1; int pos = arrayStart + 1;
while (pos < content.length() && bracketCount > 0) { while (pos < content.length() && bracketCount > 0) {
@ -465,7 +507,6 @@ void BatchConvertPage::loadFromCustJson()
void BatchConvertPage::onStartConvert() void BatchConvertPage::onStartConvert()
{ {
if (m_startBtn->text() == "停止转换") { if (m_startBtn->text() == "停止转换") {
LogManager::instance()->log("用户请求停止转换...");
m_progressLabel->setText("正在停止转换..."); m_progressLabel->setText("正在停止转换...");
emit stopConvert(); emit stopConvert();
return; return;
@ -479,26 +520,14 @@ void BatchConvertPage::onStartConvert()
m_progressBar->setValue(0); m_progressBar->setValue(0);
m_progressLabel->setText("正在准备转换..."); m_progressLabel->setText("正在准备转换...");
LogManager::instance()->log("========================================");
LogManager::instance()->log("正在写入 cust.json ...");
LogManager::instance()->log("功能列表:");
for (const QString &func : m_funcList) {
LogManager::instance()->log(QString(" - %1.service_design").arg(func));
}
if (saveToCustJson(m_funcList)) { if (saveToCustJson(m_funcList)) {
LogManager::instance()->log("cust.json 写入成功!");
m_progressLabel->setText("配置加载成功,开始转换..."); m_progressLabel->setText("配置加载成功,开始转换...");
m_progressBar->setValue(20); m_progressBar->setValue(20);
} else { } else {
LogManager::instance()->logError("cust.json 写入失败!");
LogManager::instance()->log("========================================");
m_progressLabel->setText("配置加载失败"); m_progressLabel->setText("配置加载失败");
return; return;
} }
LogManager::instance()->log("========================================");
emit startConvert(); emit startConvert();
} }
@ -536,3 +565,18 @@ void BatchConvertPage::addFunction(const QString &funcName)
QMessageBox::warning(nullptr, "警告", "添加到业务转码成功,但保存到 cust.json 失败"); QMessageBox::warning(nullptr, "警告", "添加到业务转码成功,但保存到 cust.json 失败");
} }
} }
void BatchConvertPage::onConfigFileChanged(const QString &path)
{
QStringList oldList = m_funcList;
loadFromCustJson();
if (m_configWatcher && !m_configWatcher->files().contains(path)) {
m_configWatcher->addPath(path);
}
}
QString BatchConvertPage::getConfigFilePath() const
{
return QCoreApplication::applicationDirPath() + "/uf2touft3/cust.json";
}

View File

@ -4,6 +4,8 @@
#include <QWidget> #include <QWidget>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QPushButton> #include <QPushButton>
#include <QFileSystemWatcher>
#include <QThread>
#include "ElaLineEdit.h" #include "ElaLineEdit.h"
#include "ElaTableView.h" #include "ElaTableView.h"
#include "ElaProgressBar.h" #include "ElaProgressBar.h"
@ -36,6 +38,7 @@ private slots:
void onRemoveFunction(); void onRemoveFunction();
void onClearTable(); void onClearTable();
void onEditRow(int row); void onEditRow(int row);
void onConfigFileChanged(const QString &path);
private: private:
void initUI(); void initUI();
@ -44,6 +47,7 @@ private:
bool checkFunctionExists(const QString &funcName); bool checkFunctionExists(const QString &funcName);
void updateTable(); void updateTable();
void resizeEvent(QResizeEvent *event) override; void resizeEvent(QResizeEvent *event) override;
QString getConfigFilePath() const;
ElaTableView *m_funcTable; ElaTableView *m_funcTable;
QStandardItemModel *m_tableModel; QStandardItemModel *m_tableModel;
@ -51,6 +55,7 @@ private:
ElaProgressBar *m_progressBar; ElaProgressBar *m_progressBar;
ElaText *m_progressLabel; ElaText *m_progressLabel;
QPushButton *m_startBtn; QPushButton *m_startBtn;
QFileSystemWatcher *m_configWatcher;
}; };
#endif #endif