#ifndef PLOT_SCENE_H
#define PLOT_SCENE_H

#include <QGraphicsScene>
#include <QWidget>
#include <QPainter>
#include <QGraphicsView>
#include <QLineF>
#include <QPen>
#include <QColor>
#include <QBrush>
#include <QList>
#include <QAction>
#include <math.h>

#include "my_signal.h"

#define GRIP_Z 1.0
#define AXES_Z 2.0
#define TEXT_Z 3.0
#define SIGNAL_Z 10.0

#define MAX_SIG_NUM 4

class PlotScene : public QGraphicsScene{
	Q_OBJECT
	private:
		Signal Signals[MAX_SIG_NUM];
		int signalNum;
		int selected;
		QColor colors[MAX_SIG_NUM];
		bool showGrip;
		int gripSpace;
		int border;
		//double XStep;
		double YStep;
		QPointF * zero;
		QColor * gripColor;
		QColor * axesColor;
		QColor * bgColor;
		QColor * foreColor;
		QMenu * PlotPopUp;
		QAction * actionLoad;
		QAction * actionSave;
		QAction * actionToggleGrip;
	
	public:
		PlotScene(QObject *parent);
		PlotScene( qreal width, qreal height, QObject * parent);
		PlotScene( qreal width, qreal height,QString &filename, QObject * parent);
		virtual ~PlotScene();
		
		bool loadSignal(QString &filename);
		void redrawScene();
		
		// Set
		void setForeColor(QColor & c);
		void setBgColor(QColor & c);
		void setGripColor(QColor & c);
		void setAxesColor(QColor & c);
		void setSelectedSignal(int i);

		// Get
		QColor & getForeColor() const;
		Signal  getSignal(int index);
		QColor & getSignalColor(int index);
		int getSelectedSignal();


	protected:
		void paintEvent(QPaintEvent *event);
		void contextMenuEvent(QGraphicsSceneContextMenuEvent * event );
	
    private:
    	void createMenus();
    	void setDefaults();
	double totalMaxY();
	double totalMinY();
	double totalMaxX();
	double totalMinX();
    	void resizeRectCalcStep();
		void drawGrip();
		void drawAxes();
		void clearScene();
		void drawSignal(Signal &sig, QColor &col);
		QPointF & toRealXY(Point & p) const;
		
	public slots:
		void load();
		void save();
		void toggleGrip();
	
	signals:
		void signalAdded(int signum,QColor &sigcolor);	
};


#endif
