#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "gimbaldata.h"
#include "joystick.h"

//#include <QWidget>
//#include <QString>
#include <QStandardItemModel>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QTimer>
//#include <QItemSelectionModel>
//#include <QList>
//#include "gimbalData.h"
//#include "dbTableModel.h"
//#include <QTableView>
//#include <QMdiSubWindow>
//#include <QCloseEvent>
//#include "stripChart.h"
#include "chartGraphicsItem.h"
#include "operatorDialog.h"
#include "comm.h"

//	----------------------------------------------------------------------------
//	Worker
//	----------------------------------------------------------------------------

class Worker : public QObject {
	Q_OBJECT
public:
	Worker();
	~Worker();
private:
	void transmit(unsigned char *message,int len);

public slots:
	void process();
signals:
	void finished();
	void error(QString err);
	void newMsg(unsigned char *message,int numread);
private:
	int init;
	int quit;
	comm *cons;
	qint64 tLastGood;
public:
	int numtosend;
	unsigned char *msgtosend;
	QString portname;
	int baudrate;
	int receiving;
	uint32_t frametime;
};

//	----------------------------------------------------------------------------
//	GpsThread
//	----------------------------------------------------------------------------

class GpsThread : public QObject {
	Q_OBJECT
public:
	GpsThread();
	~GpsThread();
private:
	void transmit(unsigned char *message,int len);
	enum {maxlen = 200};

public slots:
	void process();
signals:
	void finished();
	void error(QString err);
	void newMsg(unsigned char *message,int numread);
private:
	int init;
	int quit;
	int mstate;
	int index;
	char message[maxlen];
	comm *gpscomm;
public:
	int numtosend;
	unsigned char *msgtosend;

public:
	QString portname;
	int baudrate;
	double	latitude;
	double	longitude;
	double	altitude;
};


//	----------------------------------------------------------------------------
//	Constants
//	----------------------------------------------------------------------------

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
	Q_OBJECT

public:
	explicit MainWindow(QWidget *parent = 0);
	~MainWindow();

public:
	gimbalData gimbalThread;

private:
	void setupTree();
	void setupTune();
	const char *tableTypeIconString(int device,int module,int table);
	void setupTableMain(int device,int module,int table);
	Worker *startConsoleIoThread(const char *portname,int baudrate=UTIL_BAUD);
	GpsThread *startGpsThread(const char *portname,int baudrate=115200);
	void readout();
	void readoutPar();
	void readoutControls();
	void updateChart(int table, int row);
	void ranges();
	QString rangeString(unsigned int index);
	QString targetString(unsigned int index);
	void opReadouts();
	void opGUI();

private:
	Ui::MainWindow *ui;
	QStandardItemModel *treeModel;
	QGraphicsScene *graphScene;
	QGraphicsScene *graphScene2;
	QString guiGimIpAddress;
	QString guiGimComPort;
//	QStringList tableS;
//	QString atpText;
	int guiGimIpPort;
	int guiGimBaud;
	int guiGimConnect;
//	int quit;
//	QMutex mutex;
//	QPoint mdiPopupPoint;

#ifdef _OPERATOR_DIALOG
	operatorDialog *opDlg;
#endif
	chartGraphicsItem *chart01;
	chartGraphicsItem *chart02;
	chartGraphicsItem *chart03;
	chartGraphicsItem *chart04;
	chartGraphicsItem *chart05;
	chartGraphicsItem *chart06;
	QTimer *timer;
	QTimer *timer2;
	int refreshPar;
	int refreshControls;

	int m_axis;

	int charttable1;
	int charttable2;
	int charttable3;

	int chartcol1;
	int chartcol2;
	int chartcol3;

	int chartrow1;
	int chartrow2;
	int chartrow3;

	int resetIMU;
	int resetChart1;
	int resetChart2;
	int resetChart3;

signals:
	void connectToGimbal(int newstate);
	void mainStop();
	void sendLensCommand(unsigned char* msg,int len);

