#include "brokenlinedialog.h"
#include "ui_brokenlinedialog.h"

BrokenLineDialog::BrokenLineDialog(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BrokenLineDialog)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal );//用以设置窗口模态类型   ,ApplicationModal阻塞整个应用程序的所有窗口
    initWidget();
    initControl();
    initArabControlPos();
}

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

void BrokenLineDialog::initWidget()
{
    m_buttonEmbHeadList.clear();
    ui->labelContent->setText("");
    ui->labelContentInfo->setText("");
}

void BrokenLineDialog::initControl()
{
    //隔头绣界面显示的机头,暂定132个机头
    m_buttonEmbHeadList.clear();
    for(int i = 0; i < 66; i++)
    {
        QPushButton *buttonEmbHead = NULL;
        buttonEmbHead = new QPushButton(ui->frameQuiHeadEmb);
        m_buttonEmbHeadList.append(buttonEmbHead);
        connect(buttonEmbHead,
                SIGNAL(clicked()),
                this,
                SLOT(slotQuiEmbHeadBtnClick())
                );
    }

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

void BrokenLineDialog::initResolution1910()
{
    //上状态栏
    this->resize(1322,820);
    //this->move((1920-this->width())/2+g_mainWidgetPos.x(),(1080-this->height())/2+g_mainWidgetPos.y());
    this->move(192+g_mainWidgetPos.x(),62+g_mainWidgetPos.y());
    ui->frameBack->setGeometry(0,0,1322,820);
    ui->labelTitle->setGeometry(20,15,900,56);
    ui->labelContent->setGeometry(12,58,1300,700);
    ui->labelContentInfo->setGeometry(12,58,1300,700);
    ui->buttonOk->setGeometry(1110,710,174,60);

    ui->frameQuiHeadEmb->setGeometry(0,84,1322,820);
    ui->labelQuiCurColorOrder->setGeometry(80,20,300,64);
    ui->buttonQuiCurColorOrder->setGeometry(400,20,160,64);
    ui->frameQuiUpAndDown->setGeometry(600,20,500,64);
    ui->buttonUp->setGeometry(6,0,64,64);
    ui->buttonDn->setGeometry(80,0,64,64);
    for(int i = 0; i < 6; i++)
    {
        for(int j = 0; j < 11; j++)
        {
            m_buttonEmbHeadList[i*11+j]->setGeometry(20+j*118,100+i*90,100,76);
            m_buttonEmbHeadList[i*11+j]->setFont(fontNormal_1);
        }
    }
    ui->frameTwo->setGeometry(0,740,1322,64);
    ui->labelInfo->setGeometry(15,0,100,64);
    ui->buttonPgUp->setGeometry(210,0,174,64);
    ui->buttonPgDn->setGeometry(394,0,174,64);
    ui->buttonReset->setGeometry(578,0,174,64);
    ui->buttonReverse->setGeometry(762,0,174,64);
    ui->buttonHeadEmbOk->setGeometry(946,0,174,64);
    ui->buttonCancel->setGeometry(1130,0,174,64);
}

void BrokenLineDialog::initResolution1006()
{
    //上状态栏

    this->resize(710,460);
    //this->move((1024-this->width())/2+g_mainWidgetPos.x(),(600-this->height())/2);
    this->move(100+g_mainWidgetPos.x(),30+g_mainWidgetPos.y());

    ui->frameBack->setGeometry(0,0,710,460);
    ui->labelTitle->setGeometry(10,-5,500,56);

    ui->labelContent->setGeometry(15,56,680,340);
    ui->labelContentInfo->setGeometry(15,56,680,340);
    ui->buttonOk->setGeometry(590,400,96,40);

    ui->frameQuiHeadEmb->setGeometry(0,60,710,460);
    ui->labelQuiCurColorOrder->setGeometry(1,0,200,48);
    ui->buttonQuiCurColorOrder->setGeometry(209,0,160,48);
    ui->frameQuiUpAndDown->setGeometry(380,0,140,48);
    ui->buttonUp->setGeometry(6,0,48,48);
    ui->buttonDn->setGeometry(66,0,48,48);

    for(int i = 0; i < 6; i++)
    {
        for(int j = 0; j < 11; j++)
        {
            m_buttonEmbHeadList[i*11+j]->setGeometry(10+j*63,54+i*48,59,44);
            m_buttonEmbHeadList[i*11+j]->setFont(fontNormal_1);
        }
    }

    ui->frameTwo->setGeometry(15,406,700,46);
    ui->labelInfo->setGeometry(8,0,70,46);
    ui->buttonPgUp->setGeometry(86,0,96,46);
    ui->buttonPgDn->setGeometry(186,0,96,46);
    ui->buttonReset->setGeometry(286,0,96,46);
    ui->buttonReverse->setGeometry(386,0,96,46);
    ui->buttonHeadEmbOk->setGeometry(486,0,96,46);
    ui->buttonCancel->setGeometry(586,0,96,46);
}

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

    //设置控件图标
    //背景图

    ui->frameBack->setStyleSheet(TRANSPARENTSTYLE);
    this->setStyleSheet(LABELWHITESTYLE);
    ui->frameBack->setStyleSheet(setControlStyle.getStyleSheet(this->objectName()));
    ui->labelTitle->setStyleSheet(LABELWHITESTYLE);
    ui->labelTitle->setFont(fontNormal_1);
    ui->labelTitle->setAlignment(Qt::AlignLeft);
    ui->labelTitle->setAlignment(Qt::AlignVCenter);
    //数字
    ui->labelContent->setStyleSheet(LABELREDSTYLEBROKEN);  // 断线机头  红色  字体加大  -rq

    //    QFont font;
    //    font.setPixelSize(400);
    //font.setFamily(FONTTYPE);//字体
    //     ui->labelContent->setFont(font);
    ui->labelContent->setFont(fontNormal_10);
    ui->labelContent->setAlignment(Qt::AlignCenter);//文字水平垂直居中
    //ui->labelContent->setStyleSheet("font-size:400px");
    //    ui->labelContent->setAlignment(Qt::AlignHCenter);
    //    ui->labelContent->setAlignment(Qt::AlignVCenter);

    ui->labelContentInfo->setStyleSheet(LABELREDSTYLEBROKEN);  // 断线机头文字  红色  字体加大  -rq
    QFont font;//设置断线文字大小
    if(g_emResolut == resolution1006 )
    {
        font.setPixelSize(51);
    }
    else if (g_emResolut == resolution1910)
    {
        font.setPixelSize(101);
    }
    font.setFamily(FONTTYPE);//字体
    ui->labelContentInfo->setFont(font);
    ui->labelContentInfo->setAlignment(Qt::AlignCenter);//文字水平垂直居中

    ui->buttonOk->setWhiteBlueBottomStyle();
    ui->buttonOk->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonOk->objectName()+"_1"),8);

    //qui隔头绣
    ui->frameQuiHeadEmb->setStyleSheet(TRANSPARENTSTYLE);
    ui->frameQuiUpAndDown->setStyleSheet(TRANSPARENTSTYLE);
    ui->labelQuiCurColorOrder->setStyleSheet(LABELGREYSTYLE);
    ui->labelQuiCurColorOrder->setFont(fontNormal_1);
    ui->buttonQuiCurColorOrder->setFont(fontNormal_1);
    ui->buttonUp->setGreenGradientBottomStyle(BORDER_RADIUS1);
    ui->buttonUp->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonUp->objectName()));
    ui->buttonDn->setGreenGradientBottomStyle(BORDER_RADIUS1);
    ui->buttonDn->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonDn->objectName()));
    ui->frameTwo->setStyleSheet(TRANSPARENTSTYLE);
    ui->labelInfo->setStyleSheet(LABELGREYSTYLE);
    ui->labelInfo->setFont(fontNormal_1);
    ui->buttonPgUp->setWhiteBlueBottomStyle();
    ui->buttonPgUp->setFont(fontNormal_6);
    ui->buttonPgDn->setWhiteBlueBottomStyle();
    ui->buttonPgDn->setFont(fontNormal_6);   
    ui->buttonReset->setWhiteBlueBottomStyle();
    ui->buttonReset->setFont(fontNormal_6);
    ui->buttonReverse->setWhiteBlueBottomStyle();
    ui->buttonReverse->setFont(fontNormal_6);
    ui->buttonHeadEmbOk->setWhiteBlueBottomStyle();
    ui->buttonHeadEmbOk->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonOk->objectName()+"_1"),8);
    ui->buttonCancel->setWhiteBlueBottomStyle();
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonCancel->objectName()+"_1"),12);
}

