#ifndef COLOUR_TABLE_LIST_H
#define COLOUR_TABLE_LIST_H

#include <QListWidget>
#include <QListWidgetItem>
#include <QDialog>
#include <QDoubleSpinBox>
#include <QSpinBox>
#include <QLineEdit>
#include <QLabel>
#include <QGridLayout>
#include <QPushButton>
#include <QCheckBox>
#include <QGroupBox>
#include <QScrollArea>
#include <QApplication>
#include "qprofile.h"

#define PREVIEW_WIDTH 50
#define PREVIEW_HEIGHT 300

class ColourTableList;

class ColourTablePreview: public QWidget
{
    public:
        ColourTablePreview(QWidget* parent=0):
            QWidget(parent)
        {
            setMinimumSize(PREVIEW_WIDTH, PREVIEW_HEIGHT);
            setMaximumSize(PREVIEW_WIDTH, PREVIEW_HEIGHT);
            pix=QPixmap(PREVIEW_WIDTH, PREVIEW_HEIGHT);
            regenerate=false;
        }

        QPixmap pix;
        ColourTable table;
        bool regenerate;

    protected:

        void paintEvent(QPaintEvent* e);
};

class ColourTableEditor: public QDialog
{
    Q_OBJECT

    public:

        ColourTableEditor(ColourTableList* parent, const ColourTable& table);

        virtual ~ColourTableEditor(){}

        void updateColourTable(ColourTable& table);

    public slots:
        void setSize(int);

        void setRGB(int);

        void updateView();
        
    protected:

        void sortAccordingToPositions(QVector<double>& pos, QVector<double>& H, QVector<double>& S, QVector<double>& V);
        
        QLineEdit* nameEdit;
        QSpinBox* sizeEdit;
        QCheckBox* rgbCheckbox;

        QLabel* HueLabel;
        QLabel* SaturationLabel;
        QLabel* ValueLabel;

        QVector<QDoubleSpinBox*> Positions;
        QVector<QDoubleSpinBox*> Hues;
        QVector<QDoubleSpinBox*> Saturations;
        QVector<QDoubleSpinBox*> Values;
        QWidget* valuesContainer;

        ColourTablePreview* previewWidget;
        
};

class ColourTableList: public QListWidget
{
    Q_OBJECT

    public:
        ColourTableList(QWidget* parent=0);

        virtual ~ColourTableList(){}

        void getColourTables(ColourTablePalette&) const;
        void setColourTables(const ColourTablePalette&);

        void startEditor(const QModelIndex&);
        
    public slots:
        
        void editSelected();

        void deleteSelected();

        void add();
        
    protected:

        ColourTablePalette palette;
};

#endif