172 lines
3.6 KiB
C++
172 lines
3.6 KiB
C++
#include "Config.h"
|
|
|
|
|
|
Config::Config(QObject *parent) :
|
|
QObject(parent)
|
|
{
|
|
qDebug() << "Config Operation";
|
|
|
|
QDir *Dir = new QDir;
|
|
if(!Dir->exists("./Config"))
|
|
if(Dir->mkdir("./Config"))//如果文件夹不存在就新建
|
|
{
|
|
qInfo() << "create config dir";
|
|
}
|
|
delete Dir;
|
|
Dir = nullptr;
|
|
|
|
loadJson(ConfigFile);
|
|
|
|
}
|
|
|
|
|
|
Config::~Config()
|
|
{
|
|
|
|
}
|
|
|
|
void Config::loadJson(const QString& jsonFilename)
|
|
{
|
|
if (jsonFilename.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
qDebug() << "Loading " << jsonFilename;
|
|
|
|
QFile jsonFile(jsonFilename);
|
|
if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
qWarning() << "Unable to open file" << jsonFilename << jsonFile.errorString();
|
|
return;
|
|
}
|
|
|
|
QByteArray bytes = jsonFile.readAll();
|
|
|
|
jsonFile.close();
|
|
QJsonParseError jsonParseError;
|
|
QJsonDocument doc = QJsonDocument::fromJson(bytes, &jsonParseError);
|
|
if (jsonParseError.error != QJsonParseError::NoError) {
|
|
qWarning() << jsonFilename << "Unable to open json document" << jsonParseError.errorString();
|
|
return;
|
|
}
|
|
|
|
ConfigJson = doc.object();
|
|
|
|
/*
|
|
QJsonObject json = doc.object();
|
|
|
|
QJsonValue jsonValue = json.value("config");
|
|
|
|
if(jsonValue.isObject())
|
|
{
|
|
ConfigJson = jsonValue.toObject();
|
|
}
|
|
*/
|
|
|
|
|
|
}
|
|
|
|
|
|
QJsonObject Config::getJsonObject(QString key)
|
|
{
|
|
QJsonObject obj;
|
|
|
|
QJsonValue Value = ConfigJson.value(key);
|
|
|
|
if(Value.isObject())
|
|
{
|
|
obj = Value.toObject();
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
void Config::setJsonObject(QString key,QJsonValue value)
|
|
{
|
|
|
|
ConfigJson[key] = value;
|
|
|
|
|
|
|
|
QFile file(ConfigFile);
|
|
if(!file.open(QIODevice::ReadWrite|QFile::Truncate)) {
|
|
qDebug() << "File open error";
|
|
}
|
|
|
|
|
|
QJsonDocument doc(QJsonDocument::fromJson(file.readAll()));
|
|
doc.setObject(ConfigJson);
|
|
|
|
|
|
file.write(doc.toJson());
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Config::getCommandFile()
|
|
{
|
|
QJsonObject obj = getJsonObject("commandfile");
|
|
|
|
QString file = obj.value("path").toString();
|
|
|
|
return file;
|
|
}
|
|
|
|
void Config::setCommandFile(QString file)
|
|
{
|
|
QJsonObject obj;
|
|
obj["path"] = file;
|
|
|
|
setJsonObject("commandfile",obj);
|
|
}
|
|
|
|
void Config::getConnet(QString key,
|
|
QVariant *name,QVariant *type,
|
|
QVariant *iauto,
|
|
QVariant *par1,QVariant *par2,
|
|
QVariant *par3,QVariant *par4,
|
|
QVariant *par5)
|
|
{
|
|
QJsonObject obj = getJsonObject(key);
|
|
|
|
*name = obj.value("name").toString();
|
|
*type = obj.value("type").toString();
|
|
*iauto = obj.value("auto").toString();
|
|
*par1 = obj.value("param1").toString();
|
|
*par2 = obj.value("param2").toString();
|
|
*par3 = obj.value("param3").toString();
|
|
*par4 = obj.value("param4").toString();
|
|
*par5 = obj.value("param5").toString();
|
|
}
|
|
|
|
void Config::setConnet(QString key,
|
|
QVariant name,QVariant type,
|
|
QVariant iauto,
|
|
QVariant par1,QVariant par2,
|
|
QVariant par3,QVariant par4,
|
|
QVariant par5)
|
|
{
|
|
|
|
QJsonObject obj;
|
|
|
|
obj["name"] = name.toString();
|
|
obj["type"] = type.toString();
|
|
obj["auto"] = iauto.toString();
|
|
obj["param1"] = par1.toString();
|
|
obj["param2"] = par2.toString();
|
|
obj["param3"] = par3.toString();
|
|
obj["param4"] = par4.toString();
|
|
obj["param5"] = par5.toString();
|
|
|
|
setJsonObject(key,obj);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|