#include "configfilemanager.h"
ConfigFileManager::ConfigFileManager()
{
}
bool ConfigFileManager::ParseConfigFile()//读取test.config文件并获取根节点
{
QString sConfigFilePath;
#if defined(Q_OS_LINUX)
sConfigFilePath = qApp->applicationDirPath() + "/../EditPic/test.config" ;//in linux
#else
sConfigFilePath = qApp->applicationDirPath() + "/../../EditPic/test.config" ;//in windows,我的工程名为EditPic
#endif
QFile file(sConfigFilePath);
if (!file.open(QFile::ReadOnly))
{
return false;
}
QString errorStr;
int errorLine;
int errorColumn;
QDomDocument qDomConfigFile;
if (!qDomConfigFile.setContent(&file, false, &errorStr, &errorLine,&errorColumn))
{
return false;
}
QDomElement root = qDomConfigFile.documentElement();
QDomNode child = root.firstChild();
if (!child.isNull())
{
if(child.toElement().tagName() == "ROOT")
{
ParseROOT(&child);
}
}
file.close();
return true;
}
bool ConfigFileManager::SaveConfigFile()
{
qSetGlobalQHashSeed(100);//为了使 通过QDomElement保存节点属性时, 每次保存的属性的顺序固定,与qSetGlobalQHashSeed(-1);搭配着用
//projectDoc.createProcessingInstruction("xml","version = "1.0"encoding = " UTF-8"");
QDomElement root = m_domDocSaveConfig.documentElement();
if(root.isNull())
{
root = m_domDocSaveConfig.createElement("configuration");
}
SaveROOT(&root);
m_domDocSaveConfig.appendChild(root);
//#if defined(Q_OS_LINUX)
// m_wstrFilePath = qApp->applicationDirPath() + "/../Tx2Oled/XMLTest/"+strProName+".config";//in linux system
//#else
QString sFilePath = qApp->applicationDirPath() + "/../../EditPic/test2.config";//in windows system XMLTest
//#endif
QFile file(sFilePath);
if (!file.open(QFile::WriteOnly|QFile::Truncate | QFile::Text))//1.QFile::WriteOnly如果文件不存在,则创建;2.QFile::Truncate打开的同时清空
{
return false;
}
QTextStream stream( &file );
stream.setCodec("utf-8");
m_domDocSaveConfig.save(stream,4,QDomNode::EncodingFromTextStream);
file.close();
qSetGlobalQHashSeed(-1);
return true;
}
void ConfigFileManager::ParseROOT(QDomNode *node)//解析根节点
{
m_tRoot.m_sVersion = node->toElement().attribute("version");
m_tRoot.m_sTime = node->toElement().attribute("time");
ParseBRANCH_LEVEL1(node,m_tRoot.m_mapBranchLevel1);
}
void ConfigFileManager::ParseBRANCH_LEVEL1(QDomNode *node, QMap<int, BRANCH_LEVEL1> &mapBranchLevel1)//解析一级分支
{
QDomNode childnode = node->toElement().firstChild();
while(!childnode.isNull())
{
if(childnode.toElement().tagName() == "BRANCH_LEVEL1")
{
BRANCH_LEVEL1 BranchLevel1;
BranchLevel1.m_sName = childnode.toElement().attribute("name");
BranchLevel1.m_nAlgoId = childnode.toElement().attribute("AlgorithmID").toInt();
BranchLevel1.m_sNickname = childnode.toElement().attribute("nickname");
BranchLevel1.m_nIndex = childnode.toElement().attribute("index").toInt();
QString sVisible = childnode.toElement().attribute("visible");
if(sVisible=="on")
{
BranchLevel1.m_bVisible = true;
}
else
{
BranchLevel1.m_bVisible = false;
}
QString sInvokerOrder = childnode.toElement().attribute("invokeOrder");
BranchLevel1.m_vInvokeOrder = SplitXmlStringInt(sInvokerOrder);
ParseBRANCH_LEVEL2(&childnode,BranchLevel1.m_mapBranchLevel2);
mapBranchLevel1.insert(BranchLevel1.m_nIndex,BranchLevel1);
}
childnode = childnode.nextSibling();
}
}
void ConfigFileManager::ParseBRANCH_LEVEL2(QDomNode *node, QMap<int, BRANCH_LEVEL2> &mapBranchLevel2)//解析二级分支
{
QDomNode childnode = node->toElement().firstChild();
while(!childnode.isNull())
{
if(childnode.toElement().tagName() == "BRANCH_LEVEL2")
{
BRANCH_LEVEL2 BranchLevel2;
BranchLevel2.m_sName = childnode.toElement().attribute("name");
BranchLevel2.m_nAlgoId = childnode.toElement().attribute("SubAlgoID").toInt();
BranchLevel2.m_sNickname = childnode.toElement().attribute("nickname");
BranchLevel2.m_nIndex = childnode.toElement().attribute("index").toInt();
QString sVisible = childnode.toElement().attribute("visible");
if(sVisible=="on")
{
BranchLevel2.m_bVisible = true;
}
else
{
BranchLevel2.m_bVisible = false;
}
ParseParameters(&childnode,BranchLevel2.m_mapParams);
mapBranchLevel2.insert(BranchLevel2.m_nIndex,BranchLevel2);
}
childnode = childnode.nextSibling();
}
}
void ConfigFileManager::ParseParameters(QDomNode *node, QMap<int,PARAM> &mapParam)//解析参数
{
QDomNode childnode = node->toElement().firstChild();
while(!childnode.isNull())
{
if(childnode.toElement().tagName() == "param")
{
PARAM Parameter;
Parameter.m_nIndex = childnode.toElement().attribute("index").toInt();
Parameter.m_sName = childnode.toElement().attribute("name");
Parameter.m_sNickname = childnode.toElement().attribute("nickname");
Parameter.m_sValueType = childnode.toElement().attribute("valuetype");
QString Value,RangeValue;
if(Parameter.m_sValueType == "int")
{
Value = childnode.toElement().attribute("value");
Parameter.m_vIntValue = SplitXmlStringInt(Value);
RangeValue = childnode.toElement().attribute("valueRange");
QStringList list = RangeValue.split(',');
if(2 == list.size())
{
Parameter.m_nValueRange[0] = list.at(0).toInt();
Parameter.m_nValueRange[1] = list.at(1).toInt();
}
}
else if(Parameter.m_sValueType == "float")
{
Value = childnode.toElement().attribute("value");
Parameter.m_vFloatValue = SplitXmlStringFloat(Value);
RangeValue = childnode.toElement().attribute("valueRange");
QStringList list = RangeValue.split(',');
if(2 == list.size())
{
Parameter.m_fValueRange[0] = list.at(0).toFloat();
Parameter.m_fValueRange[1] = list.at(1).toFloat();
}
}
else if(Parameter.m_sValueType == "string")
{
Parameter.m_sStringValue = childnode.toElement().attribute("value");
}
else if((Parameter.m_sValueType == "enum")||((Parameter.m_sValueType == "bool")))
{
Parameter.m_sStringValue = childnode.toElement().attribute("value");
QString valuelist = childnode.toElement().attribute("valueList");
Parameter.m_slistEnumValues = valuelist.split(",");
RangeValue = childnode.toElement().attribute("valueRange");
QStringList list = RangeValue.split(',');
if(2 == list.size())
{
Parameter.m_nValueRange[0] = list.at(0).toInt();
Parameter.m_nValueRange[1] = list.at(1).toInt();
}
}
QString sVisible = childnode.toElement().attribute("visible");
if(sVisible=="on")
{
Parameter.m_bVisible = true;
}
else
{