Quilting-lw/settings/settings.cpp
2026-01-23 16:37:18 +08:00

550 lines
14 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "settings.h"
#include <qrgb.h>
#include <bits/stdc++.h>
using namespace std;
Settings::Settings()
{
m_configFile.clear();
m_HMIconfigFile.clear();
m_pSettings = NULL;
}
Settings::~Settings()
{
if (m_pSettings)
{
// QSettings 的析构函数会自动调用 sync(),确保所有内存中的更改写入文件
delete m_pSettings;
m_pSettings = NULL; // 防止悬空指针(虽然对象已销毁)
}
}
void Settings::loadSetting(QString configfile)
{
if (configfile.isEmpty() == false)
{
m_configFile = configfile;
//使用 new 动态分配 QSettings 对象
m_pSettings = new QSettings(m_configFile, QSettings::IniFormat);
}
fillDefaultColor();
}
void Settings::loadHMISetting(QString configfile)
{
if (configfile.isEmpty() == false)
{
m_HMIconfigFile = configfile;
}
}
//写入值到ini文件
void Settings::writeToIniFile(const QMap<QString, QVariant>& keyValueMap)
{
if(m_configFile.length() <= 0 || keyValueMap.isEmpty()){return;}
// 锁定(如果需要线程安全)
QMutexLocker locker(&m_mutex);
// 在同一个 QSettings 对象生命周期内设置所有值
for (QMap<QString, QVariant>::const_iterator it = keyValueMap.constBegin(); it != keyValueMap.constEnd(); ++it)
{
m_pSettings->setValue(it.key(), it.value());
}
#ifdef Q_OS_LINUX
system("sync");
#endif
}
//写入值到ini文件
void Settings::writeToInHMIiFile(QString key, QVariant value)
{
if(m_HMIconfigFile.length() <= 0){return;}
QSettings iniSetting(m_HMIconfigFile, QSettings::IniFormat);
iniSetting.setValue(key,value);
//qDebug()<<"key"<<key;
#ifdef Q_OS_LINUX
system("sync");
#endif
}
void Settings::writeOneToIniFile(QString key, QVariant value)
{
if(m_configFile.length() <= 0){return;}
QMutexLocker locker(&m_mutex);
m_pSettings->setValue(key,value);
}
QVariant Settings::readFromIniFile(QString key)
{
if(m_configFile.length() <= 0){return -1;}
QMutexLocker locker(&m_mutex);
return m_pSettings->value(key);
}
//从HMIini文件读取值
QVariant Settings::readFromInHMIiFile(QString key)
{
if(m_HMIconfigFile.length() <= 0){return -1;}
QSettings iniSetting(m_HMIconfigFile, QSettings::IniFormat);
return iniSetting.value(key);
}
bool Settings::ifKeyExists(QString key)
{
if(key.length() <= 0){return false;}
if(m_configFile.length() <= 0){return false;}
QMutexLocker locker(&m_mutex);
return m_pSettings->contains(key);
}
bool Settings::ifHMIKeyExists(QString key)
{
if(m_HMIconfigFile.length() <= 0){return false;}
QSettings iniSetting(m_HMIconfigFile, QSettings::IniFormat);
return iniSetting.contains(key);
}
//hsb转换成rgb
void Settings::HSItoRGB(float h,float s,float i,float *r,float *g,float *b)
{
int hi = ((int)h / 60) % 6;
float f = h / 60 - hi;
float p = i * (1 - s);
float q = i * (1 - f * s);
float t = i * (1 - (1 - f) * s);
switch(hi)
{
case 0:
*r = i; *g = t; *b = p;
break;
case 1:
*r = q; *g = i; *b = p;
break;
case 2:
*r = p; *g = i; *b = t;
break;
case 3:
*r = p; *g = q; *b = i;
break;
case 4:
*r = t; *g = p; *b = i;
break;
case 5:
*r = i; *g = p; *b = q;
break;
default:
break;
}
*r = (int)((*r) * 255);
*g = (int)((*g) * 255);
*b = (int)((*b) * 255);
}
void Settings::fillDefaultColor()
{
m_colorRGB.clear();
#if (1)
vector<pair<float, float> > sbv;
sbv.push_back(make_pair(1, 0.7));//尾部插入数字
sbv.push_back(make_pair(0.9, 0.7));
sbv.push_back(make_pair(0.7, 0.7));
sbv.push_back(make_pair(0.5, 0.7));
sbv.push_back(make_pair(0.3, 0.7));
sbv.push_back(make_pair(0.1, 0.7));
sbv.push_back(make_pair(0.1, 1));
sbv.push_back(make_pair(0.3, 1));
sbv.push_back(make_pair(0.5, 1));
sbv.push_back(make_pair(0.7, 1));
sbv.push_back(make_pair(0.9, 1));
sbv.push_back(make_pair(1, 1));
float hv[] = {15, 31, 46, 61, 76, 91, 106, 121, 136, 151, 166, 181, 196, 211, 226, 241, 256, 271, 286, 301, 316, 331, 346};
QRgb rgb[EMB_MC_SW_ND];
int num = 0;
for(int i = 0; i < 12; i ++)
{
float s = sbv[i].first;
float b = sbv[i].second;
for(int j = 0; j < 24; j ++)
{
if(j==24)
{
float R = 225;
float G = 225;
float B = 225;
HSItoRGB(hv[j], s, b, &R, &G, &B);
rgb[num ++] = qRgb(R, G, B);
}
else
{
float R, G, B;
HSItoRGB(hv[j], s, b, &R, &G, &B);
rgb[num ++] = qRgb(R, G, B);
}
}
}
for (int i = 0; i < EMB_MC_SW_ND; i++)
{
m_colorRGB.append((char*)(&(rgb[i])), sizeof(QRgb));
}
#else
u8 r, g, b;
u8 * prgb;
const int cnum = 243; // 3的5次方
int rcount = 0;
int gcount = 0;
int bcount = 0;
int temp;
r = 102;
g = 255;
b = 154;
for (int i = 0; i < cnum; i++)
{
if ((rcount <= gcount) && (rcount <= bcount))
{
prgb = &r;
rcount++;
rcount %= 5;
}
else if ((gcount <= rcount) && (gcount <= bcount))
{
prgb = &g;
gcount++;
gcount %= 5;
}
else if ((bcount <= rcount) && (bcount <= gcount))
{
prgb = &b;
bcount++;
bcount %= 5;
}
else
{
break;
}
temp = *prgb;
temp += (int)(51.2*3 + 0.5);
if (temp > 255)
{
temp -= 256;
}
*prgb = temp;
m_colorRGB.append(r);
m_colorRGB.append(g);
m_colorRGB.append(b);
m_colorRGB.append(255);
}
#endif
}
void Settings::writeToCsv(QString strText,int type)
{
QString filePath;
filePath.clear();
QDir apppath(qApp->applicationDirPath());
if(type == TYPE_ERROR)
{
filePath = apppath.path() + apppath.separator() + CSV_ERROR;
}
else if(type == TYPE_BREAK)
{
filePath = apppath.path() + apppath.separator() + CSV_BREAK;
}
else if(type == TYPE_DEBUGINFO)
{
filePath = apppath.path() + apppath.separator() + CSV_DEBUGINFO;
}
if(filePath.length() <= 0){return;}
QFile file(filePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
{
QString strCurTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");//时间
QString strMessage;
QTextStream in(&file);//写
if(type == TYPE_DEBUGINFO)
{
strMessage = QString("%1//%2").arg(strCurTime).arg(strText);//时间,内容
}
else
{
strMessage = QString("%1,%2").arg(strCurTime).arg(strText);//时间,内容
}
in << strMessage << '\n';
#ifdef Q_OS_LINUX
system("sync");
#endif
file.close();
}
}
void Settings::clearToCsv(int type)
{
QString filePath;
filePath.clear();
QStringList tempbar;
tempbar.clear();
QDir apppath(qApp->applicationDirPath());
if(type == TYPE_ERROR)
{
filePath = apppath.path() + apppath.separator() + CSV_ERROR;
}
else if(type == TYPE_BREAK)
{
filePath = apppath.path() + apppath.separator() + CSV_BREAK;
}
else if(type == TYPE_DEBUGINFO)
{
filePath = apppath.path() + apppath.separator() + CSV_DEBUGINFO;
}
if(filePath.length() <= 0){return;}
// 定义可能的格式列表,把最可能的格式放在前面
QStringList possibleFormats;
possibleFormats.clear();
possibleFormats.append("yyyy/M/d hh:mm:ss");
possibleFormats.append("yyyy/MM/dd hh:mm:ss");
possibleFormats.append("yyyy/M/d hh:mm");
possibleFormats.append("yyyy/MM/dd hh:mm");
QStringList strAlarmInfoList;
strAlarmInfoList.clear();
QString oneItem;
oneItem.clear();
int timeFlag = 0;
QFile file(filePath);
if (file.open(QIODevice::ReadOnly))
{
QTextStream out(&file);//读
QStringList tempOption = out.readAll().split("\n");
// 获取系统当前时间
QDateTime dateTime = QDateTime::currentDateTime();
int curSecond = dateTime.toTime_t();//当前时间转换为秒
int differSecond = curSecond - 30 * 24 * 60 * 60; //减去30天
for (int i = 0; i < tempOption.count(); i++)//tempOption文件里的所有行
{
oneItem = tempOption[i];
if(oneItem.length() <= 0)
{
continue;
}
if(type == TYPE_DEBUGINFO)
{
tempbar= oneItem.split("//");//获取每一行 以“//”号分开
}
else
{
tempbar = oneItem.split(",");//获取每一行 以“,”号分开
}
if (tempbar.size() < 2)
continue;
if (tempbar.at(0).indexOf(QString("DateTime")) != -1)
continue;
QString listTime = tempbar.at(0).trimmed(); // 必须 trim
QDateTime cdateTime;
bool parsed = false;
for (int i = 0; i < possibleFormats.size(); i++)
{
cdateTime = QDateTime::fromString(listTime, possibleFormats[i]);
if (cdateTime.isValid())
{
parsed = true;
break; // 一旦成功,就跳出循环
}
}
if (!parsed)
{
continue; // 跳过这一行,不要使用无效时间
}
int newTime = cdateTime.toTime_t();//转换为秒
//没超过30天的存在列表里
if(newTime >= differSecond)
{
if (tempbar.size() >= 2)
{
strAlarmInfoList << oneItem;
}
else
{
return;
}
}
else
{
timeFlag = 1;
}
}
file.close();
}
int reWriteFlag = 0;//重写标志
if(timeFlag == 1)
{
reWriteFlag = 1;
}
else if(strAlarmInfoList.size()>800)
{
while(strAlarmInfoList.size()>800)
{
strAlarmInfoList.removeFirst();
}
reWriteFlag = 1;
}
if(reWriteFlag == 1)
{
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
QTextStream in(&file);//写
for(int i = 0; i < strAlarmInfoList.size(); i++)
{
in << strAlarmInfoList[i] << '\n';
}
file.close();
#ifdef Q_OS_LINUX
system("sync");
#endif
}
}
return;
}
QStringList Settings::readToCsv(int type)
{
QStringList strAlarmInfoList;
strAlarmInfoList.clear();
QStringList strInfoList;
strInfoList.clear();
QStringList newTempOption;
newTempOption.clear();
QString allinfo;
allinfo.clear();
QStringList tempbar;
tempbar.clear();
QString filePath;//文件
filePath.clear();
QDir apppath(qApp->applicationDirPath());
if(type == TYPE_ERROR)
{
filePath = apppath.path() + apppath.separator() + CSV_ERROR;
}
else if(type == TYPE_BREAK)
{
filePath = apppath.path() + apppath.separator() + CSV_BREAK;
}
else if(type == TYPE_DEBUGINFO)
{
filePath = apppath.path() + apppath.separator() + CSV_DEBUGINFO;
}
if(filePath.length() <= 0){return strAlarmInfoList;}
QString time;//一共有2项时间内容
QString info;
QString oneTemp;
oneTemp.clear();
QFile file(filePath);
if (file.open(QIODevice::ReadOnly))
{
QTextStream out(&file);//读
QStringList tempOption = out.readAll().split("\n");
int tempSize = tempOption.count();
for (int i = 0; i < tempSize; i++)//tempOption文件里的所有行
{
tempbar.clear();
oneTemp = tempOption.at(i);
if(type == TYPE_DEBUGINFO)
{
tempbar= oneTemp.split("//");//获取每一行 以“//”号分开
}
else
{
tempbar = oneTemp.split(",");//获取每一行 以“,”号分开
}
//DateTime不显示
if (oneTemp.indexOf(QString("DateTime")) != -1)
continue;
time.clear();
if(tempbar.size() >= 2)
{
time = tempbar.at(0);
}
info.clear();
if(time.length() > 0)
{
if(tempbar.size() >= 2)
{
info = tempbar.at(1);
}
//time.remove(" ");//清除空格
if(type == TYPE_DEBUGINFO)
{
allinfo=time+"//"+info;
}
else
{
allinfo=time+","+info;
}
newTempOption.append(allinfo);
strInfoList << newTempOption[newTempOption.count()-1];
}
//qDebug()<<"i"<<i;
}
for(int i = 0; i < strInfoList.count(); i++)
{
//if( (i != 0) && (((strInfoList.count() - 1 - i) >= 0 ) && ((strInfoList.count() - 1 - i) < strInfoList.count())))
if( ((strInfoList.count() - 1 - i) >= 0 && ((strInfoList.count() - 1 - i) < strInfoList.count())))
{
strAlarmInfoList << strInfoList[strInfoList.count() -1- i]; //xcy 0313 日志倒序显示
}
}
}
file.close();
qDebug()<<"strAlarmInfoList.size()"<<strAlarmInfoList.size();
return strAlarmInfoList;
}
//清除文件内容,不是删除文件
void Settings::fileClear(QString filePath)
{
QFile file(filePath);
file.open(QFile::WriteOnly|QFile::Truncate);
#ifdef Q_OS_LINUX
system("sync");
#endif
file.close();
}