#include "sheetwidget.h"
#include "ui_sheetwidget.h"

SheetWidget::SheetWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SheetWidget)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);

    initWidget();
    initControl();
    initArabControlPos();
}

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

void SheetWidget::initWidget()
{
    m_btnPerPage = COLUMN_NUM * ROW_NUM;
    m_curPages = 0;
    m_pageNums = 1;
    m_reType = RETYPE_X;
    memset(&m_frameAngleParaValues, 0, sizeof(EmbMvAng));

    m_sheetBtnList.clear();
    m_sheetHLabelList.clear();
    m_sheetVLabelList.clear();
    m_itemXList.clear();
    m_itemYList.clear();

#if(0)
    for(int i = 0; i < MAX_VER; i++)
    {
        for(int j = 0; j < MAX_HOR; j++)
        {
            m_itemXList.append(0);
            m_itemYList.append(1);
        }
    }
#endif

    //动框角度调节
    for(int i = 0; i < ROW_NUM; i++)
    {
        for(int j = 0; j < COLUMN_NUM; j++)
        {
            QPushButton *buttonSheet = new QPushButton(ui->frameSheet);
            m_sheetBtnList.append(buttonSheet);
            connect(buttonSheet,
                    SIGNAL(clicked()),
                    this,
                    SLOT(slotSheetBtnClicked())
                    );
        }
    }

    for(int i = 0; i < COLUMN_NUM; i++)
    {
        QLabel *labelH = new QLabel(this);
        m_sheetHLabelList.append(labelH);
    }

    for(int i = 0; i < ROW_NUM; i++)
    {
        QLabel *labelV = new QLabel(this);
        m_sheetVLabelList.append(labelV);
    }
}

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

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

void SheetWidget::initResolution1910()
{
    this->resize(1920,1080);
    ui->buttonTypeLogo->setGeometry(128,66,78,78);
    ui->labelMainTitle->setGeometry(226,60,1000,44);
    ui->labelSubTitle->setGeometry(302,112,1000,36);

    ui->frameBack->setGeometry(0,0,1920,1080);
    ui->frameSheet->setGeometry(176,244,1680,880);
    ui->labelXY->setGeometry(84,190,56,48);

    ui->frameRightUp->setGeometry(1468,92,406,72);
    ui->buttonParaRestore->setGeometry(340,0,66,66);
    ui->buttonSwitchXY->setGeometry(252,0,66,66);

    for(int i = 0; i < ROW_NUM; i++)
    {
        for(int j = 0; j < COLUMN_NUM; j++)
        {
            m_sheetBtnList[i*COLUMN_NUM+j]->setGeometry(3+j*140,3+i*88,140,88);
        }
    }

    for(int i = 0; i < COLUMN_NUM; i++)
    {
        m_sheetHLabelList[i]->setGeometry(206+i*140,160,140,72);
    }

    for(int i = 0; i < ROW_NUM; i++)
    {
        m_sheetVLabelList[i]->setGeometry(36,245+i*88,140,88);
    }

    ui->labelPageNum->setGeometry(32,1008,400,40);
    ui->framePageBtn->setGeometry(920,990,1000,70);
    ui->buttonPgUp->setGeometry(200,0,168,70);
    ui->buttonPgDn->setGeometry(400,0,168,70);
    ui->buttonOk->setGeometry(600,0,168,70);
    ui->buttonCancel->setGeometry(800,0,168,70);
}

