#include "needlebarsetwidget.h"
#include "ui_needlebarsetwidget.h"

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

    m_mousePressX = 0;
    m_mousePressY = 0;
    m_mouseReleaseX = 0;
    m_mouseReleaseY = 0;
    initWidget();
    initControl();
    initArabControlPos();
}

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

void NeedleBarSetWidget::initWidget()
{
    m_setType = 1;
    QDir apppath(qApp->applicationDirPath());

    //配置文件路径
    m_configFile = apppath.path() + apppath.separator() + "config.ini";

    m_pColorBuf = (QRgb *)(g_pSettings->getColorRgbArray().data());
    m_buttonStyle1 = "QPushButton{outline: none;border:0px solid rgb(0,0,0);border-style:inset;border-radius:5px;";
    m_buttonStyle2 = "QPushButton:checked{outline: none;border:3px groove rgb(80,80,80);}";

    m_embNeedleBtnList.clear();
    int embNeedleItem = 32;
    for(int i = 0; i < embNeedleItem; i++)
    {
        MyButton *buttonEmbNeedle = new MyButton(ui->frameEmb);
        m_embNeedleBtnList.append(buttonEmbNeedle);
        connect(buttonEmbNeedle,
                SIGNAL(clicked()),
                this,
                SLOT(embNeedleBtnClick())
                );
    }

    int colorNumer = 448;                      // 原为288,lxs20220803客户有一个花样需要448色序,进行更改
    m_colorBtnList.clear();
    for(int i = 0; i < colorNumer; i++)
    {
        QPushButton *buttonColor = new QPushButton(ui->frameColor);
        m_colorBtnList.append(buttonColor);
        connect(buttonColor,
                SIGNAL(clicked()),
                this,
                SLOT(colorBtnClick())
                );
    }
}

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

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

void NeedleBarSetWidget::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,44);
    ui->labelSubTitle->setGeometry(302,112,1000,36);

    ui->frameRightUp->setGeometry(1366,92,582,72);
    ui->buttonEmbSet->setGeometry(264,0,66,66);
    ui->buttonSequinSet->setGeometry(176,0,66,66);
    ui->buttonBeadSet->setGeometry(88,0,66,66);
    ui->buttonTowelSet->setGeometry(0,0,66,66);

    ui->frameEmb->setGeometry(62,217,336,720);
    ui->labelEmbNeedleNum->setGeometry(10,0,228,48);
    ui->buttonEmbNeedleNum->setGeometry(240,0,94,48);

    for(int i = 0; i < 3; i++)
    {
        int num = 11;
        for(int j = 0; j < num; j++)
        {
            // 颜色显示
            QRgb rgb = m_pColorBuf[((i*num+j) % EMB_MC_SW_ND)];
            rgb |= 0xff000000;
            QColor bkcolor(rgb);            
            QColor tcolor = QColor(255-bkcolor.red(), 255-bkcolor.green(), 255-bkcolor.blue()).lighter(168);
            QString cstyle = "outline: none;background-color:" + bkcolor.name() + ";"
                    + " color:" + tcolor.name() + ";";

            if(i*num+j < m_embNeedleBtnList.size())
            {
                m_embNeedleBtnList[i*num+j]->setGeometry(10+i*115,69+j*59,94,48);
                m_embNeedleBtnList[i*num+j]->setStyleSheet(m_buttonStyle1 + cstyle + "}" + m_buttonStyle2);
                m_embNeedleBtnList[i*num+j]->setFont(fontNormal_1);
                m_embNeedleBtnList[i*num+j]->setText(QString::number(i*num+j+1));
            }
        }
    }
//右侧
    ui->frameColor->setGeometry(422,218,1426,708);
    int hColorNum = 24;
    int vColorNum = 12;
    for(int i = 0; i < vColorNum; i++)
    {
        for(int j = 0; j < hColorNum; j++)
        {
            // 颜色显示
            QRgb rgb = m_pColorBuf[((i*hColorNum+j) % EMB_MC_SW_ND)];
            rgb |= 0xff000000;
            QColor bkcolor(rgb);
            QString cstyle = "outline: none;background-color:" + bkcolor.name() + ";";
            m_colorBtnList[i*hColorNum+j]->setGeometry(7+j*59,3+i*59,50,50);
            m_colorBtnList[i*hColorNum+j]->setStyleSheet(m_buttonStyle1 + cstyle + "}" + m_buttonStyle2);
        }
    }

    ui->framePageBtn->setGeometry(1120,990,800,70);
    ui->buttonOk->setGeometry(600,0,168,70);
}

