#ifndef TDE_WIDGETS_EDITINTEGER_H
#define TDE_WIDGETS_EDITINTEGER_H

#include <QWidget>
#include <QLineEdit>

#include <stdint.h>
#include <sys/types.h>

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

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

namespace TDE {

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

/*!
 * \namespace TDE::Widgets
 * \brief Namespace for TDE-IDE widgets.
 *
 * All TDE-IDE widgets are contained inside this namespace.
 */

namespace Widgets {

class UnitLabel;

/*!
 * \class EditInteger
 * \brief Widget for editing integer DataStore variables.
 *
 * \see DataStore
 * \see WidgetInfo
 */
class EditInteger : public QLineEdit
{
    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)
    Q_PROPERTY(QString nativeUnit READ nativeUnit)
    Q_PROPERTY(QString displayUnit READ displayUnit WRITE setDisplayUnit NOTIFY displayUnitChanged)

public:
    EditInteger(QWidget *parent = 0);
    EditInteger(const int defaultValue = 0, Qt::Alignment align = Qt::AlignRight, QWidget *parent = 0);
    virtual ~EditInteger();

    SHARED_PTR<TDE::DataStore> dataStore();
    EditInteger& setDataStore(SHARED_PTR<TDE::DataStore> ds_p);
    EditInteger& 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);

    QString nativeUnit() const;
    QString displayUnit() const;
    void    setDisplayUnit(const QString& value);

    TDE::DisplayUnitPtr nativeUnitPtr() const;
    TDE::DisplayUnitPtr displayUnitPtr() const;

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

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

    void displayUnitChanged(QString);       // display unit changed

public slots:
    void contentsEdited(const QString& text);
    void contentsEditingFinished();

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

    void setIntegerValue(int value);

protected:
    virtual void showEvent(QShowEvent *event);
    void focusInEvent(QFocusEvent* e);
    void focusOutEvent(QFocusEvent* e);
    void mouseReleaseEvent(QMouseEvent* e);
    void keyReleaseEvent(QKeyEvent* e);

    SHARED_PTR<WidgetInfo> widgetInfo() { return m_winfo; }

    friend class UnitLabel;

private:
	SHARED_PTR<WidgetInfo> m_winfo;
    bool m_edited;
    std::string m_original_text;
    QString dbStr;
    QString originalTip;
};

} /* end of namespace Widgets */

} /* end of namespace TDE */

#endif /* TDE_WIDGETS_EDITINTEGER_H */
