#include "numerinputdialog.h"
#include "ui_numerinputdialog.h"

NumerInputDialog::NumerInputDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NumerInputDialog)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_TranslucentBackground, true);//设置窗体背景透明

    initDialog();
    initControl();
    initArabControlPos();
}

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

QString NumerInputDialog::getShowString(int64_t val, int pointflag)
{
    double value = val;

    if (pointflag < 0)
    {
        pointflag = 0;
    }
    else if (pointflag > 5)
    {
        pointflag = 5;
    }

    for (int i = 1; i < pointflag; i++)
    {
        value /= 10;
    }

    QString str;
    if (pointflag == 0)
    {
        str.sprintf("%.0f", value);
    }
    else if (pointflag == 1)
    {
        str.sprintf("%.0f", value);
        str += ".";
    }
    else if (pointflag == 2)
    {
        str.sprintf("%.1f", value);
    }
    else if (pointflag == 3)
    {
        str.sprintf("%.2f", value);
    }
    else if (pointflag == 4)
    {
        str.sprintf("%.3f", value);
    }
    else // if (pointflag >= 5)
    {
        str.sprintf("%.4f", value);
    }
    return str;
}

int NumerInputDialog::checkValueRange(int64_t val, int pointflag)
{
    int i, xsws;
    int64_t min, max;
    min = m_min;
    max = m_max;

    xsws = pointflag - 1;
    if (xsws < 0)
    {
        xsws = 0;
    }
    xsws = m_afterPoint - xsws;
    if (xsws < 0)
    {
        xsws = 0;
    }

    for (i = 0; i < xsws; i++)
    {
        min /= 10;
        max /= 10;
    }

    if (val < min || val > max)
    {
        return 1;
    }
    return 0;
}

int64_t NumerInputDialog::fixValue(int64_t val, int pointflag)
{
    int i, xsws;
    int64_t min, max;
    min = m_min;
    max = m_max;

    xsws = pointflag - 1;
    if (xsws < 0)
    {
        xsws = 0;
    }
    xsws = m_afterPoint - xsws;
    if (xsws < 0)
    {
        xsws = 0;
    }

    for (i = 0; i < xsws; i++)
    {
        min /= 10;
        max /= 10;
    }

    if (val < min)
    {
        val = min;
    }
    if (val > max)
    {
        val = max;
    }

    return val;
}

int NumerInputDialog::getPointMuti(int pointflag)
{
    int muti = 1;
    for (int i = 1; i < pointflag; i++)
    {
        muti *= 10;
    }
    return muti;
}

void NumerInputDialog::showValue()
{
    ui->labelValue->setText(getShowString(m_value, m_pointFlag));

    QString str = ui->labelValue->text();

    // 修复判断数值范围时,无法正确区分小数点的问题
    int val = m_value;
    int i;
    if (m_pointFlag <= 1)
    {
        for (i=0; i<m_afterPoint; i++)
        {
            val *= 10;
        }
    }
    else
    {
        for (i=m_pointFlag-1; i<m_afterPoint; i++)
        {
            val *= 10;
        }
    }

    if (str.length() > 0 && val >= m_min && val <= m_max)
    {
        ui->buttonOk->setEnabled(true);
    }
    else
    {
        ui->buttonOk->setEnabled(false);
    }
}

void NumerInputDialog::showRange()
{
    QString min, max, def;

    if (m_afterPoint == 0)
    {
        min = getShowString(m_min, 0);
        max = getShowString(m_max, 0);
        def = getShowString(m_defVal, 0);
    }
    else
    {
        min = getShowString(m_min, m_afterPoint+1);
        max = getShowString(m_max, m_afterPoint+1);
        def = getShowString(m_defVal, m_afterPoint+1);
    }

    QString rangeStr = "(" + min + "~" + max + ")" + m_unitStr;
    ui->labelRange->setText(rangeStr);
}

void NumerInputDialog::addNum(int num)
{
    if (((m_pointFlag < 5) &&
        ((m_afterPoint == 0) || (m_pointFlag < m_afterPoint + 1)) &&
        ((m_value > -100000000) && (m_value < 100000000)) && // 修复数值太大时显示错乱的问题
        1 ) ||
            (m_defFlag == 0) ||
            0 )
    {
        int64_t val;
        int pflag;

        if (m_defFlag == 0)
        {// 第一次按数字键,从0开始计数,解决每次修改值之前都需要按C键的问题
            m_value = 0;
            m_pointFlag = 0;
            m_defFlag = 1;
        }
        val = m_value;
        pflag = m_pointFlag;

        if (num < 0)
        {
            num = 0;
        }
        else if (num > 9)
        {
            num = 9;
        }

        val *= 10;
        val += num*((m_value<0)?-1:1);

        if (pflag != 0)
        {
            pflag++;
            if (pflag > 5)
            {
                pflag = 5;
            }
        }

        //if (checkValueRange(val, pflag) == 0)
        {
            m_value = val;
            m_pointFlag = pflag;
        }
    }
}