void NeedleBarSetWidget::initResolution1006()
{
    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);

    ui->frameRightUp->setGeometry(663,40,338,61);
    ui->buttonEmbSet->setGeometry(171,0,50,50);
    ui->buttonSequinSet->setGeometry(114,0,50,50);
    ui->buttonBeadSet->setGeometry(57,0,50,50);
    ui->buttonTowelSet->setGeometry(0,0,50,50);

    ui->frameEmb->setGeometry(30,90,221,441);
    ui->labelEmbNeedleNum->setGeometry(6,30,110,20);
    ui->buttonEmbNeedleNum->setGeometry(125,30,55,28);

    for(int i = 0; i < 3; i++)
    {
        int num = 11;
        for(int j = 0; j < num; j++)
        {
            // 颜色显示
            QRgb rgb = m_pColorBuf[((i*num+j) % EMB_MC_SW_ND)];
            rgb |= 0xff000000;
            QColor bkcolor(rgb);
            QColor tcolor = QColor(255-bkcolor.red(), 255-bkcolor.green(), 255-bkcolor.blue()).lighter(168);
            QString cstyle = "outline: none;background-color:" + bkcolor.name() + ";"
                    + " color:" + tcolor.name() + ";";

            if(i*num+j < m_embNeedleBtnList.size())
            {
                m_embNeedleBtnList[i*num+j]->setGeometry(5+i*63,62+j*34,54,29);//左侧颜色
                m_embNeedleBtnList[i*num+j]->setStyleSheet(m_buttonStyle1 + cstyle + "}" + m_buttonStyle2);
                m_embNeedleBtnList[i*num+j]->setFont(fontNormal_1);
                m_embNeedleBtnList[i*num+j]->setText(QString::number(i*num+j+1));
            }
        }
    }

    ui->frameColor->setGeometry(236,125,735,420);
    int hColorNum = 24;
    int vColorNum = 12;
    for(int i = 0; i < vColorNum; i++)
    {
        for(int j = 0; j < hColorNum; j++)
        {
            // 颜色显示
            QRgb rgb = m_pColorBuf[((i*hColorNum+j) % EMB_MC_SW_ND)];
            rgb |= 0xff000000;
            QColor bkcolor(rgb);
            QString cstyle = "outline: none;background-color:" + bkcolor.name() + ";";
            m_colorBtnList[i*hColorNum+j]->setGeometry(3+j*32,3+i*32,26,26);
            m_colorBtnList[i*hColorNum+j]->setStyleSheet(m_buttonStyle1 + cstyle + "}" + m_buttonStyle2);
        }
    }

    ui->framePageBtn->setGeometry(595,548,600,45);
    ui->buttonOk->setGeometry(318,0,96,40);
}

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

    QString frameBackImgPath = setControlStyle.getStyleSheet(this->objectName());
    ui->frameBack->setStyleSheet(frameBackImgPath);
    ui->frameEmb->setStyleSheet(TRANSPARENTSTYLE);
    ui->frameColor->setStyleSheet(TRANSPARENTSTYLE);
    ui->labelMainTitle->setFont(fontBold_1);
    ui->labelMainTitle->setStyleSheet(LABELWHITESTYLE);
    ui->labelSubTitle->setFont(fontNormal_1);
    ui->labelSubTitle->setStyleSheet(LABELWHITESTYLE);

    //右上方
    ui->frameRightUp->setStyleSheet(TRANSPARENTSTYLE);
    ui->buttonEmbSet->setGreenGradientBottomStyle(BORDER_RADIUS1);
    ui->buttonEmbSet->setTopImage(setControlStyle.getTopStyleSheet(ui->buttonEmbSet->objectName()));
    ui->buttonSequinSet->setGreenGradientBottomStyle(BORDER_RADIUS1);
    ui->buttonSequinSet->setTopImage(setControlStyle.getTopStyleSheet(ui->buttonSequinSet->objectName()));
    ui->buttonBeadSet->setGreenGradientBottomStyle(BORDER_RADIUS1);
    ui->buttonBeadSet->setTopImage(setControlStyle.getTopStyleSheet(ui->buttonBeadSet->objectName()));
    ui->buttonTowelSet->setGreenGradientBottomStyle(BORDER_RADIUS1);
    ui->buttonTowelSet->setTopImage(setControlStyle.getTopStyleSheet(ui->buttonTowelSet->objectName()));

    ui->labelEmbNeedleNum->setFont(fontNormal_1);
    ui->labelEmbNeedleNum->setStyleSheet(LABELWHITESTYLE);
    ui->buttonEmbNeedleNum->setFont(fontNormal_1);

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

