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

#include <QPixmap>
#include <KUrl>
#include <QDomDocument>

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

class GraphicsLinesItem;

/**
 * @brief Class for loading KidDraw images.
 *
 * Loads images from a KidDraw image file. After loading a file from a KUrl,
 * the image's information (including color palette) can be fetched from the
 * reader.
 *
 * Support for previous file versions is achieved through file versioning.
 */
class DrawingReader
{
public:
    /**
     * Constructs an empty DrawingReader, without loading any file yet.
     */
    DrawingReader();

    /**
     * Default constructor. Creates a DrawingReader and loads the file from
     * the given URL. Use isLoaded() to check if loading the file was successful
     * or not.
     *
     * @param url The URL of the drawing file to load.
     */
    explicit DrawingReader(KUrl url, QWidget *window = NULL);

    /** Destructor */
    ~DrawingReader();

    /**
     * Load a drawing from the given URL.
     *
     * @param url The URL of the drawing file to load.
     * @return Boolean indication success or failure to load the file.
     */
    bool load(KUrl url, QWidget *window = NULL);

    /**
     * Check whether a valid file was loaded.
     * @return Whether the file was valid or not.
     */
    bool isValid();

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

    /**
     * Get the background image.
     * @return The background image.
     */
    QImage backgroundImage();

    /**
     * Get the background style (position).
     * @return the background style (position).
     */
    CanvasScene::BackgroundStyle backgroundStyle();

    /**
     * Get the palette's name.
     * @return the palette's name.
     */
    QString paletteName();

    /**
     * Get the palette itself.
     * @return The palette for the image.
     */
    Palette palette();

    /**
     * Get the lines specified in the image.
     * @return A list of lines specified in the loaded image.
     */
    QList<GraphicsLinesItem *> lines();

    /**
     * Set the pen template. The pen's width and color are overwritten from the
     * line data for each line.
     * @param pen The pen template.
     */
    void setPenTemplate(QPen pen);

private:
    /**
     * Initialize an empty DrawingReader with default values.
     */
    void init();

    /** The default pen template. */
    QPen m_penTemplate;
    /** Indicator if a file is loaded or not. */
    bool m_valid;
    /** The loaded file's version. */
    int  m_version;
    /** The drawing's background pixmap. */
    QImage m_background;
    /** The drawing's background style. */
    CanvasScene::BackgroundStyle m_backgroundStyle;
    /** The palette's name. */
    QString m_paletteName;
    /** The drawing's palette. */
    Palette m_palette;
    /** The drawing's line definitions. */
    QList<GraphicsLinesItem *> m_lines;

    /**
     * Load a version 1 KidDraw drawing.
     * @param document The dom document with data.
     * @return Boolean indicating a successful load or not. If the load is successful,
     *         then all the reader's elements will be set to proper values.
     */
    bool loadVersion1(QDomDocument document);

    /**
     * Read the palette definition from a version 1 file.
     * @param document The dom document with data
     */
    void readPaletteV1(QDomDocument document);

    /**
     * Read the background definition from a version 1 file.
     * @param document The dom document with data
     */
    void readBackgroundV1(QDomDocument document);

    /**
     * Read the line definitions from a version 1 file.
     * @param document The dom document with data
     */
    void readLinesV1(QDomDocument document);
};

#endif
