first commit
This commit is contained in:
382
utils/configmanager.cpp
Normal file
382
utils/configmanager.cpp
Normal file
@@ -0,0 +1,382 @@
|
||||
#include "configmanager.h"
|
||||
#include <QFile>
|
||||
#include <QDebug>
|
||||
|
||||
ConfigManager::ConfigManager(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
ConfigManager::~ConfigManager()
|
||||
{
|
||||
}
|
||||
|
||||
bool ConfigManager::loadFromFile(const QString &filePath)
|
||||
{
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
qWarning() << "Cannot open config file:" << filePath;
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray data = file.readAll();
|
||||
file.close();
|
||||
|
||||
QJsonParseError error;
|
||||
m_configDoc = QJsonDocument::fromJson(data, &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
qWarning() << "JSON parse error:" << error.errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
m_config = m_configDoc.object();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ConfigManager::loadDefaultConfig()
|
||||
{
|
||||
// 默认配置 - 基于需求规格
|
||||
QString defaultConfig = R"({
|
||||
"name": "智能校验仪启动器",
|
||||
"version": "1.0.0",
|
||||
"colorSystem": {
|
||||
"primary": "#2196F3",
|
||||
"accent": "#FF9800",
|
||||
"success": "#4CAF50",
|
||||
"warning": "#FFC107",
|
||||
"danger": "#F44336",
|
||||
"background": "#F5F5F5"
|
||||
},
|
||||
"pages": [
|
||||
{
|
||||
"id": "page1",
|
||||
"title": "信号输出和测量",
|
||||
"apps": [
|
||||
{
|
||||
"id": "rtd",
|
||||
"name": "热电阻",
|
||||
"nameEn": "RTD",
|
||||
"color": "#2196F3",
|
||||
"gradient": ["#2196F3", "#1976D2"],
|
||||
"badge": "Ω",
|
||||
"description": "2/3/4线热电阻测量,PT100模拟输出",
|
||||
"features": ["测量2/3/4线热电阻阻值", "自动计算线电阻", "自动转换为温度", "PT100分度表模拟输出"]
|
||||
},
|
||||
{
|
||||
"id": "thermocouple",
|
||||
"name": "热电偶",
|
||||
"nameEn": "TC",
|
||||
"color": "#FF9800",
|
||||
"gradient": ["#FF9800", "#F57C00"],
|
||||
"badge": "mV",
|
||||
"description": "热电偶电压测量,分度表模拟输出"
|
||||
},
|
||||
{
|
||||
"id": "insulation",
|
||||
"name": "绝缘电阻",
|
||||
"nameEn": "Insulation",
|
||||
"color": "#F44336",
|
||||
"gradient": ["#F44336", "#D32F2F"],
|
||||
"badge": "MΩ",
|
||||
"description": "50V/100V绝缘电阻测量",
|
||||
"warning": true
|
||||
},
|
||||
{
|
||||
"id": "dc_current",
|
||||
"name": "直流电流",
|
||||
"nameEn": "DC Current",
|
||||
"color": "#4CAF50",
|
||||
"gradient": ["#4CAF50", "#388E3C"],
|
||||
"badge": "mA",
|
||||
"description": "0-20mA电流输出/测量"
|
||||
},
|
||||
{
|
||||
"id": "dc_voltage",
|
||||
"name": "直流电压",
|
||||
"nameEn": "DC Voltage",
|
||||
"color": "#9C27B0",
|
||||
"gradient": ["#9C27B0", "#7B1FA2"],
|
||||
"badge": "V",
|
||||
"description": "0-20V电压输出/测量"
|
||||
},
|
||||
{
|
||||
"id": "frequency",
|
||||
"name": "频率信号",
|
||||
"nameEn": "Frequency",
|
||||
"color": "#00BCD4",
|
||||
"gradient": ["#00BCD4", "#0097A7"],
|
||||
"badge": "Hz",
|
||||
"description": "转速测量,方波/正弦波输出"
|
||||
},
|
||||
{
|
||||
"id": "ac_voltage",
|
||||
"name": "交流电压",
|
||||
"nameEn": "AC Voltage",
|
||||
"color": "#E91E63",
|
||||
"gradient": ["#E91E63", "#C2185B"],
|
||||
"badge": "VAC",
|
||||
"description": "220VAC测量功能",
|
||||
"warning": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "page2",
|
||||
"title": "信号检测功能",
|
||||
"apps": [
|
||||
{
|
||||
"id": "switch_detect",
|
||||
"name": "开关量检测",
|
||||
"nameEn": "Switch Detect",
|
||||
"color": "#795548",
|
||||
"gradient": ["#795548", "#5D4037"],
|
||||
"badge": "SW",
|
||||
"description": "电阻通断/电压判断开关状态"
|
||||
},
|
||||
{
|
||||
"id": "ripple",
|
||||
"name": "纹波诊断",
|
||||
"nameEn": "Ripple Diagnosis",
|
||||
"color": "#FF5722",
|
||||
"gradient": ["#FF5722", "#E64A19"],
|
||||
"badge": "AI",
|
||||
"description": "电源纹波检测,AI故障诊断"
|
||||
},
|
||||
{
|
||||
"id": "ramp",
|
||||
"name": "斜波输出",
|
||||
"nameEn": "Ramp Output",
|
||||
"color": "#3F51B5",
|
||||
"gradient": ["#3F51B5", "#303F9F"],
|
||||
"badge": "△",
|
||||
"description": "可设速率的斜波信号输出"
|
||||
},
|
||||
{
|
||||
"id": "waveform",
|
||||
"name": "波形采集",
|
||||
"nameEn": "Waveform Capture",
|
||||
"color": "#009688",
|
||||
"gradient": ["#009688", "#00796B"],
|
||||
"badge": "REC",
|
||||
"description": "信号采集记录,≤25ms/点"
|
||||
},
|
||||
{
|
||||
"id": "dual_channel",
|
||||
"name": "双通道",
|
||||
"nameEn": "Dual Channel",
|
||||
"color": "#673AB7",
|
||||
"gradient": ["#673AB7", "#512DA8"],
|
||||
"badge": "2CH",
|
||||
"description": "同时输出2路,采集2路信号"
|
||||
},
|
||||
{
|
||||
"id": "trim",
|
||||
"name": "信号微调",
|
||||
"nameEn": "Signal Trim",
|
||||
"color": "#607D8B",
|
||||
"gradient": ["#607D8B", "#455A64"],
|
||||
"badge": "±",
|
||||
"description": "任意位数信号微调"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "page3",
|
||||
"title": "系统和通讯",
|
||||
"apps": [
|
||||
{
|
||||
"id": "data_management",
|
||||
"name": "数据管理",
|
||||
"nameEn": "Data Management",
|
||||
"color": "#8BC34A",
|
||||
"gradient": ["#8BC34A", "#689F38"],
|
||||
"badge": "📁",
|
||||
"description": "数据存储、导出、搜索"
|
||||
},
|
||||
{
|
||||
"id": "wireless",
|
||||
"name": "无线通讯",
|
||||
"nameEn": "Wireless",
|
||||
"color": "#03A9F4",
|
||||
"gradient": ["#03A9F4", "#0288D1"],
|
||||
"badge": "4G",
|
||||
"description": "4G/WIFI通讯设置"
|
||||
},
|
||||
{
|
||||
"id": "sop",
|
||||
"name": "规程管理",
|
||||
"nameEn": "SOP",
|
||||
"color": "#CDDC39",
|
||||
"gradient": ["#CDDC39", "#AFB42B"],
|
||||
"badge": "📋",
|
||||
"description": "自动化校验流程"
|
||||
},
|
||||
{
|
||||
"id": "rcp63",
|
||||
"name": "RCP63",
|
||||
"nameEn": "RCP63",
|
||||
"color": "#2196F3",
|
||||
"gradient": ["#2196F3", "#1976D2"],
|
||||
"badge": "🌐",
|
||||
"description": "RCP63协议通讯"
|
||||
},
|
||||
{
|
||||
"id": "settings",
|
||||
"name": "系统设置",
|
||||
"nameEn": "Settings",
|
||||
"color": "#9E9E9E",
|
||||
"gradient": ["#9E9E9E", "#757575"],
|
||||
"badge": "⚙️",
|
||||
"description": "系统参数配置"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"dock": [
|
||||
{
|
||||
"id": "dock_sop",
|
||||
"name": "规程",
|
||||
"color": "#CDDC39",
|
||||
"gradient": ["#CDDC39", "#AFB42B"]
|
||||
},
|
||||
{
|
||||
"id": "dock_rcp63",
|
||||
"name": "RCP63",
|
||||
"color": "#2196F3",
|
||||
"gradient": ["#2196F3", "#1976D2"]
|
||||
},
|
||||
{
|
||||
"id": "dock_network",
|
||||
"name": "网络测试",
|
||||
"color": "#03A9F4",
|
||||
"gradient": ["#03A9F4", "#0288D1"]
|
||||
},
|
||||
{
|
||||
"id": "dock_settings",
|
||||
"name": "系统设置",
|
||||
"color": "#9E9E9E",
|
||||
"gradient": ["#9E9E9E", "#757575"]
|
||||
}
|
||||
],
|
||||
"animations": {
|
||||
"pageTransition": {
|
||||
"duration": 300,
|
||||
"easing": "ease-out"
|
||||
},
|
||||
"iconPress": {
|
||||
"scale": 0.95,
|
||||
"duration": 100
|
||||
}
|
||||
}
|
||||
})";
|
||||
|
||||
QJsonParseError error;
|
||||
m_configDoc = QJsonDocument::fromJson(defaultConfig.toUtf8(), &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
qWarning() << "Default config parse error:" << error.errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
m_config = m_configDoc.object();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ConfigManager::saveToFile(const QString &filePath)
|
||||
{
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::WriteOnly))
|
||||
{
|
||||
qWarning() << "Cannot write config file:" << filePath;
|
||||
return false;
|
||||
}
|
||||
|
||||
file.write(m_configDoc.toJson(QJsonDocument::Indented));
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
QVector<PageConfig> ConfigManager::getPages() const
|
||||
{
|
||||
QVector<PageConfig> pages;
|
||||
|
||||
QJsonArray pagesArray = m_config["pages"].toArray();
|
||||
for (const QJsonValue &pageVal : pagesArray)
|
||||
{
|
||||
pages.append(parsePageConfig(pageVal.toObject()));
|
||||
}
|
||||
|
||||
return pages;
|
||||
}
|
||||
|
||||
QVector<AppIconData> ConfigManager::getDockApps() const
|
||||
{
|
||||
QVector<AppIconData> apps;
|
||||
|
||||
QJsonArray dockArray = m_config["dock"].toArray();
|
||||
for (const QJsonValue &appVal : dockArray)
|
||||
{
|
||||
apps.append(parseAppData(appVal.toObject()));
|
||||
}
|
||||
|
||||
return apps;
|
||||
}
|
||||
|
||||
QJsonObject ConfigManager::getColorSystem() const
|
||||
{
|
||||
return m_config["colorSystem"].toObject();
|
||||
}
|
||||
|
||||
QJsonObject ConfigManager::getAnimations() const
|
||||
{
|
||||
return m_config["animations"].toObject();
|
||||
}
|
||||
|
||||
AppIconData ConfigManager::parseAppData(const QJsonObject &obj) const
|
||||
{
|
||||
AppIconData data;
|
||||
data.id = obj["id"].toString();
|
||||
data.name = obj["name"].toString();
|
||||
data.nameEn = obj["nameEn"].toString();
|
||||
data.icon = obj["icon"].toString();
|
||||
data.color = obj["color"].toString();
|
||||
data.badge = obj["badge"].toString();
|
||||
data.group = obj["group"].toString(); // 解析分组标识
|
||||
data.description = obj["description"].toString();
|
||||
data.warning = obj["warning"].toBool(false);
|
||||
|
||||
// 解析渐变色
|
||||
QJsonArray gradientArray = obj["gradient"].toArray();
|
||||
for (const QJsonValue &val : gradientArray)
|
||||
{
|
||||
data.gradient.append(val.toString());
|
||||
}
|
||||
|
||||
// 解析功能列表
|
||||
QJsonArray featuresArray = obj["features"].toArray();
|
||||
for (const QJsonValue &val : featuresArray)
|
||||
{
|
||||
data.features.append(val.toString());
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
PageConfig ConfigManager::parsePageConfig(const QJsonObject &obj) const
|
||||
{
|
||||
PageConfig config;
|
||||
config.id = obj["id"].toString();
|
||||
config.title = obj["title"].toString();
|
||||
|
||||
QJsonArray appsArray = obj["apps"].toArray();
|
||||
for (const QJsonValue &appVal : appsArray)
|
||||
{
|
||||
config.apps.append(parseAppData(appVal.toObject()));
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
Reference in New Issue
Block a user