#ifndef SHSSLIDER_H
#define SHSSLIDER_H

#include <QFrame>
#include <QMouseEvent>
#include "shs_widgets_export.h"

namespace Ui {
	class SHSSlider;
}

QT_BEGIN_NAMESPACE

class SHS_WIDGETS_EXPORT SHSSlider: public QFrame
{
	Q_OBJECT
	Q_ENUMS(BarStyle)
	Q_PROPERTY(bool readOnly READ readOnly WRITE setReadOnly)
	Q_PROPERTY(bool showMinMax READ showMinMax WRITE setShowMinMax)
	Q_PROPERTY(bool showDecInc READ showDecInc WRITE setShowDecInc)
	Q_PROPERTY(bool titleInsideBar READ titleInsideBar WRITE setTitleInsideBar)
	Q_PROPERTY(bool noFrame READ isNoFrame WRITE setNoFrame)
	Q_PROPERTY(BarStyle barStyle READ barStyle WRITE setBarStyle)
	Q_PROPERTY(double titleSize READ titleSize WRITE setTitleSize)
	Q_PROPERTY(double minimum READ minimum WRITE setMinimum)
	Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
	Q_PROPERTY(double value READ value WRITE setValue)
	Q_PROPERTY(double precision READ precision WRITE setPrecision)
	Q_PROPERTY(QString title READ title WRITE setTitle)
	Q_PROPERTY(int clickID READ clickID WRITE setClickID)
	Q_PROPERTY(int setID READ setID WRITE setSetID)
	Q_PROPERTY(int spacing READ spacing WRITE setSpacing)
	Q_PROPERTY(QSize minimumButtonSize READ minimumButtonSize WRITE setMinimumButtonSize)

public:
	explicit SHSSlider(QWidget *parent = 0);
	~SHSSlider();

	enum BarStyle {
		From0,
		MinToValue,
		MaxToValue
	};
	
	int clickID() const {return id_click;}
	int setID() const {return id_set;}

	double minimum() const {return min_;}
	double maximum() const {return max_;}
	double value() const {return val_;}
	
	double precision() const {return prec;}
	QString title() const {return title_;}
	bool readOnly() const {return m_readOnly;}
	bool showMinMax() const {return m_showMinMax;}
	bool showDecInc() const {return m_showDecInc;}
	QSize minimumButtonSize() const;
	int spacing() const;

	SHSSlider::BarStyle barStyle() const {return style_;}
	bool titleInsideBar() const {return m_titleInsideBar;}
	double titleSize() const {return m_titleSize;}
	bool isNoFrame() const {return m_noFrame;}

private:
	bool event(QEvent * e);
	bool eventFilter(QObject * o, QEvent * e);
	void updateCaption();
	void drawBar();
	
	Ui::SHSSlider * ui;
	bool hasZero, m_readOnly, m_showMinMax, m_showDecInc;
	bool m_titleInsideBar, m_noFrame;
	double prec, pval, min_, max_, val_, m_titleSize, app_scale;
	int id_click, id_set, frac_digits;
	QString title_, title_cur;
	BarStyle style_;

public slots:
	void enable() {setEnabled(true);}
	void disable() {setEnabled(false);}
	
	void setClickID(int id) {id_click = id;}
	void setSetID(int id) {id_set = id;}
	void setSpacing(int value);
	void setMinimumButtonSize(const QSize & sz);
	
	void setValue(double val);
	void setValue(int val) {setValue((double)val);}
	
	void setTitle(const QString & text) {title_ = text; updateCaption();}
	void setPrecision(double precision);
	void setPrecision(int precision) {setPrecision((double)precision);}
	void setMinimum(double min);
	void setMinimum(int min) {setMinimum((double)min);}
	void setMaximum(double max);
	void setMaximum(int max) {setMaximum((double)max);}
	void setReadOnly(bool yes);
	void setShowMinMax(bool yes);
	void setShowDecInc(bool yes);
	
	void setValueID(int set_id, double val) {if (set_id == id_set) setValue(val);}
	void setValueID(int set_id, int val) {if (set_id == id_set) setValue(val);}
	
	void setMinimumID(int set_id, double min) {if (set_id == id_set) setMinimum(min);}
	void setMinimumID(int set_id, int min) {if (set_id == id_set) setMinimum(min);}
	void setMaximumID(int set_id, double max) {if (set_id == id_set) setMaximum(max);}
	void setMaximumID(int set_id, int max) {if (set_id == id_set) setMaximum(max);}
	void setReadOnlyID(int set_id, bool yes) {if (set_id == id_set) setReadOnly(yes);}
	void setShowMinMaxID(int set_id, bool yes) {if (set_id == id_set) setShowMinMax(yes);}
	
	void enableID(int set_id) {if (set_id == id_set) enable();}
	void disableID(int set_id) {if (set_id == id_set) disable();}
	
	void clickZero() {on_qt_pbZero_clicked();}
	void clickMin()  {on_qt_pbMin_clicked();}
	void clickMax()  {on_qt_pbMax_clicked();}
	void clickInc()  {on_qt_pbInc_clicked();}
	void clickDec()  {on_qt_pbDec_clicked();}

	void setBarStyle(SHSSlider::BarStyle s) {style_ = s; update();}
	void setTitleInsideBar(bool yes) {m_titleInsideBar = yes; updateCaption();}
	void setTitleSize(double size_) {m_titleSize = size_; update();}
	void setNoFrame(bool yes);

private slots:
	void on_qt_pbZero_clicked() {setValue(0.);}
	void on_qt_pbDec_clicked() {setValue(val_ - prec);}
	void on_qt_pbInc_clicked() {setValue(val_ + prec);}
	void on_qt_pbMax_clicked() {setValue(max_);}
	void on_qt_pbMin_clicked() {setValue(min_);}

signals:
	void valueChanged(double val);
	void valueChanged(int val);
	void valueChangedID(int id, double val);
	void valueChangedID(int id, int val);
};

QT_END_NAMESPACE

#endif // SHSSLIDER_H
