/***************************************************************************
 *   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 DRAWINGWRITER_H
#define DRAWINGWRITER_H

#include <QImage>
#include <QList>
#include <QByteArray>
#include <QDomElement>
#include <KUrl>

#include "canvasscene.h"
#include "palette.h"

class GraphicsLinesItem;
class QDataStream;

/**
 * @brief Class for saving KidDraw images.
 *
 * Saves a KidDraw image file. After creating an instance, set the available
 * properties. Once set, call save() with a given URL to write the file.
 */
class DrawingWriter
{
public:
    /**
     * Constructs an empty DrawingWriter.
     */
    DrawingWriter();

    /** Destructor */
    ~DrawingWriter();

    /**
     * Save a drawing to the given URL.
     *
     * @param url The URL of the drawing file to save to.
     * @param version Optional file version number. Defaults to the latest
     *                supported version.
     * @return Boolean indicating success or failure to write the file.
     */
    bool save(KUrl url, int version = -1);

    /**
     * Retrieve the loaded file's version.
     * @return The loaded file's version.
     */
    int fileVersion();

public slots:
    /** Set the background's pixmap. */
    void setBackgroundImage(QImage background);

    /** Set the background style. */
    void setBackgroundStyle(CanvasScene::BackgroundStyle backgroundStyle);

    /** Set the (default) color palette. */
    void setPalette(Palette palette, QString name = QString());

    /** Set the created lines. */
    void setLines(QList<GraphicsLinesItem *> lines);

private:
    /** The drawing's background pixmap. */
    QImage m_background;
    /** The drawing's background style. */
    CanvasScene::BackgroundStyle m_backgroundStyle;
    /** The drawing's palette. */
    Palette m_palette;
    /** The palette's name. */
    QString m_paletteName;
    /** The drawing's line definitions. */
    QList<GraphicsLinesItem *> m_lines;

    /**
     * Load a version 1 KidDraw drawing.
     * @return Byte array of the saved data.
     */
    QByteArray saveVersion1();
    void writePaletteV1(QDomElement rootElement);
    void writeBackgroundV1(QDomElement rootElement);
    void writeLinesV1(QDomElement rootElement);
};

#endif
