#include "logic_local_def.h"
#include "cworktablelaseroffset.h"

CWorktableLaserOffset::CWorktableLaserOffset(const QString& name,int32_t id)
	:CWorktableOffset(name, id)
{
}


CWorktableLaserOffset::~CWorktableLaserOffset(void)
{
}

QDomElement CWorktableLaserOffset::WriteConfig(QDomDocument &doc, QDomElement &root)
{
	writeOffsetList();
	writeOffsetToCsv();
	return CChannel::WriteConfig(doc,root);
}

int CWorktableLaserOffset::ReadConfig(QDomElement &root)
{
	readOffsetList();

	return CChannel::ReadConfig( root);
}

void CWorktableLaserOffset::writeOffsetList()
{
	QString strPath=
		CMakePath::make_config_path("Worktable_Laser_Offset.ldf");
	QFile file(strPath);
	if(!file.open(QIODevice::WriteOnly))
	{
		return;
		MQT_THROW_STR(QObject::trUtf8("open path %1 failed")
			.arg(strPath));
	}
	QDataStream stream(&file);
	int iSize=m_offsetList.size();
	stream<<iSize;
	OFFSETLIST::iterator ii;
	for (ii=m_offsetList.begin();ii!=m_offsetList.end();ii++)
	{
		stream<<(*ii).first<<(*ii).second;
	}
}

void CWorktableLaserOffset::readOffsetList()
{
	QString strPath=
		CMakePath::make_config_path("Worktable_Laser_Offset.ldf");
	QFile file(strPath);
	if(!file.open(QIODevice::ReadOnly))
	{
		return;
		MQT_THROW_STR(QObject::trUtf8("open path %1 failed")
			.arg(strPath));
	}
	QDataStream stream(&file);
	m_offsetList.clear();
	int iSize;
	stream>>iSize;
	for (int j=0;j<iSize;j++)
	{
		QPair<double,double> pair;
		stream>>pair.first>>pair.second;
		m_offsetList.push_back(pair);
	}
}

void CWorktableLaserOffset::writeOffsetToCsv()
{
	QString strPath=
		CMakePath::make_config_path("Worktable_Laser_Offset.csv");
	QFile file(strPath);
	if(!file.open(QIODevice::WriteOnly))
	{
		return;
		MQT_THROW_STR(QObject::trUtf8("open path %1 failed")
			.arg(strPath));
	}
	QTextStream stream(&file);
	int iSize=m_offsetList.size();
	stream<<"Pos,Offset\n";
	OFFSETLIST::iterator ii;
	for (ii=m_offsetList.begin();ii!=m_offsetList.end();ii++)
	{
		stream<<(*ii).first<<","<<(*ii).second<<"\n";
	}
}