void BrokenLineDialog::refreshQuiColorOrderHead()
{
    if (g_emMacType == QUIMACHINE_EMB )               //绗绣机
    {
        if(g_emProductType ==PRODUCT_QUI_SINGLE )//单排
        {
            for(int i = 0; i < m_buttonEmbHeadList.size(); i++)
            {
                s16 num = m_buttonEmbHeadList.size()*(m_curPage-1)+i;
                if(num < m_totalHead)
                {
                    m_buttonEmbHeadList.at(i)->show();
                    m_buttonEmbHeadList[i]->setText(QString::number(num+1));
                }
                else
                {
                    m_buttonEmbHeadList.at(i)->hide();
                }
            }

            ui->labelInfo->setText(tr("Single"));//单排
        }
        else if(g_emProductType ==PRODUCT_QUI_DOUBLE )//双排
        {
            for(int i = 0; i < m_buttonEmbHeadList.size(); i++)
            {
                s16 num = i;
                if(num < m_totalHead)
                {
                    m_buttonEmbHeadList.at(i)->show();
                    m_buttonEmbHeadList[i]->setText(QString::number(num+1));
                }
                else
                {
                    m_buttonEmbHeadList.at(i)->hide();
                }
            }

            if(m_curPage == 1)
            {
                ui->labelInfo->setText(tr("First"));//第1排
            }
            else if(m_curPage == 2)
            {
                ui->labelInfo->setText(tr("Second"));//第2排
            }
        }
    }

    if(sizeof(m_headBuf) < HEADBUF)
    {
        return;
    }

    //复制当前色序的全部工作机头缓存 512个机头，64x8=512（分两排，每排256个）
    u8 curHeadBuf[64];
    memcpy(curHeadBuf,m_headBuf+(m_curColorOrder-1)*sizeof(curHeadBuf),sizeof(curHeadBuf));

    for(u32 i = 0; i < sizeof(curHeadBuf); i++)
    {
        s8 eachBuf = curHeadBuf[i];

        for(int j = 0; j < 8; j++)
        {
            int idx = i*8+j;

//            if(i >= sizeof(curHeadBuf)/2)//第二排机头存储位置
//            {
//                idx = idx - sizeof(curHeadBuf)/2*8 + 8;//第二排按钮m_buttonEmbHeadList从索引8开始，所以减去i*8
//            }

            if(((eachBuf >> j) & 1) == 1)
            {
                if(g_emProductType ==PRODUCT_QUI_SINGLE )//单排
                {
                    if(idx >= m_buttonEmbHeadList.size() && m_curPage != 1)
                    {
                        idx -= m_buttonEmbHeadList.size();
                    }
                }
                else if(g_emProductType ==PRODUCT_QUI_DOUBLE)//双排
                {
                    if(idx >= HEADBUF/32 && m_curPage != 1)
                    {
                        idx -= HEADBUF/32;
                    }
                }
                if(idx < m_buttonEmbHeadList.size())
                {
                    m_buttonEmbHeadList.at(idx)->setStyleSheet(m_buttonStyle2);
                }
            }
            else
            {
                if(g_emProductType ==PRODUCT_QUI_SINGLE )//单排
                {
                    if(idx >= m_buttonEmbHeadList.size() && m_curPage != 1)
                    {
                        idx -= m_buttonEmbHeadList.size();
                    }
                }
                else if(g_emProductType ==PRODUCT_QUI_DOUBLE)//双排
                {
                    if(idx >= HEADBUF/32 && m_curPage != 1)
                    {
                        idx -= HEADBUF/32;
                    }
                }
                if(idx < m_buttonEmbHeadList.size())
                {
                    m_buttonEmbHeadList.at(idx)->setStyleSheet(m_buttonStyle1);
                }
            }
        }
    }

    ui->buttonQuiCurColorOrder->setText(QString::number(m_curColorOrder));
}

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

    ui->labelTitle->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    ui->labelQuiCurColorOrder->setAlignment(Qt::AlignLeft| Qt::AlignVCenter);
    ui->labelInfo->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
}

