#ifndef CD_PULT_H
#define CD_PULT_H

#include <QDialog>
#include <QSpinBox>
#include <QFont>
#include <QRect>
#include <QFile>
#include <QVector>
#include <QDir>
#include <QTextStream>
#include <QDebug>
#include <QBoxLayout>
#include <QCheckBox>
#include <QMainWindow>
#include <QTimer>
#include <QTime>
#include <QMenu>
#include <QMetaObject>
#include <QTreeWidgetItem>
#include <QLabel>
#include "piqt.h"
#include "session_manager.h"
#include "qpievaluator.h"
#include "cdutils_core.h"
#include "cdutils_k.h"
#include "qcd_kmodel.h"


namespace Ui {
	class CD_Pult;
}

class CD_Pult: public QMainWindow, public PIObject
{
	Q_OBJECT
	PIOBJECT(CD_Pult)
public:
	CD_Pult();
	~CD_Pult();

private:
	bool eventFilter(QObject * o, QEvent * e);
	void timerEvent(QTimerEvent * );
	void addToList(const QString & s, const QColor & c);
	QColor invertColor(QColor col) {return QColor(255 - col.red(), 255 - col.green(), 255 - col.blue());}
	void stopWait();
	bool calculateExpression(int i, QVector<int> trace);
	void markError(QTreeWidgetItem * item, const QString & tool_tip = QString());
	void markNormal(QTreeWidgetItem * item);
	void progress(int val, int max);
	void clearSelected();
	QString typeName(const QString & n) const;
	void makeTreeSection(CDUtils::CDSection & ks, QTreeWidgetItem * pi);
	
	EVENT_HANDLER(void, pip_sendFailed) {emit q_k_sendFailed();}
	EVENT_HANDLER(void, pip_sendSucceed) {emit q_k_sendSucceed();}
	EVENT_HANDLER(void, pip_receiveFailed) {emit q_k_receiveFailed();}
	EVENT_HANDLER(void, pip_receiveSucceed) {emit q_k_receiveSucceed();}

	Ui::CD_Pult * ui;
	PIString config_, name_x, name_c;
	PIString kdesc_file, cdesc_file, xdesc_file;
	QMenu log_menu;
	QTimer timer_diag;
	SessionManager session;
	QPIConfig config;
	//QVector<float> k, x;
	int clear_target, timer;
	bool needWrite, isPause, need_update, show_x;

private slots:
	void loading(QPIConfig & conf);
	void saving(QPIConfig & conf);
	void updateGraph();
	void updateDiag();
	void updateKDesc(bool ask_move = false);
	void updateCDesc();
	void updateTree(bool move = false);
	void updateCommands();
	void filterTree();
	//void calculate();
	//void renew(bool write = true);
	void commandClicked();
	void k_sendFailed();
	void k_sendSucceed();
	void k_receiveFailed();
	void k_receiveSucceed();
	void on_spinBuffer_editingFinished();
	void on_buttonSendK_clicked();
	void on_buttonReceiveK_clicked();
	void on_buttonShowAll_clicked();
	void on_buttonHideAll_clicked();
	void on_buttonRead_clicked();
	void on_buttonWrite_clicked();
	void on_buttonSetKDesc_clicked();
	void on_buttonReparseKDesc_clicked() {updateKDesc(true);}
	void on_buttonSetCDesc_clicked();
	void on_buttonReparseCDesc_clicked() {updateCDesc();}
	//void on_buttonCalculate_clicked() {calculate();}
	//void on_buttonApply_clicked() {renew();}
	void on_buttonPause_toggled(bool on) {isPause = on;}
	void on_treeK_itemClicked(QTreeWidgetItem * item, int column);
	void on_treeK_itemChanged(QTreeWidgetItem * item, int column);
	void on_actionClear_triggered();

signals:
	void q_k_sendFailed();
	void q_k_sendSucceed();
	void q_k_receiveFailed();
	void q_k_receiveSucceed();

};

#endif // CD_PULT_H