void SheetWidget::initResolution1006()
{
    this->resize(1024,600);
    ui->buttonTypeLogo->setGeometry(25,30,60,60);
    ui->labelMainTitle->setGeometry(100,30,200,24);
    ui->labelSubTitle->setGeometry(130,64,240,21);

    ui->frameBack->setGeometry(0,0,1024,600);
    ui->frameSheet->setGeometry(102,138,994,484);
    ui->labelXY->setGeometry(44,100,56,48);

    ui->frameRightUp->setGeometry(800,40,800,61);
    ui->buttonParaRestore->setGeometry(150,0,50,50);
    ui->buttonSwitchXY->setGeometry(93,0,50,50);

    for(int i = 0; i < ROW_NUM; i++)
    {
        for(int j = 0; j < COLUMN_NUM; j++)
        {
            m_sheetBtnList[i*COLUMN_NUM+j]->setGeometry(3+j*72,3+i*48,72,48);
        }
    }

    for(int i = 0; i < COLUMN_NUM; i++)
    {
        m_sheetHLabelList[i]->setGeometry(120+i*72,94,72,40);
    }

    for(int i = 0; i < ROW_NUM; i++)
    {
        m_sheetVLabelList[i]->setGeometry(30,139+i*48,72,48);
    }

    ui->labelPageNum->setGeometry(20,550,102,45);
    ui->framePageBtn->setGeometry(489,545,600,40);
    ui->buttonPgUp->setGeometry(106,0,96,40);
    ui->buttonPgDn->setGeometry(212,0,96,40);
    ui->buttonOk->setGeometry(318,0,96,40);
    ui->buttonCancel->setGeometry(424,0,96,40);
}

void SheetWidget::initControlStyle()
{
    SetControlStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    QString frameBackImgPath = setControlStyle.getStyleSheet(this->objectName());

    ui->frameBack->setStyleSheet(frameBackImgPath);
    ui->labelXY->setFont(fontNormal_1);
    ui->labelXY->setStyleSheet(LABELWHITESTYLE);
    ui->labelMainTitle->setFont(fontBold_1);
    ui->labelMainTitle->setStyleSheet(LABELWHITESTYLE);
    ui->labelSubTitle->setFont(fontNormal_1);
    ui->labelSubTitle->setStyleSheet(LABELWHITESTYLE);

    ui->frameRightUp->setStyleSheet(TRANSPARENTSTYLE);

    ui->buttonParaRestore->setGreenGradientBottomStyle(BORDER_RADIUS1);
    ui->buttonParaRestore->setTopImage(setControlStyle.getTopStyleSheet(ui->buttonParaRestore->objectName()));

    ui->buttonSwitchXY->setGreenGradientBottomStyle(BORDER_RADIUS1);
    ui->buttonSwitchXY->setTopImage(setControlStyle.getTopStyleSheet(ui->buttonSwitchXY->objectName()));

    for(int i = 0; i < COLUMN_NUM; i++)
    {
        m_sheetHLabelList[i]->setText(QString::number(i+1)+"mm");
        m_sheetHLabelList[i]->setFont(fontNormal_1);
        m_sheetHLabelList[i]->setStyleSheet(LABELWHITESTYLE);
        m_sheetHLabelList[i]->setAlignment(Qt::AlignCenter);
        m_sheetHLabelList[i]->setAlignment(Qt::AlignBottom);
    }

    for(int i = 0; i < ROW_NUM; i++)
    {
        m_sheetVLabelList[i]->setText(QString::number((i+1)*100)+"rpm");
        m_sheetVLabelList[i]->setFont(fontNormal_1);
        m_sheetVLabelList[i]->setStyleSheet(LABELWHITESTYLE);
        m_sheetVLabelList[i]->setAlignment(Qt::AlignRight);
        m_sheetVLabelList[i]->setAlignment(Qt::AlignCenter);
    }

    ui->frameSheet->setStyleSheet(TRANSPARENTSTYLE);
    for(int i = 0; i < ROW_NUM; i++)
    {
        for(int j = 0; j < COLUMN_NUM; j++)
        {
            m_sheetBtnList[i*COLUMN_NUM+j]->setStyleSheet(BUTTONSHEETSTYLE);
            m_sheetBtnList[i*COLUMN_NUM+j]->setFont(fontNormal_1);
        }
    }

    ui->framePageBtn->setStyleSheet(TRANSPARENTSTYLE);
    ui->labelPageNum->setFont(fontNormal_1);
    ui->labelPageNum->setStyleSheet(LABELWHITESTYLE);

    //按钮样式表
    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->buttonOk->setOrangeGradientBottomStyle(BORDER_RADIUS2);
    ui->buttonOk->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonOk->objectName()),12);

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

