#include "controlactionwidget.h"
#include "ui_controlactionwidget.h"

ControlActionWidget::ControlActionWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ControlActionWidget),
    m_itemPerPage(12),
    m_btnPerRow(5)
{
    ui->setupUi(this);

    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);

    m_mousePressX = 0;
    m_mousePressY = 0;
    m_mouseReleaseX = 0;
    m_mouseReleaseY = 0;

    m_speed = 0;

    this->move(0+g_mainWidgetPos.x(),0+g_mainWidgetPos.y());

    m_curPage = 0;
    m_itemList.clear();
    m_allItemList.clear();
    initControl();
    initArabControlPos();
}

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

//初始化窗体控件，包括位置、尺寸、样式
void ControlActionWidget::initControl()
{
    m_itemFrameList.clear();
    m_itemFrameList.append(ui->frame1);
    m_itemFrameList.append(ui->frame2);
    m_itemFrameList.append(ui->frame3);
    m_itemFrameList.append(ui->frame4);
    m_itemFrameList.append(ui->frame5);
    ui->frame6->setVisible(false);
    ui->frame7->setVisible(false);
    ui->frame8->setVisible(false);
    ui->frame9->setVisible(false);
    ui->frame10->setVisible(false);
    ui->frame11->setVisible(false);
    ui->frame12->setVisible(false);

    if(g_emResolut == resolution1910)//1920x1080的分辨率每页显示12个条目
    {
        ui->frame6->setVisible(true);
        ui->frame7->setVisible(true);
        ui->frame8->setVisible(true);
        ui->frame9->setVisible(true);
        ui->frame10->setVisible(true);
        ui->frame11->setVisible(true);
        ui->frame12->setVisible(true);
        m_itemFrameList.append(ui->frame6);
        m_itemFrameList.append(ui->frame7);
        m_itemFrameList.append(ui->frame8);
        m_itemFrameList.append(ui->frame9);
        m_itemFrameList.append(ui->frame10);
        m_itemFrameList.append(ui->frame11);
        m_itemFrameList.append(ui->frame12);
    }
    else if(g_emResolut == resolution1006)//1024x600的分辨率每页显示8个条目
   {
       ui->frame6->setVisible(true);//显示
       ui->frame7->setVisible(true);
       ui->frame8->setVisible(true);
       m_itemFrameList.append(ui->frame6);
       m_itemFrameList.append(ui->frame7);
       m_itemFrameList.append(ui->frame8);
   }
   
    m_itemLabelList.clear();
    m_itemLabelList.append(ui->label1);
    m_itemLabelList.append(ui->label2);
    m_itemLabelList.append(ui->label3);
    m_itemLabelList.append(ui->label4);
    m_itemLabelList.append(ui->label5);
    m_itemLabelList.append(ui->label6);
    m_itemLabelList.append(ui->label7);
    m_itemLabelList.append(ui->label8);
    m_itemLabelList.append(ui->label9);
    m_itemLabelList.append(ui->label10);
    m_itemLabelList.append(ui->label11);
    m_itemLabelList.append(ui->label12);

    m_itemBtnList.clear();
    m_itemBtnList.append(ui->button1_1);
    m_itemBtnList.append(ui->button1_2);
    m_itemBtnList.append(ui->button1_3);
    m_itemBtnList.append(ui->button1_4);
    m_itemBtnList.append(ui->button1_5);
    m_itemBtnList.append(ui->button2_1);
    m_itemBtnList.append(ui->button2_2);
    m_itemBtnList.append(ui->button2_3);
    m_itemBtnList.append(ui->button2_4);
    m_itemBtnList.append(ui->button2_5);
    m_itemBtnList.append(ui->button3_1);
    m_itemBtnList.append(ui->button3_2);
    m_itemBtnList.append(ui->button3_3);
    m_itemBtnList.append(ui->button3_4);
    m_itemBtnList.append(ui->button3_5);
    m_itemBtnList.append(ui->button4_1);
    m_itemBtnList.append(ui->button4_2);
    m_itemBtnList.append(ui->button4_3);
    m_itemBtnList.append(ui->button4_4);
    m_itemBtnList.append(ui->button4_5);
    m_itemBtnList.append(ui->button5_1);
    m_itemBtnList.append(ui->button5_2);
    m_itemBtnList.append(ui->button5_3);
    m_itemBtnList.append(ui->button5_4);
    m_itemBtnList.append(ui->button5_5);
    m_itemBtnList.append(ui->button6_1);
    m_itemBtnList.append(ui->button6_2);
    m_itemBtnList.append(ui->button6_3);
    m_itemBtnList.append(ui->button6_4);
    m_itemBtnList.append(ui->button6_5);
    m_itemBtnList.append(ui->button7_1);
    m_itemBtnList.append(ui->button7_2);
    m_itemBtnList.append(ui->button7_3);
    m_itemBtnList.append(ui->button7_4);
    m_itemBtnList.append(ui->button7_5);
    m_itemBtnList.append(ui->button8_1);
    m_itemBtnList.append(ui->button8_2);
    m_itemBtnList.append(ui->button8_3);
    m_itemBtnList.append(ui->button8_4);
    m_itemBtnList.append(ui->button8_5);
    m_itemBtnList.append(ui->button9_1);
    m_itemBtnList.append(ui->button9_2);
    m_itemBtnList.append(ui->button9_3);
    m_itemBtnList.append(ui->button9_4);
    m_itemBtnList.append(ui->button9_5);
    m_itemBtnList.append(ui->button10_1);
    m_itemBtnList.append(ui->button10_2);
    m_itemBtnList.append(ui->button10_3);
    m_itemBtnList.append(ui->button10_4);
    m_itemBtnList.append(ui->button10_5);
    m_itemBtnList.append(ui->button11_1);
    m_itemBtnList.append(ui->button11_2);
    m_itemBtnList.append(ui->button11_3);
    m_itemBtnList.append(ui->button11_4);
    m_itemBtnList.append(ui->button11_5);
    m_itemBtnList.append(ui->button12_1);
    m_itemBtnList.append(ui->button12_2);
    m_itemBtnList.append(ui->button12_3);
    m_itemBtnList.append(ui->button12_4);
    m_itemBtnList.append(ui->button12_5);
    for(int var = 0; var < m_itemBtnList.size(); var++)
    {
        connect(m_itemBtnList.at(var),
                SIGNAL(clicked(bool)),
                this,
                SLOT(slotItemBtnClicked())
                );

        connect(m_itemBtnList.at(var),
                SIGNAL(pressed()),
                this,
                SLOT(slotItemBtnPressed())
                );

        connect(m_itemBtnList.at(var),
                SIGNAL(released()),
                this,
                SLOT(slotItemBtnReleased())
                );
    }

    //根据不同分辨率设置控件的位置和尺寸
    switch (g_emResolut)
    {
    case resolution1910:
        initResolution1910();
        break;
    case resolution1006:
        initResolution1006();
        break;
    default:
        this->resize(1920,1080);
        break;
    }

    initControlStyle();//初始化窗体控件样式
}