void NeedleBarSetWidget::embNeedleBtnClick()
{
    m_pEmbNeedleBtn = (QPushButton*) this->sender();

    if(m_pEmbNeedleBtn == NULL)
    {
        return;
    }
    for(int i = 0; i < m_embNeedleBtnList.size(); i++)
    {
        m_embNeedleBtnList.at(i)->setChecked(false);
        m_embNeedleBtnList.at(i)->setCheckable(false);
    }
    m_pEmbNeedleBtn->setCheckable(true);
    m_pEmbNeedleBtn->setChecked(true);
}

void NeedleBarSetWidget::colorBtnClick()
{
    m_pColorBtn = (QPushButton*) this->sender();

    if(m_pColorBtn == NULL || m_pEmbNeedleBtn == NULL)
    {
        return;
    }
    int idx = 0;
    for(int i = 0; i < m_colorBtnList.size(); i++)
    {
        if(m_pColorBtn == m_colorBtnList.at(i))
        {
           idx = i;
        }
        m_colorBtnList.at(i)->setChecked(false);
        m_colorBtnList.at(i)->setCheckable(false);
    }
    m_pColorBtn->setCheckable(true);
    m_pColorBtn->setChecked(true);

    QRgb rgb = m_pColorBuf[(idx % EMB_BF_SW_ND)];
    rgb |= 0xff000000;
    QColor bkcolor(rgb);
    QColor tcolor = QColor(255-bkcolor.red(), 255-bkcolor.green(), 255-bkcolor.blue()).lighter(168);
    QString cstyle = "outline: none;background-color:" + bkcolor.name() + ";"
            + " color:" + tcolor.name() + ";";

    m_pEmbNeedleBtn->setStyleSheet(m_buttonStyle1 + cstyle + "}" + m_buttonStyle2);

    //写入配置
    for(int i = 0; i < m_embNeedleBtnList.size(); i++)
    {
        if(m_pEmbNeedleBtn == m_embNeedleBtnList.at(i))
        {
            if(m_setType == SET_TYPE_NEEDLE_EMB)
            {
                g_pSettings->writeToIniFile("EmbNeedle/"+QString::number(i+1)+"_coloridx",QString::number(idx));
                g_pCurEmbData->setNeedleColorTable(i,idx);
            }
            else if(m_setType == SET_TYPE_SEQUIN_COLOR)
            {
                g_pSettings->writeToIniFile("SequinColor/"+QString::number(i+1)+"_coloridx",QString::number(idx));
                g_pCurEmbData->setSequinColorTable(i,idx);
            }
            else if(m_setType == SET_TYPE_BEAD_COLOR)
            {
                g_pSettings->writeToIniFile("BeadColor/"+QString::number(i+1)+"_coloridx",QString::number(idx));
                g_pCurEmbData->setBeadColorTable(i,idx);
            }
            else if(m_setType == SET_TYPE_BEAD_TOWEL)
            {
                //点击颜色表，就把哪个针杆选的颜色写入了配置文件
                //毛巾和链式用的一样的
                g_pSettings->writeToIniFile("TowelNeedle/"+QString::number(i+1)+"_coloridx",QString::number(idx));
                g_pCurEmbData->setNeedleColorTable(i,idx);//idx是颜色值
                //g_pCurEmbData->setNeedleColorTable(i,idx);//idx是颜色值
            }         
            //g_pCurEmbData->setNeedleColorTable(i,idx);

            break;
        }
    }
}

void NeedleBarSetWidget::setTypeLogo(QString tStyle)
{
    ui->buttonTypeLogo->setUnPreBtnLogo(tStyle);
}

void NeedleBarSetWidget::setMainTitle(QString str)
{
    ui->labelMainTitle->setText(str);
}

void NeedleBarSetWidget::setSubTitle(QString str)
{
    ui->labelSubTitle->setText(str);
}