void NumerInputDialog::changeValue(int weights)
{
    int64_t val;
    //int pflag;

    val = m_value;

    m_defFlag = 0;      // 初始值
    if (weights > -1 && weights < 1)    // 小数
    {

    }

    //pflag = m_pointFlag;
    val += weights*getPointMuti(m_pointFlag);

    //if (checkValueRange(val, pflag) == 0)
    {
      //  val = fixValue(val, m_pointFlag);
       // if (val != m_value)
        {
            m_value = val;
            showValue();
        }
    }
}

//初始化窗体
void NumerInputDialog::initDialog()
{
    setTitleStr("");
    m_value = 0;
    m_min = 0;
    m_max = 0;
    m_afterPoint = 0;
    m_defVal = 0;
    m_defFlag = 0;
    m_pointFlag = 0;
    m_unitStr.clear();

    m_leftNumberBtnList.clear();
    m_leftNumberBtnList.append(ui->buttonPlusMinus);
    m_leftNumberBtnList.append(ui->buttonClean);
    m_leftNumberBtnList.append(ui->buttonDelete);
    m_leftNumberBtnList.append(ui->buttonNum7);
    m_leftNumberBtnList.append(ui->buttonNum8);
    m_leftNumberBtnList.append(ui->buttonNum9);
    m_leftNumberBtnList.append(ui->buttonNum4);
    m_leftNumberBtnList.append(ui->buttonNum5);
    m_leftNumberBtnList.append(ui->buttonNum6);
    m_leftNumberBtnList.append(ui->buttonNum1);
    m_leftNumberBtnList.append(ui->buttonNum2);
    m_leftNumberBtnList.append(ui->buttonNum3);
    m_leftNumberBtnList.append(ui->buttonNum00);
    m_leftNumberBtnList.append(ui->buttonNum0);
    m_leftNumberBtnList.append(ui->buttonPoint);

    m_rightNumberBtnList.clear();
    m_rightNumberBtnList.append(ui->buttonDec1000);
    m_rightNumberBtnList.append(ui->buttonInc1000);
    m_rightNumberBtnList.append(ui->buttonDec100);
    m_rightNumberBtnList.append(ui->buttonInc100);
    m_rightNumberBtnList.append(ui->buttonDec10);
    m_rightNumberBtnList.append(ui->buttonInc10);
    m_rightNumberBtnList.append(ui->buttonDec1);
    m_rightNumberBtnList.append(ui->buttonInc1);
    m_rightNumberBtnList.append(ui->buttonDec01);
    m_rightNumberBtnList.append(ui->buttonInc01);
}

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

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

//初始化窗体控件为1920x1080分辨率
void NumerInputDialog::initResolution1910()
{
    this->resize(1044,800);
    this->move((1920-this->width())/2+g_mainWidgetPos.x(),(1080-this->height())/2+g_mainWidgetPos.y());

    ui->frameBack->setGeometry(0,0,1044,800);
    ui->labelTitle->setGeometry(26,4,980,56);
    ui->labelValue->setGeometry(32,140,550,56);
    ui->labelRange->setGeometry(598,140,420,56);

    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 3; j ++)
        {
            m_leftNumberBtnList[i*3+j]->setGeometry(40+j*194,266+i*82,174,74);
        }
    }

    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 2; j ++)
        {
            m_rightNumberBtnList[i*2+j]->setGeometry(640+j*194,266+i*82,174,74);
        }
    }

    ui->buttonOk->setGeometry(640,686,174,74);
    ui->buttonCancel->setGeometry(834,686,174,74);
}

//初始化窗体控件为1024x600分辨率
void NumerInputDialog::initResolution1006()
{
    this->resize(584,448);
    this->move((1024-this->width())/2+g_mainWidgetPos.x(),(600-this->height())/2+g_mainWidgetPos.y());
    ui->frameBack->setGeometry(0,0,584,448);
    ui->labelTitle->setGeometry(7,-8,544,48);
    ui->labelValue->setGeometry(40,74,284,44);
    ui->labelRange->setGeometry(340,84,220,21);

    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 3; j ++)
        {
            m_leftNumberBtnList[i*3+j]->setGeometry(23+j*107,129+i*52,96,40);
        }
    }

    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 2; j ++)
        {
            m_rightNumberBtnList[i*2+j]->setGeometry(364+j*107,129+i*52,96,40);
        }
    }
    ui->buttonOk->setGeometry(365,389,96,40);
    ui->buttonCancel->setGeometry(471,389,96,40);
}