//public slots:
private slots:
	void closing();
	void editingFinished_pGain();
	void editingFinished_iGain();
	void editingFinished_dGain();
	void editingFinished_limit();
	void editingFinished_kT();
	void editingFinished_kC();
	void editingFinished_kP();
	void editingFinished_kFF();
	void editingFinished_kTach();
	void currentIndexChanged_fType1(QString value);
	void currentIndexChanged_fType2(QString value);
	void currentIndexChanged_fType3(QString value);
	void currentIndexChanged_fType4(QString value);
	void currentIndexChanged_fType5(QString value);
	void editingFinished_fFreq1();
	void editingFinished_fFreq2();
	void editingFinished_fFreq3();
	void editingFinished_fFreq4();
	void editingFinished_fFreq5();
	void editingFinished_fQ1();
	void editingFinished_fQ2();
	void editingFinished_fQ3();
	void editingFinished_fQ4();
	void editingFinished_fQ5();
	void pressedYaw();
	void pressedPit();
	void pressedRol();
	void stateChangedRaw(int state);
	void currentIndexChanged_plotAxis(QString value);
	void currentIndexChanged_plotVar(QString value);
	void displayError(int count, int socketError, const QString &message);
	void errorString(QString message);
	void newGimAddress(QString gimAddress);
	void connectedState(int clr, QString lbl);
	void lastTableMain();
	void selectionChangedSlot(const QItemSelection & newSelection, const QItemSelection & oldSelection);
	void newTableDataMain(int cmd);
	void newImuDataMain();
	void newTuneDataMain();
	void onTimer();
	void onTimer2();
	void updateTree();
	void jsReadingMain(int time, int value, int type, int number);
	void on_MainWindow_destroyed();
	void stateChangedEnable0(int state);
	void stateChangedEnable1(int state);
	void stateChangedEnable2(int state);
	void stateChangedRemote0(int state);
	void stateChangedRemote1(int state);
	void stateChangedRemote2(int state);
	void editingFinished_sinA0();
	void editingFinished_sinA1();
	void editingFinished_sinA2();
	void editingFinished_sinF0();
	void editingFinished_sinF1();
	void editingFinished_sinF2();
	void currentIndexChanged_backdrive0(QString);
	void currentIndexChanged_backdrive1(QString);
	void currentIndexChanged_backdrive2(QString);
	void on_pushButton_clicked();
	void on_pushButton_2_clicked();
	void on_pushButton_3_clicked();
	void on_pushButton_4_clicked();
	void on_pushButton_5_clicked();
	void on_pushButton_6_clicked();
	void on_autoTargetPB_clicked();
	void on_nextTargetPB_clicked();
	void on_prevTargetPB_clicked();
	void on_prevSetPB_clicked();
	void on_nextSetPB_clicked();
	void on_desiredPos0E_editingFinished();
	void on_desiredPos1E_editingFinished();
	void on_desiredPos2E_editingFinished();
    void on_standby_Button_clicked();
    void on_null_Button_pressed();
    void on_null_Button_released();
	void on_radioButton_Standby_clicked();
	void on_radioButton_Rate_clicked();
	void on_radioButton_Slave_clicked();
	void on_radioButton_Outer_clicked();
	void on_desiredVel0E_editingFinished();
	void on_desiredVel1E_editingFinished();
	void on_desiredVel2E_editingFinished();
	void on_radioButton_Geo_clicked();
	void on_pushButton_7_clicked();
	void on_radioButton_Stow_clicked();
	void on_kPGE_editingFinished();
	void on_kPG2E_editingFinished();
	void on_kIGE_editingFinished();
	void on_kDGE_editingFinished();
	void on_kILimGE_editingFinished();
	void on_kMysGE_editingFinished();
	void on_kPPE_editingFinished();
	void on_kPDE_editingFinished();
	void on_kPLimE_editingFinished();
	void on_fftMax_editingFinished();
	void on_kIE_editingFinished();
	void on_kILimE_editingFinished();
	void on_enableO0CB_clicked();
	void on_enableO1CB_clicked();
	void on_waypoint1_Lat_editingFinished();
	void on_waypoint1_Lon_editingFinished();
	void on_waypoint1_Alt_editingFinished();
	void on_waypoint2_Lat_editingFinished();
	void on_waypoint2_Lon_editingFinished();
	void on_waypoint2_Alt_editingFinished();
	void on_simSpeed_editingFinished();
	void on_pushButton_14_clicked();
	void on_pushButton_8_clicked();
};

#endif // MAINWINDOW_H