//初始化窗体控件为1920x1080分辨率
void ControlActionWidget::initResolution1910()
{
    this->resize(1920,1080);
    ui->frameBack->setGeometry(0,0,1920,1080);
    ui->buttonTypeLogo->setGeometry(128,66,78,78);
    ui->labelMainTitle->setGeometry(226,60,1000,48);
    ui->labelSubTitle->setGeometry(302,112,1000,36);

    for(int i = 0; i < m_itemPerPage; i++)
    {
        m_itemFrameList[i]->setGeometry(295,180 + i * 64,1330,58);
        m_itemLabelList[i]->setGeometry(40,0,820,58);
    }

    for(int i = 0; i < m_btnPerRow; i++)
    {
        for(int j = 0; j < m_itemPerPage; j++)
        {
            m_itemBtnList[m_btnPerRow*j+i]->setGeometry(648+i*132,0,120,54);
        }
    }

    ui->framePageBtn->setGeometry(1120,990,800,70);
    ui->labelPage->setGeometry(32,1008,400,40);
    ui->buttonPgUp->setGeometry(200,0,168,70);
    ui->buttonPgDn->setGeometry(400,0,168,70);
    ui->buttonBack->setGeometry(600,0,168,70);
}
//初始化窗体控件为1024x600分辨率
void ControlActionWidget::initResolution1006()
{
    m_itemPerPage = m_itemPerPage - 4;
    this->resize(1024,600);

    ui->frameBack->setGeometry(0,0,1024,600);
    ui->buttonTypeLogo->setGeometry(25,30,60,60);
    ui->labelMainTitle->setGeometry(100,30,200,24);
    ui->labelSubTitle->setGeometry(130,64,240,21);

    for(int i = 0; i < m_itemPerPage; i++)
    {
        m_itemFrameList[i]->setGeometry(154,98 + i * 53,718,53);
        //m_itemLabelList[i]->setGeometry(10,22,331,26);
        m_itemLabelList[i]->setGeometry(10,22,340,26);
    }
    for(int i = 0; i < m_btnPerRow; i++)
    {
        for(int j = 0; j < m_itemPerPage; j++)
        {
            m_itemBtnList[m_btnPerRow*j+i]->setGeometry(283+i*86,12,80,32);//蓝色按钮大小位置
        }
    }


    ui->framePageBtn->setGeometry(700,545,600,45);
    ui->labelPage->setGeometry(20,550,102,45);
    ui->buttonPgUp->setGeometry(0,0,96,40);
    ui->buttonPgDn->setGeometry(106,0,96,40);
    ui->buttonBack->setGeometry(212,0,96,40);
}

