#include "cmwcalibeamrate.h"
#include "ui_cmwcalibeamrate.h"
#include "common_include.h"
CMWCaliBeamRate::CMWCaliBeamRate(QWidget* parent) :
    CMWidget(parent),
    ui(new Ui::CMWCaliBeamRate)
{
    ui->setupUi(this);
    m_dCurWidth = -1;
}

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

void CMWCaliBeamRate::MFresh()
{
    CLaserPath* pPath = GET_CHDIRECT(LASERPATH_D_WIDE, CLaserPath);
    Q_ASSERT(pPath);
    int isize = pPath->m_mBeamPowerRate.size();
    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(isize);
    auto ii = pPath->m_mBeamPowerRate.begin();
    int i = 0;
    for (; ii != pPath->m_mBeamPowerRate.end(); ii++)
    {
        QTableWidgetItem* pitem =
            new QTableWidgetItem(QString::number(ii.key() / 1000.0, 'f', 3));
        ui->tableWidget->setItem(i, 0, pitem);
        pitem = new QTableWidgetItem(QString::number((*ii).dPowerRate, 'f', 3));
        ui->tableWidget->setItem(i, 1, pitem);
        i++;
    }
}

void CMWCaliBeamRate::MApply()
{
    CLaserPath* pPath = GET_CHDIRECT(LASERPATH_D_WIDE, CLaserPath);
    Q_ASSERT(pPath);
    int isize = ui->tableWidget->rowCount();
    pPath->m_mBeamPowerRate.clear();
    for (int i = 0; i < isize; i++)
    {
        QTableWidgetItem* pitem = ui->tableWidget->item(i, 0);
        int iwidth = pitem->text().toDouble() * 1000;
        pitem = ui->tableWidget->item(i, 1);
        double dPowerRate = pitem->text().toDouble();
        if (pPath->m_mBeamPowerRate.contains(iwidth))
        {
            pPath->m_mBeamPowerRate[iwidth].dPowerRate = dPowerRate;
        }
        else
        {
            DBeamPowerRate data;
            data.dBeamWidth = iwidth / 1000.0;
            data.dPowerRate = dPowerRate;
            pPath->m_mBeamPowerRate[iwidth] = data;
        }
    }
}

void CMWCaliBeamRate::on_tableWidget_cellClicked(int row, int column)
{
    UNUSED(column);
    m_dCurWidth = ui->tableWidget->item(row, 0)->text().toDouble();
    ui->lineEdit_Width->setText(ui->tableWidget->item(row, 0)->text());
    ui->lineEdit_Pos->setText(ui->tableWidget->item(row, 1)->text());
}

void CMWCaliBeamRate::on_pushButton_Del_clicked()
{
    CLaserPath* pPath = GET_CHDIRECT(LASERPATH_D_WIDE, CLaserPath);
    Q_ASSERT(pPath);
    pPath->DelPowerRate(m_dCurWidth);
    MFresh();
}

void CMWCaliBeamRate::on_pushButton_Add_clicked()
{
    CLaserPath* pPath = GET_CHDIRECT(LASERPATH_D_WIDE, CLaserPath);
    Q_ASSERT(pPath);
    double dWidth = ui->lineEdit_Width->text().toDouble();
    double dPos = ui->lineEdit_Pos->text().toDouble();
    pPath->AddPowerRate(dWidth, dPos);
    MFresh();
}

void CMWCaliBeamRate::on_pushButton_Move_clicked()
{
    /*int iPos = static_cast<int>(ui->lineEdit_Pos->text().toDouble());
    V_MsgQueue msgqueue;

    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Move Gripper Sig"),
                          QVariantList() << HGRIPPER_DRIVER
                          << CDHGripperRS485::MOVEPOS
                          << iPos);
    m_gSystem.start_test(SM_MAIN, msgqueue, false);*/
}
