/***************************************************************************
 *   Copyright (c) 2009  Peter Mylemans <peter.mylemans@gmail.com>         *
 *                                                                         *
 *   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 "SideBarButton.h"
#include <QStylePainter>
#include <QStyleOptionButton>

/*!
    \class SideBarButton

    The SideBarButton class represents a button of SideBar.

    \sa SideBar
*/

/*!
    Construct a new sidebar button and associates the widget \a associated.

    \pre \a associated can't be NULL.
*/
SideBarButton::SideBarButton(QWidget *parent, QWidget *associated)
    : QPushButton(parent)
{
    Q_ASSERT(associated != NULL);

    setCheckable(true);
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
    m_associatedWidget = associated;
}

/*!
    \reimp
*/
void SideBarButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    // Draw control offscreen
    QPixmap buffer(height(), width());
    buffer.fill(this, 0, 0); // fill background

    QStylePainter p(&buffer, this);
    QStyleOptionButton option;
    initStyleOption(&option);
    if (isChecked()) {
        // Use has focus style for checked.
        option.state |= QStyle::State_HasFocus;
        option.state &= ~QStyle::State_On;
    } else {
        option.state &= ~QStyle::State_HasFocus;
    }
    option.rect.setWidth(height());
    option.rect.setHeight(width());
    p.drawControl(QStyle::CE_PushButton, option);

    // Rotate offscreen image and display control.
    QPainter dp(this);
    QTransform tr;
    tr.translate(0, height());
    tr.rotate(-90);
    dp.setTransform(tr);
    dp.drawPixmap(0, 0, buffer);
}

/*!
    \reimp
*/
QSize SideBarButton::sizeHint() const
{
    QSize size = QPushButton::sizeHint();
    return QSize(size.height(), size.width());
}