//初始化窗体控件样式
void ControlActionWidget::initControlStyle()
{
    SetControlStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    //背景图
    QString frameBackImgPath = setControlStyle.getSharedStyleSheet();
    ui->frameBack->setStyleSheet(frameBackImgPath);
    ui->labelMainTitle->setFont(fontBold_1);
    ui->labelMainTitle->setStyleSheet(LABELWHITESTYLE);
    ui->labelSubTitle->setFont(fontNormal_1);
    ui->labelSubTitle->setStyleSheet(LABELWHITESTYLE);
    ui->labelPage->setFont(fontNormal_1);
    ui->labelPage->setStyleSheet(LABELWHITESTYLE);

    //条目frame
    for(int i = 0; i < m_itemPerPage; i++)
    {
       m_itemFrameList[i]->setStyleSheet(TRANSPARENTSTYLE);
    }

    //条目label
    for(int i = 0; i < m_itemPerPage; i++)
    {
        m_itemLabelList[i]->setFont(fontNormal_1);
        m_itemLabelList[i]->setStyleSheet(LABELWHITESTYLE);
    }

    //条目按钮
    for(int i = 0; i < m_itemBtnList.size(); i++)
    {
        m_itemBtnList[i]->setBlueGradientBottomStyle();//都用一个底图
        m_itemBtnList[i]->setFont(fontNormal_1);
    }

    //四个按钮样式表一样
    ui->buttonPgUp->setOrangeGradientBottomStyle(BORDER_RADIUS2);
    ui->buttonPgUp->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgUp->objectName()),12);

    ui->buttonPgDn->setOrangeGradientBottomStyle(BORDER_RADIUS2);
    ui->buttonPgDn->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgDn->objectName()),12);

    ui->buttonBack->setOrangeGradientBottomStyle(BORDER_RADIUS2);
    ui->buttonBack->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonBack->objectName()),12);
}

void ControlActionWidget::initArabControlPos()
{
    int language = g_pSettings->readFromInHMIiFile("HMI/language").toInt();
    if(language != arabic)
    {
        return;
    }

    ui->labelMainTitle->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    ui->labelSubTitle->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

    for(int i = 0; i < m_itemLabelList.size(); i++)
    {
        m_itemLabelList[i]->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    }

    ui->labelPage->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
}

