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

#include "palette.h"

#include <QObject>
#include <QMap>
#include <QList>


/**
 * This base class manages a collection of palettes and colors. Loading the
 * information from a file is not supported in this base class, but derived
 * classes can implement this.
 */
class PaletteManager : public QObject
{
Q_OBJECT
public:
    enum colorType {
        value,
        border,
        background
    };

    /**
     * Constructor.
     * @param parent The parent object.
     */
    PaletteManager(QObject *parent = 0);

    /**
     * Destructor.
     */
    ~PaletteManager();

    /**
     * Load an file with palette and color information. Must be reimplemented by
     * a subclass, default implementation just returns false.
     * @param file The name of the file to load.
     * @return Whether the load was successful or not.
     */
    virtual bool    loadFromFile(QString file);

    /**
     * Save the palette and color information to an XML file.  Must be
     * reimplemented by a subclass, default implementation just returns false.
     * @param file The name of the file to save to.
     * @return Whether the save was successful or not.
     */
    virtual bool    saveToFile(QString file) const;

    /**
     * Get the amount of named colors in the palette manager.
     * @return The amount of named colors in the palette manager.
     */
    int             countColors() const;

    /**
     * Get the amount of palettes in the palette manager.
     * @return The amount of palettes in the palette manager.
     */
    int             countPalettes() const;

    /**
     * Remove all named colors from the list.
     */
    virtual void    clearColors();

    /**
     * Remove all palettes from the list.
     */
    virtual void    clearPalettes();

    /**
     * Add a new color to the named color list.
     * @param color The color information.
     * @param name The name of the color. If this name already exists, the color
     *  will be replaced.
     */
    virtual void    addColor(QString name, Color color);

    /**
     * Add a new palette to the list.
     * @param name The name of the palette. If this name already exists, the
     *  palette will be replaced.
     * @param palette The palette to add.
     */
    virtual void    addPalette(QString name, Palette *palette);

    /**
     * Get a list of all the names of the colors, in the order they were
     * added.
     */
    QList<QString>  colorNames() const;

    /**
     * Get a list of all the names of the palettes, in the order they were
     * added.
     */
    QList<QString>  paletteNames() const;

    /**
     * Retrieve the mapping of named colors.
     * @return The mapping of named colors.
     */
    QMap<QString, Color> colors() const;

    /**
     * Retrieve the list of palettes.
     * @return The list of palettes.
     */
    QMap<QString, Palette *> palettes() const;

    /**
     * Remove a color from the colors list (by name).
     * @param name The name of the color to remove.
     */
    virtual void    removeColor(QString name);

    /**
     * Remove a color from the colors list (by color).
     * @param name The color element to remove.
     */
    virtual void    removeColor(Color color);

    /**
     * Remove the palette at the given index from the palette list.
     * @param name The name of the palette to remove.
     */
    virtual void    removePalette(QString name);

    /**
     * Convert a palette to a Qt color table.
     * @param palette Pointer to the palette to convert.
     * @param type The color entry (the value, border or background) to use.
     *  Defaults to the color's values.
     */
    static QVector<QRgb> paletteToColorTable(Palette *palette,
            colorType type = value);

public slots:
    /**
     * Set the list of colors to a new list.
     * @param colors The color mapping to set.
     */
    virtual void    setColors(QMap<QString, Color> colors);

    /**
     * Set the list of palettes to a new list.
     * @param palettes The list of palettes to set.
     */
    virtual void    setPalettes(QMap<QString, Palette *> palettes);

private:
    QMap<QString, Color> m_colors;
    QMap<QString, Palette *> m_palettes;
    QList<QString>  m_colorNames;
    QList<QString>  m_paletteNames;
};

#endif