//初始化窗体控件样式
void NumerInputDialog::initControlStyle()
{
    //白蓝色渐变、灰蓝色渐变
    QString style = "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(255, 255, 255), stop:1 rgb(215, 243, 254));border-radius:5px;color:rgb(0, 0, 0);}";
    style += "QPushButton:pressed{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(217, 222, 224), stop:1 rgb(191,213,223));color:rgb(0, 0, 0);}";
    style += "QPushButton:disabled{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(230, 230, 230), stop:1 rgb(182,182,182));color:rgb(0, 0, 0);}";

    SetControlStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    ui->frameBack->setStyleSheet(setControlStyle.getStyleSheet(this->objectName()));

    for(int i = 0; i < m_leftNumberBtnList.size(); i++)
    {
        m_leftNumberBtnList[i]->setStyleSheet(style);
        m_leftNumberBtnList[i]->setFont(fontNormal_1);
    }

    for(int i = 0; i < m_rightNumberBtnList.size(); i++)
    {
        m_rightNumberBtnList[i]->setStyleSheet(style);
        m_rightNumberBtnList[i]->setFont(fontNormal_1);
    }

    ui->labelTitle->setStyleSheet(LABELWHITESTYLE);
    ui->labelTitle->setFont(fontNormal_1);
    ui->labelTitle->setAlignment(Qt::AlignLeft);
    ui->labelTitle->setAlignment(Qt::AlignVCenter);

    ui->labelValue->setStyleSheet(LABELNAVYBLUESTYLE);
    ui->labelValue->setFont(fontNormal_1);

    ui->labelRange->setStyleSheet(LABELGREYSTYLE);
    ui->labelRange->setFont(fontNormal_1);
    ui->labelRange->setAlignment(Qt::AlignLeft);
    ui->labelRange->setAlignment(Qt::AlignVCenter);

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

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

    ui->labelTitle->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    ui->labelValue->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    ui->labelRange->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
}

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

void NumerInputDialog::setUnitStr(QString str)
{
    m_unitStr = str;
//    showRange();
}

void NumerInputDialog::setValueAndRange(int64_t val, int64_t min, int64_t max, int afterpoint, int defen, int64_t defvalue)
{
    m_value = val;
    m_min = min;
    m_max = max;
    m_afterPoint = afterpoint;

    if (m_min < -999999999)
    {
        m_min = -999999999;     // 这个值看起来更舒服些
    }
    if (m_max > 999999999)
    {
        m_max = 999999999;     // 这个值看起来更舒服些
    }
    if (m_min > m_max)
    {
        double tmp = m_min;
        m_min = m_max;
        m_max = tmp;
    }

    if (m_afterPoint < 0)
    {
        m_afterPoint = 0;
    }
    if (m_afterPoint > 4)
    {
        m_afterPoint = 4;
    }

    if (m_value < m_min)
    {
        m_value = m_min;
    }
    if (m_value > m_max)
    {
        m_value = m_max;
    }

    if (m_afterPoint != 0)
    {
        m_pointFlag = m_afterPoint + 1;
    }
    else
    {
        m_pointFlag = 0;
    }

    m_defFlag = defen;
    m_defVal = defvalue;

    showRange();
    showValue();
}

int64_t NumerInputDialog::getValue()
{
    int i, j;
    int64_t val = m_value;
    i = m_pointFlag;
    j = m_afterPoint;
    if (j != 0)
    {
        j += 1;
    }

    while (i < j)
    {
        if (i != 0)
        {
            val *= 10;
        }
        i++;
    }

    return val;
}

void NumerInputDialog::on_buttonClean_clicked()
{
    m_pointFlag = 0;
    m_value = 0;
    m_defFlag = 0;
    showValue();
}

void NumerInputDialog::on_buttonNum00_clicked()
{
    addNum(0);
    addNum(0);
    showValue();
}

