#include "cmwparammodelinfo.h"
#include "ui_cmwparammodelinfo.h"
#include "logic.h"
#include <QTableWidgetItem>
#include "cctrllineedit.h"
CMWParamModelInfo::CMWParamModelInfo(QWidget *parent,
									 int iCamID,
									 int iSideID) :
	CMWidget(parent),
	ui(new Ui::CMWParamModelInfo)
{
	ui->setupUi(this);

	m_iCamID=iCamID;
	m_iSideID=iSideID;

    ui->tableWidget->horizontalHeader()->
            setSectionResizeMode(QHeaderView::Stretch);
}

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

void CMWParamModelInfo::MFresh()
{
	for(int i=0;i<5;i++)
	{
		ui->tableWidget->removeCellWidget(i,1);
	}
    ui->tableWidget->setCurrentCell(-1,-1);
	Q_ASSERT(m_iCamID==CAM2X||m_iCamID==CAM6X);
    ui->label_CamName->setText(m_gSystem.m_mapNameList[m_iCamID]);
	CProductSide* pSide=GET_CHDIRECT(m_iSideID,CProductSide);
	Q_ASSERT(pSide);
	MODEL_INFO& info=m_iCamID==CAM2X?pSide->lModels[0]:pSide->lModels[1];
	ui->tableWidget->item(0,1)->setText(QString::number(info.dMinScore));
    ui->tableWidget->item(1,1)->setText(QString::number(info.iDirectLight));
    ui->tableWidget->item(2,1)->setText(QString::number(info.iSideLight));
    ui->tableWidget->item(3,1)->setText(QString::number(info.iExposureUs));
	ui->tableWidget->item(4,1)->setText(QString::number(info.iGain));

}

void CMWParamModelInfo::MApply()
{
    on_tableWidget_currentItemChanged(0,ui->tableWidget->currentItem());
	Q_ASSERT(m_iCamID==CAM2X||m_iCamID==CAM6X);
	CProductSide* pSide=GET_CHDIRECT(m_iSideID,CProductSide);
	Q_ASSERT(pSide);
	MODEL_INFO& info=m_iCamID==CAM2X?pSide->lModels[0]:pSide->lModels[1];
	info.dMinScore=ui->tableWidget->item(0,1)->text().toDouble();
    info.iDirectLight=ui->tableWidget->item(1,1)->text().toDouble();
    info.iSideLight=ui->tableWidget->item(2,1)->text().toDouble();
    info.iExposureUs=ui->tableWidget->item(3,1)->text().toInt();
	info.iGain = ui->tableWidget->item(4,1)->text().toInt();
}

void CMWParamModelInfo::MExit()
{

	ui->tableWidget->clearContents();
}
void CMWParamModelInfo::on_tableWidget_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
{
	if(previous)
	{
		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()!=1)return;
        CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(current->text(),this);
        pLineEdit->SetRange(-10000,10000,true);
		ui->tableWidget->setCellWidget(current->row(),
									   current->column(),
									   pLineEdit);
	}
}