void BrokenLineDialog::initDialog(int sel)
{
    ui->buttonPgUp->setEnabled(false);
    ui->buttonPgDn->setEnabled(false);

    m_curPage = 1;
    m_totalHead = 0;
    m_curColorOrder = 0;//当前色序数
    m_totalColorOrder = 0;//总色序数
    memset(m_headBuf, 0, sizeof(m_headBuf));

    //蓝色
    m_buttonStyle1 = "QPushButton{outline: none;color:rgb(10,10,10);background-color:rgb(0,151,226);border:0px solid rgb(0,0,0);border-style:inset;border-radius:5px;}";
    //白色
    m_buttonStyle2 = "QPushButton{outline: none;color:rgb(10,10,10);background-color:rgb(250,250,250);border:0px solid rgb(0,0,0);border-style:inset;border-radius:5px;}";

    if (sel == BTN_BROKEN)
    {
        ui->labelContent->show();
        ui->labelContentInfo->show();
        ui->buttonOk->show();
        ui->frameQuiHeadEmb->hide();
        ui->frameTwo->hide();
    }
    else if (sel == BTN_HEADEMB)
    {
        ui->frameQuiHeadEmb->show();
        ui->frameTwo->show();
        ui->labelContent->hide();
        ui->labelContentInfo->hide();
        ui->buttonOk->hide();
    }
}

