#include "stextedit.h"

#include <QKeyEvent>
#include <QMouseEvent>
#include <QInputMethodEvent>
#include <QDebug>
#include <QThread>

STextEdit::STextEdit(QWidget *parent) : QPlainTextEdit(parent)
{

}

STextEdit::~STextEdit()
{

}


void STextEdit::keyPressEvent(QKeyEvent *e)
{
    int nKey = e->key();

    qDebug() << "KeyDown: " << nKey << "Type: " << e->type();

    //65293 : Enter
    //65288 : Backspace
    //65361 : Left
    //65363 : Right
    //65362 : Up
    //65364 : Down
    //65535 : Delete
    //65360 : Home
    //65367 : End

    //16777251 : 한글키


    if(nKey != 16777220 && nKey != 16777221 && nKey!=65293)
    {
        if(nKey==65288)
        {
            Qt::KeyboardModifier m;
            QKeyEvent newEvent(e->type(), Qt::Key_Backspace, e->modifiers());
            QPlainTextEdit::keyPressEvent(&newEvent);
            return;
        }
        else if(nKey==65289)
        {
            Qt::KeyboardModifier m;
            QKeyEvent newEvent(e->type(), Qt::Key_Tab, e->modifiers());
            QPlainTextEdit::keyPressEvent(&newEvent);
            focusNextPrevChild(true);
            return;
        }
        else if(nKey==65361)
        {
            Qt::KeyboardModifier m;
            QKeyEvent newEvent(e->type(), Qt::Key_Left, e->modifiers());
            QPlainTextEdit::keyPressEvent(&newEvent);
            return;
        }
        else if(nKey==65363)
        {
            Qt::KeyboardModifier m;
            QKeyEvent newEvent(e->type(), Qt::Key_Right, e->modifiers());
            QPlainTextEdit::keyPressEvent(&newEvent);
            return;
        }
        else if(nKey==65362)
        {
            Qt::KeyboardModifier m;
            QKeyEvent newEvent(e->type(), Qt::Key_Up, e->modifiers());
            QPlainTextEdit::keyPressEvent(&newEvent);
            return;
        }
        else if(nKey==65364)
        {
            Qt::KeyboardModifier m;
            QKeyEvent newEvent(e->type(), Qt::Key_Down, e->modifiers());
            QPlainTextEdit::keyPressEvent(&newEvent);
            return;
        }
        else if(nKey==65360)
        {
            Qt::KeyboardModifier m;
            QKeyEvent newEvent(e->type(), Qt::Key_Home, e->modifiers());
            QPlainTextEdit::keyPressEvent(&newEvent);
            return;
        }
        else if(nKey==65367)
        {
            Qt::KeyboardModifier m;
            QKeyEvent newEvent(e->type(), Qt::Key_End, e->modifiers());
            QPlainTextEdit::keyPressEvent(&newEvent);
            return;
        }
        else if(nKey==65535)
        {
            Qt::KeyboardModifier m;
            //Key_Hangul
            /*
            QKeyEvent newEvent(e->type(), Qt::Key_Hangul_End, e->modifiers());
            QPlainTextEdit::keyPressEvent(&newEvent);
            QKeyEvent newEventUp(e->type(), Qt::Key_Alt, e->modifiers());
            QPlainTextEdit::keyReleaseEvent(&newEventUp);
            */

            QKeyEvent newEventDeleteDown(e->type(), Qt::Key_Delete, m);
            QPlainTextEdit::keyPressEvent(&newEventDeleteDown);
            QThread::msleep(40);
            QPlainTextEdit::keyReleaseEvent(&newEventDeleteDown);

            return;

        }

        QPlainTextEdit::keyPressEvent(e);
    }


}

void STextEdit::keyReleaseEvent(QKeyEvent *e)
{
    int nKey = e->key();
    if(nKey != 16777220 && nKey != 16777221)
        {
        QPlainTextEdit::keyReleaseEvent(e);
    }

    if(nKey==Qt::Key_F10)
    {
        parent()->event(e);
    }

    qDebug() << "KeyRelease: " << nKey;
}

void STextEdit::mousePressEvent(QMouseEvent *e)
{
    qDebug() << "MousePress: " << e->type();
    QPlainTextEdit::mousePressEvent(e);
}

void STextEdit::mouseMoveEvent(QMouseEvent *e)
{
    qDebug() << "MousePress: " << e->type();
    //QPlainTextEdit::mouseMoveEvent(e);
}

void STextEdit::mouseReleaseEvent(QMouseEvent *e)
{
    qDebug() << "MousePress: " << e->type();
    QPlainTextEdit::mouseReleaseEvent(e);
}


void STextEdit::inputMethodEvent(QInputMethodEvent *pEvent)
{
    qDebug() << "inputMehodEvent: " << pEvent->type();
    QPlainTextEdit::inputMethodEvent(pEvent);
}