void ControlActionWidget::refreshUi(int idx)
{
    // m_itemList 所有的控制动作
    if(idx != 0)
    {
        m_itemList.clear();

        for( int i= 0; i < m_allItemList.size();i++)
        {
            m_itemList.append(m_allItemList[i]);
        }
    }

    int itemNum,pageNums;
    if (m_itemList.size() == 0)
    {
        itemNum = 0;
    }
    else
    {
        itemNum = m_itemList.size();
    }

    pageNums = (itemNum+m_itemPerPage-1) / m_itemPerPage;   // 计算页数

    if (m_curPage > pageNums)                 // 当前页
    {
        m_curPage = pageNums;
    }
    if (m_curPage <= 1)
    {
        m_curPage = 1;
    }

    int itemidx = (m_curPage-1) * m_itemPerPage;

    for (int i = 0; i < m_itemPerPage; i++)
    {
        if (itemidx < itemNum)
        {
            m_itemFrameList.at(i)->show();
            OutCtrlItem item = m_itemList.at(itemidx);
            m_itemLabelList[i]->setText(item.m_name);

            m_itemBtnList[i*m_btnPerRow]->setText(item.m_btn1Str);
            m_itemBtnList[i*m_btnPerRow+1]->setText(item.m_btn2Str);
            m_itemBtnList[i*m_btnPerRow+2]->setText(item.m_btn3Str);
            m_itemBtnList[i*m_btnPerRow+3]->setText(item.m_btn4Str);
            m_itemBtnList[i*m_btnPerRow+4]->setText(item.m_btn5Str);

            m_itemBtnList[i*m_btnPerRow]->setVisible(item.m_btn1Visible);
            m_itemBtnList[i*m_btnPerRow+1]->setVisible(item.m_btn2Visible);
            m_itemBtnList[i*m_btnPerRow+2]->setVisible(item.m_btn3Visible);
            m_itemBtnList[i*m_btnPerRow+3]->setVisible(item.m_btn4Visible);
            m_itemBtnList[i*m_btnPerRow+4]->setVisible(item.m_btn5Visible);

            itemidx++;
        }
        else
        {
            m_itemFrameList.at(i)->hide();
        }
    }

    // 翻页按钮
    if (m_curPage <= 1)
    {
        ui->buttonPgUp->setEnabled(false);
    }
    else
    {
        ui->buttonPgUp->setEnabled(true);
    }
    if (m_curPage >= pageNums)
    {
        ui->buttonPgDn->setEnabled(false);
    }
    else
    {
        ui->buttonPgDn->setEnabled(true);
    }

    if(pageNums == 0)
    {
        m_curPage = 0;
    }

    // 页信息
    QString str = tr("pageNum: ") + QString("%1/%2").arg(m_curPage).arg(pageNums);//页数:
    ui->labelPage->setText(str);
}

//按钮动作
void ControlActionWidget::btnAction(int rowidx, int btnidx, int event)
{
    int nidx = (rowidx - 1) + (m_curPage - 1) * m_itemPerPage;
    int size = m_itemList.size();
    if(size <= 0)
    {
        return;
    }

    if(nidx >= size)
    {
        return;
    }

    OutCtrlItem item = m_itemList[nidx];
    int def = item.m_def;
    int fun = -1;

    switch(btnidx)
    {
    case 1:
        switch(event)
        {
        case BTNCLICK:
            fun = item.m_btn1ClickFun;
            break;
        case BTNPRE:
            fun = item.m_btn1PreFun;
            break;
        case BTNREL:
            fun = item.m_btn1RelFun;
            break;
        }
        break;
    case 2:
        switch(event)
        {
        case BTNCLICK:
            fun = item.m_btn2ClickFun;
            break;
        case BTNPRE:
            fun = item.m_btn2PreFun;
            break;
        case BTNREL:
            fun = item.m_btn2RelFun;
            break;
        }
        break;
    case 3:
        switch(event)
        {
        case BTNCLICK:
            fun = item.m_btn3ClickFun;
            break;
        case BTNPRE:
            fun = item.m_btn3PreFun;
            break;
        case BTNREL:
            fun = item.m_btn3RelFun;
            break;
        }
        break;
    case 4:
        switch(event)
        {
        case BTNCLICK:
            fun = item.m_btn4ClickFun;
            break;
        case BTNPRE:
            fun = item.m_btn4PreFun;
            break;
        case BTNREL:
            fun = item.m_btn4RelFun;
            break;
        }
        break;
    case 5:
        switch(event)
        {
        case BTNCLICK:
            fun = item.m_btn5ClickFun;
            break;
        case BTNPRE:
            fun = item.m_btn5PreFun;
            break;
        case BTNREL:
            fun = item.m_btn5RelFun;
            break;
        }
        break;
    default:
        fun = -1;
        break;
    }
    excuteOrder(def,fun);
}

