#ifndef TDE_WIDGETS_SPINBOX_H
#define TDE_WIDGETS_SPINBOX_H

#include <QWidget>
#include <QSpinBox>

#include "common.h"
#include "opaque.h"

// -- TDE NAMESPACE ----------------------------------------------------

namespace TDE {

// -- Widgets NAMESPACE ------------------------------------------------

namespace Widgets {

/*!
 * \class SpinBox
 * \brief Widget for editing a DataStore variable value through a spin box.
 *
 * \see DataStore
 * \see WidgetInfo
 */
class SpinBox : public QSpinBox
{
    Q_OBJECT
	Q_PROPERTY(QString variableName READ variableName WRITE setVariableName NOTIFY variableChanged)
    Q_PROPERTY(bool trace READ trace WRITE setTrace NOTIFY traceChanged)
    Q_PROPERTY(int refresh READ refresh WRITE setRefresh NOTIFY refreshChanged)

public:
    SpinBox(QWidget *parent = 0);
    virtual ~SpinBox();

    SHARED_PTR<TDE::DataStore> dataStore();
    SpinBox& setDataStore(SHARED_PTR<TDE::DataStore> ds_p);
    SpinBox& setVariableName(const std::string& name);

    int     variableIndex() const;
    const TDE::VariableDescr& variableDescr() const;

    // properties getters/setters

    QString variableName() const;
    void    setVariableName(const QString&);

    bool trace() const;
    bool setTrace(bool value = true);

    int refresh() const;
    int setRefresh(int value = 0);

signals:
    void variableChanged(QString);
    void integerVariableValueChanged(int);
    void floatVariableValueChanged(float);

    void traceChanged(bool);                // trace option changed
    void refreshChanged(int);               // refresh value changed

public slots:
    void contentsEdited(int value);
    void contentsEditingFinished();

    void ui2ds();    // set DataStore variable with value from UI
    void ds2ui();    // set UI with value from DataStore

protected:
    void focusInEvent(QFocusEvent* e);
    void focusOutEvent(QFocusEvent* e);
    void mouseReleaseEvent(QMouseEvent* e);
    void keyReleaseEvent(QKeyEvent* e);

private:
	SHARED_PTR<WidgetInfo> m_winfo;
    bool m_edited;
    int m_original_value;
};

} /* end of namespace Widgets */

} /* end of namespace TDE */

#endif /* TDE_WIDGETS_SPINBOX_H */
