215 lines
6.9 KiB
C++
215 lines
6.9 KiB
C++
#include "DeviceParameterConfigList.h"
|
|
#include <QVBoxLayout>
|
|
#include <QHBoxLayout>
|
|
#include <QListWidget>
|
|
#include <QPushButton>
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QInputDialog>
|
|
#include <QMessageBox>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
#include <QDebug>
|
|
#include <QDateTime>
|
|
#include <QLabel>
|
|
DeviceParameterConfigList::DeviceParameterConfigList(QWidget *parent)
|
|
: QWidget(parent)
|
|
, m_listWidget(nullptr)
|
|
, m_btnAdd(nullptr)
|
|
, m_btnDelete(nullptr)
|
|
{
|
|
setupUi();
|
|
}
|
|
|
|
DeviceParameterConfigList::~DeviceParameterConfigList()
|
|
{
|
|
}
|
|
|
|
void DeviceParameterConfigList::setupUi()
|
|
{
|
|
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
|
mainLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
QLabel *ListWidgetName = new QLabel(QStringLiteral(u"设备参数配置列表"));
|
|
mainLayout->addWidget(ListWidgetName);
|
|
|
|
m_listWidget = new QListWidget(this);
|
|
mainLayout->addWidget(m_listWidget);
|
|
|
|
QHBoxLayout *btnLayout = new QHBoxLayout();
|
|
m_btnAdd = new QPushButton(QStringLiteral(u"添加"), this);
|
|
m_btnDelete = new QPushButton(QStringLiteral(u"删除"), this);
|
|
btnLayout->addWidget(m_btnAdd);
|
|
btnLayout->addWidget(m_btnDelete);
|
|
btnLayout->addStretch();
|
|
mainLayout->addLayout(btnLayout);
|
|
|
|
connect(m_listWidget, &QListWidget::itemClicked, this, &DeviceParameterConfigList::onItemClicked);
|
|
connect(m_btnAdd, &QPushButton::clicked, this, &DeviceParameterConfigList::onAddClicked);
|
|
connect(m_btnDelete, &QPushButton::clicked, this, &DeviceParameterConfigList::onDeleteClicked);
|
|
}
|
|
|
|
void DeviceParameterConfigList::loadConfigsFromFolder(const QString &folderPath)
|
|
{
|
|
if (folderPath.isEmpty()) {
|
|
m_folderPath = QDir::currentPath() + "/parameterConfig";
|
|
} else {
|
|
m_folderPath = folderPath;
|
|
}
|
|
|
|
QDir dir(m_folderPath);
|
|
if (!dir.exists()) {
|
|
dir.mkpath(m_folderPath);
|
|
}
|
|
|
|
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
|
|
QStringList files = dir.entryList();
|
|
updateList(files);
|
|
}
|
|
|
|
void DeviceParameterConfigList::updateList(const QStringList &fileList)
|
|
{
|
|
m_listWidget->clear();
|
|
for (const QString &file : fileList) {
|
|
if (file.endsWith(".json", Qt::CaseInsensitive)) {
|
|
m_listWidget->addItem(file);
|
|
}
|
|
}
|
|
}
|
|
|
|
QString DeviceParameterConfigList::getCurrentFilePath() const
|
|
{
|
|
QListWidgetItem *item = m_listWidget->currentItem();
|
|
if (!item) return QString();
|
|
return m_folderPath + "/" + item->text();
|
|
}
|
|
|
|
QString DeviceParameterConfigList::getCurrentFileName() const
|
|
{
|
|
QListWidgetItem *item = m_listWidget->currentItem();
|
|
return item ? item->text() : QString();
|
|
}
|
|
|
|
void DeviceParameterConfigList::setCurrentFile(const QString &fileName)
|
|
{
|
|
QList<QListWidgetItem*> items = m_listWidget->findItems(fileName, Qt::MatchExactly);
|
|
if (!items.isEmpty()) {
|
|
m_listWidget->setCurrentItem(items.first());
|
|
emit configSelected(getCurrentFilePath());
|
|
}
|
|
}
|
|
|
|
void DeviceParameterConfigList::refresh()
|
|
{
|
|
loadConfigsFromFolder(m_folderPath);
|
|
}
|
|
|
|
void DeviceParameterConfigList::onItemClicked(QListWidgetItem *item)
|
|
{
|
|
if (item) {
|
|
emit configSelected(m_folderPath + "/" + item->text());
|
|
}
|
|
}
|
|
|
|
void DeviceParameterConfigList::onAddClicked()
|
|
{
|
|
bool ok;
|
|
QString fileName = QInputDialog::getText(this, QStringLiteral(u"新建配置"),
|
|
QStringLiteral(u"请输入配置文件名(无需后缀):"),
|
|
QLineEdit::Normal,
|
|
QString("NewConfig_%1").arg(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss")),
|
|
&ok);
|
|
if (!ok || fileName.isEmpty()) return;
|
|
|
|
if (!fileName.endsWith(".json", Qt::CaseInsensitive))
|
|
fileName += ".json";
|
|
|
|
QString filePath = m_folderPath + "/" + fileName;
|
|
if (QFile::exists(filePath)) {
|
|
QMessageBox::warning(this, QStringLiteral(u"错误"), QStringLiteral(u"文件已存在,请使用其他名称。"));
|
|
return;
|
|
}
|
|
|
|
// 发出创建请求,由外部(主窗口)决定默认内容
|
|
emit createNewConfigRequested(fileName);
|
|
}
|
|
|
|
void DeviceParameterConfigList::onDeleteClicked()
|
|
{
|
|
QListWidgetItem *currentItem = m_listWidget->currentItem();
|
|
if (!currentItem) {
|
|
QMessageBox::information(this, QStringLiteral(u"提示"), QStringLiteral(u"请先选择要删除的配置文件。"));
|
|
return;
|
|
}
|
|
|
|
QString fileName = currentItem->text();
|
|
QString filePath = m_folderPath + "/" + fileName;
|
|
|
|
int ret = QMessageBox::question(this, QStringLiteral(u"确认删除"),
|
|
QStringLiteral(u"确定要删除文件 \"%1\" 吗?").arg(fileName),
|
|
QMessageBox::Yes | QMessageBox::No);
|
|
if (ret != QMessageBox::Yes) return;
|
|
|
|
QFile file(filePath);
|
|
if (file.remove()) {
|
|
delete currentItem;
|
|
emit configDeleted(fileName);
|
|
QMessageBox::information(this, QStringLiteral(u"成功"), QStringLiteral(u"文件已删除。"));
|
|
} else {
|
|
QMessageBox::warning(this, QStringLiteral(u"错误"), QStringLiteral(u"无法删除文件:%1").arg(file.errorString()));
|
|
}
|
|
}
|
|
|
|
bool DeviceParameterConfigList::createDefaultConfigFile(const QString &filePath)
|
|
{
|
|
// 默认配置内容(与原来保持一致)
|
|
QJsonObject rootObj;
|
|
rootObj["command"] = "SET";
|
|
QJsonArray channelsArray;
|
|
|
|
QJsonObject defaultChannel;
|
|
defaultChannel["eTransferModel"] = "eSpecturmMode";
|
|
defaultChannel["iDeviceGain"] = 1;
|
|
defaultChannel["iDeviceGainSelectIndex"] = 1;
|
|
defaultChannel["iSoftGain"] = 3000;
|
|
defaultChannel["dConstTime"] = 45.0;
|
|
defaultChannel["iFormTime"] = 3;
|
|
defaultChannel["iFastChannelTrigerValue"] = 10.0;
|
|
defaultChannel["iChannelNum"] = 1024;
|
|
defaultChannel["iHighVoltage"] = 0;
|
|
defaultChannel["iInputVoltageDesc"] = 0;
|
|
defaultChannel["iCRDivMode"] = 0;
|
|
defaultChannel["iInputSignalPostive"] = 0;
|
|
defaultChannel["iCurrentOffset"] = 0;
|
|
defaultChannel["iMaxEnergy"] = 0;
|
|
defaultChannel["iAMPeakDiv"] = 0;
|
|
defaultChannel["iHVDelt"] = 0;
|
|
defaultChannel["iHVCtrl"] = 0;
|
|
defaultChannel["iGetSpecturmPeirod"] = 1;
|
|
defaultChannel["iTotalMeasureTime"] = 10;
|
|
defaultChannel["iTotalMeasureCount"] = 1;
|
|
defaultChannel["iTrapeTopShitBit"] = 0;
|
|
defaultChannel["iRiseTime"] = 2;
|
|
defaultChannel["iTopTime"] = 2;
|
|
defaultChannel["bICRCorrect"] = false;
|
|
defaultChannel["iCRZAValue"] = 0;
|
|
defaultChannel["iZAEnable"] = 30;
|
|
defaultChannel["reserve"] = QJsonArray();
|
|
defaultChannel["pUserConfigInfo"] = QJsonObject();
|
|
|
|
for (int i = 0; i < 32; ++i) {
|
|
channelsArray.append(defaultChannel);
|
|
}
|
|
rootObj["channels"] = channelsArray;
|
|
|
|
QJsonDocument doc(rootObj);
|
|
QFile file(filePath);
|
|
if (!file.open(QIODevice::WriteOnly)) {
|
|
return false;
|
|
}
|
|
file.write(doc.toJson());
|
|
file.close();
|
|
return true;
|
|
}
|