void ControlActionWidget::slotSpeed(int speed)          // 手动换挡
{
    m_speed = speed;
}

//执行按钮动作
void ControlActionWidget::excuteOrder(int def, int fun)
{
    if(def < 0 || fun < 0)
    {
        return;
    }

    switch(fun)
    {
    case OUTCONTROL_ENABLE:
        g_pMachine->motoServoCtrl(def, UP_OPEN_ON);
        break;
    case OUTCONTROL_DISABLE:
        g_pMachine->motoServoCtrl(def, DOWN_CLOSE_OFF);
        break;
    case OUTCONTROL_MS_ENABLE://平绣主轴
        g_pMachine->embMsServoOn();
        break;
    case OUTCONTROL_MS_DISABLE:
        g_pMachine->embMsServoOff();
        break;
    case OUTCONTROL_TOWEL_ENABLE://毛巾主轴
        g_pMachine->towelMsServoOn();
        break;
    case OUTCONTROL_TOWEL_DISABLE:
        g_pMachine->towelMsServoOff();
        break;
    case OUTCONTROL_TOWELM_ENABLE://毛巾M轴
        g_pMachine->towelMMsServoOn();
        break;
    case OUTCONTROL_TOWELM_DISABLE:
        g_pMachine->towelMMsServoOff();
        break;
    case OUTCONTROL_TOWELRM_ENABLE://毛巾打环轴
        g_pMachine->towelRMMsServoOn();
        break;
    case OUTCONTROL_TOWELRM_DISABLE:
        g_pMachine->towelRMMsServoOff();
        break;
    case OUTCONTROL_COIL_ENABLE:
        g_pMachine->coilMsServoOn();
        break;
    case OUTCONTROL_COIL_DISABLE:
        g_pMachine->coilMsServoOff();
        break;
    case OUTCONTROL_COILM_ENABLE:
        g_pMachine->coilMMsServoOn();
        break;
    case OUTCONTROL_COILM_DISABLE:
        g_pMachine->coilMMsServoOff();
        break;

    case OUTCONTROL_JOGP://电机点动正转
         if(m_speed == 1)                   // 手动换挡
        {
            g_pMachine->motoMove(def, MT_MOVE_DIR_POSI, 1);
            break;
        }
         else if(m_speed == 2)
         {
             g_pMachine->motoMove(def, MT_MOVE_DIR_POSI, 2);
             break;
         }
         else if(m_speed == 3)
         {
             g_pMachine->motoMove(def, MT_MOVE_DIR_POSI, 3);
             break;
         }
         else
         {
             g_pMachine->motoMove(def, MT_MOVE_DIR_POSI, 0);
             break;
         }

    case OUTCONTROL_JOGN://电机点动反转
        if(m_speed == 1)
       {
           g_pMachine->motoMove(def, MT_MOVE_DIR_NEGA, 1);
           break;
       }
        else if(m_speed == 2)
        {
            g_pMachine->motoMove(def, MT_MOVE_DIR_NEGA, 2);
            break;
        }
        else if(m_speed == 3)
        {
            g_pMachine->motoMove(def, MT_MOVE_DIR_NEGA, 3);
            break;
        }
        else
        {
            g_pMachine->motoMove(def, MT_MOVE_DIR_NEGA, 0);
            break;
        }

    case OUTCONTROL_STOP://电机点动停止
        g_pMachine->motoMove(def, MT_MOVE_DIR_STOP, 0);
        break;
    case OUTCONTROL_ZERO://电机归零
        g_pMachine->motoToZero(def);
        if(def == MT_LTMM)//毛巾M轴
        {
            emit siTowelMZero(CSV_02_CODE_1,CSV_MC_TYPE_02);
        }
        else if(def == MT_LRM)//毛巾打环轴
        {
            emit siTowelRZero(CSV_02_CODE_2,CSV_MC_TYPE_02);
        }
        else if(def == MT_LCF)//缠绕压脚电机
        {
            emit siCoilCFZero(CSV_03_CODE_1,CSV_MC_TYPE_03);
        }
        else if(def == MT_LCZ)//缠绕锯齿电机
        {
            emit siCoilCZZero(CSV_03_CODE_2,CSV_MC_TYPE_03);
        }
        break;
    case OUTCONTROL_LEFT://电机位置前
        g_pMachine->outputCtrl(def, UP_OPEN_ON, 0);
        break;
    case OUTCONTROL_MIDDLE://电机位置中
        g_pMachine->outputCtrl(def, DOWN_CLOSE_OFF, 0);
        break;
    case OUTCONTROL_RIGHT://电机位置后
        g_pMachine->outputCtrl(def, OT_ACTION, 0);
        break;
    case OUTCONTROL_UP://气缸上升,输出控制方式的电机正转
        g_pMachine->outputCtrl(def, UP_OPEN_ON, 0);
        break;
    case OUTCONTROL_DOWN://气缸下降,输出控制方式的电机反转
        g_pMachine->outputCtrl(def, DOWN_CLOSE_OFF, 0);
        break;
    case MANUALACTION_RUN://手动动作执行
        g_pMachine->manualAction(def);
        break;
    case OUTCONTROL_OPEN://电磁阀开
        g_pMachine->outputCtrl(def, UP_OPEN_ON, 0);
        break;
    case OUTCONTROL_CLOSE://电磁阀合
        g_pMachine->outputCtrl(def, DOWN_CLOSE_OFF, 0);
        break;
    case OUTCONTROL_FOLLOWPOS://跟随位  缠绕压脚 跟随位
        g_pMachine->outputCtrl(def, UP_OPEN_ON, 2);
        break;
    case OUTCONTROL_CTRL1://缠绕锯齿摆动位1
        g_pMachine->outputCtrl(def, UP_OPEN_ON, 0);
        break;
    case OUTCONTROL_CTRL2://缠绕锯齿摆动位2
        g_pMachine->outputCtrl(def, UP_OPEN_ON, 2);
        break;
    case OUTCONTROL_ZEROPOS://缠绕锯齿零位
        g_pMachine->outputCtrl(def, DOWN_CLOSE_OFF, 0);
        break;
    case OUTCONTROL_TOUP://缠绕锯齿上位
        g_pMachine->outputCtrl(def, DOWN_CLOSE_OFF, 2);
        break;
        //绣花机上的,其他机型目前没用到
    case OUTCONTROL_STARTOIL://开始(如手动加油)
        g_pMachine->manualOil(1);
        break;
    case OUTCONTROL_STOPOIL://停止(如手动加油)
        g_pMachine->manualOil(0);
        break;
    }
}

