#ifndef QADSPLOT_H
#define QADSPLOT_H

#include "qcustomplot.h"

class QFastCustomPlot : public QCustomPlot
{
  Q_OBJECT

  Q_PROPERTY(bool fastRealTimeMode READ fastRealTimeMode WRITE setFastRealTimeMode)

public:
  explicit QFastCustomPlot(QWidget *parent = 0): QCustomPlot(parent), mFastRealTimeMode(true)
  {
  }

  bool fastRealTimeMode() const { return  mFastRealTimeMode;}

  Q_SLOT void setFastRealTimeMode(bool rtMode=true)
  {
    mFastRealTimeMode=rtMode;
  }

  Q_SLOT void replot();
protected:
  bool mFastRealTimeMode;

  virtual void fastDraw(QCPPainter *painter);
  virtual void paintEvent(QPaintEvent *event);

  void replotUnderEvent();
};

class QAdsPlot : public QFastCustomPlot
{
  Q_OBJECT
  /// \cond INCLUDE_QPROPERTIES
  Q_PROPERTY(bool realtimeMode READ realtimeMode WRITE setRealtimeMode)
  Q_PROPERTY(bool showScalesLabels READ showScalesLabels WRITE setShowScalesLabels)
  /// \endcond

public:
  explicit QAdsPlot(QWidget *parent = 0);
  explicit QAdsPlot(QAdsPlot& master, QWidget *parent = 0);

  bool realtimeMode() const;
  bool showScalesLabels() const;

signals:
  void cursorMoved(int c, double x);

public slots:
  void setRealtimeMode(bool enable);
  void setShowScalesLabels(bool show);
  void setCursor(int c, double x);

  void adsAddData(int plot, double key, double value);
  void contextMenuRequest(QPoint);

  void cpMouseWheel(QWheelEvent *);
  void cpMousePress(QMouseEvent*);
  void cpAxisDoubleClick(QCPAxis*,QCPAxis::SelectablePart,QMouseEvent*);

  void clearData();

private:
  class ads_implementation_t;
  ads_implementation_t& p_;
  QCustomPlot* customPlot;

  void initialize(QAdsPlot* master, QWidget *parent);
};


#include <QToolButton>
#include <QStandardItemModel>

class QAdsPlotWidget: public QWidget
{
  Q_OBJECT
public:
  explicit QAdsPlotWidget(QWidget* parent=0);

  QAdsPlot& plot(int n=0);

  void clear();
  void rescaleAll();

  void setRealtimeMode(bool enable);

  void sync(QCPAxis *axis);

  QToolButton* channelSelctor(int channel);

  void setSelectionModel(QStandardItemModel*);
  void* channelUserData(int channel) const;

public slots:
  void setChannelEnabled(int channel, bool enabled=true);
  void setChannelName(int channel, const QString& name, void* usrdata);

  void xAxisRangeChanged(QCPRange);
  void yAxisRangeChanged(QCPRange);
  void setCursor(int c, double x);

  void addData(int channel, double key, double value);
  void updateData();

  void cpMouseMove(QMouseEvent*);
  void dcbStateChange(int);
  void dcbPaneChange(int);

private:
  class implementation_t;
  implementation_t& p_;
};

#include <QDialog>
#include <QModelIndex>

class QTreeView;
class QDialogButtonBox;
class QModelIndex;

class QTreeInputDialog: public QDialog
{
  Q_OBJECT
public:
  QTreeInputDialog(const QString& name, QWidget *parent=0);
  void setParentSelectable(bool enable);

public slots:
  void setModel(QStandardItemModel* model);
  void itemClicked();
  void itemDoubleClicked();


public:
  QTreeView* w;
  QDialogButtonBox* bbox;
  bool parent_selectable;
};

#endif // QADSPLOT_H
