#include "cmwparamkerfside.h"
#include "ui_cmwparamkerfside.h"
#include "logic.h"

#include "cctrllineedit.h"
#include "cctrlcombo.h"
CMWParamKerfSide::CMWParamKerfSide(QWidget *parent, int iSideID) :
	CMWidget(parent),
	ui(new Ui::CMWParamKerfSide), m_bAlignCheck(false)
{
	
	ui->setupUi(this);
	m_iSideID=iSideID;
    m_KerfStyle << tr("LaserOnly")<<tr("Laser&Halfcut")<<tr("HalfcutOnly");

    ui->tableWidget->horizontalHeader()->
            setSectionResizeMode(QHeaderView::Interactive);
	//ui->tableWidget->horizontalHeader()->
	//	setSectionResizeMode(QHeaderView::Interactive);

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

	ui->tableWidget->setColumnHidden(1, true);
	ui->tableWidget->setColumnWidth(2, 120);
}

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

void CMWParamKerfSide::MFresh()
{
    CProcess_Level* pLevel = GET_PROCESS(PROCESS_LEVELING,
                                         SM_MAIN, CProcess_Level);
	bool bAlignCheck = pLevel->m_Variants.get_VData(Var_Process_EnableHalfcut_Check)->toInt() != 1;
	if (m_bAlignCheck != bAlignCheck)
	{
        if (bAlignCheck)
        {
            m_KerfStyle.clear();
            m_KerfStyle << tr("LaserOnly") << tr("Laser&Target") << tr("Target");
        }
        else
        {
            m_KerfStyle.clear();
            m_KerfStyle << tr("LaserOnly") << tr("Laser&Halfcut") << tr("HalfcutOnly");
        }
		m_bAlignCheck = bAlignCheck;
	}
	
    
	CProductSide* pSide=GET_CHDIRECT( m_iSideID,
										CProductSide);
	Q_ASSERT(pSide);
	ui->tableWidget->clearContents();
	int iSize=pSide->m_vCutParam.size();
	ui->tableWidget->setRowCount(iSize);
	for(int i=0;i<iSize;i++)
	{
		SINGLECUTPARAM& param=pSide->m_vCutParam[i];
		QTableWidgetItem* pItem=0;

        pItem=new QTableWidgetItem(QString::fromUtf8("PASS%1").arg(i+1));
        ui->tableWidget->setItem(i,0,pItem);

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

		pItem=new QTableWidgetItem(m_KerfStyle[param.iKerfStyle]);
		pItem->setData(Qt::UserRole,param.iKerfStyle);
        ui->tableWidget->setItem(i,2,pItem);

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

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

		pItem=new QTableWidgetItem(QString::number(param.iKerfIndex+1));
		pItem->setData(Qt::UserRole,param.iKerfIndex);
        ui->tableWidget->setItem(i,5,pItem);

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

void CMWParamKerfSide::MApply()
{
	CProductSide* pSide=GET_CHDIRECT( m_iSideID,
										CProductSide);
	Q_ASSERT(pSide);
	int iSize=ui->tableWidget->rowCount();
    pSide->m_vCutParam.resize(iSize);
    on_tableWidget_currentItemChanged(0,ui->tableWidget->currentItem());
	for(int i=0;i<iSize;i++)
    {
		SINGLECUTPARAM& param=pSide->m_vCutParam[i];
        param.dKerfExpand=ui->tableWidget->item(i,1)->text().toDouble();
        param.iKerfStyle=ui->tableWidget->item(i,2)->data(Qt::UserRole)
				.toInt();
        param.iKerfFirst=ui->tableWidget->item(i,3)->text().toDouble();
        param.iKerfFreq=ui->tableWidget->item(i,4)->text().toDouble();
        param.iKerfIndex=ui->tableWidget->item(i,5)->data(Qt::UserRole)
				.toInt();

        param.dKerfOffset=ui->tableWidget->item(i,6)->text().toDouble();

	}
}

void CMWParamKerfSide::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());
			}

		}
        else 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);
		}
        else if(current->column()!=0)
		{
            CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(current->text(),this);
            pLineEdit->SetRange(-10000,10000,true);
			ui->tableWidget->setCellWidget(current->row(),
										   current->column(),
										   pLineEdit);
		}
	}
}
