#include <QMessageBox>
#include <QMoveEvent>
#include <QDesktopWidget>
#include "numpad.h"

Numpad::Numpad(QString caption, QString min, QString max, QString value, QWidget *parent) :
    QDialog(parent)
{
    init();
    ui.leMax->setText(max);
    ui.leMin->setText(min);
    ui.leValue->setText(value);
    ui.leValue->selectAll();
    setWindowTitle(caption);
}

Numpad::Numpad(QString caption, int min, int max, int value, QWidget *parent) :
    QDialog(parent)
{
    init();
    ui.leMax->setText(QString::number(max));
    ui.leMin->setText(QString::number(min));
    ui.leValue->setText(QString::number(value));
    ui.leValue->selectAll();
    setWindowTitle(caption);
}

Numpad::Numpad(QString caption, double min, double max, double value, QWidget *parent) :
    QDialog(parent)
{
    init();
    ui.leMax->setText(QString::number(max));
    ui.leMin->setText(QString::number(min));
    ui.leValue->setText(QString::number(value));
    ui.leValue->selectAll();
    setWindowTitle(caption);
}

void Numpad::init()
{
    m_bClearSelection=false;
    m_bHasSelectedText=false;
    m_actValue=QString();
    ui.setupUi(this);

    //setze Window Flags
    Qt::WindowFlags flags = windowFlags();
    flags |= Qt::MSWindowsFixedSizeDialogHint;
    flags &= ~Qt::WindowMaximizeButtonHint;
    flags &= ~Qt::WindowMinimizeButtonHint;
    flags &= ~Qt::WindowContextHelpButtonHint;
    flags &= ~Qt::WindowSystemMenuHint;
    setWindowFlags(flags);

    QList<QPushButton *> allPButtons = findChildren<QPushButton *>();
    for (int i=0; i<allPButtons.size(); ++i)
        connect(allPButtons.at(i), SIGNAL(clicked()), this, SLOT(slotClicked()));
}

void Numpad::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui.retranslateUi(this);
        break;
    default:
        break;
    }
}

void Numpad::slotClicked()
{
    if (QObject::sender()->objectName()== ui.btnKey_OK->objectName())
    {
        double valueTmp = ui.leValue->text().toDouble();
        double min = ui.leMin->text().toDouble();
        double max = ui.leMax->text().toDouble();
        if((valueTmp<=max && valueTmp>=min)
                || (ui.leMin->text().isEmpty() && ui.leMax->text().isEmpty())
                || (!min && !max))
        {
            m_actValue=ui.leValue->text();
            value(m_actValue);
            m_bClearSelection=false;
            accept();
        }
        else
        {
            if (!ui.leMin->text().isEmpty() && valueTmp<min){
                QMessageBox::critical(this,
                                      tr("Fehler"),
                                      tr("Der Eingegebene Wert: <b>\"%1\"</b> ist kleiner\n"
                                         "als das vorgegebene Minimum: <b>\"%2\"</b>!")
                                      .arg(ui.leValue->text())
                                      .arg(ui.leMin->text()));
            }else if (!ui.leMax->text().isEmpty() && valueTmp>max){
                QMessageBox::critical(this,
                                      tr("Fehler"),
                                      tr("Der Eingegebene Wert: <b>\"%1\"</b> ist groesser\n"
                                         "als das vorgegebene Maximum: <b>\"%2\"</b!")
                                      .arg(ui.leValue->text())
                                      .arg(ui.leMax->text()));
            }
            return;
        }
    }
    else if (QObject::sender()->objectName()== ui.btnKey_ESC->objectName())
    {
        reject();
    }
    else if (QObject::sender()->objectName()== ui.btnKey_CLEAR->objectName())
    {
        ui.leValue->setText("");
        return;
    }
    else if (QObject::sender()->objectName()== ui.btnKey_minus->objectName())
    {
        QString tmp=ui.leValue->text();

        if ((tmp.size()>0) && (tmp.at(0)==QChar('-')))
            tmp.remove(0, 1);
        else
            tmp.insert(0, "-");
        ui.leValue->setText(tmp);
        return;
    }
    else if (QObject::sender()->objectName()== ui.btnKey_BACK->objectName())
    {
        QString tmp=ui.leValue->text();
        tmp=tmp.remove(tmp.size()-1, 1);
        ui.leValue->setText(tmp);
        return;
    }
    else if (QObject::sender()->objectName()== ui.btnKey_dot->objectName())
    {
        if (ui.leValue->text().contains('.'))
            return;
    }
    if(!m_bClearSelection && !m_bHasSelectedText)
    {
        ui.leValue->clear();
        m_bClearSelection=true;
    }
    QPushButton *btn=qobject_cast<QPushButton*>(QObject::sender());
    ui.leValue->setText(ui.leValue->text()+btn->text());
}

QString Numpad::value()
{
    return m_actValue;
}

int Numpad::valueInt()
{
    bool ok;
    int result = m_actValue.toInt(&ok);
    if (ok)
        return result;
    else
        return 0;
}

double Numpad::valueDouble()
{
    bool ok;
    double result = m_actValue.toDouble(&ok);
    if (ok)
        return result;
    else
        return 0;
}

void Numpad::moveEvent(QMoveEvent *event)
{
    QDialog::moveEvent(event);
    if(event->pos().y()<0)
        move(event->pos().x(), 0);

    if(event->pos().y()>(QApplication::desktop()->height()-(height()/2)))
        move(event->pos().x(), QApplication::desktop()->height() - (height()/2));

    if(event->pos().x()<0)
        move(0, event->pos().y());

    if(event->pos().x()>(QApplication::desktop()->width()-(width()/2)))
        move(QApplication::desktop()->width()-(width()/2), event->pos().y() );
}

