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

#include <QWidget>
#include <KUrl>

#include "palette.h"

#define PICK_BORDER_WIDTH 5

class Canvas;
class SimpleColorPick;
class SimpleWidthPick;
class PaletteManager;
class PaletteButton;
class ThemeLoader;

class QAction;
class QMenu;
class QTextEdit;
class QGridLayout;
class QHBoxLayout;
class QVBoxLayout;

/**
 * The main area for KidDraw. This widget contains all 'buttons' and the canvas
 * for KidDraw.
 */

class KidDrawArea : public QWidget
{
Q_OBJECT
public:
    KidDrawArea(QWidget *parent = 0);

    ~KidDrawArea();

public slots:
    /**
     * Set the current painting (pen) color.
     */
    void    setColor(QColor color);

    /**
     * Set the current painting pen width.
     */
    void    setLineWidth(int lineWidth);

    /**
     * Load the previous palette from the palette manager.
     */
    void    previousPalette();

    /**
     * Load the next palette from the palette manager.
     */
    void    nextPalette();

    /**
     * Requests an URL from the user, and saves the image there.
     */
    bool    save();

    /**
     * Save the image to a given URL.
     * @param url The url to save to.
     */
    bool    save(KUrl url);

    /**
     * Requests an URL from the user, and loads the image on that URL
     */
    bool    load();

    /**
     * Load an image from a given URL.
     * @param url The url to load from.
     */
    bool    load(KUrl url);

protected:
    /**
     * Reimplementation of QWidget's mousePressEvent. Sets the pen on the
     * canvas down if the mouse is over the canvas.
     * @param event The mouse event.
     */
    void    mousePressEvent(QMouseEvent *event);

    /**
     * Reimplementation of QWidget's mouseReleaseEvent. Sets the pen on the
     * canvas up.
     * @param event The mouse event.
     */
    void    mouseReleaseEvent(QMouseEvent *event);

    /**
     * Reimplementation of QWidget's mouseMoveEvent. Captures the move events if
     * the canvas doesn't capture them, and moves the canvas pointer
     * accordingly.
     * @param event The mouse event.
     */
    void    mouseMoveEvent(QMouseEvent *event);


signals:
    /**
     * Signal emitted when a new palette is loaded.
     */
    void    newPalette();

private slots:
    /**
     * Update the color picks to the new palette.
     */
    void    updatePicks();

    /**
     * Update the background and line colors on the canvas.
     */
    void    updateCanvasColors();

    /**
     * Loads the given theme
     * @param theme The name of the theme to load
     */
    void    loadTheme();

private:
    /**
     * Creates the main canvas area
     */
    void    createCanvas();

    /**
     * Load a color palette for the colors to use.
     */
    void    createPalette();

    /**
     * Creates the color pickers on the left and right side.
     */
    void    createColorPicks();

    /**
     * Create the pen width pickers at the bottom.
     */
    void    createWidthPicks();

    /**
     * Create the image pickers at the top.
     */
    void    createImagePicks();

    /**
     * Create the clear/stop buttons.
     */
    void    createButtons();

    /**
     * Put all the elements in a layout.
     */
    void    createLayout();

    /**
     * Load a given palette.
     * @param palette The palette to load.
     */
    void    loadPalette(Palette *palette);

    /**
     * Load a given palette by name. This is an overloaded function, provided
     *   for convenience.
     * @param name The name of the palette to load from the palette manager.
     */
    void    loadPalette(QString name);

    /**
     * Load a given palette by index. This is an overloaded function, provided
     *   for convenience.
     * @param index The index of the palette from the palette manager.
     */
    void    loadPalette(int index);

    Canvas  *m_canvas;
    PaletteManager *m_paletteManager;
    QVector<QRgb> *m_palette;
    QVector<QRgb> *m_borderPalette;
    QVector<QRgb> *m_backgroundPalette;
    QList<SimpleColorPick *> *m_colorPicks;
    QList<SimpleWidthPick *> *m_widthPicks;

    QHBoxLayout *m_imagePicksBox;
    QVBoxLayout *m_colorPicksBoxLeft;
    QVBoxLayout *m_colorPicksBoxRight;
    QHBoxLayout *m_widthPicksBox;
    PaletteButton *m_leftPaletteButton;
    PaletteButton *m_rightPaletteButton;
    QGridLayout *m_layout;

    ThemeLoader *m_theme;

    int     m_paletteNumber;
    qint16  m_maxTag;
};

#endif
