#include "cmwcamproperty.h"
#include "ui_cmwcamproperty.h"
#include "logic.h"
#include "cmwcammorphology_h.h"
#include "cmwcamlight.h"
#include <QMessageBox>
CMWCamProperty::CMWCamProperty(QWidget *parent, int32_t iCamID) :
	CMWCamAlgorithm(parent,iCamID),
    ui(new Ui::CMWCamProperty)
{
	ui->setupUi(this);
    m_iTimerInterval=200;
}

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

void CMWCamProperty::MInit()
{
	CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
	Q_ASSERT(pCam);
    ui->widget_exposure->MParamInit(pCam->m_Variants.get_Variant(Var_Cam_Exposure_ID));
    ui->widget_gain->MParamInit(pCam->m_Variants.get_Variant(Var_Cam_Gain_ID));
    ui->widget_constrast->MParamInit(pCam->m_Variants.get_Variant(Var_Cam_Contrast_ID));
    ui->widget_brightness->MParamInit(pCam->m_Variants.get_Variant(Var_Cam_Brightness_ID));

    ui->widget_width->MParamInit(pCam->m_Variants.get_Variant(Var_Cam_Width_ID));
    ui->widget_height->MParamInit(pCam->m_Variants.get_Variant(Var_Cam_Height_ID));

    ui->widget_direct->MParamInit(pCam->m_Variants.get_Variant(Var_Cam_LightDirect_default_ID));
    ui->widget_side->MParamInit(pCam->m_Variants.get_Variant(Var_Cam_LightSide_default_ID));
    mchildwidgets[Var_Cam_Exposure_ID]=ui->widget_exposure;
    mchildwidgets[Var_Cam_Gain_ID]=ui->widget_gain;
    mchildwidgets[Var_Cam_Contrast_ID]=ui->widget_constrast;
    mchildwidgets[Var_Cam_Brightness_ID]=ui->widget_brightness;

    mchildwidgets[Var_Cam_Width_ID]=ui->widget_width;
    mchildwidgets[Var_Cam_Height_ID]=ui->widget_height;

    mchildwidgets[Var_Cam_LightDirect_default_ID]=ui->widget_direct;
    mchildwidgets[Var_Cam_LightSide_default_ID]=ui->widget_side;
    connect(ui->widget_exposure->sliderparam,SIGNAL(sliderReleased()),this,SLOT(set_exposure()));
    connect(ui->widget_gain->sliderparam,SIGNAL(sliderReleased()),this,SLOT(set_gain()));
    connect(ui->widget_constrast->sliderparam,SIGNAL(sliderReleased()),this,SLOT(set_constrast()));
    connect(ui->widget_brightness->sliderparam,SIGNAL(sliderReleased()),this,SLOT(set_brightness()));


    CMWCamMorphology_h* pMWMorphology=new CMWCamMorphology_h(this);
    pMWMorphology->m_pAlgorithm=pCam->m_pAMorphology;
    pMWMorphology->m_iCamID=m_iCamID;

    pMWMorphology->m_pMutex=&pCam->m_CHMutex;
    ui->horizontalLayout_Morphology->addWidget(pMWMorphology);
    mchildwidgets[uintptr_t(pMWMorphology)]=pMWMorphology;


}

void CMWCamProperty::MTimeFresh()
{
    if(m_gSystem.m_bNeedFreshUI)
    {
        m_gSystem.m_bNeedFreshUI=false;
        MFresh();
    }
	CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
	Q_ASSERT(pCam);

    CRect m_roiTool=pCam->m_pAMorphology->m_rect_ROI;
    ui->lineEdit_pixel_width->setText(QString::fromUtf8("%1").arg(m_roiTool.m_width));
    ui->lineEdit_pixel_height->setText(QString::fromUtf8("%1").arg(m_roiTool.m_height));
}

void CMWCamProperty::MFresh()
{
	CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
	Q_ASSERT(pCam);
    CMWidget::MFresh();
    ui->lineEdit_X_resolution->setText(QString::fromUtf8("%1").
									   arg(pCam->m_CoordTrans.m_dresolution_X));
    ui->lineEdit_Y_resolution->setText(QString::fromUtf8("%1").
									   arg(pCam->m_CoordTrans.m_dresolution_Y));


	CWorker* pWorker=GET_CHDIRECT(M_WORKER,CWorker);
	if(pWorker->m_workLevel>=LEVEL_FACTORAY){
		ui->lineEdit_X_resolution->setEnabled(true);
		ui->lineEdit_Y_resolution->setEnabled(true);
		ui->pushButton_EXE->setEnabled(true);
	}else{
		ui->pushButton_EXE->setEnabled(false);
		ui->lineEdit_X_resolution->setEnabled(false);
		ui->lineEdit_Y_resolution->setEnabled(false);
	}
}