void NumerInputDialog::on_buttonDelete_clicked()
{
    m_defFlag = 1;

    if (m_pointFlag != 1)
    {
        int64_t val;
        int pflag;
        val = m_value;
        pflag = m_pointFlag;
        val /= 10;
        pflag--;
        if (pflag < 0)
        {
            pflag = 0;
        }
        //if (checkValueRange(val, pflag) == 0)
        {
            m_value = val;
        }
    }
    if (m_pointFlag != 0)
    {
        m_pointFlag--;
    }
    showValue();
}

void NumerInputDialog::on_buttonNum7_clicked()
{
    addNum(7);
    showValue();
}

void NumerInputDialog::on_buttonNum8_clicked()
{
    addNum(8);
    showValue();
}

void NumerInputDialog::on_buttonNum9_clicked()
{
    addNum(9);
    showValue();
}

void NumerInputDialog::on_buttonNum4_clicked()
{
    addNum(4);
    showValue();
}

void NumerInputDialog::on_buttonNum5_clicked()
{
    addNum(5);
    showValue();
}

void NumerInputDialog::on_buttonNum6_clicked()
{
    addNum(6);
    showValue();
}

void NumerInputDialog::on_buttonNum1_clicked()
{
    addNum(1);
    showValue();
}

void NumerInputDialog::on_buttonNum2_clicked()
{
    addNum(2);
    showValue();
}

void NumerInputDialog::on_buttonNum3_clicked()
{
    addNum(3);
    showValue();
}

void NumerInputDialog::on_buttonPlusMinus_clicked()
{
    int64_t val;
    val = m_value;
    val *= -1;
    //if (checkValueRange(val, m_pointFlag) == 0)
    {
        m_value = val;
        showValue();
    }
}

void NumerInputDialog::on_buttonNum0_clicked()
{
    addNum(0);
    showValue();
}

void NumerInputDialog::on_buttonPoint_clicked()
{
    if (m_pointFlag == 0 && m_afterPoint != 0)
    {
        m_pointFlag = 1;
        showValue();
    }
}

void NumerInputDialog::on_buttonDec1000_clicked()
{
    changeValue(-1000);
}

void NumerInputDialog::on_buttonInc1000_clicked()
{
    changeValue(1000);
}

void NumerInputDialog::on_buttonDec100_clicked()
{
    changeValue(-100);
}

void NumerInputDialog::on_buttonInc100_clicked()
{
    changeValue(100);
}

void NumerInputDialog::on_buttonDec10_clicked()
{
    changeValue(-10);
}

void NumerInputDialog::on_buttonInc10_clicked()
{
    changeValue(10);
}

void NumerInputDialog::on_buttonDec1_clicked()
{
    changeValue(-1);
}

void NumerInputDialog::on_buttonInc1_clicked()
{
    changeValue(1);
}

void NumerInputDialog::on_buttonDec01_clicked()
{
    if (m_afterPoint != 0)
    {

        m_defFlag = 0;
        int64_t val;
        int pflag, muti;

        int dir;
        int weights = -1;
        muti = 1;

        //dir = m_value > 0 ? 1:-1;
        dir = m_value > 0 ? 1:1;
        val = m_value;
        pflag = m_pointFlag;

        if (m_pointFlag <= 1)   // 是整数
        {
            val *= 10;
            pflag = 2;      // 转换为1位小数
        }
        else       // 小数
        {
            for (int i = 2; i < m_pointFlag; i++)
            {
                muti *= 10;
            }
        }

        val += weights * dir * muti;

        //if (checkValueRange(val, pflag) == 0)
        {
           // val = fixValue(val, pflag);
            m_value = val;
            m_pointFlag = pflag;
            showValue();
        }
    }
}

void NumerInputDialog::on_buttonInc01_clicked()
{
    if (m_afterPoint != 0)
    {

        m_defFlag = 0;
        int64_t val;
        int pflag, muti;
        int dir;
        int weights = 1;

        //dir = m_value > 0 ? 1:-1;
        dir = m_value > 0 ? 1:1;
        muti = 1;

        val = m_value;
        pflag = m_pointFlag;

        if (m_pointFlag <= 1)   // 是整数
        {
            val *= 10;
            pflag = 2;      // 转换为1位小数
        }
        else       // 小数
        {

            for (int i = 2; i < m_pointFlag; i++)
            {
                muti *= 10;
            }
        }

        val += weights * dir * muti;

        //if (checkValueRange(val, pflag) == 0)
        {
          //  val = fixValue(val, pflag);
            m_value = val;
            m_pointFlag = pflag;
            showValue();
        }
    }
}

void NumerInputDialog::on_buttonOk_clicked()
{
    done(1);
}

void NumerInputDialog::on_buttonCancel_clicked()
{
    done(0);
}