void BrokenLineDialog::initEmbHead(int headNum, int colorOrderNum, QString path)
{
    m_totalHead = headNum;
    m_totalColorOrder = colorOrderNum;
    path = path + ".fcg";

    if(headNum <= 0)
    {
        ui->buttonHeadEmbOk->setEnabled(false);
    }
    else
    {
        ui->buttonHeadEmbOk->setEnabled(true);
    }

    if(m_totalHead > m_buttonEmbHeadList.size() || g_emProductType ==PRODUCT_QUI_DOUBLE)
    {
        ui->buttonPgUp->setEnabled(false);
        ui->buttonPgDn->setEnabled(true);
    }

    QFile file(path);
    if(file.exists())//存在fcg文件
    {
        if(file.open(QIODevice::ReadOnly))
        {
            QByteArray ary = file.readAll();
            if((u32)ary.size() >= sizeof(DataFileHead)+sizeof(m_headBuf))
            {
                memcpy(m_headBuf,ary.data()+sizeof(DataFileHead),sizeof(m_headBuf));
            }
        }
        file.close();
    }

    m_curColorOrder = 1;
    refreshQuiColorOrderHead();
}

//设置标题
void BrokenLineDialog::setTitleStr(QString str)
{
    ui->labelTitle->setText(str);
}

//设置内容
void BrokenLineDialog::setContentInfoStr(QString str)
{
    ui->labelContent->setText("");//数字的控件清空
    ui->labelContent->hide();//数字的控件隐藏
    ui->labelContentInfo->setText(str);//设置文字的控件内容
    ui->labelContentInfo->show();//文字的控件显示
}

void BrokenLineDialog::setContentStr(QString str)
{
    ui->labelContentInfo->setText("");
    ui->labelContentInfo->hide();
    ui->labelContent->setText(str);
    ui->labelContent->show();
}

void BrokenLineDialog::show(int flag)
{
    if(flag == 1)
    {
        ui->labelContent->setStyleSheet(LABELREDSTYLEBROKENBLUE);  // 断线机头  蓝色  字体加大  -rq
    }
    else if (flag == 3)
    {
        ui->labelContent->setStyleSheet(LABELBLACKSTYLE);  // 断线机头  黑色  字体加大  -rq
    }
    QWidget::show();
}

void BrokenLineDialog::on_buttonOk_clicked()
{
    this->hide();
}

void BrokenLineDialog::slotQuiEmbHeadBtnClick()
{
    if(sizeof(m_headBuf) < HEADBUF)
    {
        return;
    }

    QPushButton *headNeedleBtn = (QPushButton*) this->sender();

    if(headNeedleBtn == NULL)
    {
        return;
    }

    int size = 64;//每个色序对应的机头数 64x8=512（第一排存储在前32字节，第二排存储在后32字节）
    u16 idx = 0;
    u16 oft = 0;
    for(int i = 0; i < m_buttonEmbHeadList.size(); i++)
    {
        if(headNeedleBtn == m_buttonEmbHeadList.at(i))
        {
            if(g_emProductType == PRODUCT_QUI_DOUBLE)//绗绣机且双排
            {
                if(m_curPage == 1)//第一排
                {
                    idx = i/8;
                    oft = i%8;
                }
                else if(m_curPage == 2)//第二排
                {
                    idx = size/2;//=32即从256开始
                    oft = i%8;
                }
            }
            else
            {
                idx = (i+m_buttonEmbHeadList.size()*(m_curPage-1))/8;
                oft = (i+m_buttonEmbHeadList.size()*(m_curPage-1))%8;
            }

            s8 eachBuf = m_headBuf[idx+(m_curColorOrder-1)*size];

            if(((eachBuf >> oft) & 1) == 1)
            {
                //置0
                m_headBuf[idx+(m_curColorOrder-1)*size] = ~((~(m_headBuf[idx+(m_curColorOrder-1)*size])) | (1 << oft));
            }
            else
            {
                //置1
                m_headBuf[idx+(m_curColorOrder-1)*size] = m_headBuf[idx+(m_curColorOrder-1)*size] | (1 << oft);//0000 0001
            }

            break;
        }
    }

    refreshQuiColorOrderHead();
}

