新增缓存数据库存储、转码进度显示
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "batchconvertpage.h"
|
||||
#include "src/utils/uf2configreader.h"
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
@@ -12,6 +13,7 @@
|
||||
#include <QInputDialog>
|
||||
#include <QDialog>
|
||||
#include <QTextEdit>
|
||||
#include "src/utils/logmanager.h"
|
||||
|
||||
BatchConvertPage::BatchConvertPage(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
@@ -63,37 +65,62 @@ void BatchConvertPage::initUI()
|
||||
|
||||
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 *clearLogBtn = new QPushButton("清空日志");
|
||||
connect(clearLogBtn, &QPushButton::clicked, this, &BatchConvertPage::onClearLog);
|
||||
btnLayout->addStretch();
|
||||
btnLayout->addWidget(startBtn);
|
||||
btnLayout->addWidget(clearLogBtn);
|
||||
layout->addLayout(btnLayout);
|
||||
QGroupBox *progressBox = new QGroupBox("转换进度");
|
||||
progressBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
|
||||
QVBoxLayout *progressLayout = new QVBoxLayout(progressBox);
|
||||
progressLayout->setSpacing(10);
|
||||
|
||||
m_logEdit = new QTextEdit;
|
||||
m_logEdit->setReadOnly(true);
|
||||
m_logEdit->setStyleSheet("background-color: #2c3e50; color: #ecf0f1;");
|
||||
m_logEdit->append("转码工具已准备就绪...");
|
||||
layout->addWidget(m_logEdit);
|
||||
m_progressLabel = new QLabel("等待开始...");
|
||||
m_progressLabel->setStyleSheet("color: #666;");
|
||||
m_progressLabel->setFixedHeight(20);
|
||||
m_progressLabel->setWordWrap(false);
|
||||
m_progressLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
progressLayout->addWidget(m_progressLabel);
|
||||
|
||||
m_progressBar = new QProgressBar;
|
||||
m_progressBar->setRange(0, 100);
|
||||
m_progressBar->setValue(0);
|
||||
m_progressBar->setFixedHeight(25);
|
||||
m_progressBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
m_progressBar->setStyleSheet(R"(
|
||||
QProgressBar {
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
background-color: #f0f0f0;
|
||||
text-align: center;
|
||||
}
|
||||
QProgressBar::chunk {
|
||||
background-color: #1abc9c;
|
||||
border-radius: 4px;
|
||||
}
|
||||
)");
|
||||
progressLayout->addWidget(m_progressBar);
|
||||
|
||||
layout->addWidget(progressBox);
|
||||
|
||||
QHBoxLayout *btnLayout = new QHBoxLayout;
|
||||
m_startBtn = new QPushButton("开始转换");
|
||||
m_startBtn->setStyleSheet("background-color: #1abc9c; color: white; padding: 10px 30px; font-size: 14px; border: none; border-radius: 4px;");
|
||||
connect(m_startBtn, &QPushButton::clicked, this, &BatchConvertPage::onStartConvert);
|
||||
btnLayout->addStretch();
|
||||
btnLayout->addWidget(m_startBtn);
|
||||
layout->addLayout(btnLayout);
|
||||
}
|
||||
|
||||
void BatchConvertPage::onProgressUpdate(int progress, const QString& status)
|
||||
{
|
||||
m_progressBar->setValue(progress);
|
||||
|
||||
QString displayStatus = status;
|
||||
if (displayStatus.length() > 80) {
|
||||
displayStatus = displayStatus.left(80) + "...";
|
||||
}
|
||||
m_progressLabel->setText(displayStatus);
|
||||
}
|
||||
|
||||
bool BatchConvertPage::checkFunctionExists(const QString &funcName)
|
||||
{
|
||||
QString jsonPath = QCoreApplication::applicationDirPath() + "/uf2touft3/cust.json";
|
||||
QFile file(jsonPath);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray data = file.readAll();
|
||||
file.close();
|
||||
|
||||
QString content = QString::fromUtf8(data);
|
||||
return content.contains("\"" + funcName + "\"");
|
||||
return UF2ConfigReader::checkFuncExistsStatic(funcName);
|
||||
}
|
||||
|
||||
void BatchConvertPage::updateTable()
|
||||
@@ -123,15 +150,15 @@ void BatchConvertPage::updateTable()
|
||||
}
|
||||
m_funcTable->setItem(i, 1, statusItem);
|
||||
|
||||
QPushButton *delBtn = new QPushButton;
|
||||
delBtn->setIcon(QIcon(":/resources/images/delect.png"));
|
||||
delBtn->setIconSize(QSize(20, 20));
|
||||
delBtn->setStyleSheet("padding: 2px; border: none; background-color: transparent;");
|
||||
delBtn->setToolTip("删除");
|
||||
connect(delBtn, &QPushButton::clicked, this, [this, i]() {
|
||||
onDeleteRow(i);
|
||||
QPushButton *editBtn = new QPushButton;
|
||||
editBtn->setIcon(QIcon(":/resources/images/bianji.png"));
|
||||
editBtn->setIconSize(QSize(20, 20));
|
||||
editBtn->setStyleSheet("padding: 2px; border: none; background-color: transparent;");
|
||||
editBtn->setToolTip("修改");
|
||||
connect(editBtn, &QPushButton::clicked, this, [this, i]() {
|
||||
onEditRow(i);
|
||||
});
|
||||
m_funcTable->setCellWidget(i, 2, delBtn);
|
||||
m_funcTable->setCellWidget(i, 2, editBtn);
|
||||
}
|
||||
|
||||
int totalWidth = m_funcTable->width();
|
||||
@@ -151,12 +178,47 @@ void BatchConvertPage::resizeEvent(QResizeEvent *event)
|
||||
}
|
||||
}
|
||||
|
||||
void BatchConvertPage::onDeleteRow(int row)
|
||||
void BatchConvertPage::onEditRow(int row)
|
||||
{
|
||||
if (row >= 0 && row < m_funcList.size()) {
|
||||
m_funcList.removeAt(row);
|
||||
updateTable();
|
||||
if (row < 0 || row >= m_funcList.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString oldName = m_funcList[row];
|
||||
QString newName = oldName;
|
||||
|
||||
while (true) {
|
||||
QInputDialog dialog(this);
|
||||
dialog.setWindowTitle("修改函数名");
|
||||
dialog.setLabelText("请输入新的函数名:");
|
||||
dialog.setTextValue(oldName);
|
||||
dialog.resize(300, 150);
|
||||
|
||||
if (dialog.exec() != QDialog::Accepted) {
|
||||
return;
|
||||
}
|
||||
|
||||
newName = dialog.textValue().trimmed();
|
||||
|
||||
if (newName.isEmpty()) {
|
||||
QMessageBox::warning(this, "提示", "函数名不能为空!");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (newName == oldName) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_funcList.contains(newName)) {
|
||||
QMessageBox::warning(this, "提示", "该函数名已存在,请重新输入!");
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
m_funcList[row] = newName;
|
||||
updateTable();
|
||||
}
|
||||
|
||||
void BatchConvertPage::onAddFunction()
|
||||
@@ -264,7 +326,7 @@ bool BatchConvertPage::saveToCustJson(const QStringList &funcList)
|
||||
|
||||
QFile file(jsonPath);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
m_logEdit->append("[ERROR] 无法打开文件: " + jsonPath);
|
||||
LogManager::instance()->logError(QString("无法打开文件: %1").arg(jsonPath));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -278,7 +340,7 @@ bool BatchConvertPage::saveToCustJson(const QStringList &funcList)
|
||||
dirStart = content.indexOf("\"dir\" : [");
|
||||
}
|
||||
if (dirStart < 0) {
|
||||
m_logEdit->append("[ERROR] 未找到 \"dir\" 节点");
|
||||
LogManager::instance()->logError("未找到 \"dir\" 节点");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -302,7 +364,7 @@ bool BatchConvertPage::saveToCustJson(const QStringList &funcList)
|
||||
content.replace(arrayStart, arrayEnd - arrayStart, newDirArray);
|
||||
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
m_logEdit->append("[ERROR] 无法写入文件: " + jsonPath);
|
||||
LogManager::instance()->logError(QString("无法写入文件: %1").arg(jsonPath));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -314,33 +376,54 @@ bool BatchConvertPage::saveToCustJson(const QStringList &funcList)
|
||||
|
||||
void BatchConvertPage::onStartConvert()
|
||||
{
|
||||
if (m_funcList.isEmpty()) {
|
||||
m_logEdit->append("请先添加需要转码的函数!");
|
||||
if (m_startBtn->text() == "停止转换") {
|
||||
LogManager::instance()->log("用户请求停止转换...");
|
||||
m_progressLabel->setText("正在停止转换...");
|
||||
emit stopConvert();
|
||||
return;
|
||||
}
|
||||
|
||||
m_logEdit->append("========================================");
|
||||
m_logEdit->append("正在写入 cust.json ...");
|
||||
m_logEdit->append("功能列表:");
|
||||
if (m_funcList.isEmpty()) {
|
||||
QMessageBox::warning(this, "提示", "请先添加需要转码的函数!");
|
||||
return;
|
||||
}
|
||||
|
||||
m_progressBar->setValue(0);
|
||||
m_progressLabel->setText("正在准备转换...");
|
||||
|
||||
LogManager::instance()->log("========================================");
|
||||
LogManager::instance()->log("正在写入 cust.json ...");
|
||||
LogManager::instance()->log("功能列表:");
|
||||
|
||||
for (const QString &func : m_funcList) {
|
||||
m_logEdit->append(" - " + func + ".service_design");
|
||||
LogManager::instance()->log(QString(" - %1.service_design").arg(func));
|
||||
}
|
||||
|
||||
if (saveToCustJson(m_funcList)) {
|
||||
m_logEdit->append("[OK] cust.json 写入成功!");
|
||||
LogManager::instance()->log("cust.json 写入成功!");
|
||||
m_progressLabel->setText("配置加载成功,开始转换...");
|
||||
m_progressBar->setValue(20);
|
||||
} else {
|
||||
m_logEdit->append("[FAIL] cust.json 写入失败!");
|
||||
m_logEdit->append("========================================");
|
||||
LogManager::instance()->logError("cust.json 写入失败!");
|
||||
LogManager::instance()->log("========================================");
|
||||
m_progressLabel->setText("配置加载失败");
|
||||
return;
|
||||
}
|
||||
|
||||
m_logEdit->append("========================================");
|
||||
LogManager::instance()->log("========================================");
|
||||
emit startConvert();
|
||||
}
|
||||
|
||||
void BatchConvertPage::onClearLog()
|
||||
void BatchConvertPage::onConvertStarted()
|
||||
{
|
||||
m_logEdit->clear();
|
||||
m_logEdit->append("转码工具已准备就绪...");
|
||||
}
|
||||
m_startBtn->setText("停止转换");
|
||||
m_startBtn->setStyleSheet("background-color: #e74c3c; color: white; padding: 10px 30px; font-size: 14px; border: none; border-radius: 4px;");
|
||||
}
|
||||
|
||||
void BatchConvertPage::onConvertFinished()
|
||||
{
|
||||
m_progressBar->setValue(100);
|
||||
m_progressLabel->setText("转换完成");
|
||||
m_startBtn->setText("开始转换");
|
||||
m_startBtn->setStyleSheet("background-color: #1abc9c; color: white; padding: 10px 30px; font-size: 14px; border: none; border-radius: 4px;");
|
||||
}
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
|
||||
#include <QWidget>
|
||||
#include <QLineEdit>
|
||||
#include <QTextEdit>
|
||||
#include <QTableWidget>
|
||||
#include <QPushButton>
|
||||
#include <QProgressBar>
|
||||
#include <QLabel>
|
||||
|
||||
class BatchConvertPage : public QWidget
|
||||
{
|
||||
@@ -15,18 +16,24 @@ public:
|
||||
explicit BatchConvertPage(QWidget *parent = nullptr);
|
||||
~BatchConvertPage();
|
||||
|
||||
QTextEdit* getLogEdit() { return m_logEdit; }
|
||||
|
||||
signals:
|
||||
void startConvert();
|
||||
void stopConvert();
|
||||
|
||||
public slots:
|
||||
void onProgressUpdate(int progress, const QString& status);
|
||||
void onConvertFinished();
|
||||
void onConvertStarted();
|
||||
|
||||
int getFunctionCount() const { return m_funcList.size(); }
|
||||
QStringList getFunctionList() const { return m_funcList; }
|
||||
|
||||
private slots:
|
||||
void onStartConvert();
|
||||
void onClearLog();
|
||||
void onAddFunction();
|
||||
void onRemoveFunction();
|
||||
void onClearTable();
|
||||
void onDeleteRow(int row);
|
||||
void onEditRow(int row);
|
||||
|
||||
private:
|
||||
void initUI();
|
||||
@@ -36,8 +43,10 @@ private:
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
|
||||
QTableWidget *m_funcTable;
|
||||
QTextEdit *m_logEdit;
|
||||
QStringList m_funcList;
|
||||
QProgressBar *m_progressBar;
|
||||
QLabel *m_progressLabel;
|
||||
QPushButton *m_startBtn;
|
||||
};
|
||||
|
||||
#endif // BATCHCONVERTPAGE_H
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user