void CMWCamProperty::MApply()
{
	CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
	Q_ASSERT(pCam);
    CMWidget::MApply();
	pCam->m_CoordTrans.m_dresolution_X=ui->lineEdit_X_resolution->text( ).toDouble();
	pCam->m_CoordTrans.m_dresolution_Y=ui->lineEdit_Y_resolution->text( ).toDouble();

}

void CMWCamProperty::showEvent(QShowEvent *pEvent)
{
    CMWidget::showEvent(pEvent);
    MqDebug()<<"CMWCamProperty::showEvent";
}


void CMWCamProperty::on_pushButton_TOOL_clicked()
{
	CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
	Q_ASSERT(pCam);
    pCam->m_imageContainer.addShape(&pCam->m_pAMorphology->m_rect_ROI,
									CImageContainer::ACTIVE_SHAPE);
}

void CMWCamProperty::on_pushButton_EXE_clicked()
{
	CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
	Q_ASSERT(pCam);
    float64_t realwidth,realheight;
    float64_t pixelwidth,pixelheight;
    pixelwidth=pCam->m_pAMorphology->m_rect_ROI.m_width;
    pixelheight=pCam->m_pAMorphology->m_rect_ROI.m_height;
    realwidth=ui->lineEdit_real_width->text().toDouble();
    realheight=ui->lineEdit_real_height->text().toDouble();
    if(DEQUAL0(pixelwidth)||DEQUAL0(pixelheight))
    {
        m_gSystem.m_SysMsg.SetMsg(QObject::trUtf8("pixel width or pixel height can not be zero."));
        return ;
    }

	if(QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"), 
		QObject::tr("Are you sure modify resolution")))
	{
		return;
	}
	pCam->m_CoordTrans.m_dresolution_X=realwidth/pixelwidth;
	pCam->m_CoordTrans.m_dresolution_Y=realheight/pixelheight;
    MFresh();
}

void CMWCamProperty::set_exposure()
{
    int ivalue= ui->widget_exposure->sliderparam->value();

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Set Cam Exposure Sig"),
                          QVariantList()
                          <<CAM_DRIVER<<CCamDriver::SET_CAM_PROPERTY
                          <<m_iCamID<<Var_Cam_Exposure_ID
                          <<ivalue<<true,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);
}

void CMWCamProperty::set_gain()
{

    int ivalue= ui->widget_gain->sliderparam->value();

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Set Cam Gain Sig"),
                          QVariantList()
                          <<CAM_DRIVER<<CCamDriver::SET_CAM_PROPERTY
                          <<m_iCamID<<Var_Cam_Gain_ID
                          <<ivalue<<true,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);
}

void CMWCamProperty::set_constrast()
{

    int ivalue= ui->widget_constrast->sliderparam->value();

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Set Cam Contrast Sig"),
                          QVariantList()
                          <<CAM_DRIVER<<CCamDriver::SET_CAM_PROPERTY
                          <<m_iCamID<<Var_Cam_Contrast_ID
                          <<ivalue<<true,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);
}

void CMWCamProperty::set_brightness()
{
    int ivalue= ui->widget_brightness->sliderparam->value();

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Set Cam Brightness Sig"),
                          QVariantList()
                          <<CAM_DRIVER<<CCamDriver::SET_CAM_PROPERTY
                          <<m_iCamID<<Var_Cam_Brightness_ID
                          <<ivalue<<true,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);

 }


void CMWCamProperty::on_pushButton_AutoLight_clicked()
{

    if(m_gSystem.m_pMWCamLight2X&&m_iCamID==CAM2X)
    {
        CMWCamLight* pCamLight=(CMWCamLight*)m_gSystem.m_pMWCamLight2X;
        pCamLight->SetDefaultLight();
    }
    if(m_gSystem.m_pMWCamLight6X&&m_iCamID==CAM6X)
    {
        CMWCamLight* pCamLight=(CMWCamLight*)m_gSystem.m_pMWCamLight6X;
        pCamLight->SetDefaultLight();
    }
#ifdef __USE_THIRD_CAMERA__
	if(m_gSystem.m_pMWCamLight4X&&m_iCamID==CAMKerf)
	{
		CMWCamLight* pCamLight=(CMWCamLight*)m_gSystem.m_pMWCamLight4X;
		pCamLight->SetDefaultLight();
	}
#endif

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Cam Auto Adjust Sig"),
                          QVariantList()
                          <<CAM_DRIVER<<CCamDriver::AUTOADJUST_PROPERTY
                          <<m_iCamID,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);

}