void SheetWidget::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_sheetVLabelList.size(); i++)
    {
        m_sheetVLabelList[i]->setAlignment(Qt::AlignLeft | Qt::AlignCenter);
    }

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

//刷新参数数值
void SheetWidget::refreshValues()
{
    m_itemXList.clear();
    m_itemYList.clear();

    for(int i = 0; i < MAX_VER; i++)
    {
        for(int j = 0; j < MAX_HOR; j++)
        {
            u16 valueX = m_frameAngleParaValues.xMvStAng[i][j];
            u16 valueY = m_frameAngleParaValues.yMvStAng[i][j];

            m_itemXList.append(valueX);
            m_itemYList.append(valueY);
        }
    }
}

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

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

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

void SheetWidget::setMinMax()
{
    m_minValue = 0;
    m_maxValue = 0;

    if (g_emMacType == QUIMACHINE_EMB )               //绗绣机
    {
        //动框角度的范围 = 主轴零位角度(100°+5°) ~ ((360°+主轴零位角度(100°)) - 动框持续角度(200°)
        ParaStruct mcParaValues;
        mcParaValues = g_pMachine->getMcPara();
        m_minValue = mcParaValues.buf[74]/100;//主轴停车角度

        ParaStruct wkParaValues;
        wkParaValues = g_pMachine->getWkPara();
        m_maxValue = 360+mcParaValues.buf[74]/100-wkParaValues.buf[15]/100;
    }
    else    //绣花机
    {
        m_minValue = 100;
        m_maxValue = 360;
    }
}