void BrokenLineDialog::on_buttonHeadEmbOk_clicked()
{
    emit siSetHeadData(m_headBuf);
    this->hide();
}

void BrokenLineDialog::on_buttonPgUp_clicked()
{
    m_curPage--;
    if(m_curPage < 0)
    {
        m_curPage = 1;
    }

    ui->buttonPgUp->setEnabled(false);
    ui->buttonPgDn->setEnabled(true);

    refreshQuiColorOrderHead();
}

void BrokenLineDialog::on_buttonPgDn_clicked()
{
    m_curPage++;
    if(m_curPage > 2)
    {
        m_curPage = 2;
    }

    ui->buttonPgDn->setEnabled(false);
    ui->buttonPgUp->setEnabled(true);

    refreshQuiColorOrderHead();
}

void BrokenLineDialog::on_buttonReset_clicked()
{
    memset(m_headBuf, 0, sizeof(m_headBuf));
    refreshQuiColorOrderHead();
}

void BrokenLineDialog::on_buttonReverse_clicked()
{
    u8 headBuf[HEADBUF];
    memset(headBuf,0,sizeof(headBuf));

    u8 cpHeadBuf[HEADBUF];
    memcpy(cpHeadBuf,m_headBuf,sizeof(cpHeadBuf));

    for(s16 i = 0; i < m_totalColorOrder; i++)
    {
        //当前色序的512个机头设置
        u8 curHeadBuf[64];
        memset(curHeadBuf,0,sizeof(curHeadBuf));
        memcpy(curHeadBuf,cpHeadBuf+i*sizeof(curHeadBuf),sizeof(curHeadBuf));

        u8 head1 = curHeadBuf[0];
        u8 head2 = curHeadBuf[32];

        for(u16 j = 0; j < sizeof(curHeadBuf); j++)//256个机头
        {
            if(j >= sizeof(curHeadBuf)/2)//第二排
            {
                curHeadBuf[j] = head2;
            }
            else
            {
                curHeadBuf[j] = head1;//第一排
            }
        }
        memcpy(headBuf+i*sizeof(curHeadBuf),curHeadBuf,sizeof(curHeadBuf));
    }

    memcpy(m_headBuf,headBuf,sizeof(headBuf));
    refreshQuiColorOrderHead();
}

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

void BrokenLineDialog::on_buttonQuiCurColorOrder_clicked()
{
    int rslt;
    NumerInputDialog ndlg(this);
    ndlg.setTitleStr(tr("Color order num"));//色序号
    ndlg.setUnitStr("");
    ndlg.setValueAndRange(m_curColorOrder, 1, m_totalColorOrder, 0);
    rslt = ndlg.exec();

    if (rslt == 1)
    {
        s32 val = ndlg.getValue();
        m_curColorOrder = val;
        refreshQuiColorOrderHead();
    }
}

void BrokenLineDialog::on_buttonUp_clicked()
{
    m_curColorOrder--;
    if(m_curColorOrder <= 0)
    {
        m_curColorOrder = 1;
    }
    refreshQuiColorOrderHead();
}

void BrokenLineDialog::on_buttonDn_clicked()
{
    m_curColorOrder++;
    if(m_curColorOrder > m_totalColorOrder)
    {
        m_curColorOrder = m_totalColorOrder;
    }
    refreshQuiColorOrderHead();
}
//断线弹窗时，点击窗口外部 ，  窗口关闭
void BrokenLineDialog::mousePressEvent(QMouseEvent* event)
{
    if (!rect().contains(event->pos())) { // 点击了主窗口外部
        close();
    }
}
