折叠按钮修改,转码页面优化
This commit is contained in:
@@ -6,6 +6,12 @@
|
||||
#include <QGroupBox>
|
||||
#include <QFile>
|
||||
#include <QCoreApplication>
|
||||
#include <QTableWidgetItem>
|
||||
#include <QHeaderView>
|
||||
#include <QMessageBox>
|
||||
#include <QInputDialog>
|
||||
#include <QDialog>
|
||||
#include <QTextEdit>
|
||||
|
||||
BatchConvertPage::BatchConvertPage(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
@@ -27,13 +33,33 @@ void BatchConvertPage::initUI()
|
||||
QVBoxLayout *funcLayout = new QVBoxLayout(funcBox);
|
||||
funcLayout->setSpacing(10);
|
||||
|
||||
QLabel *funcLabel = new QLabel("请输入需要转码的功能名称(每行一个):");
|
||||
funcLayout->addWidget(funcLabel);
|
||||
m_funcTable = new QTableWidget;
|
||||
m_funcTable->setColumnCount(3);
|
||||
m_funcTable->setHorizontalHeaderLabels({"UF20函数名", "是否存在", "操作"});
|
||||
m_funcTable->setMinimumHeight(200);
|
||||
m_funcTable->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
m_funcTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
|
||||
m_funcTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
|
||||
m_funcTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
|
||||
m_funcTable->horizontalHeader()->setStretchLastSection(false);
|
||||
m_funcTable->setColumnWidth(2, 80);
|
||||
funcLayout->addWidget(m_funcTable);
|
||||
|
||||
m_funcEdit = new QTextEdit;
|
||||
m_funcEdit->setPlaceholderText("");
|
||||
m_funcEdit->setMinimumHeight(120);
|
||||
funcLayout->addWidget(m_funcEdit);
|
||||
QHBoxLayout *tableBtnLayout = new QHBoxLayout;
|
||||
QPushButton *addBtn = new QPushButton("添加");
|
||||
addBtn->setStyleSheet("padding: 6px 20px;");
|
||||
connect(addBtn, &QPushButton::clicked, this, &BatchConvertPage::onAddFunction);
|
||||
QPushButton *removeBtn = new QPushButton("删除选中");
|
||||
removeBtn->setStyleSheet("padding: 6px 15px;");
|
||||
connect(removeBtn, &QPushButton::clicked, this, &BatchConvertPage::onRemoveFunction);
|
||||
QPushButton *clearBtn = new QPushButton("清空列表");
|
||||
clearBtn->setStyleSheet("padding: 6px 15px;");
|
||||
connect(clearBtn, &QPushButton::clicked, this, &BatchConvertPage::onClearTable);
|
||||
tableBtnLayout->addWidget(addBtn);
|
||||
tableBtnLayout->addStretch();
|
||||
tableBtnLayout->addWidget(removeBtn);
|
||||
tableBtnLayout->addWidget(clearBtn);
|
||||
funcLayout->addLayout(tableBtnLayout);
|
||||
|
||||
layout->addWidget(funcBox);
|
||||
|
||||
@@ -41,11 +67,11 @@ void BatchConvertPage::initUI()
|
||||
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);
|
||||
QPushButton *clearLogBtn = new QPushButton("清空日志");
|
||||
connect(clearLogBtn, &QPushButton::clicked, this, &BatchConvertPage::onClearLog);
|
||||
btnLayout->addStretch();
|
||||
btnLayout->addWidget(startBtn);
|
||||
btnLayout->addWidget(clearBtn);
|
||||
btnLayout->addWidget(clearLogBtn);
|
||||
layout->addLayout(btnLayout);
|
||||
|
||||
m_logEdit = new QTextEdit;
|
||||
@@ -55,6 +81,183 @@ void BatchConvertPage::initUI()
|
||||
layout->addWidget(m_logEdit);
|
||||
}
|
||||
|
||||
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 + "\"");
|
||||
}
|
||||
|
||||
void BatchConvertPage::updateTable()
|
||||
{
|
||||
m_funcTable->setRowCount(m_funcList.size());
|
||||
|
||||
for (int i = 0; i < m_funcList.size(); ++i) {
|
||||
QString funcName = m_funcList[i];
|
||||
|
||||
QTableWidgetItem *nameItem = new QTableWidgetItem(funcName);
|
||||
nameItem->setFlags(nameItem->flags() & ~Qt::ItemIsEditable);
|
||||
m_funcTable->setItem(i, 0, nameItem);
|
||||
|
||||
bool exists = checkFunctionExists(funcName);
|
||||
QString statusText = exists ? "是" : "否";
|
||||
|
||||
QTableWidgetItem *statusItem = new QTableWidgetItem(statusText);
|
||||
statusItem->setFlags(statusItem->flags() & ~Qt::ItemIsEditable);
|
||||
statusItem->setTextAlignment(Qt::AlignCenter);
|
||||
|
||||
if (exists) {
|
||||
statusItem->setBackground(QColor(144, 238, 144));
|
||||
statusItem->setForeground(QColor(0, 100, 0));
|
||||
} else {
|
||||
statusItem->setBackground(QColor(255, 182, 193));
|
||||
statusItem->setForeground(QColor(139, 0, 0));
|
||||
}
|
||||
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);
|
||||
});
|
||||
m_funcTable->setCellWidget(i, 2, delBtn);
|
||||
}
|
||||
|
||||
int totalWidth = m_funcTable->width();
|
||||
m_funcTable->setColumnWidth(0, (totalWidth - 80) * 8 / 10);
|
||||
m_funcTable->setColumnWidth(1, (totalWidth - 80) * 2 / 10);
|
||||
m_funcTable->setColumnWidth(2, 80);
|
||||
}
|
||||
|
||||
void BatchConvertPage::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
QWidget::resizeEvent(event);
|
||||
if (m_funcTable) {
|
||||
int totalWidth = m_funcTable->width();
|
||||
m_funcTable->setColumnWidth(0, (totalWidth - 80) * 8 / 10);
|
||||
m_funcTable->setColumnWidth(1, (totalWidth - 80) * 2 / 10);
|
||||
m_funcTable->setColumnWidth(2, 80);
|
||||
}
|
||||
}
|
||||
|
||||
void BatchConvertPage::onDeleteRow(int row)
|
||||
{
|
||||
if (row >= 0 && row < m_funcList.size()) {
|
||||
m_funcList.removeAt(row);
|
||||
updateTable();
|
||||
}
|
||||
}
|
||||
|
||||
void BatchConvertPage::onAddFunction()
|
||||
{
|
||||
QDialog dialog(this);
|
||||
dialog.setWindowTitle("添加UF20函数");
|
||||
dialog.resize(400, 250);
|
||||
|
||||
QVBoxLayout *layout = new QVBoxLayout(&dialog);
|
||||
|
||||
QTextEdit *textEdit = new QTextEdit;
|
||||
textEdit->setPlaceholderText("每行输入一个函数名");
|
||||
layout->addWidget(textEdit);
|
||||
|
||||
QHBoxLayout *btnLayout = new QHBoxLayout;
|
||||
QPushButton *okBtn = new QPushButton("确定");
|
||||
QPushButton *cancelBtn = new QPushButton("取消");
|
||||
btnLayout->addStretch();
|
||||
btnLayout->addWidget(okBtn);
|
||||
btnLayout->addWidget(cancelBtn);
|
||||
layout->addLayout(btnLayout);
|
||||
|
||||
connect(okBtn, &QPushButton::clicked, &dialog, &QDialog::accept);
|
||||
connect(cancelBtn, &QPushButton::clicked, &dialog, &QDialog::reject);
|
||||
|
||||
if (dialog.exec() != QDialog::Accepted) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString text = textEdit->toPlainText().trimmed();
|
||||
if (text.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QStringList funcNames = text.split('\n', Qt::SkipEmptyParts);
|
||||
int addedCount = 0;
|
||||
int existCount = 0;
|
||||
|
||||
for (const QString &name : funcNames) {
|
||||
QString trimmedName = name.trimmed();
|
||||
if (trimmedName.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (m_funcList.contains(trimmedName)) {
|
||||
existCount++;
|
||||
continue;
|
||||
}
|
||||
m_funcList.append(trimmedName);
|
||||
addedCount++;
|
||||
}
|
||||
|
||||
if (existCount > 0) {
|
||||
QMessageBox::information(this, "提示",
|
||||
QString("成功添加 %1 个函数,%2 个函数已存在").arg(addedCount).arg(existCount));
|
||||
} else if (addedCount > 0) {
|
||||
QMessageBox::information(this, "提示",
|
||||
QString("成功添加 %1 个函数").arg(addedCount));
|
||||
}
|
||||
|
||||
updateTable();
|
||||
}
|
||||
|
||||
void BatchConvertPage::onRemoveFunction()
|
||||
{
|
||||
QList<QTableWidgetItem*> selectedItems = m_funcTable->selectedItems();
|
||||
if (selectedItems.isEmpty()) {
|
||||
QMessageBox::warning(this, "提示", "请先选中要删除的行!");
|
||||
return;
|
||||
}
|
||||
|
||||
QList<int> rowsToRemove;
|
||||
for (QTableWidgetItem *item : selectedItems) {
|
||||
int row = item->row();
|
||||
if (!rowsToRemove.contains(row)) {
|
||||
rowsToRemove.append(row);
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(rowsToRemove.begin(), rowsToRemove.end(), std::greater<int>());
|
||||
for (int row : rowsToRemove) {
|
||||
m_funcList.removeAt(row);
|
||||
}
|
||||
|
||||
updateTable();
|
||||
}
|
||||
|
||||
void BatchConvertPage::onClearTable()
|
||||
{
|
||||
if (m_funcList.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QMessageBox::StandardButton reply;
|
||||
reply = QMessageBox::question(this, "确认清空", "确定要清空所有函数吗?",
|
||||
QMessageBox::Yes | QMessageBox::No);
|
||||
if (reply == QMessageBox::Yes) {
|
||||
m_funcList.clear();
|
||||
updateTable();
|
||||
}
|
||||
}
|
||||
|
||||
bool BatchConvertPage::saveToCustJson(const QStringList &funcList)
|
||||
{
|
||||
QString jsonPath = QCoreApplication::applicationDirPath() + "/uf2touft3/cust.json";
|
||||
@@ -111,23 +314,8 @@ bool BatchConvertPage::saveToCustJson(const QStringList &funcList)
|
||||
|
||||
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("请输入有效的功能名称!");
|
||||
if (m_funcList.isEmpty()) {
|
||||
m_logEdit->append("请先添加需要转码的函数!");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -135,11 +323,11 @@ void BatchConvertPage::onStartConvert()
|
||||
m_logEdit->append("正在写入 cust.json ...");
|
||||
m_logEdit->append("功能列表:");
|
||||
|
||||
for (const QString &func : trimmedFuncs) {
|
||||
for (const QString &func : m_funcList) {
|
||||
m_logEdit->append(" - " + func + ".service_design");
|
||||
}
|
||||
|
||||
if (saveToCustJson(trimmedFuncs)) {
|
||||
if (saveToCustJson(m_funcList)) {
|
||||
m_logEdit->append("[OK] cust.json 写入成功!");
|
||||
} else {
|
||||
m_logEdit->append("[FAIL] cust.json 写入失败!");
|
||||
@@ -155,4 +343,4 @@ void BatchConvertPage::onClearLog()
|
||||
{
|
||||
m_logEdit->clear();
|
||||
m_logEdit->append("转码工具已准备就绪...");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user