void NeedleBarSetWidget::initNeedleBar()
{
    //标题显示
    if(m_setType == SET_TYPE_NEEDLE_EMB)
    {
        ui->labelEmbNeedleNum->setText(tr("EmbNeedleNum:"));//绣花针杆个数:
    }
    else if(m_setType == SET_TYPE_BEAD_TOWEL)
    {
        ui->labelEmbNeedleNum->setText(tr("Chenille Num:"));//毛巾针杆个数:
    }
    else if(m_setType == SET_TYPE_SEQUIN_COLOR)
    {
        ui->labelEmbNeedleNum->setText(tr("SequinNum:"));//亮片个数:
    }
    else if(m_setType == SET_TYPE_BEAD_COLOR)
    {
        ui->labelEmbNeedleNum->setText(tr("BeadNum:"));//散珠个数:
    }

    //读入配置
    QFile iniFile(m_configFile);

    int num,idx;
    num = idx = 0;
    if(!iniFile.exists())
    {
        num = 1;
        idx = 0;

        QRgb rgb = m_pColorBuf[(idx % EMB_BF_SW_ND)];
        rgb |= 0xff000000;
        QColor bkcolor(rgb);
        QColor tcolor = QColor(255-bkcolor.red(), 255-bkcolor.green(), 255-bkcolor.blue()).lighter(168);
        QString cstyle = "outline: none;background-color:" + bkcolor.name() + ";"
                         + " color:" + tcolor.name() + ";";

        for(int i = 0; i < m_embNeedleBtnList.size(); i++)
        {
            if(i < num)
            {
                m_embNeedleBtnList.at(i)->setStyleSheet(m_buttonStyle1 + cstyle + "}" + m_buttonStyle2);
                m_embNeedleBtnList.at(i)->show();
            }
            else
            {
                m_embNeedleBtnList.at(i)->hide();
            }
        }
    }
    else
    {
        QRgb rgb = qRgb(0,255,0);

        if(m_setType == SET_TYPE_NEEDLE_EMB)
        {
            num = g_pSettings->readFromIniFile("EmbNeedle/number").toInt();
        }
        else if(m_setType == SET_TYPE_SEQUIN_COLOR)
        {
            num = g_pSettings->readFromIniFile("SequinColor/number").toInt();
        }
        else if(m_setType == SET_TYPE_BEAD_COLOR)
        {
            num = g_pSettings->readFromIniFile("BeadColor/number").toInt();
        }
        else if(m_setType == SET_TYPE_BEAD_TOWEL)
        {
            //显示的针杆数
            num = g_pSettings->readFromIniFile("TowelNeedle/number").toInt();
        }

        for(int i = 0; i < m_embNeedleBtnList.size(); i++)//32个针杆
        {
            QString idxStr;
            if(m_setType == SET_TYPE_NEEDLE_EMB)
            {
                idxStr = "EmbNeedle/"+QString::number(i+1)+"_coloridx";
            }
            else if(m_setType == SET_TYPE_SEQUIN_COLOR)
            {
                idxStr = "SequinColor/"+QString::number(i+1)+"_coloridx";
            }
            else if(m_setType == SET_TYPE_BEAD_COLOR)
            {
                idxStr = "BeadColor/"+QString::number(i+1)+"_coloridx";
            }
            else if(m_setType == SET_TYPE_BEAD_TOWEL)
            {
                //如果是毛巾的类型，第几个针杆
                idxStr = "TowelNeedle/"+QString::number(i+1)+"_coloridx";
            }

            if(g_pSettings->ifKeyExists(idxStr) == true)
            {
                idx = g_pSettings->readFromIniFile(idxStr).toInt();//第几个针杆的颜色值
                rgb = m_pColorBuf[(idx % EMB_BF_SW_ND)];
            }
            else
            {
                rgb = m_pColorBuf[(i % EMB_MC_SW_ND)];
            }
            rgb |= 0xff000000;
            QColor bkcolor(rgb);
            QColor tcolor = QColor(255-bkcolor.red(), 255-bkcolor.green(), 255-bkcolor.blue()).lighter(168);
            QString cstyle = "outline: none;background-color:" + bkcolor.name() + ";"
                             + " color:" + tcolor.name() + ";";
            if(i < num)
            {
                m_embNeedleBtnList.at(i)->setStyleSheet(m_buttonStyle1 + cstyle + "}" + m_buttonStyle2);
                m_embNeedleBtnList.at(i)->show();
            }
            else
            {
                m_embNeedleBtnList.at(i)->hide();
            }
        }
    }
    ui->buttonEmbNeedleNum->setText(QString::number(num));
}

