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.

155 lines
4.0 KiB
C++

#include "StdAfx.h"
#include "Propertie.h"
#include "LogMgr.h"
#include "FileMgr.h"
#include "CStringFuc.h"
CPropertie::CPropertie()
{
m_pVal = NULL;
m_pModule = NULL;
}
CPropertie::~CPropertie()
{
}
//改变属性的值,并通知属性对应的模块
void CPropertie::PropertyChangeVal(const COleVariant &OleVar)
{
//if(m_Name.GetLength()<=0)
//return;
CString Log;
switch(m_Type)
{
case _PROP_TYPE_BOOL:
Log.Format("[%s]---->[%s]",Bool2Str(*((bool *)m_pVal)),Bool2Str(OleVar.boolVal));
*((bool *)m_pVal) = OleVar.boolVal;
break;
case _PROP_TYPE_COLOR:
*((COLORREF *)m_pVal) = (COLORREF)OleVar.lVal;
break;
case _PROP_TYPE_DOUBLE:
Log.Format("[%lf]---->[%lf]",(*((double *)m_pVal)),(OleVar.dblVal));
*((double *)m_pVal) = OleVar.dblVal;
break;
case _PROP_TYPE_INT:
Log.Format("[%ld]---->[%ld]",(*((int *)m_pVal)),(OleVar.lVal));
*((int *)m_pVal) = OleVar.lVal;
break;
case _PROP_TYPE_STRING:
Log.Format("[%s]---->[%s]",(*((CString *)m_pVal)),(OleVar.bstrVal));
*((CString *)m_pVal) = OleVar.bstrVal;
break;
default:
break;
}
CString str("PropertyChange :");
Log = m_Name + Log;
Log = str + Log;
gLogMgr->WriteDebugLog(Log);
//写入文件
WriteRead(false);
//通知对应模块属性变化了
if(m_pModule)
{
m_pModule->OnPropertyChanged();
}
}
void CPropertie::WriteRead(bool bRead)
{
CString FileName = _T("\\Parameter\\") + m_Path + _T("\\")+m_Name+_T(".var");
if(bRead)
ReadFromFile(FileName);
else
WriteToFile(FileName);
}
//保存到文件
void CPropertie::WriteToFile(CString &FileName)
{
CFileMgr FileMgr;
char filepath[1024];
FileMgr.GetFullFilePath(filepath,FileName);//获取完整路径
CFile file(filepath,CFile::modeReadWrite|CFile::modeCreate);
CArchive ar(&file,CArchive::store);
switch(m_Type)
{
case _PROP_TYPE_BOOL:
ar<<(*((bool *)m_pVal));
break;
case _PROP_TYPE_DOUBLE:
ar<<(*((double *)m_pVal));
break;
case _PROP_TYPE_INT:
ar<<(*((int *)m_pVal));
break;
case _PROP_TYPE_COLOR:
ar<<(*((int *)m_pVal));
break;
case _PROP_TYPE_STRING:
ar<<(*((CString *)m_pVal));
break;
default:
break;
}
}
void CPropertie::WriteToStream(ofstream &FileStream)
{
bool b;
switch(m_Type)
{
case _PROP_TYPE_BOOL:
b = (*((bool *)m_pVal));
FileStream<<Bool2Str(b);
break;
case _PROP_TYPE_DOUBLE:
FileStream<<(*((double *)m_pVal));
break;
case _PROP_TYPE_INT:
FileStream<<(*((int *)m_pVal));
break;
case _PROP_TYPE_COLOR:
FileStream<<(*((int *)m_pVal));
break;
case _PROP_TYPE_STRING:
FileStream<<(*((CString *)m_pVal));
break;
default:
break;
}
}
//从文件读取属性值
void CPropertie::ReadFromFile(CString &FileName)
{
CFileMgr FileMgr;
char filepath[1024];
FileMgr.GetFullFilePath(filepath,FileName);//获取完整路径
CFile file;
if(file.Open(filepath,CFile::modeRead))
{
CArchive ar(&file,CArchive::load);
switch(m_Type)
{
case _PROP_TYPE_BOOL:
ar>>(*((bool *)m_pVal));
break;
case _PROP_TYPE_DOUBLE:
ar>>(*((double *)m_pVal));
break;
case _PROP_TYPE_INT:
ar>>(*((int *)m_pVal));
break;
case _PROP_TYPE_COLOR:
ar>>(*((int *)m_pVal));
break;
case _PROP_TYPE_STRING:
ar>>(*((CString *)m_pVal));
break;
default:
break;
}
}
}