#include "cdlgkeyboard.h"
#include "ui_cdlgkeyboard.h"
#include "cctrllineedit.h"
#include <QKeyEvent>
#include <QCoreApplication>
#include <QEvent>
CDlgKeyboard::CDlgKeyboard(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CDlgKeyboard)
{
    ui->setupUi(this);
    ui->horizontalLayout->setSizeConstraint(QLayout::SetFixedSize);
    m_bEnterClick=false;
    setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
    m_bEnabled = false;
    m_MainWindow = nullptr;
}

CDlgKeyboard& CDlgKeyboard::GetInstance()
{
    static CDlgKeyboard m_instance;
    return m_instance;
}

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

void CDlgKeyboard::showEvent(QShowEvent *event)
{
    QDialog::showEvent(event);
}

void CDlgKeyboard::hideEvent(QHideEvent *event)
{
    QDialog::hideEvent(event);
}

void CDlgKeyboard::MoveToPos(QWidget *pWidget)
{
    if(pWidget)
    {
        QRect rect= pWidget->geometry();
        QPoint GlobalPoint(pWidget->mapToGlobal(QPoint(0, rect.height())));
        QRect rect1= frameGeometry();
        QRect rect2= normalGeometry();
        int iTitleHeight=rect1.height()-rect2.height();
        QRect rect3=QRect(GlobalPoint.x(),GlobalPoint.y()+iTitleHeight,
                          rect2.width(),rect2.height());
        Q_ASSERT(m_MainWindow&&"keyboard mainform not setting");
        QRect rect4= m_MainWindow->frameGeometry();
        if(rect3.bottom()>rect4.bottom())
        {
            int top=rect3.top();
            rect3.moveTop(top-rect.height()-rect1.height());
        }
        if(rect3.right()>rect4.right())
        {
            int left=rect3.left()-(rect3.width()-rect.width());
            rect3.moveLeft(left);
        }
        setGeometry(rect3);

    }
}

void CDlgKeyboard::done(int)
{
    hide();
    m_bEnterClick=true;
}

void CDlgKeyboard::MSendEvent(int key,const QString& text)
{
    if(m_gFoucsLineEdit)
    {
    QKeyEvent tabKey(QEvent::KeyPress, key, Qt::NoModifier,text);
    QCoreApplication::sendEvent(m_gFoucsLineEdit, &tabKey);
    QApplication::beep();
    }
}

void CDlgKeyboard::ShowPos(bool abc)
{
    ui->widget_ABC->setVisible(abc);
    show();
    m_babc=abc;
}


bool CDlgKeyboard::getEnabled()
{
    return m_bEnabled;
}

void CDlgKeyboard::SetMainWindow(QWidget* window)
{
    static_assert(true, "must set main window instance at first");
    m_MainWindow = window;
    if (nullptr != window) {
        this->setParent(window);
    }
}

void CDlgKeyboard::setEnabled(bool bEnabled)
{
    m_bEnabled = bEnabled;
}

void CDlgKeyboard::on_pushButton_0_clicked() { MSendEvent(Qt::Key_0,"0"); }
void CDlgKeyboard::on_pushButton_1_clicked(){ MSendEvent(Qt::Key_1,"1"); }
void CDlgKeyboard::on_pushButton_2_clicked(){ MSendEvent(Qt::Key_2,"2"); }
void CDlgKeyboard::on_pushButton_3_clicked(){ MSendEvent(Qt::Key_3,"3"); }
void CDlgKeyboard::on_pushButton_4_clicked(){ MSendEvent(Qt::Key_4,"4"); }
void CDlgKeyboard::on_pushButton_5_clicked(){ MSendEvent(Qt::Key_5,"5"); }
void CDlgKeyboard::on_pushButton_6_clicked(){ MSendEvent(Qt::Key_6,"6"); }
void CDlgKeyboard::on_pushButton_7_clicked(){ MSendEvent(Qt::Key_7,"7"); }
void CDlgKeyboard::on_pushButton_8_clicked(){ MSendEvent(Qt::Key_8,"8"); }
void CDlgKeyboard::on_pushButton_9_clicked(){ MSendEvent(Qt::Key_9,"9"); }
void CDlgKeyboard::on_pushButton_Point_clicked(){ MSendEvent(Qt::Key_Greater,"."); }
void CDlgKeyboard::on_pushButton_Sub_clicked(){ MSendEvent(Qt::Key_Minus,"-"); }