//刷新右上方按钮的显示,包括选中状态
void NeedleBarSetWidget::refreshTypeBtnShow()
{
    ui->buttonBeadSet->setChecked(false);//不是选中状态，可见
    ui->buttonBeadSet->setVisible(true);
    ui->buttonSequinSet->setChecked(false);
    ui->buttonSequinSet->setVisible(true);
    ui->buttonEmbSet->setChecked(false);
    ui->buttonEmbSet->setVisible(true);
    ui->buttonTowelSet->setVisible(true);
    if(g_emResolut == resolution1910)
    {
        ui->buttonSequinSet->setGeometry(176,0,66,66);
        ui->buttonBeadSet->setGeometry(88,0,66,66);
        ui->buttonTowelSet->setGeometry(0,0,66,66);
    }
    else if(g_emResolut == resolution1006)
    {
        ui->buttonSequinSet->setGeometry(114,0,50,50);
        ui->buttonBeadSet->setGeometry(57,0,50,50);
        ui->buttonTowelSet->setGeometry(0,0,50,50);
    }

    if(g_emMacType == MACHINE_EMB||
            g_emMacType == QUIMACHINE_EMB) //等于绣花机或绗绣机
    {
        m_setType = SET_TYPE_NEEDLE_EMB;
        ui->labelEmbNeedleNum->setText(tr("EmbNeedleNum:"));//绣花针杆个数:
        //ui->buttonEmbSet->move(ui->buttonSewSet->x()+g_mainWidgetPos.x(),ui->buttonSewSet->y()+g_mainWidgetPos.y());//绣花的图标移动到这个位置
        //ui->buttonSequinSet->move(ui->buttonPunchSet->x()+g_mainWidgetPos.x(),ui->buttonPunchSet->y()+g_mainWidgetPos.y());
        //ui->buttonBeadSet->move(ui->buttonEmbSet->x()+g_mainWidgetPos.x(),ui->buttonEmbSet->y()+g_mainWidgetPos.y());
        //ui->buttonTowelSet->move(ui->buttonSequinSet->x()+g_mainWidgetPos.x(),ui->buttonSequinSet->y()+g_mainWidgetPos.y());
        //ui->buttonEmbSet->move(ui->buttonSewSet->x()+g_mainWidgetPos.x(),ui->buttonSewSet->y()+g_mainWidgetPos.y());//绣花的图标移动到这个位置
        int bead = g_pSettings->readFromInHMIiFile("HMI/bead").toInt();//是否有散珠功能
        int sequin = g_pSettings->readFromInHMIiFile("HMI/sequin").toInt();//是否有亮片功能
        int towel = g_pSettings->readFromInHMIiFile("HMI/towel").toInt();//是否有毛巾功能
        if(sequin != 1)//带亮片
        {
            ui->buttonSequinSet->setVisible(false);//亮片的不可见
        }
        if(bead != 1 )//带散珠
        {
            ui->buttonBeadSet->setVisible(false);//散珠的不可见
        }
        if(towel !=1)//带毛巾
        {
            ui->buttonTowelSet->setVisible(false);//毛巾的不可见
        }
        if(sequin != 1 && towel !=1&& bead != 1)//只是纯平绣
        {
            ui->buttonEmbSet->setVisible(false);
        }

//        if(g_emProductType == PRODUCT_EMB_EMB  || PRODUCT_EMB_FEED)//纯平绣
//        {
//            ui->buttonBeadSet->setVisible(false);//右上角散珠亮片等按钮不显示
//            ui->buttonSequinSet->setVisible(false);
//            ui->buttonEmbSet->setVisible(false);
//            ui->buttonTowelSet->setVisible(false);
//        }

        ui->buttonEmbSet->setCheckable(true);//设置绣花为默认选中
        ui->buttonEmbSet->setChecked(true);
    }

    if(g_emMacType == MACHINE_EMB && g_emProductType == PRODUCT_CHEN)
    {
        m_setType = SET_TYPE_BEAD_TOWEL;
        ui->labelEmbNeedleNum->setText(tr("Chenille Num:"));//毛巾针杆个数:
        ui->buttonSequinSet->setVisible(false);//亮片的不可见
        ui->buttonBeadSet->setVisible(false);//散珠的不可见
        ui->buttonEmbSet->setVisible(false);

        ui->buttonTowelSet->setCheckable(true);//设置绣花为默认选中
        ui->buttonTowelSet->setChecked(true);
    }


}

