/***************************** LICENSE START ***********************************

 Copyright 2012 ECMWF and INPE. This software is distributed under the terms
 of the Apache License version 2.0. In applying this license, ECMWF does not
 waive the privileges and immunities granted to it by virtue of its status as
 an Intergovernmental Organization or submit itself to any jurisdiction.

 ***************************** LICENSE END *************************************/

#ifndef MVQCOLOURGRADLINE_H
#define MVQCOLOURGRADLINE_H

#include "MvQRequestPanelLine.h"
#include "MvIconParameter.h"

#include <QPen>
#include <QRect>
#include <QString>
#include <QVector>
#include <QWidget>

class QColor;
class QLabel;

class RequestPanel;

struct ColorCell
{
    ColorCell() {}
    ColorCell(QColor c) :
        col(c) {}
    QColor col;
    int xp;
    int yp;
    int w;
    int h;
    void render(QPainter* painter);
    void renderSelection(QPainter* painter);
};

class MvQColourGradWidget : public QWidget
{
    Q_OBJECT

public:
    MvQColourGradWidget(QWidget* parent = 0);
    void reset(QList<QColor>, const string&, const vector<string>&, const string&, const vector<string>&);
    QColor currentColour() const;
    void setCurrentColour(QColor);
    QList<QColor> colours() const;
    void setEdited(bool);
    bool setTechs(const vector<string>& vals, bool redraw);
    bool setSteps(const vector<string>& vals, bool redraw);
    bool setWayMethod(const string& val, bool redraw);
    bool setDirection(const string& val, bool redraw);

public slots:
    void slotInsertBeforeCell();
    void slotInsertAfterCell();
    void slotDeleteCell();
    void slotReverseLeft();
    void slotReverseRight();
    void slotReverseAll();
    void slotShowInfo();

protected slots:
    void slotCopyColour();
    void slotPasteColour();

signals:
    void changed();
    void currentChanged(QColor);
    void singleCellClicked();

protected:
    void resizeEvent(QResizeEvent*);
    void changeEvent(QEvent* event);
    void paintEvent(QPaintEvent*);
    void mousePressEvent(QMouseEvent*);
    void createPixmap();
    void renderBlock(QPainter*, QColor c1, QColor c2, QString tech, int num, QRect r, int, int);
    void renderCurrentCell(QPainter*);
    void paintCell(QPainter* painter, QRectF cr, QColor c1, QColor c2, float pos, QString tech);
    void setCurrentCell(QPoint);
    void setCurrentCell(int);
    void deleteCell(int);
    void insertBeforeCell(int);
    void insertAfterCell(int);
    void checkActionState();
    bool isTechValid(QString v);
    void reverseLeft(int);
    void reverseRight(int);

    QColor interpolateLinear(QColor c1, QColor c2, float p);
    QColor interpolateHslClockwise(QColor c1, QColor c2, float p);
    QColor interpolateHslAntiClockwise(QColor c1, QColor c2, float p);
    QColor interpolateHslShort(QColor c1, QColor c2, float p);
    QColor interpolateHslLong(QColor c1, QColor c2, float p);
    QColor interpolateHclClockwise(QColor c1, QColor c2, float p);
    QColor interpolateHclAntiClockwise(QColor c1, QColor c2, float p);
    QColor interpolateHclShort(QColor c1, QColor c2, float p);
    QColor interpolateHclLong(QColor c1, QColor c2, float p);

    bool edited_;
    QPixmap pix_;
    QVector<ColorCell> cells_;
    QVector<QColor> allColours_;
    int cellHeight_;
    int currentCell_;
    QPen blackPen_;
    QPen whitePen_;
    QAction* insertBeforeAc_;
    QAction* insertAfterAc_;
    QAction* deleteCellAc_;
    QAction* reverseLeftAc_;
    QAction* reverseRightAc_;
    QAction* reverseAllAc_;
    QAction* copyColAc_;
    QAction* pasteColAc_;
    QAction* infoAc_;
    int editorGapX_;
    int editorGapY_;
    int gradHeight_;
    QVector<int> steps_;
    QStringList techs_;
    QStringList validTechs_;
    QString wayMethod_;
    QStringList validWayMethods_;
    QString direction_;
    QStringList validDirections_;
};

class MvQColourGradLine : public MvQRequestPanelLine
{
    Q_OBJECT

public:
    MvQColourGradLine(RequestPanel& owner, const MvIconParameter& param);
    ~MvQColourGradLine() {}

    QString currentValue() { return QString(); }
    void refresh(const vector<string>&);
    void changed(const char* param, const vector<string>&);
    void changed(const char* param, const string&);
    bool isDependent() const { return true; }

public slots:
    void slotListChanged();
    void slotCurrentChanged(QColor);
    void slotHelperEdited(const vector<string>&);
    void slotSingleCellClicked();
    void slotHelperOpened(bool);

protected:
    void buildHelper();
    void updateHelper();

    MvQColourGradWidget* widget_;
    string waypointParam_;
    string techParam_;
    string directionParam_;
    string stepParam_;
};

#endif  // MVQCOLOURGRADLINE_H
