#include "cctrllineedit.h"
#include <QDoubleValidator>
#include "base.h"
#include <QMouseEvent>
#include "def_global_gui.h"
#include "cdlgkeyboard.h"
CCtrlLineEdit* m_gFoucsLineEdit=0;

MyDoubleValidator::MyDoubleValidator(QObject *parent):
    QDoubleValidator( parent)
{

}

MyDoubleValidator::MyDoubleValidator(double bottom, double top,
                                     int decimals, QObject * parent ) :
    QDoubleValidator(bottom, top, decimals, parent)
{

}

QValidator::State MyDoubleValidator::validate(QString &s, int &i) const
{
    UNUSED(i);
    if (s.isEmpty() || s == "-") {
        return QValidator::Intermediate;
    }

    QChar decimalPoint = locale().decimalPoint();

    if(s.indexOf(decimalPoint) != -1) {
        int charsAfterPoint = s.length() - s.indexOf(decimalPoint) - 1;

        if (charsAfterPoint > decimals()) {
            return QValidator::Invalid;
        }
    }

    bool ok;
    double d = locale().toDouble(s, &ok);

    if (ok && d >= bottom() && d <= top()) {
        return QValidator::Acceptable;
    } else {
        return QValidator::Invalid;
    }
}

CCtrlLineEdit::CCtrlLineEdit(QWidget *parent)
    : QLineEdit(parent)
{
    m_bNumOnly=true;
    m_dMax=10000;
    m_dMin=-10000;
    m_bDouble=false;
    connect(this,SIGNAL(textChanged(const QString&))
            ,SLOT(FreshBKColor(const QString&)));
    m_bSetRed=false;
    n_NumValidator.setRegExp(QRegExp("[0-9]-.+$"));
}


CCtrlLineEdit::CCtrlLineEdit(const QString &s, QWidget *parent)
    :QLineEdit(s, parent)
{
    m_bNumOnly=true;
    m_dMax=0;
    m_dMin=0;
    m_bDouble=false;
    connect(this,SIGNAL(textChanged(const QString&))
            ,SLOT(FreshBKColor(const QString&)));
    n_NumValidator.setRegExp(QRegExp("[0-9]-.+$"));
}

CCtrlLineEdit::~CCtrlLineEdit()
{
    HideKeyboard();
}

void CCtrlLineEdit::focusInEvent(QFocusEvent *event)
{
    QLineEdit::focusInEvent(event);
    selectAll();
    toggleKeyboard();
    m_gFoucsLineEdit=this;
}

void CCtrlLineEdit::focusOutEvent(QFocusEvent *event)
{
    if(true == m_bNumOnly)
    {
        if(this->text().toDouble()>m_dMax){
            this->setText(QString::number(m_dMax));
        }else if(this->text().toDouble()<m_dMin){
            this->setText(QString::number(m_dMin));
        }
    }
    QLineEdit::focusOutEvent(event);
}

void CCtrlLineEdit::mouseReleaseEvent(QMouseEvent *event)
{
    selectAll();
    QLineEdit::mouseReleaseEvent(event);
}

void CCtrlLineEdit::mousePressEvent(QMouseEvent *event)
{
    QLineEdit::mousePressEvent(event);
    selectAll();
    toggleKeyboard();
}



void CCtrlLineEdit::ShowKeyboard()
{
    if (CDlgKeyboard::GetInstance().isHidden()) {
        CDlgKeyboard::GetInstance().ShowPos(!m_bNumOnly);
        setFocus();
    }
    CDlgKeyboard::GetInstance().MoveToPos(this);
}

void CCtrlLineEdit::HideKeyboard()
{
    CDlgKeyboard::GetInstance().hide();
    CDlgKeyboard::GetInstance().hide();
}

void CCtrlLineEdit::toggleKeyboard()
{
    /*bool bEnable=m_gSystem.m_Variants.get_VData(Var_AllSystem_EnableSoftKeyboard_ID)
            ->toBool();*/
    bool bEnable = true;
    if(CDlgKeyboard::GetInstance().getEnabled())
    {
        if(CDlgKeyboard::GetInstance().m_bEnterClick)
        {
            HideKeyboard();//m_gFoucsLineEdit=0;
            CDlgKeyboard::GetInstance().m_bEnterClick=false;
           // MqDebug()<<"Soft KeyBoard Hide";
        }
        else
        {

            ShowKeyboard();
           // m_gFoucsLineEdit=this;
           // MqDebug()<<"Soft KeyBoard Show";
        }
    }
}

void CCtrlLineEdit::SetRange(double dMin, double dMax, bool bDouble)
{
    m_bNumOnly=true;
//    setValidator(&n_NumValidator);

    if(bDouble)
    {
//        m_DValidator.setNotation(QDoubleValidator::StandardNotation);
//        m_DValidator.setRange(dMin,dMax,6);
     //   setValidator(&m_DValidator);
    }
    else
    {
//        m_IValidator.setRange((int)dMin,(int)dMax);
     //   setValidator(&m_IValidator);
    }

    m_dMax=dMax;
    m_dMin=dMin;
    m_bDouble=bDouble;
}


void CCtrlLineEdit::FreshBKColor(const QString &str)
{
    if(m_bNumOnly)
    {
        m_bSetRed=false;
        if(m_bDouble)
        {
            double dData=str.toDouble();
            if(dData>m_dMax||dData<m_dMin)
            {
                m_bSetRed=true;
            }
        }
        else
        {
            int iData=str.toInt();
            if(iData>(int)m_dMax||iData<(int)m_dMin)
            {
                m_bSetRed=true;
            }
        }
        if(m_bSetRed)
        {
             setStyleSheet("background:rgb(255, 0, 0);color:rgb(0, 255, 255);");
        }
        else
        {
             setStyleSheet("background:rgb(255, 255, 255);color:#000;");
        }
    }
}


void CCtrlLineEdit::hideEvent(QHideEvent *)
{
    HideKeyboard();
}

void CCtrlLineEdit::showEvent(QShowEvent *)
{
    FreshBKColor(text());
}


