#ifndef TDE_WIDGETS_EDITSTRING_H
#define TDE_WIDGETS_EDITSTRING_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 Widgets {

/*!
 * \class EditString
 * \brief Widget for editing string DataStore variables.
 *
 * \see DataStore
 * \see WidgetInfo
 */
class EditString : 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)

public:
    EditString(QWidget *parent = 0);
    EditString(const QString& defaultValue = "string", Qt::Alignment align = Qt::AlignLeft, QWidget *parent = 0);
    virtual ~EditString();

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

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

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 setStringValue(const std::string& value);
    void setStringValue(const QString& value);

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

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

} /* end of namespace Widgets */

} /* end of namespace TDE */

#endif /* TDE_WIDGETS_EDITSTRING_H */
