#include "cstatusmotorpos.h"
#include "ui_cstatusmotorpos.h"
#include "logic.h"
#include <QGridLayout>
#include "cctrlparamstatic.h"
#include "gui.h"
#include <QPixmap>
#include <QPainter>
#include <QColor>
CStatusMotorPos::CStatusMotorPos(QWidget *parent, bool btrans) :
    CMWidget(parent),
    ui(new Ui::CStatusMotorPos)
{
    ui->setupUi(this);

    m_pGridLayOut=new QGridLayout(this);
    setLayout(m_pGridLayOut);
    m_pGridLayOut->setContentsMargins(0,0,0,0);
    m_pGridLayOut->setSpacing(0);
    m_iTimerInterval=30;
    m_iColumnCount=3;
    m_iRowHeight=30;
    m_BKColor=Qt::transparent;
    m_bTrans = btrans;
    if(!btrans)
    {
        m_motorMap[MOTORX]=MOTORX;
        m_motorMap[MOTORY]=MOTORY;
        m_motorMap[MOTORZ]=MOTORZ;
        m_motorMap[MOTORW]=MOTORW;

        //m_motorMap[MOTORG1_PCHANGE]=MOTORG1_PCHANGE;
        m_motorMap[MOTORG2_POWER]=MOTORG2_POWER;
        /*m_motorMap[MOTORG3_WIDEINDEX]=MOTORG3_WIDEINDEX;
        m_motorMap[MOTORG4_WIDE5050]=MOTORG4_WIDE5050;*/
#ifdef __USE_G5_G6__
        m_motorMap[MOTORG5_NARROWINDEX]=MOTORG5_NARROWINDEX;
        m_motorMap[MOTORG6_NARROW5050]=MOTORG6_NARROW5050;
#endif
        //m_motorMap[MOTORG7_MASK]=MOTORG7_MASK;
		m_motorMap[MOTORH1_UPDOWN]=MOTORH1_UPDOWN;
		m_motorMap[MOTORH2_TRANS]=MOTORH2_TRANS;
		m_motorMap[MOTORJ1_UPDOWN]=MOTORJ1_UPDOWN;
		m_motorMap[MOTORJ2_TRANS]=MOTORJ2_TRANS;
        //m_motorMap[MOTOR_SHUTTER]=MOTOR_SHUTTER;

        m_motorMap[HGRIPPER_DRIVER] = HGRIPPER_DRIVER;
        
        m_sName=tr("WT Pos");
    }
    else
    {

        m_motorMap[MOTORA_CLAW]=MOTORA_CLAW;
        m_motorMap[MOTORB1_LOCK]=MOTORB1_LOCK;
        m_motorMap[MOTORB2_RAIL] = MOTORB2_RAIL;
        m_motorMap[MOTORC_CASS]=MOTORC_CASS;
        m_motorMap[MOTORD_TRANS]=MOTORD_TRANS;

        m_motorMap[MOTORE_COAT_SPINDLE]=MOTORE_COAT_SPINDLE;
        m_motorMap[MOTORE1_COAT_CLEAN_ARM]=MOTORE1_COAT_CLEAN_ARM;
        m_motorMap[MOTORE2_COAT_COAT_ARM]=MOTORE2_COAT_COAT_ARM;
        m_motorMap[MOTORE3_COAT_BUMP]=MOTORE3_COAT_BUMP;
        //m_motorMap[MOTORE4_COAT_CURTAIN]=MOTORE4_COAT_CURTAIN;

        m_motorMap[MOTORF_CLEAN_SPINDLE]=MOTORF_CLEAN_SPINDLE;
        m_motorMap[MOTORF1_CLEAN_ARM]=MOTORF1_CLEAN_ARM;
        //m_motorMap[MOTORF4_CLEAN_CURTAIN]=MOTORF4_CLEAN_CURTAIN;
		//m_motorMap[MOTORF2_CLEAN_ARM2]=MOTORF2_CLEAN_ARM2;
        
  
        m_sName=tr("Transfer Pos");
    }

}

CStatusMotorPos::~CStatusMotorPos()
{
    delete m_pGridLayOut;
    delete ui;
}

void CStatusMotorPos::MInit()
{

}

void CStatusMotorPos::showEvent(QShowEvent *event)
{
    UNUSED(event);
    CMWidget::showEvent(event);
}

void CStatusMotorPos::hideEvent(QHideEvent *event)
{
    UNUSED(event);
    CMWidget::hideEvent(event);
}

void CStatusMotorPos::paintEvent(QPaintEvent * )
{
    QPainter painter(this);
    painter.drawPixmap(0,0,m_Pixmap);
}

