You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.1 KiB
C++

#include "AlgorithmOption.h"
#include "baseConstant.h"
const char CAlgorithmOption::cs_szSuffix[] = ".dat";
CAlgorithmOption::CAlgorithmOption(const QString& strFile, int nAlgorithm, QSqliteAlgorithm* pDbAlg)
: m_pDbAlg(pDbAlg)
{
m_fileName = strFile;
m_nAlgorithm = nAlgorithm;
QString szFileName = strFile + "_" + QString::number(nAlgorithm);
szFileName.append(TP_STR::cszJsonSuffix).append(cs_szSuffix);
int error = -1;
m_objJson = QZkJsonParser::ReadJsonObject(szFileName, &error);
if (0 != error)
{
szFileName = strFile + "_" + QString::number(nAlgorithm);
szFileName.append(TP_STR::cszJsonSuffix);
m_objJson = QZkJsonParser::ReadJsonObject(szFileName, &error);
}
m_bNeedSave = false;
m_nAlgorithmsUsings = 0;
m_pDbAlg->CreateTable(nAlgorithm);
}
CAlgorithmOption::~CAlgorithmOption()
{
}
int CAlgorithmOption::GetIntValue(const QString& skey, int nDefault)
{
QJsonValue v = m_objJson.value(skey);
if (v.isUndefined() || !v.isDouble())
{
Insert(skey, QJsonValue(nDefault));
return nDefault;
}
else
{
return v.toInt(nDefault);
}
}
void CAlgorithmOption::SetIntValue(const QString& skey, int value)
{
Insert(skey, QJsonValue(value));
}
QString CAlgorithmOption::GetStringValue(const QString& skey, const QString& default/* = ""*/)
{
QJsonValue v = m_objJson.value(skey);
if (v.isUndefined() || !v.isString())
{
Insert(skey, QJsonValue(default));
return default;
}
else
{
return v.toString();
}
}
void CAlgorithmOption::SetStringValue(const QString& skey, const QString& value)
{
Insert(skey, QJsonValue(value));
}
QJsonObject CAlgorithmOption::GetObjectValue(const QString& skey)
{
QJsonValue v = m_objJson.value(skey);
if (v.isUndefined() || !v.isString())
{
return QJsonObject();
}
else
{
return v.toObject();
}
}
bool CAlgorithmOption::Save(bool bWait/* = true*/)
{
if (!m_bNeedSave)
{
return true;
}
if (!bWait)
{
if (!m_wLock.tryLockForWrite())
{
return false;
}
}
else
{
m_wLock.lockForWrite();
}
//if (!m_bNeedSave || !m_wLock.tryLockForWrite())
//{
// return;
//}
QString szFileName = m_fileName + "_" + QString::number(m_nAlgorithm);
szFileName.append(TP_STR::cszJsonSuffix).append(cs_szSuffix);
bool b = QZkJsonParser::WriteJsonObject(szFileName, m_objJson);
m_bNeedSave = !b;
m_wLock.unlock();
return b;
}
QVariantMap CAlgorithmOption::VariantMap()
{
return m_objJson.toVariantMap();
}
bool CAlgorithmOption::DbSetValue(const QString& key, int type, const QBuffer& data)
{
if (NULL == m_pDbAlg)
{
return false;
}
return m_pDbAlg->ReplaceRecord(m_nAlgorithm, key, type, data.buffer());
}
bool CAlgorithmOption::DbSetValue(const QString& key, int type, const QByteArray& data)
{
if (NULL == m_pDbAlg)
{
return false;
}
return m_pDbAlg->ReplaceRecord(m_nAlgorithm, key, type, data);
}
bool CAlgorithmOption::DbDelValue(const QString& key)
{
if (NULL == m_pDbAlg)
{
return false;
}
return m_pDbAlg->DeleteRecord(m_nAlgorithm, key);
}
bool CAlgorithmOption::DbGetValue(const QString& key, int type, QByteArray& data)
{
if (NULL == m_pDbAlg)
{
return false;
}
return m_pDbAlg->ReadRecord(m_nAlgorithm, key, type, data);
//m_pDbAlg->ReplaceRecord(m_nAlgorithm, key, type, data);
}
QStringList CAlgorithmOption::DbGetKeysByType(int type)
{
if (NULL == m_pDbAlg)
{
return QStringList();
}
return m_pDbAlg->KeysByType(m_nAlgorithm, type);
}
QVariant CAlgorithmOption::GetValue(const QString& skey, const QVariant& def)
{
QJsonValue v = m_objJson.value(skey);
if (v.isUndefined())
{
Insert(skey, QJsonValue::fromVariant(def));
return def;
}
else
{
return v.toVariant();
}
}
void CAlgorithmOption::SetValue(const QString& skey, const QVariant& value)
{
Insert(skey, QJsonValue::fromVariant(value));
}