#include "cmwcammatchshape_h.h"
#include "ui_cmwcammatchshape_h.h"
#include "logic.h"
#include <QFileDialog>
#include "cmakepath.h"
#include "cmwcammorphology_h.h"
CMWCamMatchShape_h::CMWCamMatchShape_h(QWidget *parent, int32_t iCamID) :
    CMWCamAlgorithm(parent,iCamID),
    ui(new Ui::CMWCamMatchShape_h)
{
    ui->setupUi(this);
}

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

void CMWCamMatchShape_h::MInit()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchShape_H* pMatch=(CAMatchShape_H*)pCam->Find_Algorithm(M_MATCHSHAPE);
    Q_ASSERT(pMatch);

    Variant* pparam[50]={0};
    pparam[0]=pMatch->m_Variants.get_Variant(Var_Match_AngleExtent_ID);
    pparam[1]=pMatch->m_Variants.get_Variant(Var_Match_AngleStart_ID);
    pparam[2]=pMatch->m_Variants.get_Variant(Var_Match_AngleStep_ID);
    pparam[3]=pMatch->m_Variants.get_Variant(Var_Match_Contrast_ID);
    pparam[4]=pMatch->m_Variants.get_Variant(Var_Match_Greediness_ID);
    pparam[5]=pMatch->m_Variants.get_Variant(Var_Match_MaxOverlap_ID);
    pparam[6]=pMatch->m_Variants.get_Variant(Var_MatchShape_Metric_ID);
    pparam[7]=pMatch->m_Variants.get_Variant(Var_Match_MinContrast_ID);
    pparam[8]=pMatch->m_Variants.get_Variant(Var_Match_MiniScore_ID);
    pparam[9]=pMatch->m_Variants.get_Variant(Var_Match_ThresScore_ID);
    pparam[10]=pMatch->m_Variants.get_Variant(Var_Match_NumLevels_ID);
    pparam[11]=pMatch->m_Variants.get_Variant(Var_Match_NumMatches_ID);
    pparam[12]=pMatch->m_Variants.get_Variant(Var_MatchShape_Optimization_ID);
    pparam[13]=pMatch->m_Variants.get_Variant(Var_Match_SubPixel_ID);
    pparam[14]=pMatch->m_Variants.get_Variant(Var_Algorithm_EnableROI_ID);
    pparam[15]=pMatch->m_Variants.get_Variant(Var_MatchShape_ScaleMin_ID);
    pparam[16]=pMatch->m_Variants.get_Variant(Var_MatchShape_ScaleMax_ID);
    pparam[17]=pMatch->m_Variants.get_Variant(Var_MatchShape_ScaleStep_ID);

    ui->widget->MParamInit_list(pparam,18);
    mchildwidgets[(uintptr_t)ui->widget]=ui->widget;

    ui->widget_Model->MInit();

    CMWCamMorphology_h* pMWMorphology=new CMWCamMorphology_h(this);
    pMWMorphology->m_pAlgorithm=pMatch;
    pMWMorphology->m_iCamID=m_iCamID;
    ui->horizontalLayout_Morphology->addWidget(pMWMorphology);
    mchildwidgets[uintptr_t(pMWMorphology)]=pMWMorphology;
}

void CMWCamMatchShape_h::MFresh()
{
    CMWidget::MFresh();
    FreshModel();
}

void CMWCamMatchShape_h::MApply()
{

    CMWidget::MApply();
}

void CMWCamMatchShape_h::on_pushButton_Tool_clicked()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchShape_H* pMatch=(CAMatchShape_H*)pCam->Find_Algorithm(M_MATCHSHAPE);
    Q_ASSERT(pMatch);
    if(!pMatch->m_bModelRectInited)
    {
        pMatch->m_bModelRectInited=true;
        int32_t width=pCam->m_Variants.get_VData(Var_Cam_Width_ID)->toInt();
        int32_t height=pCam->m_Variants.get_VData(Var_Cam_Height_ID)->toInt();
        pMatch->model_rect=CRect(width/4,height/4,CMPointd(width/2,height/2));
    }
    pMatch->model_rect.m_bCenterLocked=false;
    pMatch->showragion_active(&pCam->m_imageContainer);
}