//设置类型图标
void ControlActionWidget::setTypeLogo(QString tStyle)
{
    ui->buttonTypeLogo->setUnPreBtnLogo(tStyle);
}

//设置主title
void ControlActionWidget::setMainTitle(QString str)
{
    ui->labelMainTitle->setText(str);
}

//设置副title
void ControlActionWidget::setSubTitle(QString str)
{
    ui->labelSubTitle->setText(str);
}

void ControlActionWidget::addItemList(QList<OutCtrlItem> itemList)
{
    m_itemList.append(itemList);
    m_allItemList.append(itemList);
}

//控制按钮被点击
void ControlActionWidget::slotItemBtnClicked()
{
    MyButton *button = (MyButton*) this->sender() ;

    if(button == NULL)
    {
        return;
    }

    //获取所触发按钮的行和按钮索引
    int rowIdx,btnIdx;
    rowIdx = btnIdx = 1;
    for(int i = 0; i < m_itemBtnList.size(); i++)
    {
        if(button == m_itemBtnList[i])
        {
            rowIdx = i / m_btnPerRow + 1;
            btnIdx = i % m_btnPerRow + 1;
            btnAction(rowIdx,btnIdx,BTNCLICK);
            break;
        }
    }
}

//控制按钮按下
void ControlActionWidget::slotItemBtnPressed()
{
    MyButton *button = (MyButton*) this->sender() ;

    if(button == NULL)
    {
        return;
    }

    //获取所触发按钮的行和按钮索引
    int rowIdx,btnIdx;
    rowIdx = btnIdx = 1;
    for(int i = 0; i < m_itemBtnList.size(); i++)
    {
        if(button == m_itemBtnList[i])
        {
            rowIdx = i / m_btnPerRow + 1;
            btnIdx = i % m_btnPerRow + 1;
            btnAction(rowIdx,btnIdx,BTNPRE);
            break;
        }
    }
}