//刷新针杆数量及颜色
void NeedleBarSetWidget::refreshNeedleColorShow()
{
    ui->buttonBeadSet->setChecked(false);
    ui->buttonSequinSet->setChecked(false);
    ui->buttonEmbSet->setChecked(false);
    ui->buttonTowelSet->setChecked(false);
    initNeedleBar();
}

void NeedleBarSetWidget::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);

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

void NeedleBarSetWidget::on_buttonEmbNeedleNum_clicked()
{
    NumerInputDialog numberInputDlg;
    numberInputDlg.setTitleStr(tr("Number set"));//个数设置
    numberInputDlg.setUnitStr("");

    if(m_setType == SET_TYPE_NEEDLE_EMB)
    {
        numberInputDlg.setValueAndRange(ui->buttonEmbNeedleNum->text().toInt(), 1, 15, 0);
    }
    else if(m_setType == SET_TYPE_SEQUIN_COLOR)
    {
        numberInputDlg.setValueAndRange(ui->buttonEmbNeedleNum->text().toInt(), 1, 12, 0);
    }
    else if(m_setType == SET_TYPE_BEAD_COLOR)
    {
        numberInputDlg.setValueAndRange(ui->buttonEmbNeedleNum->text().toInt(), 1, 6, 0);
    }
    else if(m_setType == SET_TYPE_BEAD_TOWEL)
    {
        numberInputDlg.setValueAndRange(ui->buttonEmbNeedleNum->text().toInt(), 1, 16, 0);
    }
//针杆设置的弹窗点击确认按钮就写在配置文件了
    if(numberInputDlg.exec() == 1)
    {
        s32 val = numberInputDlg.getValue();
        ui->buttonEmbNeedleNum->setText(QString::number(val));

        for(int i = 0; i < m_embNeedleBtnList.size(); i++)
        {
            if(i < val)
            {
                m_embNeedleBtnList.at(i)->show();
            }
            else
            {
                m_embNeedleBtnList.at(i)->hide();
            }
        }

        //写入配置
        if(m_setType == SET_TYPE_NEEDLE_EMB)
        {
            g_pSettings->writeToIniFile("EmbNeedle/number",val);
        }
        else if(m_setType == SET_TYPE_SEQUIN_COLOR)
        {
            g_pSettings->writeToIniFile("SequinColor/number",val);
        }
        else if(m_setType == SET_TYPE_BEAD_COLOR)
        {
            g_pSettings->writeToIniFile("BeadColor/number",val);
        }
        else if(m_setType == SET_TYPE_BEAD_TOWEL)
        {
            g_pSettings->writeToIniFile("TowelNeedle/number",val);
        }
    }
}

void NeedleBarSetWidget::on_buttonOk_clicked()
{
    //缠绕，盘带，锯齿 不需要在针杆设置中设置针杆数，是默认写死的
//    g_pSettings->writeToIniFile("CoilNeedle/number",9);
//    g_pSettings->writeToIniFile("SawtoothNeedle/number",6);
//    g_pSettings->writeToIniFile("RibbonNeedle/number",2);
//    g_pSettings->writeToIniFile("LaserNeedle/number",1);
    //密码权限,单次有效
     g_emUser = operate;
    emit siNeedlebarSet();
    this->hide();
}

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

void NeedleBarSetWidget::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;
}

//散珠颜色设置
void NeedleBarSetWidget::on_buttonBeadSet_clicked()
{
    m_setType = SET_TYPE_BEAD_COLOR;
    refreshNeedleColorShow();
    ui->buttonBeadSet->setCheckable(true);
    ui->buttonBeadSet->setChecked(true);
}

//亮片颜色设置
void NeedleBarSetWidget::on_buttonSequinSet_clicked()
{
    m_setType = SET_TYPE_SEQUIN_COLOR;
    refreshNeedleColorShow();
    ui->buttonSequinSet->setCheckable(true);
    ui->buttonSequinSet->setChecked(true);
}

//绣花针杆颜色设置
void NeedleBarSetWidget::on_buttonEmbSet_clicked()
{
    m_setType = SET_TYPE_NEEDLE_EMB;
    refreshNeedleColorShow();
    ui->buttonEmbSet->setCheckable(true);
    ui->buttonEmbSet->setChecked(true);
}

//毛巾针杆设置   -rq
void NeedleBarSetWidget::on_buttonTowelSet_clicked()
{
    m_setType = SET_TYPE_BEAD_TOWEL;
    refreshNeedleColorShow();
    ui->buttonTowelSet->setCheckable(true);
    ui->buttonTowelSet->setChecked(true);
}
