/***************************************************************************
 *   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 "simplecolorpick.h"

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

SimpleColorPick::SimpleColorPick(QColor color, QWidget *parent)
 : QWidget(parent)
{
    setColor(color);
    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()));
}

SimpleColorPick::~SimpleColorPick()
{
}

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

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

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

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

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

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

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

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

void SimpleColorPick::emitPicked()
{
    emit(picked(this->color()));
}

void SimpleColorPick::paintEvent(QPaintEvent * /*event*/)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    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_color);
    QRect rect(m_borderWidth / 2, m_borderWidth / 2,
                     this->width() - m_borderWidth,
                     this->height() - m_borderWidth);

    painter.drawRect(rect);
}

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

QSize SimpleColorPick::minimumSizeHint() const
{
    return QSize(10, 10);
}

#include "simplecolorpick.moc"
