#include "stable.h"
#include "cmwparamhalfkerfside.h"
#include "ui_cmwparamhalfkerfside.h"
#include "cctrlcombo.h"
#include "cctrllineedit.h"

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

	ui->tableWidget->setEditTriggers(QTableWidget::NoEditTriggers);
	//connect(ui->tableWidget, &QTableWidget::currentItemChanged, this, &CMWParamHalfKerfSide::on_tableWidget_currentItemChanged);

	ui->tableWidget->resizeColumnsToContents();
	for(int i=0;i<ui->tableWidget->columnCount();i++){
		ui->tableWidget->setColumnWidth(i, 70);
	}
}

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

void CMWParamHalfKerfSide::MFresh()
{
	CProcess_HalfCutKerf* pKerf=GET_PROCESS( PROCESS_HALFKERF, SM_MAIN, CProcess_HalfCutKerf);
	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 CMWParamHalfKerfSide::MApply()
{
	CProcess_HalfCutKerf* pKerf=GET_PROCESS( PROCESS_HALFKERF,
		SM_MAIN,
		CProcess_HalfCutKerf);
	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();

		{
			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 CMWParamHalfKerfSide::MInit()
{
	m_strKerPos<<tr("Center")<<tr("Side")<<tr("Spec")<<tr("Platform");
}

void CMWParamHalfKerfSide::on_tableWidget_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
{
	if(previous)
	{
		//if(previous->column()==2||previous->column()==5)
		//{
		//	CCtrlCombo* pCombo=
		//		(CCtrlCombo*)ui->tableWidget->cellWidget(previous->row(),
		//		previous->column());
		//	if(pCombo)
		//	{
		//		previous->setText(pCombo->currentText());
		//		previous->setData(Qt::UserRole,pCombo->currentData());
		//	}

		//}
		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()==2||
		current->column()==5)
		{
		CCtrlCombo* pCombo=new CCtrlCombo(this);
		if(current->column()==5)
		{
		CProcess_Kerf* pKerf=GET_PROCESS( PROCESS_KERFCHECK,
		SM_MAIN,
		CProcess_Kerf);
		Q_ASSERT(pKerf);
		int iSize=pKerf->m_vKerfParam.size();
		for(int i=0;i<iSize;i++)
		{
		pCombo->addItem(QString::number(i+1),i);
		}
		int idata= current->data(Qt::UserRole).toInt();
		pCombo->setCurrentIndex(idata);

		}
		else
		{
		int isize=m_KerfStyle.size();
		for(int i=0;i<isize;i++)
		{
		pCombo->addItem(m_KerfStyle[i],i);
		}
		int idata= current->data(Qt::UserRole).toInt();
		pCombo->setCurrentIndex(idata);
		}
		ui->tableWidget->setCellWidget(current->row(),
		current->column(),
		pCombo);
		}*/
		if(current->column()!=0)
		{
			CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(current->text(),this);
			pLineEdit->SetRange(0,25000,true);
			ui->tableWidget->setCellWidget(current->row(),
				current->column(),
				pLineEdit);
		}
	}
}
