备份灯条新增自定义备份名称
This commit is contained in:
parent
15bb1ce07d
commit
61a27c6882
@ -5,6 +5,8 @@
|
||||
#include <QFile>
|
||||
#include <QJsonArray>
|
||||
#include <QFileDialog>
|
||||
#include <QInputDialog>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
// 如果不再需要可以删除: #include <QRandomGenerator>
|
||||
@ -213,7 +215,7 @@ void LightStripManager::setupControlPanel()
|
||||
connect(refreshBtn, &QPushButton::clicked, this, &LightStripManager::onRefreshClicked);
|
||||
|
||||
// 新增:备份按钮
|
||||
backupBtn = new QPushButton("备份列表");
|
||||
backupBtn = new QPushButton("备份灯条");
|
||||
backupBtn->setMinimumHeight(35);
|
||||
backupBtn->setStyleSheet(
|
||||
"QPushButton { "
|
||||
@ -1040,13 +1042,31 @@ void LightStripManager::onBackupClicked()
|
||||
return;
|
||||
}
|
||||
|
||||
// 执行备份
|
||||
backupSnList();
|
||||
|
||||
// 弹出输入框,支持自定义备份文件名
|
||||
bool ok = false;
|
||||
QString inputName = QInputDialog::getText(
|
||||
this,
|
||||
"输入备份文件名",
|
||||
"备份文件名(可留空自动生成):",
|
||||
QLineEdit::Normal,
|
||||
"",
|
||||
&ok
|
||||
);
|
||||
if (!ok) {
|
||||
return; // 用户取消
|
||||
}
|
||||
|
||||
// 执行备份:使用用户输入或自动生成的文件名
|
||||
if (inputName.trimmed().isEmpty()) {
|
||||
backupSnList();
|
||||
} else {
|
||||
createBackupFile(inputName.trimmed());
|
||||
}
|
||||
|
||||
// 显示备份成功消息
|
||||
QMessageBox msgBox;
|
||||
msgBox.setWindowTitle("备份完成");
|
||||
msgBox.setText(QString("已成功备份 %1 个灯条SN到新文件。\n备份文件保存在应用数据目录的backups文件夹中。").arg(uniqueSnSet.size()));
|
||||
msgBox.setText(QString("已成功备份 %1 个灯条SN。\n备份文件保存在应用数据目录的backups文件夹中。").arg(uniqueSnSet.size()));
|
||||
msgBox.setIcon(QMessageBox::Information);
|
||||
msgBox.setStyleSheet(
|
||||
"QMessageBox { "
|
||||
@ -2128,6 +2148,76 @@ QString LightStripManager::generateBackupFileName() const
|
||||
return QString("lightstrip_sn_backup_%1.json").arg(timestamp);
|
||||
}
|
||||
|
||||
void LightStripManager::createBackupFile(const QString &fileName)
|
||||
{
|
||||
QString backupDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/backups";
|
||||
|
||||
// 创建备份目录
|
||||
QDir dir;
|
||||
if (!dir.exists(backupDir)) {
|
||||
if (!dir.mkpath(backupDir)) {
|
||||
qDebug() << "创建备份目录失败:" << backupDir;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
QString finalFileName = fileName;
|
||||
if (finalFileName.trimmed().isEmpty()) {
|
||||
finalFileName = generateBackupFileName();
|
||||
}
|
||||
if (!finalFileName.endsWith(".json", Qt::CaseInsensitive)) {
|
||||
finalFileName += ".json";
|
||||
}
|
||||
QString backupFilePath = backupDir + "/" + finalFileName;
|
||||
|
||||
// 若文件已存在,弹出确认是否覆盖
|
||||
QFileInfo fi(backupFilePath);
|
||||
if (fi.exists()) {
|
||||
QMessageBox::StandardButton reply = QMessageBox::question(
|
||||
this,
|
||||
"文件已存在",
|
||||
QString("备份文件 %1 已存在,是否覆盖?").arg(finalFileName),
|
||||
QMessageBox::Yes | QMessageBox::No,
|
||||
QMessageBox::No
|
||||
);
|
||||
if (reply != QMessageBox::Yes) {
|
||||
qDebug() << "用户取消覆盖,备份中止";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 创建备份文件内容
|
||||
QJsonObject backupData;
|
||||
backupData["timestamp"] = QDateTime::currentDateTime().toString(Qt::ISODate);
|
||||
backupData["version"] = "1.0";
|
||||
backupData["count"] = uniqueSnSet.size();
|
||||
|
||||
QJsonArray snArray;
|
||||
QStringList snList = uniqueSnSet.values();
|
||||
for (const QString &sn : snList) {
|
||||
QJsonObject snObject;
|
||||
snObject["sn"] = sn;
|
||||
// 如果有电量信息,也一并备份
|
||||
if (batteryInfoMap.contains(sn)) {
|
||||
snObject["battery"] = batteryInfoMap[sn];
|
||||
}
|
||||
snArray.append(snObject);
|
||||
}
|
||||
backupData["lightstrips"] = snArray;
|
||||
|
||||
// 写入文件
|
||||
QJsonDocument doc(backupData);
|
||||
QFile file(backupFilePath);
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
file.write(doc.toJson());
|
||||
file.close();
|
||||
qDebug() << "SN列表备份成功:" << backupFilePath;
|
||||
qDebug() << "备份了" << uniqueSnSet.size() << "个灯条SN";
|
||||
} else {
|
||||
qDebug() << "创建备份文件失败:" << backupFilePath;
|
||||
}
|
||||
}
|
||||
|
||||
// 新增:SN列表恢复功能实现
|
||||
void LightStripManager::restoreSnList()
|
||||
{
|
||||
|
||||
@ -107,6 +107,7 @@ private:
|
||||
// 新增:SN列表备份功能
|
||||
void backupSnList();
|
||||
void createBackupFile();
|
||||
void createBackupFile(const QString &fileName);
|
||||
QString generateBackupFileName() const;
|
||||
|
||||
// 新增:SN列表恢复功能
|
||||
|
||||
Loading…
Reference in New Issue
Block a user