/***************************************************************************
 *   Copyright 2007-2008 Wouter Haffmans <cheetah@simply-life.net>         *
 *                                                                         *
 *   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) version 3 or any later version        *
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 *   defined in Section 14 of version 3 of the license.                    *
 *                                                                         *
 *   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, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "simplewidthpick.h"

#include <QColor>
#include <QPainter>
#include <QTimer>

SimpleWidthPick::SimpleWidthPick(int lineWidth, QWidget *parent)
 : QWidget(parent)
{
    setLineWidth(lineWidth);
    setColor(Qt::black);
    setBackgroundColor(Qt::white);
    setBorderColor(Qt::transparent);
    setBorderWidth(0);

    QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    policy.setHeightForWidth(true);
    setSizePolicy(policy);

    // Configure timer
    m_pickTimer = new QTimer(this);
    m_pickTimer->setSingleShot(true);
    m_pickTimer->setInterval(100);
    connect(m_pickTimer, SIGNAL(timeout()), this, SLOT(emitPicked()));
}

SimpleWidthPick::~SimpleWidthPick()
{
}

int SimpleWidthPick::lineWidth() const
{
    return m_lineWidth;
}

void SimpleWidthPick::setLineWidth(int lineWidth)
{
    m_lineWidth = lineWidth;
    update();
}

QColor SimpleWidthPick::color() const
{
    return m_color;
}

void SimpleWidthPick::setColor(QColor color)
{
    m_color = color;
    update();
}

QColor SimpleWidthPick::backgroundColor() const
{
    return m_backgroundColor;
}

void SimpleWidthPick::setBackgroundColor(QColor backgroundColor)
{
    m_backgroundColor = backgroundColor;
    update();
}

QColor SimpleWidthPick::borderColor() const
{
    return m_borderColor;
}

void SimpleWidthPick::setBorderColor(QColor borderColor)
{
    m_borderColor = borderColor;
    update();
}

int SimpleWidthPick::borderWidth() const
{
    return m_borderWidth;
}

void SimpleWidthPick::setBorderWidth(int borderWidth)
{
    if (borderWidth >= 0) {
        m_borderWidth = borderWidth;
        update();
    }
}

void SimpleWidthPick::enterEvent(QEvent * /*event*/)
{
    m_pickTimer->start();
}

void SimpleWidthPick::leaveEvent(QEvent * /*event */)
{
    if (m_pickTimer->isActive()) {
        m_pickTimer->stop();
    }
}

void SimpleWidthPick::emitPicked()
{
    emit(picked(lineWidth()));
}

void SimpleWidthPick::paintEvent(QPaintEvent * /*event*/)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // Draw background and border
    QPen pen(m_borderColor, m_borderWidth, Qt::SolidLine, Qt::SquareCap,
             Qt::MiterJoin);
    if (m_borderWidth <= 0) {
        pen.setStyle(Qt::NoPen);
    }
    painter.setPen(pen);
    painter.setBrush(m_backgroundColor);
    QRect rect(m_borderWidth / 2, m_borderWidth / 2,
                     this->width() - m_borderWidth,
                     this->height() - m_borderWidth);

    painter.drawRect(rect);

    // Draw line
    pen.setBrush(m_color);
    pen.setWidth(m_lineWidth);
    pen.setCapStyle(Qt::RoundCap);
    if (m_lineWidth > 0) {
        pen.setStyle(Qt::SolidLine);
    }
    else {
        pen.setStyle(Qt::NoPen);
    }
    painter.setPen(pen);
    painter.drawLine(width() / 4, height() / 2,
                     (width() * 3) / 4, height() / 2);
}

QSize SimpleWidthPick::sizeHint() const
{
    return QSize(100, 100);
}

QSize SimpleWidthPick::minimumSizeHint() const
{
    return QSize(50, 50);
}

#include "simplewidthpick.moc"
