// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | bytran@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Class to implement the color plot used to select the color value to be used for displaying molecular spectra (color-encoded)
#include <QtQuick/QQuickPaintedItem>

#ifndef COLORPLOT_H
#define COLORPLOT_H

#include <QObject>
#include <QPainter>
#include <QQuickPaintedItem>
#include <QMouseEvent>

class ColorPlot : public QQuickPaintedItem
{
    Q_OBJECT

    Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
    Q_PROPERTY(double xMinPadding READ xMinPadding WRITE setXMinPadding NOTIFY xMinPaddingChanged)
    Q_PROPERTY(double xMaxPadding READ xMaxPadding WRITE setXMaxPadding NOTIFY xMaxPaddingChanged)
    Q_PROPERTY(double yMinPadding READ yMinPadding WRITE setYMinPadding NOTIFY yMinPaddingChanged)
    Q_PROPERTY(double yMaxPadding READ yMaxPadding WRITE setYMaxPadding NOTIFY yMaxPaddingChanged)
    Q_PROPERTY(int selectorWidth READ selectorWidth WRITE setSelectorWidth NOTIFY selectorWidthChanged)
    Q_PROPERTY(int colorBarHeight READ colorBarHeight WRITE setColorBarHeight NOTIFY colorBarHeightChanged)
    Q_PROPERTY(qreal hueValue READ hueValue WRITE setHueValue NOTIFY hueValueChanged)
    Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged)
    Q_PROPERTY(int y READ y WRITE setY NOTIFY yChanged)


public:

    ColorPlot(QQuickItem *parent = 0);
    void paint(QPainter *painter);

    // Plot padding (distance between the edges and the axes area)
    double xMinPadding() const;
    void setXMinPadding(const double &xMinPadding);
    double xMaxPadding() const;
    void setXMaxPadding(const double &xMaxPadding);
    double yMinPadding() const;
    void setYMinPadding(const double &yMinPadding);
    double yMaxPadding() const;
    void setYMaxPadding(const double &yMaxPadding);

    // Getting and setting the selector width
    int selectorWidth() const;
    void setSelectorWidth(const int &selectorWidth);

    // Methods to get and set the current color
    QString color() const;
    void setColor(const QString &color);

    // Methods to get and set the color bar height
    int colorBarHeight() const;
    void setColorBarHeight(const int &colorBarHeight);

    // Methods to get and set the slider value
    qreal hueValue() const;
    void setHueValue(const qreal &hueValue);

    // Coordinates of the mouse
    int x() const;
    void setX(const int &x);
    int y() const;
    void setY(const int &y);

signals:

    // Signal emitted when the color is changed
    void colorChanged();    

    // Signals emitted when the plto padding has been changed
    void xMinPaddingChanged();
    void xMaxPaddingChanged();
    void yMinPaddingChanged();
    void yMaxPaddingChanged();

    // Signal emitted when the selector width is changed
    void selectorWidthChanged();

    // Signal to notify that the height of the color bar has changed
    void colorBarHeightChanged();

    // Signal emited when the slider value is changed (responsible for changing the hue)
    void hueValueChanged();

    // Signals emited when the mouse coordinates are changed
    void xChanged();
    void yChanged();

private:

    // Method to draw the color to select
    void drawHslColor(QPainter *painter);

    // Method is executed when the lightness has been changed
    void changeLightness();

    // Padding of the plot
    int m_xMinPadding;
    int m_yMinPadding;
    int m_xMaxPadding;
    int m_yMaxPadding;

    int m_selectorWidth;   // Width of the selector element
    int m_colorBarHeight;  // Height of the color bar to be used
    QRectF colorRect;  // Color rectangle used to select the color for the plot
    QRectF colorBar;   // Bar with the different colors selectable for the plot
    QString m_color;   // Currently selected color
    qreal m_hueValue;  // Value of the slider used to change the currently selected color
    int m_x;  // X - mouse coordinate
    int m_y;  // Y - mouse coordinate


};

#endif // COLORPLOT_H
