/***************************************************************************
 *   Copyright (C) 2007 by Arnold Kraschinski                              *
 *   arnold.kra67@gmx.de                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

#include "charbuttonsh.h"

// Qt header files
#include <QWidget>
#include <QPainter>
#include <QSvgRenderer>
#include <QSizePolicy>
#include <QMouseEvent>
#include <QPaintEvent>

// KDE header files
#include <kdebug.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kglobalsettings.h>

// Project header files
#include "trainer.h"

const double CharButtonsH::svgWidth = 800.0;
const double CharButtonsH::svgHeight = 170.0;

const double CharButtonsH::buttonWidth = 65.0;
const double CharButtonsH::buttonHeight = 73.0;
const double CharButtonsH::buttonScaleX = buttonWidth / svgWidth;
const double CharButtonsH::buttonScaleY = buttonHeight / svgHeight;

// Position of the upper left corner of the button viewed from the upper left corner
const double CharButtonsH::xPosN = 43.552;
const double CharButtonsH::xPosA = 171.689;
const double CharButtonsH::xPosE = 299.826;
const double CharButtonsH::xPosI = 427.964;
const double CharButtonsH::xPosO = 556.101;
const double CharButtonsH::xPosU = 684.238;

const double CharButtonsH::yPos = 48.500;



CharButtonsH::CharButtonsH(QWidget* parent)
: QWidget(parent), m_overN(false), m_overA(false), m_overE(false),
  m_overI(false), m_overO(false), m_overU(false)
{
    setAttribute(Qt::WA_StaticContents);
    m_renderer = new QSvgRenderer(KStandardDirs::locate("appdata", "images/buttons_h.svg"));

    setMouseTracking(true);

    setFixedSize(300, 40);
    setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
}

void CharButtonsH::paintEvent(QPaintEvent *)
{
    //m_yRatio = (double)height() / svgHeight;
    //m_xRatio = m_yRatio;

    int buttonsWidth = int ( svgWidth / svgHeight * height() );
    QPixmap buf(buttonsWidth, height() );
    QPainter p(&buf);
    p.setRenderHint(QPainter::Antialiasing);

    p.scale(1, 1);
    m_renderer->render(&p, "background");
    p.resetMatrix();

    double y = yPos * m_yRatio;
    if (m_overN) {
        p.translate(xPosN * m_xRatio, y );
        p.scale(buttonScaleX, buttonScaleY );
        m_renderer->render(&p, "letterN");
        p.resetMatrix();
    }

    if (m_overA) {
        p.translate(xPosA * m_xRatio, y );
        p.scale(buttonScaleX, buttonScaleY );
        m_renderer->render(&p, "letterA");
        p.resetMatrix();
    }

    if (m_overE) {
        p.translate(xPosE * m_xRatio, y );
        p.scale(buttonScaleX, buttonScaleY );
        m_renderer->render(&p, "letterE");
        p.resetMatrix();
    }

    if (m_overI) {
        p.translate(xPosI * m_xRatio, y );
        p.scale(buttonScaleX, buttonScaleY );
        m_renderer->render(&p, "letterI");
        p.resetMatrix();
    }

    if (m_overO) {
        p.translate(xPosO * m_xRatio, y );
        p.scale(buttonScaleX, buttonScaleY );
        m_renderer->render(&p, "letterO");
        p.resetMatrix();
    }

    if (m_overU) {
        p.translate(xPosU * m_xRatio, y );
        p.scale(buttonScaleX, buttonScaleY );
        m_renderer->render(&p, "letterU");
        p.resetMatrix();
    }

    QPainter p2(this);
    p2.drawPixmap(0, 0, buf);
}

void CharButtonsH::resizeEvent(QResizeEvent *)
{
    m_yRatio = (double)height() / svgHeight;
    m_xRatio = m_yRatio;

    double w = buttonWidth * m_xRatio;
    double h = buttonHeight * m_yRatio;
    double y = yPos * m_yRatio;
    double x = xPosN * m_xRatio;
    m_nRect = QRect(int(x), int(y), int(w), int(h));
    x = xPosA * m_xRatio;
    m_aRect = QRect(int(x), int(y), int(w), int(h));
    x = xPosE * m_xRatio;
    m_eRect = QRect(int(x), int(y), int(w), int(h));
    x = xPosI * m_xRatio;
    m_iRect = QRect(int(x), int(y), int(w), int(h));
    x = xPosO * m_xRatio;
    m_oRect = QRect(int(x), int(y), int(w), int(h));
    x = xPosU * m_xRatio;
    m_uRect = QRect(int(x), int(y), int(w), int(h));

    update();
}

/** sets or clears flag based on whether p is in rect, and set changed if flag changes */
void CharButtonsH::CheckRect(const QRect &rect, const QPoint &p, bool &flag, bool &changed)
{
    bool inside = rect.contains(p);
    changed = (!inside && flag) || (!flag && inside) || changed;
    flag = inside;
}

void CharButtonsH::mouseMoveEvent(QMouseEvent *e)
{
    QPoint p = e->pos();
    bool haveToUpdate = false;

    CheckRect(m_nRect, p, m_overN, haveToUpdate);
    CheckRect(m_aRect, p, m_overA, haveToUpdate);
    CheckRect(m_eRect, p, m_overE, haveToUpdate);
    CheckRect(m_iRect, p, m_overI, haveToUpdate);
    CheckRect(m_oRect, p, m_overO, haveToUpdate);
    CheckRect(m_uRect, p, m_overU, haveToUpdate);

    if(m_overN || m_overA || m_overE || m_overI || m_overO || m_overU )
    {
        this->setCursor(Qt::PointingHandCursor);
    }
    else {
        this->unsetCursor();
    }

    if (haveToUpdate) {
        update();
    }
}

void CharButtonsH::mousePressEvent(QMouseEvent *e)
{
    QString ch = "";

    if(m_nRect.contains(e->pos())) {
        ch = QString::fromUtf8("ñ");
    }

    if(m_aRect.contains(e->pos())) {
        ch = QString::fromUtf8("á");
    }

    if(m_eRect.contains(e->pos())) {
        ch = QString::fromUtf8("é");
    }

    if(m_iRect.contains(e->pos())) {
        ch = QString::fromUtf8("í");
    }

    if (m_oRect.contains(e->pos())) {
        ch = QString::fromUtf8("ó");
    }

    if(m_uRect.contains(e->pos())) {
        ch = QString::fromUtf8("ú");
    }
    if ( ch != "")
        emit signalChar(ch);
}

void CharButtonsH::connectButtons(Trainer* trainer)
{
    connect( this, SIGNAL( signalChar(QString) ), trainer, SLOT( slotChar(QString) ) );
}


#include "charbuttonsh.moc"
