#include "cctrlparamedit.h"

#include <QHBoxLayout>
#include <QMessageBox>
#include "CCtrlLineEdit.h"
#include "clog.h"

CCtrlParamEdit::CCtrlParamEdit(QWidget *parent) :
    CCtrlParamBase(parent)
{
    editparam=new CCtrlLineEdit(this);
    editparam->setSizePolicy(m_hor,m_ver);
    layout->addWidget(editparam);
    layout->addWidget(staticuint);
    editparam->setText("0");
    editparam->setFrame(true);
    editparam->setMinimumWidth(100);
    setLayout(layout);

    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);


}

void CCtrlParamEdit::MParamInit(Variant *pParam, QString name)
{
    CCtrlParamBase::MParamInit(pParam,name);
    Q_ASSERT(pParam&&pParam->m_slRange.size()>=2);
    editparam->SetRange(pParam->m_slRange.at(0).toDouble(),
                        pParam->m_slRange.at(1).toDouble(),
                        pParam->m_vVar.type()==QVariant::Double);
}



void CCtrlParamEdit::MTimeFresh()
{

}

void CCtrlParamEdit::MFresh()
{
    if(m_pParam->m_vVar.type()!=QVariant::Int&&
            m_pParam->m_vVar.type()!=QVariant::Double) return;
    if(m_pParam->m_vVar.type()==QVariant::Double)
    {
        editparam->setText(QString::number(m_pParam->m_vVar.toDouble()));
    }
    else
    {
        editparam->setText(m_pParam->m_vVar.toString());
    }
}

void CCtrlParamEdit::MApply()
{
    if(m_pParam->m_vVar.type()!=QVariant::Int&&
            m_pParam->m_vVar.type()!=QVariant::Double) return;
    QVariant v=editparam->text();
    if(m_pParam->m_vVar.type()==QVariant::Double)
    {
        if (m_pParam->m_vVar != v)
        {
            __LOGINSTANCE__.AddRecord(fmt::format("Param:{} ID:{} change from: {} to {}", m_pParam->m_sName.toLocal8Bit().data(), m_pParam->m_iID, m_pParam->m_vVar.toDouble(), v.toDouble()), "ParamChanged");
        }
        m_pParam->m_vVar.setValue(v.toDouble());
    }
    else if(m_pParam->m_vVar.type()==QVariant::Int)
    {
        m_pParam->m_vVar.setValue(v.toInt());
		if (m_pParam->m_vVar != v)
        {
            __LOGINSTANCE__.AddRecord(fmt::format("Param:{} ID:{} change from: {} to {}", m_pParam->m_sName.toLocal8Bit().data(), m_pParam->m_iID, m_pParam->m_vVar.toInt(), v.toInt()), "ParamChanged");
        }
    }
}
