#include "cmwcammorphology_h.h"
#include "ui_cmwcammorphology_h.h"
#include "logic.h"
#include <QComboBox>
#include "cctrllineedit.h"
#include "CAMorphologyExternal.h"


CMWCamMorphology_h::CMWCamMorphology_h(QWidget *parent) :
    CMWCamAlgorithm(parent,0),
    ui(new Ui::CMWCamMorphology_h)
{
    ui->setupUi(this);
    m_pAlgorithm=0;
    ui->tableWidget_Morphology->setColumnWidth(0, 120);
    m_pMutex=0;
}

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

void CMWCamMorphology_h::MFresh()
{
    if(!m_pAlgorithm) return;
    MorphologyList::iterator ii=m_pAlgorithm->m_lMorphology.begin();
    ui->tableWidget_Morphology->clearContents();
    ui->tableWidget_Morphology->setRowCount(m_pAlgorithm->m_lMorphology.size());
    int32_t idx=0;
    for(;ii!=m_pAlgorithm->m_lMorphology.end();ii++)
    {
        QTableWidgetItem* pitem=
                new QTableWidgetItem((*ii)->m_Variants.get_Index(Var_Morphology_Type_ID));
        ui->tableWidget_Morphology->setItem(idx,0,pitem);
        pitem->setData(Qt::UserRole,(*ii)->m_Variants.get_VData(Var_Morphology_Type_ID)
                       ->toUInt());
        double d=(*ii)->m_Variants.get_VData(Var_Morphology_Thres_ID)->toDouble();
        pitem= new QTableWidgetItem(QString::number(d,'f',1));
        ui->tableWidget_Morphology->setItem(idx,1,pitem);
        pitem->setData(Qt::UserRole, d);
        d=(*ii)->m_Variants.get_VData(Var_Morphology_Width_ID)->toDouble();
        pitem= new QTableWidgetItem(QString::number(d,'f',1));
        ui->tableWidget_Morphology->setItem(idx,2,pitem);
        pitem->setData(Qt::UserRole, d);
        d=(*ii)->m_Variants.get_VData(Var_Morphology_Height_ID)->toDouble();
        pitem= new QTableWidgetItem(QString::number(d,'f',1));
        ui->tableWidget_Morphology->setItem(idx,3,pitem);
        pitem->setData(Qt::UserRole, d);
        idx++;
    }
}

void CMWCamMorphology_h::MApply()
{
    if(!m_pAlgorithm) return;

    if(m_pMutex)
    {
        QMutexLocker locker(m_pMutex);
        m_pAlgorithm->ClearMorphology();
    }
    else
    {
        m_pAlgorithm->ClearMorphology();
    }
    int32_t icnt= ui->tableWidget_Morphology->rowCount();
    int32_t i =0;

    ui->tableWidget_Morphology->setCurrentCell(-1,-1);
    for(;i<icnt;i++)
    {
        CAMorphology_h* pMorphology=new CAMorphologyExternal(QObject::trUtf8("Morphology"),0);

        QTableWidgetItem* pitem=ui->tableWidget_Morphology->item(i,0);
        QComboBox* pComboBox =(QComboBox*)ui->tableWidget_Morphology->cellWidget(i,0);
        if(pComboBox)
        {
            pitem->setText(pComboBox->currentText());
        }
        pMorphology->m_Variants.get_VData(Var_Morphology_Type_ID)->setValue(pitem->data(Qt::UserRole).toUInt());

        pitem=ui->tableWidget_Morphology->item(i,1);
        CCtrlLineEdit* pLineEdit =(CCtrlLineEdit*)ui->tableWidget_Morphology->cellWidget(i,1);
        if(pLineEdit)
        {
            pitem->setText(pLineEdit->text());
        }
        pMorphology->m_Variants.get_VData(Var_Morphology_Thres_ID)->setValue(pitem->data(Qt::UserRole).toDouble());
        pitem=ui->tableWidget_Morphology->item(i,2);
        pLineEdit =(CCtrlLineEdit*)ui->tableWidget_Morphology->cellWidget(i,2);
        if(pLineEdit)
        {
            pitem->setText(pLineEdit->text());
        }
        pMorphology->m_Variants.get_VData(Var_Morphology_Width_ID)->setValue(pitem->data(Qt::UserRole).toDouble());
        pitem=ui->tableWidget_Morphology->item(i,3);
        pLineEdit =(CCtrlLineEdit*)ui->tableWidget_Morphology->cellWidget(i,3);
        if(pLineEdit)
        {
            pitem->setText(pLineEdit->text());
        }
        pMorphology->m_Variants.get_VData(Var_Morphology_Height_ID)->setValue(pitem->data(Qt::UserRole).toDouble());


        if(m_pMutex)
        {
            QMutexLocker locker(m_pMutex);
            m_pAlgorithm->m_lMorphology.push_back(pMorphology);
        }
        else
        {
            m_pAlgorithm->m_lMorphology.push_back(pMorphology);
        }
    }
}

