/*
Copyright (c) 2007 Matt Williams matt@milliams.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) version 3 or, at the discretion of KDE e.V. (which shall
act as a proxy as in section 14 of the GPLv3), 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, see <http://www.gnu.org/licenses/>.
*/

#include <block.h>

#include <QDebug>

#include <QPainter>
#include <QGraphicsLinearLayout>
#include <KColorScheme>

Block::Block()
{
    setLayout(new QGraphicsLinearLayout(Qt::Vertical));
    
    setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum, QSizePolicy::DefaultType));
    
    QRectF rect = QRectF(QPoint(0, 0), sizeHint(Qt::PreferredSize));
    setGeometry(rect);
    updateGeometry();
}

void Block::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
    QGraphicsWidget::paint(painter, option, widget);
    
    qDebug() << "paint() pos()" << pos();
    /*qDebug() << "paint() mapFromParent(pos())" << mapFromParent(pos());
    qDebug() << "paint() mapFromScene(pos())" << mapFromScene(pos());
    qDebug() << "paint() mapToParent(pos())" << mapToParent(pos());
    qDebug() << "paint() mapToScene(pos())" << mapToScene(pos());*/

    KColorScheme colorScheme(QPalette::Active);
    /*qDebug() << colorScheme.background(KColorScheme::NegativeBackground).color();
    qDebug() << colorScheme.foreground().color();*/

    //QTransform initialTransform = painter->transform(); //Should be (0,0) in item coords
    painter->save();
    
    //BEGIN Title Text
    QRect textRect = painter->fontMetrics().tightBoundingRect(mTitle);
    int padd = 4;
    mTextHeight = textRect.width()+(2*padd);
    mTextWidth = textRect.height()+(2*padd);
    //END Title Text
    
    //BEGIN Boxes
    painter->setBrush(colorScheme.background());
    painter->restore();
    painter->drawRect(0, 0, mTextWidth, mTextHeight); //Text box
    //painter->drawRect(textWidth, 0, size().width() - textWidth, qMax(layout()->preferredSize().height(), mTextHeight)); //Contents box
    painter->drawRect(mTextWidth, 0, size().width() - mTextWidth, size().height()); //Contents box
    //END Boxes
    
    //BEGIN Draw Title Text
    painter->setBrush(colorScheme.foreground());
    painter->rotate(-90);
    painter->translate(-1*(textRect.right()+(2*padd)), -1*textRect.top());
    painter->drawText(padd, padd, mTitle); //Use QLabel?
    //END Draw Title Text
    
    //qDebug() << "contentsRect()" << contentsRect();
}

QRectF Block::geometry() const
{
    //Does this have to come from the size actually painted?
    QRectF rect = QGraphicsWidget::geometry();
    /*rect.setHeight(layout()->preferredSize().height());
    qDebug() << "preferedHeight: " << layout()->preferredSize().height();
    qDebug() << "size: " << rect;*/
    return rect;
}

void Block::getContentsMargins(qreal* left, qreal* top, qreal* right, qreal* bottom) const
{
    //QGraphicsLayoutItem::getContentsMargins(left, top, right, bottom); //Send call upwards for now.
    
    if(left)
    {
        *left = 23; //Should come from the FontMetric (plus padding)
    }
    if(top)
    {
        *top = 3;
    }
    if(right)
    {
        *right = 3;
    }
    if(bottom)
    {
        *bottom = 3;
    }
}

void Block::setGeometry(const QRectF& rect)
{
    QGraphicsWidget::setGeometry(rect);
}

QSizeF Block::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
{
    //TODO Set sensible widths
    switch(which)
    {
        case Qt::MinimumSize:
            return QSizeF(50.0, qMax(layout()->preferredSize().height(), mTextHeight));
        case Qt::PreferredSize:
            return QSizeF(400.0, qMax(layout()->preferredSize().height(), mTextHeight));
        case Qt::MaximumSize:
            return QSizeF(500.0, 10000.0); //?
    }

    return QSizeF();
}

void Block::updateGeometry()
{
    QGraphicsLayoutItem::updateGeometry();
}

void Block::addItem(QGraphicsLayoutItem* item)
{
    dynamic_cast<QGraphicsLinearLayout*>(layout())->addItem(item);
}

void Block::addStretch(int stretch)
{
    dynamic_cast<QGraphicsLinearLayout*>(layout())->addStretch(stretch);
}
