#include "stable.h"
#include "cmwparamlaserkerfside.h"
#include "ui_cmwparamlaserkerfside.h"

CMWParamLaserKerfSide::CMWParamLaserKerfSide(QWidget *parent)
	: CMWidget(parent)
{
	ui = new Ui::CMWParamLaserKerfSide();
	ui->setupUi(this);

	ui->tableWidget->setEditTriggers(QTableWidget::NoEditTriggers);
}

CMWParamLaserKerfSide::~CMWParamLaserKerfSide()
{
	delete ui;
}

void CMWParamLaserKerfSide::MFresh()
{
	CProcess_LaserKerf* pKerf=GET_PROCESS( PROCESS_LASERKERF, SM_MAIN, CProcess_LaserKerf);
	Q_ASSERT(pKerf);
	ui->tableWidget->clearContents();
	int iSize=pKerf->m_vKerfParam.size();
	ui->tableWidget->setRowCount(iSize);
	for(int i=0;i<iSize;i++)
	{
		KERFPARAM& param=pKerf->m_vKerfParam[i];
		QTableWidgetItem* pItem=0;
		if(0 == i)
			pItem=new QTableWidgetItem("CH1");
		else
			pItem=new QTableWidgetItem("CH2");
		pItem->setData(Qt::UserRole,param.iLineCount);
		ui->tableWidget->setItem(i,0,pItem);

		pItem=new QTableWidgetItem(QString::number(param.iLight1));
		ui->tableWidget->setItem(i,1,pItem);
		pItem=new QTableWidgetItem(QString::number(param.iLight2));
		ui->tableWidget->setItem(i,2,pItem);
		pItem=new QTableWidgetItem(QString::number(param.dRange));
		ui->tableWidget->setItem(i,3,pItem);
		pItem=new QTableWidgetItem(QString::number(param.dRangeMin));
		ui->tableWidget->setItem(i,4,pItem);
		pItem=new QTableWidgetItem(QString::number(param.dOffsetMax));
		ui->tableWidget->setItem(i,5,pItem);
		pItem=new QTableWidgetItem(QString::number(param.dOffsetMin));
		ui->tableWidget->setItem(i,6,pItem);

		pItem=new QTableWidgetItem(QString::number(param.dWidthMax));
		ui->tableWidget->setItem(i,7,pItem);
		pItem=new QTableWidgetItem(QString::number(param.dWidthMin));
		ui->tableWidget->setItem(i,8,pItem);

		pItem=new QTableWidgetItem(QString::number(param.iPixelThres));
		ui->tableWidget->setItem(i,9,pItem);
		pItem=new QTableWidgetItem(m_strKerPos[param.ikerfPos]);
		pItem->setData(Qt::UserRole,param.ikerfPos);
		ui->tableWidget->setItem(i,10,pItem);
		pItem=new QTableWidgetItem(QString::number(param.iExposure));
		ui->tableWidget->setItem(i,11,pItem);

		pItem=new QTableWidgetItem(QString::number(param.iGain));
		ui->tableWidget->setItem(i,12,pItem);

		pItem=new QTableWidgetItem(QString::number(param.iBinaryThres));
		ui->tableWidget->setItem(i,13,pItem);

		pItem=new QTableWidgetItem(QString::number(param.dSpecKerfPos,'f',3));
		ui->tableWidget->setItem(i,14,pItem);
	}
}

void CMWParamLaserKerfSide::MApply()
{
	CProcess_LaserKerf* pKerf=GET_PROCESS( PROCESS_LASERKERF,
		SM_MAIN,
		CProcess_LaserKerf);
	Q_ASSERT(pKerf);
	int iSize=ui->tableWidget->rowCount();
	pKerf->m_vKerfParam.resize(iSize);
	on_tableWidget_currentItemChanged(0,ui->tableWidget->currentItem());
	for(int i=0;i<iSize;i++)
	{
		KERFPARAM& param=pKerf->m_vKerfParam[i];
		//param.iLineCount=ui->tableWidget->item(i,0)->data(Qt::UserRole).toInt();
		param.iLineCount = 1;
		param.iLight1=ui->tableWidget->item(i,1)->text().toInt();
		param.iLight2=ui->tableWidget->item(i,2)->text().toInt();
		param.dRange=ui->tableWidget->item(i,3)->text().toDouble();
		param.dRangeMin=ui->tableWidget->item(i,4)->text().toDouble();
		param.dOffsetMax=ui->tableWidget->item(i,5)->text().toDouble();
		param.dOffsetMin=ui->tableWidget->item(i,6)->text().toDouble();

		//		if(param.iLineCount==1)
		{
			param.dWidthMax=ui->tableWidget->item(i,7)->text().toDouble();
			param.dWidthMin=ui->tableWidget->item(i,8)->text().toDouble();
		}
		param.iPixelThres=ui->tableWidget->item(i,9)->text().toInt();
		param.ikerfPos=ui->tableWidget->item(i,10)->data(Qt::UserRole).toInt();
		param.iExposure=ui->tableWidget->item(i,11)->text().toInt();
		param.iGain=ui->tableWidget->item(i,12)->text().toInt();

		param.iBinaryThres=ui->tableWidget->item(i,13)->text().toInt();
		param.dSpecKerfPos=ui->tableWidget->item(i,14)->text().toDouble();

	}
}

void CMWParamLaserKerfSide::MInit()
{
	m_strKerPos<<tr("Center")<<tr("Side")<<tr("Spec")<<tr("Platform");
}

void CMWParamLaserKerfSide::on_tableWidget_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
{
	if(previous)
	{
		if(previous->column()!=0)
		{
			CCtrlLineEdit* pLineEdit=
				(CCtrlLineEdit*)ui->tableWidget->cellWidget(previous->row(),
				previous->column());
			if(pLineEdit)
			{
				previous->setText(pLineEdit->text());
			}

		}
		ui->tableWidget->removeCellWidget(previous->row(),
			previous->column());
	}

	if(current&&!m_bReadOnly)
	{
		if(current->column()!=0)
		{
			CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(current->text(),this);
			pLineEdit->SetRange(0,25000,true);
			ui->tableWidget->setCellWidget(current->row(),
				current->column(),
				pLineEdit);
		}
	}
}

