#include "passworddialog.h"
#include "ui_passworddialog.h"

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

    initControl();
    initArabControlPos();
    m_inputStr.clear();
    m_ifShow = false;
    m_cleanFlag = 0;
    m_ipLength = 0;
    refreshUi();
}

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

//初始化窗体控件，包括位置、尺寸、样式
void PassWordDialog::initControl()
{
    m_NumerBtnlist.clear();
    m_NumerBtnlist.append(ui->buttonNum7);
    m_NumerBtnlist.append(ui->buttonNum8);
    m_NumerBtnlist.append(ui->buttonNum9);
    m_NumerBtnlist.append(ui->buttonNum4);
    m_NumerBtnlist.append(ui->buttonNum5);
    m_NumerBtnlist.append(ui->buttonNum6);
    m_NumerBtnlist.append(ui->buttonNum1);
    m_NumerBtnlist.append(ui->buttonNum2);
    m_NumerBtnlist.append(ui->buttonNum3);
    m_NumerBtnlist.append(ui->buttonNum0);

    for(int var = 0; var < m_NumerBtnlist.size(); var++)
    {
        connect(m_NumerBtnlist.at(var),
                SIGNAL(clicked()),
                this,
                SLOT(numerBtnClick())
                );
    }

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

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

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

    ui->frameBack->setGeometry(0,0,785,880);
    ui->labelTitle->setGeometry(110,72,500,56);
    ui->labelValue->setGeometry(198,221,538,56);

    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 3; j ++)
        {
            if(i*3+j == m_NumerBtnlist.size()-1)
            {
                m_NumerBtnlist[i*3+j]->setGeometry(118+(j+1)*188,326+i*88,172,74);
                break;
            }
            else
            {
                m_NumerBtnlist[i*3+j]->setGeometry(118+j*188,326+i*88,172,74);
            }
        }
    }

    ui->buttonClean->setGeometry(120,590,172,74);
    ui->buttonBack->setGeometry(496,590,172,74);

    ui->buttonOk->setGeometry(306,688,172,74);
    ui->buttonCancel->setGeometry(496,688,172,74);
}
//初始化窗体控件为1024x600分辨率
void PassWordDialog::initResolution1006()
{
    this->resize(416,505);
    this->move((1024-this->width())/2+g_mainWidgetPos.x(),(600-this->height())/2+g_mainWidgetPos.y());
    ui->frameBack->setGeometry(0,0,416,505);
    ui->labelTitle->setGeometry(52,33,328,48);
    ui->labelValue->setGeometry(70,122,270,44);

    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 3; j ++)
        {
            if(i*3+j == m_NumerBtnlist.size()-1)
            {
                m_NumerBtnlist[i*3+j]->setGeometry(58+(j+1)*102,210+i*46,96,40);
                break;
            }
            else
            {
                m_NumerBtnlist[i*3+j]->setGeometry(58+j*102,210+i*46,96,40);
            }
        }
    }

    ui->buttonClean->setGeometry(58,348,96,40);
    ui->buttonBack->setGeometry(262,348,96,40);

    ui->buttonOk->setGeometry(160,404,96,40);
    ui->buttonCancel->setGeometry(262,404,96,40);
}
//初始化窗体控件样式
void PassWordDialog::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;}";
    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));}";
    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));}";

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

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

    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->labelValue->setStyleSheet(LABELNAVYBLUESTYLE);
    ui->labelValue->setFont(fontNormal_1);
    ui->labelValue->setAlignment(Qt::AlignCenter);

    ui->buttonClean->setStyleSheet(style);
    ui->buttonClean->setFont(fontNormal_1);
    ui->buttonBack->setStyleSheet(style);
    ui->buttonBack->setFont(fontNormal_1);

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

void PassWordDialog::refreshUi()
{
    QTextCodec *cod = QTextCodec::codecForLocale();
    QString showStr("");
    QString password = cod->toUnicode("●");
    for (int var = 0; var < m_inputStr.size(); var++)
    {
        showStr = showStr + password;
    }

    if(m_ifShow == false)//不可见
    {
        ui->labelValue->setText(showStr);
    }
    else//可见
    {
      ui->labelValue->setText(m_inputStr);
    }

    if(g_emResolut == resolution1910)
    {
    	if(m_inputStr.length() > 0)
		{
        	ui->buttonOk->setEnabled(true);
        }
        else
        {
        	ui->buttonOk->setEnabled(false);
        }
    }
    else
    {

        if(m_inputStr.length() > 0)
        {
            ui->buttonOk->setEnabled(true);
        }
        else
        {
            ui->buttonOk->setEnabled(false);
        }
    }
}

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

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

void PassWordDialog::numerBtnClick()
{
    QPushButton *button = (QPushButton*) this->sender() ;

    if(button == NULL)
    {
        return;
    }
    QString str = button->text();
    m_inputStr.append(str);
    refreshUi();
}

void PassWordDialog::setTitleStr(QString str,int cFlag)
{
    ui->labelTitle->setText(str);
    m_inputStr.clear();
    if(cFlag != 0)
    {
        m_cleanFlag = cFlag;
        ui->buttonClean->setText(".");
    }
    refreshUi();
}

void PassWordDialog::setShowIfVisible(bool bShow)
{
    m_ifShow = bShow;
}

void PassWordDialog::setIpStr(QString ip)
{
    m_inputStr= ip;
    m_ipLength = ip.length();
    refreshUi();
}

QString PassWordDialog::getInputStr()
{
    return m_inputStr;
}
void PassWordDialog::on_buttonClean_clicked()
{
    if(m_cleanFlag != 0)
    {
        QString str = ui->buttonClean->text();
        m_inputStr.append(str);
    }
    else
    {
        m_inputStr.clear();
    }
    refreshUi();
}

void PassWordDialog::on_buttonBack_clicked()
{
    if(m_inputStr.length() <= m_ipLength)
    {
        return;
    }
    m_inputStr = m_inputStr.mid(0 , m_inputStr.size() - 1);
    refreshUi();
}

void PassWordDialog::on_buttonOk_clicked()
{
    if(m_inputStr.indexOf(".") != -1)
    {
        //检测IP地址是否合法
        QRegExp regExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
        if (!regExp.exactMatch(m_inputStr))
        {
            PromptDialog promptDlg(this);
            promptDlg.initDialog(PromptDialog::BTN_OK);
            promptDlg.setTitleStr(tr("Prompt"));
            QString str = tr("IP settings are unreasonable!");//IP设置不合理!
            promptDlg.setContentStr(str);
            promptDlg.exec();

            return;
        }
    }

    done(1);
}

void PassWordDialog::on_buttonCancel_clicked()
{
    m_inputStr.clear();
    done(0);
}
