This commit is contained in:
2025-10-20 22:28:37 +08:00
parent eeb5af98eb
commit 7d148d79c9
429 changed files with 13624 additions and 586480 deletions

View File

@@ -8,8 +8,48 @@ ConfigFiles::ConfigFiles(QObject *parent) : QObject(parent)
}
/**
* 读取配置值,若读取失败则使用默认值并写入配置文件
* @param group 配置组名
* @param key 配置项名
* @param defaultValue 读取失败时使用的默认值
* @return 读取到的值(或默认值)
*/
template <typename T>
T ConfigFiles::readAndSetDefault(const QString &Path ,const QString& group, const QString& key, const T& defaultValue) {
// 检查配置文件是否存在
QSettings SystemConfig(Path, QSettings::IniFormat);
SystemConfig.setIniCodec("UTF-8"); // 尝试UTF-8编码
QFileInfo configCheck(Path);
bool fileExists = configCheck.exists() && configCheck.isFile();
// 读取配置值
SystemConfig.beginGroup(group);
QVariant value = SystemConfig.value(key);
SystemConfig.endGroup();
// 判断是否读取成功
if (fileExists && value.isValid()) {
// 读取成功,返回转换后的值
return value.value<T>();
} else {
// 读取失败(文件不存在或键不存在),使用默认值并写入配置
if (isDebug) {
qDebug() << "配置项" << group << "/" << key << "读取失败,使用默认值:" << defaultValue;
}
// 写入默认值到配置文件
SystemConfig.beginGroup(group);
SystemConfig.setValue(key, defaultValue);
SystemConfig.endGroup();
SystemConfig.sync(); // 立即同步到文件
return defaultValue;
}
}
void ConfigFiles::SystemConfig(){
QFileInfo checkFile("./ProgramConfig.ini");
QSettings File("./ProgramConfig.ini", QSettings::IniFormat);
if (checkFile.exists() && checkFile.isFile()) {
@@ -23,17 +63,29 @@ void ConfigFiles::SystemConfig(){
QString Path = ConfigurationPath + "系统配置/SystemConfig.ini";
QFileInfo SystemConfigCheck(Path);
QSettings SystemConfig(Path, QSettings::IniFormat);
if (SystemConfigCheck.exists() && SystemConfigCheck.isFile()) {
TrailRefreshTime = SystemConfig.value("System/TrailRefreshTime").toUInt();
RTSP_Url = SystemConfig.value("System/RTSP_Url").toString();
if(isDebug) qDebug()<<"曲线刷新时间(秒):"<<TrailRefreshTime;
}else{
qCritical()<<"未找到曲线刷新时间配置!";
SystemConfig.setValue("System/TrailRefreshTime",10);
// 读取刷新时间配置失败时自动写入默认值10
TrailRefreshTime = readAndSetDefault(Path,"RefreshTime", "TrailRefreshTime(s)", 10);
// 读取UI刷新时间失败时自动写入默认值100
UIRefreshTime = readAndSetDefault(Path,"RefreshTime", "UIRefreshTime(ms)", 100);
// 读取RTSP地址失败时自动写入默认URL
RTSP_Url = readAndSetDefault(Path,"System", "RTSP_Url", QString("rtsp://admin:sshw1212@192.168.1.64:554/Streaming/Channels/101")
);
// 读取页面名称-按钮名称
QString AllPage = readAndSetDefault(Path,"PageName","AllPage",QString("P01#P02#P03#P04#P05#P06#P07#P08#P09#P10#P11#P12.0#P12.1#P12.2#P12.3#P12.4#P12.5#P12.6#P12.7#P12.8#P12.9#P13#P14#P15#P16"));
QStringList Pages = AllPage.split('#',Qt::SkipEmptyParts); // 忽略空段
QStringList PageNmaes={"主预览","屏蔽条件","油泵设备","截割设备","装载设备","运输设备","液压设备","故障记录","遥控测试","腔内监控","绝缘检测","网络拓扑","IOX1","IOX2","阀控1","电流模块","油质信息","PLC箱1","PLC箱2","PLC箱3","PLC箱4","本地控制","随机图册","参数设置","系统设置"};
uint8_t i=0;
for (const QString &page : Pages){
gPageName[page] = readAndSetDefault(Path,"PageName",page,QString(PageNmaes[i]));
i++;
}
}
tsServerAddr ConfigFiles::ReadServerAddr(){
tsServerAddr SAddr;
QString Path = ConfigurationPath + "OpcUa配置/OpcUa_ServerConfig.ini";

View File

@@ -0,0 +1,152 @@
#include "ConfigFiles.h"
#include <QFile>
#include <QDebug>
#include <QFileInfo>
#include <QSettings>
ConfigFiles::ConfigFiles(QObject *parent) : QObject(parent)
{
}
void ConfigFiles::SystemConfig(){
QFileInfo checkFile("./ProgramConfig.ini");
QSettings File("./ProgramConfig.ini", QSettings::IniFormat);
if (checkFile.exists() && checkFile.isFile()) {
ConfigurationPath = File.value("System/ConfigurationPath").toString();
if(isDebug) qDebug()<<"配置文件路径:"<<ConfigurationPath;
}else{
qCritical()<<"未找到系统配置文件路径!";
File.setValue("System/ConfigurationPath","./");
}
QString Path = ConfigurationPath + "系统配置/SystemConfig.ini";
QFileInfo SystemConfigCheck(Path);
QSettings SystemConfig(Path, QSettings::IniFormat);
if (SystemConfigCheck.exists() && SystemConfigCheck.isFile()) {
TrailRefreshTime = SystemConfig.value("System/TrailRefreshTime").toUInt();
RTSP_Url = SystemConfig.value("System/RTSP_Url").toString();
if(isDebug) qDebug()<<"曲线刷新时间(秒):"<<TrailRefreshTime;
}else{
qCritical()<<"未找到曲线刷新时间配置!";
SystemConfig.setValue("System/TrailRefreshTime",10);
}
}
tsServerAddr ConfigFiles::ReadServerAddr(){
tsServerAddr SAddr;
QString Path = ConfigurationPath + "OpcUa配置/OpcUa_ServerConfig.ini";
qDebug()<<"OpcUa服务配置文件路径:"<<Path;
// 检查文件是否存在
QFileInfo checkFile(Path);
// 创建QSettings对象指定INI文件路径
QSettings File(Path, QSettings::IniFormat);
if (checkFile.exists() && checkFile.isFile()) {
// 如果文件已经存在
SAddr.Host = File.value("PLC_Server/Host").toString();
SAddr.UserName = File.value("PLC_Server/UserName").toString();
SAddr.Passwd = File.value("PLC_Server/Password").toString();
qDebug()<<"读取到的PLC 地址配置:"<<SAddr.Host<<" 用户名:"<<SAddr.UserName<<" 密码:"<<SAddr.Passwd;
} else {
// 如果文件不存在
File.setValue("PLC_Server/Host", SAddr.Host);
File.setValue("PLC_Server/UserName", SAddr.UserName);
File.setValue("PLC_Server/Password", SAddr.Passwd);
qDebug()<<"写配置文件 地址配置:"<<SAddr.Host<<" 用户名:"<<SAddr.UserName<<" 密码:";
}
return SAddr;
}
/**
* @brief ReadFile_Csv 读取 CSV 文件,返回行列表
* @param &filePath CSV 文件路径
* @return QList<QStringList> 每个 QStringList 为一行的字段
*/
QList<QStringList> ConfigFiles::ReadFile_Csv(const QString &filePath){
QList<QStringList> rows;
QFile file(filePath);
QFileInfo testFile(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qWarning() << "CSV 打开失败:" << file.errorString()<<testFile.absoluteFilePath();
return rows;
}
QTextStream in(&file);
#if defined(Q_OS_LINUX) // 判断操作系统是否为Linux
in.setCodec("GBK"); // 如有需要可改成 "GBK"
#elif defined(Q_OS_WIN) // 判断操作系统是否为Windows
#else // 如果不是Linux也不是Windows
#endif
QStringList currentRow;
QString currentField;
bool inQuote = false;
while (!in.atEnd())
{
QString line = in.readLine();
// 处理跨行引号:如果上一行未闭合,继续拼
if (inQuote)
currentField += "\n";
else
currentField.clear();
for (int i = 0; i < line.size(); ++i)
{
QChar ch = line.at(i);
if (ch == '\"')
{
// 连续两个引号表示转义
if (i + 1 < line.size() && line.at(i + 1) == '\"')
{
currentField += '\"';
++i; // 跳过下一个引号
}
else
{
inQuote = !inQuote;
}
}
else if (ch == ',' && !inQuote)
{
// 字段结束
currentRow << currentField.trimmed();
currentField.clear();
}
else
{
currentField += ch;
}
}
// 如果引号已闭合,把最后一列也加入并结束本行
if (!inQuote)
{
currentRow << currentField.trimmed();
rows << currentRow;
currentRow.clear();
}
}
// 如果文件结尾时引号未闭合,把剩余部分作为最后一列
if (!currentField.isEmpty() || !currentRow.isEmpty())
{
currentRow << currentField.trimmed();
rows << currentRow;
}
file.close();
//qDebug() << "CSV 打开成功:" << file.errorString()<<testFile.absoluteFilePath()<<rows;
return rows;
}

View File

@@ -11,9 +11,13 @@ public:
QList<QStringList> ReadFile_Csv(const QString &filePath);
static tsServerAddr ReadServerAddr();
void SystemConfig();
template <typename T>
T readAndSetDefault(const QString &Path ,const QString& group, const QString& key, const T& defaultValue);
bool isDebug = true;
uint16_t TrailRefreshTime;
uint16_t UIRefreshTime;
QString RTSP_Url;
signals:

View File

@@ -0,0 +1,22 @@
#ifndef CONFIGFILES_H
#define CONFIGFILES_H
#include <QObject>
#include <GlobalDefinitions/Variable.h>
class ConfigFiles : public QObject
{
Q_OBJECT
public:
explicit ConfigFiles(QObject *parent = nullptr);
QList<QStringList> ReadFile_Csv(const QString &filePath);
static tsServerAddr ReadServerAddr();
void SystemConfig();
bool isDebug = true;
uint16_t TrailRefreshTime;
QString RTSP_Url;
signals:
};
#endif // CONFIGFILES_H