#ifndef QCD_VIEW_H
#define QCD_VIEW_H

#include "piobject.h"
#include <QTreeView>

namespace CDUtils {
	class CDType;
	class CDSection;
}

class CDItemModel;
class QSortFilterProxyModel;

class CDView: public QTreeView, public PIObject
{
	Q_OBJECT
	PIOBJECT(CDView)
public:
	explicit CDView(QWidget *parent = 0);
	~CDView();

	void setType(int cdt);
	void setFile(const QString & filename);
	bool inProgress() const;
	void startX(double freq = 20.);
	CDUtils::CDSection * root();
	QString typeLetter() const;

	CDItemModel * CDModel() {return model_;}

protected:
	void mousePressEvent(QMouseEvent * );
	void mouseReleaseEvent(QMouseEvent * );
	void currentChanged(const QModelIndex & cur, const QModelIndex & prev);

public slots:
	void refresh();
	void refreshValues();
	void send();
	void receive();
	void save();
	void load();
	void clear();
	void buildFromHeader(const QString & description, int mode = 2);
	void calculate();
	void filter(const QString & f);

private slots:
	void indexClicked(const QModelIndex & i);
	void cd_sendFailed();
	void cd_sendSucceed();
	void cd_receiveFailed();
	void cd_receiveSucceed();
	void cd_receivedX();
	void cd_changedGlobal();

private:
	bool filterTree(const QModelIndex & ti, const QString & filter);
	EVENT_HANDLER(void, pi_cd_sendFailed)     {emit _qcd_sendFailed();}
	EVENT_HANDLER(void, pi_cd_sendSucceed)    {emit _qcd_sendSucceed();}
	EVENT_HANDLER(void, pi_cd_receiveFailed)  {emit _qcd_receiveFailed();}
	EVENT_HANDLER(void, pi_cd_receiveSucceed) {emit _qcd_receiveSucceed();}
	EVENT_HANDLER(void, pi_cd_receivedX)      {emit _qcd_receivedX();}
	EVENT_HANDLER(void, pi_cd_changedGlobal)  {emit _qcd_changedGlobal();}
	EVENT_HANDLER3(void, pi_cd_messageReceived, PIDeque<int>, path, int, type, PIString, msg);

	CDItemModel * model_;
	QSortFilterProxyModel * proxy_;
	int type_;

signals:
	void sendFailed();
	void sendSucceed();
	void receiveFailed();
	void receiveSucceed();
	void receivedX();
	void changedGlobal();
	void messageStatus(QString msg);
	void commandSended(QString msg);
	void messageReceived(QString path, int type, QString msg);
	void busyStatusChanged(bool busy);

	void _qcd_sendFailed(); // PRIVATE
	void _qcd_sendSucceed(); // PRIVATE
	void _qcd_receiveFailed(); // PRIVATE
	void _qcd_receiveSucceed(); // PRIVATE
	void _qcd_receivedX(); // PRIVATE
	void _qcd_changedGlobal(); // PRIVATE

};

#endif // QCD_VIEW_H
