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

#include "palettemanager.h"

#include <QVector>

PaletteManager::PaletteManager(QObject *parent)
 : QObject(parent)
{
}
//------------------------------------------------------------------------------

PaletteManager::~PaletteManager()
{
}
//------------------------------------------------------------------------------

bool PaletteManager::loadFromFile(QString)
{
    return false;
}
//------------------------------------------------------------------------------

bool PaletteManager::saveToFile(QString) const
{
    return false;
}
//------------------------------------------------------------------------------

int PaletteManager::countColors() const
{
    return m_colors.count();
}
//------------------------------------------------------------------------------

int PaletteManager::countPalettes() const
{
    return m_palettes.count();
}
//------------------------------------------------------------------------------

void PaletteManager::clearColors()
{
    m_colors.clear();
    m_colorNames.clear();
}
//------------------------------------------------------------------------------

void PaletteManager::clearPalettes()
{
    m_palettes.clear();
    m_paletteNames.clear();
}
//------------------------------------------------------------------------------

void PaletteManager::addColor(QString name, Color color)
{
    m_colors.insert(name, color);
    m_colorNames.append(name);
}
//------------------------------------------------------------------------------

void PaletteManager::addPalette(QString name, Palette *palette)
{
    m_palettes.insert(name, palette);
    m_paletteNames.append(name);
}
//------------------------------------------------------------------------------

QMap<QString, Color> PaletteManager::colors() const
{
    return m_colors;
}
//------------------------------------------------------------------------------

QList<QString> PaletteManager::colorNames() const
{
    return m_colorNames;
}
//------------------------------------------------------------------------------

QMap<QString, Palette *> PaletteManager::palettes() const
{
    return m_palettes;
}
//------------------------------------------------------------------------------

QList<QString> PaletteManager::paletteNames() const
{
    return m_paletteNames;
}
//------------------------------------------------------------------------------

void PaletteManager::removeColor(QString name)
{
    m_colors.remove(name);
    for (QList<QString>::iterator i = m_colorNames.begin();
            i != m_colorNames.end(); ++i) {
        if ((*i) == name) {
            i = m_colorNames.erase(i);
        }
    }
}
//------------------------------------------------------------------------------

void PaletteManager::removeColor(Color color)
{
    QMap<QString, Color>::iterator i = m_colors.begin();
    QString name;
    while (i != m_colors.end()) {
        if (i.value().color == color.color &&
                i.value().border == color.border &&
                i.value().background == color.background) {
            removeColor(i.key());
            i++;
        }
        else {
            i++;
        }
    }
}
//------------------------------------------------------------------------------

void PaletteManager::removePalette(QString name)
{
    m_palettes.remove(name);
    for (QList<QString>::iterator i = m_paletteNames.begin();
            i != m_paletteNames.end(); ++i) {
        if ((*i) == name) {
            i = m_paletteNames.erase(i);
        }
    }
}
//------------------------------------------------------------------------------

void PaletteManager::setColors(QMap<QString, Color> colors)
{
    m_colors = colors;
    m_colorNames = colors.keys();
}
//------------------------------------------------------------------------------

void PaletteManager::setPalettes(QMap<QString, Palette *> palettes)
{
    m_palettes = palettes;
    m_paletteNames = palettes.keys();
}
//------------------------------------------------------------------------------

QVector<QRgb> PaletteManager::paletteToColorTable(Palette *palette,
        colorType type)
{
    QVector<QRgb> colorTable;

    for (Palette::const_iterator i = palette->constBegin();
         i != palette->constEnd(); ++i) {
        switch(type) {
        default:
        case value:
            colorTable.append((*i).color.rgba());
            break;
        case border:
            colorTable.append((*i).border.rgba());
            break;
        case background:
            colorTable.append((*i).background.rgba());
            break;
        }
    }

    return colorTable;
}
//------------------------------------------------------------------------------

#include "palettemanager.moc"