void CStatusMotorPos::MTimeFresh()
{

    int iSMID=SM_IO;
    VS_Thread* m_pState=(VS_Thread*)m_gSystem.FindSM(iSMID)->GetIdleState();
    if(m_pState->QueueSize()<100)
    {
        m_pState->pushback_Msg(VarMsg_Driver_ID,QObject::trUtf8("Fresh Pos"),
                               QVariantList()
                               <<MOTOR_DRIVER<<CMotorDriver::FRESHPOS_MULTI
                               <<32,0);

        if(!m_bTrans) {
            m_pState->pushback_Msg(VarMsg_Driver_ID, QObject::trUtf8("Fresh Pos"),
                                   QVariantList()
                                   << HGRIPPER_DRIVER << CDHGripperRS485::kFreshPos, 0);
        }
    }
    MFresh();


}

void CStatusMotorPos::MFresh()
{
    int index=0;
    int iWidth=rect().width()/m_iColumnCount;
    QRect rect=QRect(0,0,iWidth,m_iRowHeight);
    rect.adjust(5,5,-5,-5);
    QPoint center=rect.center();
    m_Pixmap=QPixmap(size());
    m_Pixmap.fill(m_BKColor);
    QPainter painter(&m_Pixmap);
    if(!painter.isActive()) return;
    painter.initFrom(this);
 //   painter.setPen(Qt::white);
    MOTORMAP::iterator ii=m_motorMap.begin();
    for(; ii != m_motorMap.end(); ii++) {
        if(ii.key() == HGRIPPER_DRIVER){
            CDHGripperRS485* pHgripDriver = GET_CARD(HGRIPPER_DRIVER, CDHGripperRS485);
            QString name = tr("%1").arg(pHgripDriver->m_sName);
            painter.drawText(rect, Qt::AlignVCenter, name);
            QRect rect1 = rect;
            rect1.setLeft(rect1.left() + iWidth * 0.46);

            int iValue = pHgripDriver->m_Variants.get_VData(Var_Motor_Pos_ID)->toInt();
            QString data = QString::number(iValue);
            painter.drawText(rect1, Qt::AlignVCenter, data);
            
            QRect rect2 = rect;
            rect2.adjust(-5, -5, 5, 5);

            painter.drawRoundedRect(rect2, 2, 2);
            index++;
            QPoint pt = QPoint((index % m_iColumnCount) * iWidth,
                               index / m_iColumnCount * m_iRowHeight) + center;
            rect.moveCenter(pt);
            break;
        }

        CMotor* pMotor=GET_CHDIRECT(ii.key(),CMotor);
        Q_ASSERT(pMotor);
        QString name=tr("%1(%2):")
                .arg(pMotor->m_sName)
                .arg(pMotor->m_Variants.get_VData(Var_Base_SerialID_ID)->toString());
        painter.drawText(rect,Qt::AlignVCenter,name);
        QRect rect1=rect;
        rect1.setLeft(rect1.left()+iWidth*0.46);
        QString data;
		if(MOTORX == pMotor->m_iID || MOTORY == pMotor->m_iID
			|| MOTORW == pMotor->m_iID || MOTORZ == pMotor->m_iID)
		{
			data=QString::number(pMotor->m_Variants.get_VData(Var_Motor_Pos_ID)->toDouble(),'f',4);
		}
        else if (/*MOTORG2_POWER == pMotor->m_iID || */MOTORG3_WIDEINDEX == pMotor->m_iID
                 || MOTORG4_WIDE5050 == pMotor->m_iID || MOTORG5_NARROWINDEX == pMotor->m_iID
                 || MOTORG6_NARROW5050 == pMotor->m_iID) {
            data = QString::number(pMotor->m_Variants.get_VData(Var_Motor_Pos_ID)->toDouble(), 'f', 2);
        } else {
            data = QString::number(pMotor->m_Variants.get_VData(Var_Motor_Pos_ID)->toDouble(), 'f', 3);
        }
        painter.drawText(rect1,Qt::AlignVCenter,data);
        QString uint=pMotor->m_Variants.get_VData(Var_Motor_Unit_ID)->toString();

        painter.drawText(rect,Qt::AlignVCenter|Qt::AlignRight,uint);

        QRect rect2=rect;
        rect2.adjust(-5,-5,5,5);

        painter.drawRoundedRect(rect2, 2, 2);
        index++;
        QPoint pt=QPoint((index%m_iColumnCount)*iWidth,
                         index/m_iColumnCount*m_iRowHeight)+center;
        rect.moveCenter(pt);
    }
    update();
}

