#include "cmwparamkerf.h"
#include "ui_cmwparamkerf.h"
#include "logic.h"
#include "cctrllineedit.h"
#include "cctrlcombo.h"
#include "cmwparamkerfside.h"
CMWParamKerf::CMWParamKerf(QWidget *parent) :
	CMWidget(parent),
	ui(new Ui::CMWParamKerf)
{
	ui->setupUi(this);

	ui->tableWidget->setColumnHidden(9, true);
	ui->tableWidget->setColumnHidden(10, true);

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

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

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

void CMWParamKerf::MFresh()
{
	CMWidget::MFresh();
	MFreshKerf();
}

void CMWParamKerf::MApply()
{
	CMWidget::MApply();
	MApplyKerf();
}

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

	CMWParamKerfSide* pMWKerfSideA=new CMWParamKerfSide(this,SIDE_A);
	mchildwidgets[(uintptr_t)pMWKerfSideA]=pMWKerfSideA;
	ui->horizontalLayout_A->addWidget(pMWKerfSideA);
	CMWParamKerfSide* pMWKerfSideB=new CMWParamKerfSide(this,SIDE_B);
	mchildwidgets[(uintptr_t)pMWKerfSideB]=pMWKerfSideB;
    ui->horizontalLayout_B->addWidget(pMWKerfSideB);
}

void CMWParamKerf::MFreshKerf()
{
	CProcess_Kerf* pKerf=GET_PROCESS( PROCESS_KERFCHECK,
										SM_MAIN,
									 CProcess_Kerf);
	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;
		pItem=new QTableWidgetItem(QString::number(param.iLineCount));
		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);
//		if(param.iLineCount==1)
		{
			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);
		}
//		else
//		{
//			pItem=new QTableWidgetItem("-");
//			ui->tableWidget->setItem(i,7,pItem);
//			pItem=new QTableWidgetItem("-");
//			ui->tableWidget->setItem(i,8,pItem);
//		}
		if(param.iLineCount==2)
		{
			pItem=new QTableWidgetItem(QString::number(param.dGAPMax));
			ui->tableWidget->setItem(i,9,pItem);
			pItem=new QTableWidgetItem(QString::number(param.dGAPMin));
			ui->tableWidget->setItem(i,10,pItem);
		}
		else
		{
			pItem=new QTableWidgetItem("-");
			ui->tableWidget->setItem(i,9,pItem);
			pItem=new QTableWidgetItem("-");
			ui->tableWidget->setItem(i,10,pItem);
		}
		pItem=new QTableWidgetItem(QString::number(param.iPixelThres));
		ui->tableWidget->setItem(i,11,pItem);
		pItem=new QTableWidgetItem(m_strKerPos[param.ikerfPos]);
		pItem->setData(Qt::UserRole,param.ikerfPos);
		ui->tableWidget->setItem(i,12,pItem);
        pItem=new QTableWidgetItem(QString::number(param.iExposure));
        ui->tableWidget->setItem(i,13,pItem);

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

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

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

void CMWParamKerf::MApplyKerf()
{

	CProcess_Kerf* pKerf=GET_PROCESS( PROCESS_KERFCHECK,
										SM_MAIN,
									 CProcess_Kerf);
	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.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();
		}
		if(param.iLineCount==2)
		{
			param.dGAPMax=ui->tableWidget->item(i,9)->text().toDouble();
			param.dGAPMin=ui->tableWidget->item(i,10)->text().toDouble();
		}
		param.iPixelThres=ui->tableWidget->item(i,11)->text().toInt();
		param.ikerfPos=ui->tableWidget->item(i,12)->data(Qt::UserRole).toInt();
        param.iExposure=ui->tableWidget->item(i,13)->text().toInt();
        param.iGain=ui->tableWidget->item(i,14)->text().toInt();

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

    }
}

void CMWParamKerf::MTimeFresh()
{
    CWorker* pWork=GET_CHDIRECT(M_WORKER,CWorker);
    if(m_iCurLevel!=pWork->m_workLevel)
    {
        m_iCurLevel=pWork->m_workLevel;
        ui->pushButton_del->setEnabled(m_iCurLevel>=LEVEL_ENGINEER);
        ui->pushButton_add->setEnabled(m_iCurLevel>=LEVEL_ENGINEER);
    }
}

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

		}
		else
		{
			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)
	{
		m_iSelectRow=current->row();

		if(current->column()==0||
				current->column()==12)
		{
			CCtrlCombo* pCombo=new CCtrlCombo(this);
			if(current->column()==0)
			{
				pCombo->addItem("1",1);
				pCombo->addItem("2",2);
				int idata= current->data(Qt::UserRole).toInt();
				pCombo->setCurrentIndex(idata==1?0:1);

			}
			else
			{
				pCombo->addItem(m_strKerPos[0],0);
				pCombo->addItem(m_strKerPos[1],1);
                pCombo->addItem(m_strKerPos[2],2);
				pCombo->addItem(m_strKerPos[3],3);
				int idata= current->data(Qt::UserRole).toInt();
				pCombo->setCurrentIndex(idata);
			}
			ui->tableWidget->setCellWidget(current->row(),
										   current->column(),
										   pCombo);
		}
		else
		{
            CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(current->text(),this);
            pLineEdit->SetRange(0,25000,true);
			ui->tableWidget->setCellWidget(current->row(),
										   current->column(),
										   pLineEdit);
		}
	}
}

void CMWParamKerf::on_pushButton_add_clicked()
{
	MApply();
	CProcess_Kerf* pKerf=GET_PROCESS( PROCESS_KERFCHECK,
										SM_MAIN,
									 CProcess_Kerf);
	Q_ASSERT(pKerf);
	int iSize=pKerf->m_vKerfParam.size();
	KERFPARAM  KerfParam;
	if (iSize)
	{
		KerfParam=pKerf->m_vKerfParam[iSize-1];
	}
	pKerf->m_vKerfParam.push_back(KerfParam);
	MFresh();
}

void CMWParamKerf::on_pushButton_del_clicked()
{
	CProcess_Kerf* pKerf=GET_PROCESS( PROCESS_KERFCHECK,
										SM_MAIN,
									 CProcess_Kerf);
	Q_ASSERT(pKerf);
	int iSize=pKerf->m_vKerfParam.size();
	int iIndex=m_iSelectRow;

	if (!OUTOFRANGE(iIndex,0,iSize))
	{
		pKerf->m_vKerfParam.erase(pKerf->m_vKerfParam.begin()+iIndex);
		MFresh();
	}
}
