#include "cstatusmodel.h"
#include "logic.h"
#include <QFileInfo>
#include <cpp/HalconCpp.h>
CStatusModel::CStatusModel(QWidget *parent) : CMWidget(parent)
{
    setMinimumHeight(110);
    setMinimumWidth(220);
    m_pGridLayOut=new QGridLayout(this);
	setLayout(m_pGridLayOut);
    if(m_gSystem.m_iMatchType==M_MATCHNCC)
    {
        m_pMtachForLoad=new CAMatchNCC_H("matchshapeforload",0);
    }
    else if(m_gSystem.m_iMatchType==M_MATCHSHAPE)
    {
        m_pMtachForLoad=new CAMatchShape_H("matchshapeforload",0);
    }
	m_iSideID=-1;
}

CStatusModel::~CStatusModel()
{
    delete m_pMtachForLoad;
    ClearContainer();
}

void CStatusModel::MTimeFresh()
{
	CProductSide* pSide=GET_CHDIRECT(m_iSideID,CProductSide);
	Q_ASSERT(pSide);
    if(  strCurFile!=m_gSystem.GetCurFile()||
			pSide->m_bModelChanged)
    {
		pSide->m_bModelChanged=false;
        strCurFile=m_gSystem.GetCurFile();
        MFresh();
	}
}

void CStatusModel::MInit()
{
	CreateModelImage();
}

void CStatusModel::CreateModelImage()
{
	ClearContainer();
	int i=0;
	for(i=0;i<2;i++)
    {
        CCtrlMImg_OPenGL* pCtrlContainer=new CCtrlMImg_OPenGL(this);
        pCtrlContainer->setMinimumHeight(100);
        pCtrlContainer->setMinimumWidth(120);
        pCtrlContainer->setMaximumHeight(200);
        pCtrlContainer->setMaximumWidth(240);
        pCtrlContainer->MInit();
		m_pGridLayOut->addWidget(pCtrlContainer,i/2,i%2);
		limageContainer.push_back(pCtrlContainer);
		pCtrlContainer->refreshPixmap();
    }
	setMinimumHeight(((i+1)/2)*110);

}

void CStatusModel::ClearContainer()
{
    QList<CCtrlMImg_OPenGL*>::iterator ii= limageContainer.begin();
    while(m_pGridLayOut->takeAt(0)!=0);
    for(;ii!=limageContainer.end();ii++)
    {
        if(*ii)
        {
            delete *ii;*ii=0;
        }
    }
    limageContainer.clear();
}

void CStatusModel::loadModel(int32_t icamID, CCtrlMImg_OPenGL *pCtrlContainer)
{
    CProcess_Level* pLevel=GET_PROCESS(PROCESS_LEVELING,
                                       SM_MAIN,
                                       CProcess_Level);
    ModelData* pModelData=pLevel->GetModelData(m_iSideID,icamID);
    pCtrlContainer->m_pContainer->SetName(m_gSystem.m_mapNameList[icamID]);
    if(pModelData->iModelID!=-1)
	{
//		CProductSide* pSide=GET_CHDIRECT(m_iSideID,CProductSide);
//		Q_ASSERT(pSide);
//		QString modelpath= pSide->makeModelPath(icamID);

//        m_pMtachForLoad->loadModel(modelpath,pModelData);

//        QFileInfo info(modelpath);
//        pCtrlContainer->m_pContainer->clearShape(-1);
//        pCtrlContainer->m_pContainer->clearString();
//        pCtrlContainer->m_pContainer->addString(info.fileName());
        m_pMtachForLoad->setModelData(pModelData);
        m_pMtachForLoad->showmodel_xld(pCtrlContainer->m_pContainer);
        pCtrlContainer->setImage(pModelData->ModelImage);
    }
    else
    {
        pModelData->ModelImage.resetImage();
        pCtrlContainer->setImage(pModelData->ModelImage);
    }
    pModelData->bChanged=false;
}

void CStatusModel::MFresh()
{
	loadModel(  CAM2X, limageContainer[0]);
	loadModel(  CAM6X, limageContainer[1]);
    QList<CCtrlMImg_OPenGL*>::iterator ii= limageContainer.begin();
    for(;ii!=limageContainer.end();ii++)
    {
        if(*ii)
        {
            (*ii)->refreshPixmap();
        }
    }
}

