/***************************************************************************
 *   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/>. *
 ***************************************************************************/

#ifndef THEMELOADER_H
#define THEMELOADER_H

#include <QObject>
#include <QColor>
#include <QString>
#include <QRectF>
#include <QDomDocument>

class KSvgRenderer;
class QDomDocument;
class QDomElement;
class QPainter;

/**
 * KidDraw's ThemeLoader provides an interface to KidDraw themes. It can load
 * the necessary SVG files and draw elements as necessary. A partial style
 * (colors and line width) can be set by KidDraw to change the colors of elements
 * as necessary.
 */
class ThemeLoader : public QObject
{
Q_OBJECT
    Q_PROPERTY(QColor colorValue READ colorValue WRITE setColorValue)
    Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
    Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor)
    Q_PROPERTY(int lineWidth READ lineWidth WRITE setLineWidth)
    Q_PROPERTY(QString themeName READ themeName)
    Q_PROPERTY(bool loaded READ isLoaded)

public:
    /** Constructor */
    explicit ThemeLoader(const QString& name = QString(), QObject *parent = 0);
    bool loadTheme(const QString& name);

    bool isLoaded();

    QColor  colorValue() const;
    QColor  backgroundColor() const;
    QColor  borderColor() const;
    int     lineWidth() const;
    QString themeName() const;

    void setColorValue(const QColor &color);
    void setBackgroundColor(const QColor &color);
    void setBorderColor(const QColor &color);
    void setLineWidth(int width);

    QSizeF elementSize(const QString &elementId);

    void render(QPainter *painter, const QString & elementId, const QRectF & bounds = QRectF());

private:
    void writeStyle();
    void reloadSvg();

    KSvgRenderer *m_renderer;
    QString m_themeName;
    QColor m_value, m_background, m_border;
    int m_lineWidth;
    bool m_dirty, m_svgDirty;
    bool m_loaded;

    QDomDocument m_theme;
    QDomElement m_styleElement;
    QString m_cachedStyle;
};

#endif // THEMELOADER_H