void CDlgKeyboard::on_pushButton_Enter_clicked()
{
    if(m_gFoucsLineEdit)
    {
      hide();
      m_bEnterClick=true;
    }
}

void CDlgKeyboard::on_pushButton_ABCEnter_clicked()
{
    on_pushButton_Enter_clicked();
}

void CDlgKeyboard::on_pushButton_Clear_clicked()
{
    if(m_gFoucsLineEdit)
    {
       m_gFoucsLineEdit->clear();
       if(m_babc==false)
       {
           MSendEvent(Qt::Key_0,"0");
           m_gFoucsLineEdit->selectAll();
       }
    }
}

//void CDlgKeyboard::on_pushButton_Extra_clicked()
//{
//    if(ui->widget_ABC->isHidden())
//    {
//        ui->widget_ABC->show();
//    }
//    else
//    {
//        ui->widget_ABC->hide();
//    }
//}
void CDlgKeyboard::on_pushButton_11W_clicked(){MSendEvent(Qt::Key_W,"W");}
void CDlgKeyboard::on_pushButton_10Q_clicked(){MSendEvent(Qt::Key_Q,"Q");}
void CDlgKeyboard::on_pushButton_19P_clicked(){MSendEvent(Qt::Key_P,"P");}
void CDlgKeyboard::on_pushButton_15Y_clicked(){MSendEvent(Qt::Key_Y,"Y");}
void CDlgKeyboard::on_pushButton_15Z_clicked(){MSendEvent(Qt::Key_Z,"Z");}
void CDlgKeyboard::on_pushButton_14T_clicked(){MSendEvent(Qt::Key_T,"T");}
void CDlgKeyboard::on_pushButton_16U_clicked(){MSendEvent(Qt::Key_U,"U");}
void CDlgKeyboard::on_pushButton_30X_clicked(){MSendEvent(Qt::Key_X,"X");}
void CDlgKeyboard::on_pushButton_32V_clicked(){MSendEvent(Qt::Key_V,"V");}
void CDlgKeyboard::on_pushButton_21S_clicked(){MSendEvent(Qt::Key_S,"S");}
void CDlgKeyboard::on_pushButton_18O_clicked(){MSendEvent(Qt::Key_O,"O");}
void CDlgKeyboard::on_pushButton_13R_clicked(){MSendEvent(Qt::Key_R,"R");}
void CDlgKeyboard::on_pushButton_22D_clicked(){MSendEvent(Qt::Key_D,"D");}
void CDlgKeyboard::on_pushButton_17I_clicked(){MSendEvent(Qt::Key_I,"I");}
void CDlgKeyboard::on_pushButton_31C_clicked(){MSendEvent(Qt::Key_C,"C");}
void CDlgKeyboard::on_pushButton_12E_clicked(){MSendEvent(Qt::Key_E,"E");}
void CDlgKeyboard::on_pushButton_20A_clicked(){MSendEvent(Qt::Key_A,"A");}
void CDlgKeyboard::on_pushButton_33B_clicked(){MSendEvent(Qt::Key_B,"B");}
void CDlgKeyboard::on_pushButton_26J_clicked(){MSendEvent(Qt::Key_J,"J");}
void CDlgKeyboard::on_pushButton_23F_clicked(){MSendEvent(Qt::Key_F,"F");}
void CDlgKeyboard::on_pushButton_25H_clicked(){MSendEvent(Qt::Key_H,"H");}
void CDlgKeyboard::on_pushButton_24G_clicked(){MSendEvent(Qt::Key_G,"G");}
void CDlgKeyboard::on_pushButton_27K_clicked(){MSendEvent(Qt::Key_K,"K");}
void CDlgKeyboard::on_pushButton_28L_clicked(){MSendEvent(Qt::Key_L,"L");}
void CDlgKeyboard::on_pushButton_35M_clicked(){MSendEvent(Qt::Key_M,"M");}
void CDlgKeyboard::on_pushButton_34N_clicked(){MSendEvent(Qt::Key_N,"N");}