//刷新界面显示
void SheetWidget::refreshPageUi()
{
    QList<u16> itemList;
    if(m_reType == RETYPE_X)
    {
        itemList = m_itemXList;
    }
    else
    {
        itemList = m_itemYList;
    }

    ui->buttonSwitchXY->setEnabled(true);
    ui->buttonParaRestore->setEnabled(true);
    ui->buttonOk->setEnabled(true);

    int itemNum;
    if (itemList.size() <= 0)
    {
        itemNum = 0;

        for (int i = 0; i < m_btnPerPage; i++)
        {
            m_sheetBtnList.at(i)->hide();
        }
        ui->labelPageNum->setText(tr("PageNum: 0/0"));//页数: 0/0
        ui->buttonPgUp->setEnabled(false);
        ui->buttonPgDn->setEnabled(false);
        ui->buttonSwitchXY->setEnabled(false);
        ui->buttonParaRestore->setEnabled(false);
        ui->buttonOk->setEnabled(false);
        return;
    }
    else
    {
        itemNum = itemList.size();
    }

    m_pageNums = (itemNum + m_btnPerPage - 1)/m_btnPerPage;// 计算页数
    if (m_curPages > m_pageNums)// 当前页
    {
        m_curPages = m_pageNums;
    }
    if (m_curPages <= 1)
    {
        m_curPages = 1;
    }

    int itemidx = (m_curPages - 1) * m_btnPerPage;

    for (int i = 0; i < m_btnPerPage; i++)
    {
        if (itemidx < itemNum)
        {
            m_sheetBtnList.at(i)->setText(QString::number(itemList[itemidx]));
            m_sheetBtnList.at(i)->show();

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

    for (int i = 0; i < ROW_NUM; i++)
    {
        int labelIdx = (m_curPages - 1) * ROW_NUM + i;
        if(labelIdx < MAX_VER)
        {
            m_sheetVLabelList[i]->show();
            m_sheetVLabelList[i]->setText(QString::number((ROW_NUM*(m_curPages-1)+i+1)*100)+"rpm");
        }
        else
        {
            m_sheetVLabelList[i]->hide();
        }
    }

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

    // 页信息
    if(m_pageNums == 0)
    {
        m_curPages = 0;
    }

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

void SheetWidget::show()
{
    //等于机器参数或工作参数
    if (g_pMachine != NULL)
    {
        if(g_pMachine->isConnected() == 3)
        {
            g_pMachine->getParasFromMachine(EMB_MVSTANG_PARA);
        }
        else
        {
#ifdef Q_OS_LINUX//为了方便windows下查看参数所以加了linux编译条件
            PromptDialog dlg(this);
            dlg.initDialog(PromptDialog::BTN_OK);
            dlg.setTitleStr(tr("Network error"));
            dlg.setContentStr(tr("No connection"));
            dlg.exec();
            return;
#endif
        }
    }

    refreshPageUi();
    QWidget::show();
}

//表格中按钮被点击
void SheetWidget::slotSheetBtnClicked()
{
    MyButton *button = (MyButton*) this->sender() ;

    if(button == NULL)
    {
        return;
    }
    u16 value = button->text().toInt();

    NumerInputDialog ndlg(this);
    ndlg.setTitleStr(tr("Frame angle"));
    ndlg.setUnitStr(tr("deg"));
    ndlg.setValueAndRange(value, m_minValue, m_maxValue, 0, 0, 240);

    if (ndlg.exec() == 1)
    {
        u16 val = ndlg.getValue();
        if (val != value)
        {
            button->setText(QString::number(val));
            for(int i = 0; i < m_sheetBtnList.size(); i++)
            {
                if(button == m_sheetBtnList[i])
                {
                    if(m_reType == RETYPE_X)
                    {
                        m_itemXList[(m_curPages-1)*m_btnPerPage+i] = val;
                    }
                    else if(m_reType == RETYPE_Y)
                    {
                        m_itemYList[(m_curPages-1)*m_btnPerPage+i] = val;
                    }
                    break;
                }
            }
        }
    }
}

void SheetWidget::on_buttonPgUp_clicked()
{
    m_curPages--;
    refreshPageUi();
}

void SheetWidget::on_buttonPgDn_clicked()
{
    m_curPages++;
    refreshPageUi();
}

void SheetWidget::on_buttonOk_clicked()
{
    for(int i = 0; i < MAX_VER; i++)
    {
        for(int j = 0; j < MAX_HOR; j++)
        {
            m_frameAngleParaValues.xMvStAng[i][j] = m_itemXList[i*MAX_HOR+j];
            m_frameAngleParaValues.yMvStAng[i][j] = m_itemYList[i*MAX_HOR+j];
        }
    }

    if(g_pMachine != NULL)
    {
        g_pMachine->setFrameAnglePara(&m_frameAngleParaValues);
    }

    this->hide();
}

void SheetWidget::on_buttonCancel_clicked()
{
    this->hide();
}

//切换xy
void SheetWidget::on_buttonSwitchXY_clicked()
{
    if(m_reType == RETYPE_X)
    {
        m_reType = RETYPE_Y;
        ui->labelXY->setText("Y");
    }
    else
    {
        m_reType = RETYPE_X;
        ui->labelXY->setText("X");
    }
    m_curPages = 1;
    refreshPageUi();
}

//还原回默认参数
void SheetWidget::on_buttonParaRestore_clicked()
{
    PromptDialog promptDlg(this);
    promptDlg.initDialog(PromptDialog::BTN_OK_CANCEL);
    promptDlg.setTitleStr(tr("Prompt"));
    promptDlg.setContentStr(tr("Whether to recover the default parameters for the frame angle?"));//是否恢复为动框角度默认参数?

    if(promptDlg.exec() == 1)
    {
        if(g_pMachine != NULL)
        {
            g_pMachine->setToDefaultParas(UCMD_SET_MVANGDEF_PARA, 0);
        }
    }
}

//动框角度参数改变
void SheetWidget::slotFrameAngleParaChange(int type, int id)
{
    if (type == EMB_MVSTANG_PARA)
    {
        if (id == 0)
        {
            memcpy(&m_frameAngleParaValues, &g_pMachine->getFrameAnglePara(), sizeof(EmbMvAng));
        }
    }

    refreshValues();
    refreshPageUi();
}