//控制按钮抬起
void ControlActionWidget::slotItemBtnReleased()
{
    MyButton *button = (MyButton*) this->sender() ;

    if(button == NULL)
    {
        return;
    }

    //获取所触发按钮的行和按钮索引
    int rowIdx,btnIdx;
    rowIdx = btnIdx = 1;
    for(int i = 0; i < m_itemBtnList.size(); i++)
    {
        if(button == m_itemBtnList[i])
        {
            rowIdx = i / m_btnPerRow + 1;
            btnIdx = i % m_btnPerRow + 1;
            btnAction(rowIdx,btnIdx,BTNREL);
            break;
        }
    }
}

void ControlActionWidget::on_buttonPgUp_clicked()
{
    m_curPage--;
    refreshUi();
}

void ControlActionWidget::on_buttonPgDn_clicked()
{
    m_curPage++;
    refreshUi();
}

void ControlActionWidget::on_buttonBack_clicked()
{
    this->hide();
}

void ControlActionWidget::mousePressEvent(QMouseEvent *e)
{
    if ( e->button() == Qt::LeftButton)
    {
        m_mousePressX = e->x();
        m_mousePressY = e->y();
        m_mouseReleaseX = 0;
        m_mouseReleaseY = 0;
    }
}

void ControlActionWidget::mouseReleaseEvent(QMouseEvent *e)
{
    if ( e->button() == Qt::LeftButton)
    {
        m_mouseReleaseX = e->x();
        m_mouseReleaseY = e->y();
    }

    int width = this->width();
    int height = this->height();
    if ((m_mousePressX >= 0 && m_mousePressX <= 100)  &&
            ((m_mousePressY >= (height - 100)) && m_mousePressY <= height) &&
            ((m_mouseReleaseX >= (width - 100)) && m_mouseReleaseX <= width) &&
            ((m_mouseReleaseY >= (height - 100)) && m_mouseReleaseY <= height))
    {
        MainWidgetFunction mainFunction;
        QString usbPath;//优盘路径
        usbPath = mainFunction.detectUsb();//优盘检测
        if(usbPath.length() <= 0)
        {
            //优盘不存在
            return;
        }
        else
        {
            QPixmap pixmap=QPixmap::grabWindow(this->winId(),0,0,this->width(),this->height());//截屏存放在pixmap
            QString name_pc = usbPath +  "EMB" + //
                    QDateTime::currentDateTime().toString("yyyyMMdd") +//获取时期时间
                    +"_"+
                    QDateTime::currentDateTime().toString("hhmmss")    + ".png";

            pixmap.save(name_pc,"png"); // 路径
        }
    }

    m_mousePressX = 0;
    m_mousePressY = 0;
    m_mouseReleaseX = 0;
    m_mouseReleaseY = 0;
}