void CMWCamMatchShape_h::on_pushButton_Train_clicked()
{
    try
    {
        MApply();
        CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
        Q_ASSERT(pCam);
        CAMatchShape_H* pMatch=(CAMatchShape_H*)pCam->Find_Algorithm(M_MATCHSHAPE);
        Q_ASSERT(pMatch);
        pMatch->createModel(&pCam->m_imageContainer.m_Image,
                            &pMatch->modelData);
        FreshModel();
    }
    catch(QString& s)
    {
        m_gSystem.m_SysMsg.SetMsg(s);
    }
    //   pCamDriver->m_bTransImage=true;
}

void CMWCamMatchShape_h::on_pushButton_Exe_clicked()
{

    MApply();
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Match Shape Sig"),
                          QVariantList()
                          <<CAM_DRIVER<<CCamDriver::EXE_ALGORITHM
                          <<m_iCamID<<M_MATCHSHAPE,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);
}

void CMWCamMatchShape_h::on_pushButton_Load_clicked()
{
    try
    {
        QString path= QFileDialog::getOpenFileName(this,QString( ),
                                                   CMakePath::make_pattern_path(""),QString("*.msm" )  );
        if(path.isEmpty()) return;
        CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
        Q_ASSERT(pCam);
        CAMatchShape_H* pMatch=(CAMatchShape_H*)pCam->Find_Algorithm(M_MATCHSHAPE);
        Q_ASSERT(pMatch);

        pMatch->loadModel(path,&pMatch->modelData);
        FreshModel();

    }
    catch(QString& s)
    {
        m_gSystem.m_SysMsg.SetMsg(s);
    }
}

void CMWCamMatchShape_h::on_pushButton_Save_clicked()
{
    try
    {
        QString path= QFileDialog::getSaveFileName(this,QString( ),
                                                   CMakePath::make_pattern_path(""),QString("*.msm" ) );
        if(path.isEmpty()) return;

        QFileInfo info(path);
        path=info.absolutePath();
        path+='/'+info.baseName()+".msm";

        CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
        Q_ASSERT(pCam);
        CAMatchShape_H* pMatch=(CAMatchShape_H*)pCam->Find_Algorithm(M_MATCHSHAPE);
        Q_ASSERT(pMatch);

        pMatch->saveModel(path,&pMatch->modelData);

    }
    catch(QString& s)
    {
        m_gSystem.m_SysMsg.SetMsg(s);
    }
}

void CMWCamMatchShape_h::FreshModel()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchShape_H* pMatch=(CAMatchShape_H*)pCam->Find_Algorithm(M_MATCHSHAPE);
    Q_ASSERT(pMatch);
    pMatch->showmodel_xld(ui->widget_Model->m_pContainer);
    ui->widget_Model->setImage(pMatch->modelData.ModelImage);
    ui->widget_Model->MFresh();
}


//void CMWCamMatchShape_h::on_checkBox_ROI_clicked(bool checked)
//{

//    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
//    Q_ASSERT(pCam);
//    CAMatchShape_H* pMatch=(CAMatchShape_H*)pCam->Find_Algorithm(M_MATCHSHAPE);
//    Q_ASSERT(pMatch);
//    pMatch->m_Variants.set_VData(Var_Algorithm_EnableROI_ID,checked);
//    if(checked)
//    {
//        pCam->m_imageContainer.addShape(&pMatch->m_rect_ROI,
//                                        CImageContainer::ACTIVE_SHAPE);

//    }
//}


void CMWCamMatchShape_h::on_pushButton_ROI_clicked()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchShape_H* pMatch=(CAMatchShape_H*)pCam->Find_Algorithm(M_MATCHSHAPE);
    Q_ASSERT(pMatch);
    pCam->m_imageContainer.addShape(&pMatch->m_rect_ROI,
                                    CImageContainer::ACTIVE_SHAPE);


}
