diff --git a/src/pages/batchconvert/batchconvertpage.cpp b/src/pages/batchconvert/batchconvertpage.cpp index c884e3c..c94d996 100644 --- a/src/pages/batchconvert/batchconvertpage.cpp +++ b/src/pages/batchconvert/batchconvertpage.cpp @@ -48,10 +48,20 @@ BatchConvertPage::BatchConvertPage(QWidget *parent) { initUI(); 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() { + if (m_configWatcher) { + m_configWatcher->removePaths(m_configWatcher->files()); + } } void BatchConvertPage::initUI() @@ -244,6 +254,9 @@ void BatchConvertPage::onEditRow(int row) m_funcList[row] = newName; updateTable(); + if (saveToCustJson(m_funcList)) { + loadFromCustJson(); + } } void BatchConvertPage::onAddFunction() @@ -328,6 +341,9 @@ void BatchConvertPage::onRemoveFunction() } updateTable(); + if (saveToCustJson(m_funcList)) { + loadFromCustJson(); + } } void BatchConvertPage::onClearTable() @@ -342,6 +358,9 @@ void BatchConvertPage::onClearTable() if (reply == QMessageBox::Yes) { m_funcList.clear(); updateTable(); + if (saveToCustJson(m_funcList)) { + loadFromCustJson(); + } } } @@ -351,7 +370,6 @@ bool BatchConvertPage::saveToCustJson(const QStringList &funcList) QFile file(jsonPath); if (!file.open(QIODevice::ReadOnly)) { - LogManager::instance()->logError(QString("无法打开文件: %1").arg(jsonPath)); return false; } @@ -360,12 +378,19 @@ bool BatchConvertPage::saveToCustJson(const QStringList &funcList) 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) { - dirStart = content.indexOf("\"dir\" : ["); + dirStart = content.indexOf("\"dir\" : [", convertStart); } if (dirStart < 0) { - LogManager::instance()->logError("未找到 \"dir\" 节点"); return false; } @@ -389,7 +414,6 @@ bool BatchConvertPage::saveToCustJson(const QStringList &funcList) content.replace(arrayStart, arrayEnd - arrayStart, newDirArray); if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { - LogManager::instance()->logError(QString("无法写入文件: %1").arg(jsonPath)); return false; } @@ -404,7 +428,16 @@ void BatchConvertPage::loadFromCustJson() QString jsonPath = QCoreApplication::applicationDirPath() + "/uf2touft3/cust.json"; 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; } @@ -421,7 +454,16 @@ void BatchConvertPage::loadFromCustJson() 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 pos = arrayStart + 1; while (pos < content.length() && bracketCount > 0) { @@ -465,7 +507,6 @@ void BatchConvertPage::loadFromCustJson() void BatchConvertPage::onStartConvert() { if (m_startBtn->text() == "停止转换") { - LogManager::instance()->log("用户请求停止转换..."); m_progressLabel->setText("正在停止转换..."); emit stopConvert(); return; @@ -479,26 +520,14 @@ void BatchConvertPage::onStartConvert() m_progressBar->setValue(0); 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)) { - LogManager::instance()->log("cust.json 写入成功!"); m_progressLabel->setText("配置加载成功,开始转换..."); m_progressBar->setValue(20); } else { - LogManager::instance()->logError("cust.json 写入失败!"); - LogManager::instance()->log("========================================"); m_progressLabel->setText("配置加载失败"); return; } - LogManager::instance()->log("========================================"); emit startConvert(); } @@ -536,3 +565,18 @@ void BatchConvertPage::addFunction(const QString &funcName) 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"; +} diff --git a/src/pages/batchconvert/batchconvertpage.h b/src/pages/batchconvert/batchconvertpage.h index 3453142..fe7060a 100644 --- a/src/pages/batchconvert/batchconvertpage.h +++ b/src/pages/batchconvert/batchconvertpage.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "ElaLineEdit.h" #include "ElaTableView.h" #include "ElaProgressBar.h" @@ -36,6 +38,7 @@ private slots: void onRemoveFunction(); void onClearTable(); void onEditRow(int row); + void onConfigFileChanged(const QString &path); private: void initUI(); @@ -44,6 +47,7 @@ private: bool checkFunctionExists(const QString &funcName); void updateTable(); void resizeEvent(QResizeEvent *event) override; + QString getConfigFilePath() const; ElaTableView *m_funcTable; QStandardItemModel *m_tableModel; @@ -51,6 +55,7 @@ private: ElaProgressBar *m_progressBar; ElaText *m_progressLabel; QPushButton *m_startBtn; + QFileSystemWatcher *m_configWatcher; }; #endif