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

CMWCamMatchNCC_h::~CMWCamMatchNCC_h()
{
    delete ui;
}
void CMWCamMatchNCC_h::MInit()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_MATCHNCC);
    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_Greediness_ID);
    pparam[3]=pMatch->m_Variants.get_Variant(Var_Match_MaxOverlap_ID);
    pparam[4]=pMatch->m_Variants.get_Variant(Var_MatchNcc_Metric_ID);
    pparam[5]=pMatch->m_Variants.get_Variant(Var_Match_MiniScore_ID);
    pparam[6]=pMatch->m_Variants.get_Variant(Var_Match_ThresScore_ID);
    pparam[7]=pMatch->m_Variants.get_Variant(Var_Match_NumLevels_ID);
    pparam[8]=pMatch->m_Variants.get_Variant(Var_Match_NumMatches_ID);
    pparam[9]=pMatch->m_Variants.get_Variant(Var_Match_SubPixel_ID);
    pparam[10]=pMatch->m_Variants.get_Variant(Var_Algorithm_EnableROI_ID);

    ui->widget->MParamInit_list(pparam,11);
    mchildwidgets[(intptr_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[intptr_t(pMWMorphology)]=pMWMorphology;

}

void CMWCamMatchNCC_h::MFresh()
{
    CMWidget::MFresh();
    FreshModel();
}
void CMWCamMatchNCC_h::MApply()
{
    CMWidget::MApply();
}

void CMWCamMatchNCC_h::FreshModel()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_MATCHNCC);
    Q_ASSERT(pMatch);
    if(pMatch->modelData.iModelID!=-1)
    {
        pMatch->showmodel_xld(ui->widget_Model->m_pContainer);
        ui->widget_Model->setImage(pMatch->modelData.ModelImage);

    }
    else
    {
        ui->widget_Model->setImage(pMatch->modelData.ModelImage);
    }
    ui->widget_Model->MFresh();
}

void CMWCamMatchNCC_h::on_pushButton_Tool_clicked()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_MATCHNCC);
    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->showragion_active(&pCam->m_imageContainer);
}

void CMWCamMatchNCC_h::on_pushButton_Train_clicked()
{
    try
    {
        CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
        Q_ASSERT(pCam);
        CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_MATCHNCC);
        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 CMWCamMatchNCC_h::on_pushButton_Load_clicked()
{
    try
    {
        QString path= QFileDialog::getOpenFileName(this,QString( ),
                                                   CMakePath::make_pattern_path(""),
                                                   QString("*.mnm" )  );
        if(path.isEmpty()) return;
        CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
        Q_ASSERT(pCam);
        CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_MATCHNCC);
        Q_ASSERT(pMatch);


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

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

void CMWCamMatchNCC_h::on_pushButton_Save_clicked()
{
    try
    {
        QString path= QFileDialog::getSaveFileName(this,QString( ),
                                                   CMakePath::make_pattern_path(""),\
                                                   QString(".mnm" ) );
        if(path.isEmpty()) return;

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

        CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
        Q_ASSERT(pCam);
        CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_MATCHNCC);
        Q_ASSERT(pMatch);

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

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

void CMWCamMatchNCC_h::on_pushButton_Exe_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Match Ncc Sig"),
                          QVariantList()
                          <<CAM_DRIVER<<CCamDriver::EXE_ALGORITHM
                          <<m_iCamID<<M_MATCHNCC,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);

}

//void CMWCamMatchNCC_h::on_checkBox_ROI_clicked()
//{

//}

//void CMWCamMatchNCC_h::on_checkBox_ROI_clicked(bool checked)
//{
//    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
//    Q_ASSERT(pCam);
//    CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_MATCHNCC);
//    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 CMWCamMatchNCC_h::on_pushButton_ROI_clicked()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_MATCHNCC);
    Q_ASSERT(pMatch);
    pCam->m_imageContainer.addShape(&pMatch->m_rect_ROI,
                                    CImageContainer::ACTIVE_SHAPE);
}
