/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2014 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/keychooser/cscrollerwidgetset.h"

#include <QString>
#include <QToolButton>
#include <QVBoxLayout>
#include <QWheelEvent>
#include "frontend/keychooser/cscrollbutton.h"


#define WIDTH (static_cast<unsigned int>(16))
#define ARROW_HEIGHT (static_cast<unsigned int>(12))
#define MOVER_HEIGHT (static_cast<unsigned int>(6))


CScrollerWidgetSet::CScrollerWidgetSet(QWidget * parent)
    : QWidget(parent)
{
    m_layout = new QVBoxLayout(this);
    m_layout->setSpacing(0);
    m_layout->setContentsMargins(0, 0, 0, 0);
    m_layout->setAlignment(this, Qt::AlignHCenter | Qt::AlignCenter);

    m_buttonUp = new QToolButton(this);
    m_buttonUp->setArrowType(Qt::UpArrow);

    m_buttonUp->setFixedSize(WIDTH, ARROW_HEIGHT);
    m_buttonUp->setFocusPolicy(Qt::NoFocus);
    m_buttonUp->setAutoRaise(true);

    m_scrollButton = new CScrollButton(this);
    m_scrollButton->setFixedSize(WIDTH, MOVER_HEIGHT);
    m_scrollButton->setFocusPolicy(Qt::NoFocus);

    m_buttonDown = new QToolButton(this);
    m_buttonDown->setArrowType(Qt::DownArrow);
    m_buttonDown->setFixedSize(WIDTH, ARROW_HEIGHT);
    m_buttonDown->setFocusPolicy(Qt::NoFocus);
    m_buttonDown->setAutoRaise(true);

    m_layout->addWidget(m_buttonUp, 0);
    m_layout->addWidget(m_scrollButton, 0);
    m_layout->addWidget(m_buttonDown, 0);
    setMinimumWidth(WIDTH); // Kludge to add some spacing but seems to work.

    connect(m_scrollButton, SIGNAL(lock()),                SLOT(slotLock()));
    connect(m_scrollButton, SIGNAL(unlock()),              SLOT(slotUnlock()));
    connect(m_scrollButton, SIGNAL(change_requested(int)), SLOT(slotScroller(int)));
    connect(m_buttonUp,     SIGNAL(clicked()),             SLOT(slotUpClick()));
    connect(m_buttonDown,   SIGNAL(clicked()),             SLOT(slotDownClick()));
}

/** Sets the tooltips for the given entries using the parameters as text. */
void CScrollerWidgetSet::setToolTips(const QString & nextEntryTip,
                                     const QString & scrollButtonTip,
                                     const QString & previousEntryTip)
{
    m_scrollButton->setToolTip(scrollButtonTip);
    m_buttonDown->setToolTip(nextEntryTip);
    m_buttonUp->setToolTip(previousEntryTip);
}


void CScrollerWidgetSet::wheelEvent(QWheelEvent * e) {
    /**
    * The problem is, that wheel events do everytime have the delta value 120
    */
    const int vchange = ((e->delta() > 0) ? -1 : 1);

    if (vchange != 0) { // Do not emit a change with value 0
        emit change(vchange);
        e->accept();
    } else {
        e->ignore();
    }
}

void CScrollerWidgetSet::slotLock() {
    emit scroller_pressed();
}

void CScrollerWidgetSet::slotUnlock() {
    emit scroller_released();
}

void CScrollerWidgetSet::slotScroller(int n) {
    emit change(n);
}

void CScrollerWidgetSet::slotUpClick() {
    slotScroller(-1);
}

void CScrollerWidgetSet::slotDownClick() {
    slotScroller(1);
}