void CMWCamMorphology_h::on_pushButton_ADD_clicked()
{
    int iCurRow=ui->tableWidget_Morphology->currentRow();
    MApply();
    if(m_pMutex)
    {
        QMutexLocker locker(m_pMutex);
        m_pAlgorithm->InsertMorphology(iCurRow);
    }
    else
    {
        m_pAlgorithm->InsertMorphology(iCurRow);

    }
    //m_pAlgorithm->AddMorphology();
    MFresh();
}

void CMWCamMorphology_h::on_pushButton_DEL_clicked()
{
    if(m_pMutex)
    {
        QMutexLocker locker(m_pMutex);
        m_pAlgorithm->DelMorphology(ui->tableWidget_Morphology->currentRow());
    }
    else
    {
        m_pAlgorithm->DelMorphology(ui->tableWidget_Morphology->currentRow());

    }

    MFresh();
}

void CMWCamMorphology_h::on_pushButton_TEST_clicked()
{
    //TODO:如果调用太快，内部循环还没有处理就被外部删除了，会导致崩溃
    //因为上一个消息可能正在执行中
    MApply();
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Exe Morphology Sig"),
                          QVariantList()
                          <<CAM_DRIVER<<CCamDriver::EXE_MORPHOLOGY
                          <<m_iCamID<<m_pAlgorithm->m_iID,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);
}

void CMWCamMorphology_h::on_tableWidget_Morphology_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
{

    QWidget * pWidget=ui->tableWidget_Morphology->cellWidget(previousRow,previousColumn);
    if(pWidget)
    {
        if(previousColumn!=0)
        {
            QTableWidgetItem *item =ui->tableWidget_Morphology->item(previousRow,previousColumn);
            item->setText(((QLineEdit*)pWidget)->text());
            item->setData(Qt::UserRole,item->text());
        }
        else
        {
            QTableWidgetItem *item =ui->tableWidget_Morphology->item(previousRow,previousColumn);
            item->setText(((QComboBox*)pWidget)->currentText());
            item->setData(Qt::UserRole,uint(((QComboBox*)pWidget)->currentIndex()));
        }
    }
    ui->tableWidget_Morphology->removeCellWidget(previousRow,previousColumn);
    QTableWidgetItem *item =ui->tableWidget_Morphology->item(currentRow,currentColumn);
    if(item)
    {
        if(currentColumn!=0)
        {
            CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(item->text(),this);
            pLineEdit->SetRange(0,255,false);
            ui->tableWidget_Morphology->setCellWidget(currentRow,currentColumn,pLineEdit);

        }
        else
        {
            QComboBox* pComboBox =new QComboBox();
            CAMorphology* pMorphology=m_pAlgorithm->m_lMorphology.at(currentRow);
            Q_ASSERT(pMorphology);
            pComboBox->addItems(pMorphology->m_Variants.get_IndexList(Var_Morphology_Type_ID));
            pComboBox->setCurrentIndex(item->data(Qt::UserRole).toUInt());
            ui->tableWidget_Morphology->setCellWidget(currentRow,currentColumn,pComboBox);

        }
    }
}
