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

#include <QPainter>

CanvasScene::CanvasScene(QObject *parent)
 : QGraphicsScene(parent)
{
    setBackgroundStyle(center);
}


CanvasScene::~CanvasScene()
{
}

QPixmap CanvasScene::backgroundTexture()
{
    return m_texture;
}

QImage  CanvasScene::backgroundImage()
{
    return m_texture.toImage();
}

CanvasScene::BackgroundStyle CanvasScene::backgroundStyle()
{
    return m_style;
}

void CanvasScene::setBackgroundTexture(QPixmap texture)
{
    m_texture = texture;
}

void CanvasScene::setBackgroundImage(QImage image)
{
    m_texture = QPixmap::fromImage(image);
}

void CanvasScene::setBackgroundStyle(BackgroundStyle style)
{
    m_style = style;
}

void CanvasScene::drawBackground(QPainter *painter, const QRectF &exposed)
{
     QGraphicsScene::drawBackground(painter, exposed);

     QRectF rect = this->sceneRect();

     if (!m_texture.isNull()) {
        switch(m_style) {
        default:
        case center: {
            QRectF textureRect = QRectF((rect.width() - m_texture.width()) / 2,
                    (rect.height() - m_texture.height()) / 2,
                    m_texture.width(), m_texture.height());
            if (exposed.intersects(textureRect)) {
                painter->drawPixmap(textureRect, m_texture, m_texture.rect());
            }
            break;
            }
        case topLeft: {
            QRectF textureRect = QRectF(rect.left(), rect.top(),
                    m_texture.width(), m_texture.height());
            if (exposed.intersects(textureRect)) {
                painter->drawPixmap(textureRect, m_texture, m_texture.rect());
            }
            break;
            }
        case topRight: {
            QRectF textureRect = QRectF(
                    rect.left() + rect.width() - m_texture.width(), rect.top(),
                    m_texture.width(), m_texture.height());
            if (exposed.intersects(textureRect)) {
                painter->drawPixmap(textureRect, m_texture, m_texture.rect());
            }
            break;
            }
        case bottomLeft: {
            QRectF textureRect = QRectF(rect.left(),
                    rect.top() + rect.height() - m_texture.height(),
                    m_texture.width(), m_texture.height());
            if (exposed.intersects(textureRect)) {
                painter->drawPixmap(textureRect, m_texture, m_texture.rect());
            }
            break;
            }
        case bottomRight: {
            QRectF textureRect = QRectF(
                    rect.left() + rect.width() - m_texture.width(),
                    rect.top() + rect.height() - m_texture.height(),
                    m_texture.width(), m_texture.height());
            if (exposed.intersects(textureRect)) {
                painter->drawPixmap(textureRect, m_texture, m_texture.rect());
            }
            break;
            }
        case tile: {
            qreal t = rect.top();
            QRectF textureRect;
            while (t < rect.top() + rect.height()) {
                qreal l = rect.left();
                while (l < rect.left() + rect.width()) {
                    textureRect = QRectF(l, t, m_texture.width(),
                            m_texture.height());
                    if (exposed.intersects(textureRect)) {
                        painter->drawPixmap(textureRect, m_texture,
                                m_texture.rect());
                    }
                    l += m_texture.width();
                }
                t += m_texture.height();
            }
            break;
            }
        case stretch:
            if (exposed.intersects(rect)) {
                painter->drawPixmap(rect, m_texture, m_texture.rect());
            }
            break;
        }
    }
}

void CanvasScene::resizeEvent(QResizeEvent * /*event*/)
{
    this->invalidate(this->sceneRect(), QGraphicsScene::BackgroundLayer);
}

CanvasScene::BackgroundStyle CanvasScene::backgroundStyleFromString(QString string) {
    if (string == "center") {
        return CanvasScene::center;
    }
    else if (string == "stretch") {
        return CanvasScene::stretch;
    }
    else if (string == "tile") {
        return CanvasScene::tile;
    }
    else if (string == "topLeft") {
        return CanvasScene::topLeft;
    }
    else if (string == "topRight") {
        return CanvasScene::topRight;
    }
    else if (string == "bottomLeft") {
        return CanvasScene::bottomLeft;
    }
    else if (string == "bottomRight") {
        return CanvasScene::bottomRight;
    }
    // Default
    return CanvasScene::center;
}

QString CanvasScene::backgroundStyleToString(CanvasScene::BackgroundStyle style)
{
    switch(style) {
        default:
        case CanvasScene::center:
            return "center";
        case CanvasScene::stretch:
            return "stretch";
        case CanvasScene::tile:
            return "tile";
        case CanvasScene::topLeft:
            return "topleft";
        case CanvasScene::topRight:
            return "topright";
        case CanvasScene::bottomLeft:
            return "bottomleft";
        case CanvasScene::bottomRight:
            return "bottomright";
    }
}


#include "canvasscene.moc"
