#ifndef MATH_PLOT_PANEL
#define MATH_PLOT_PANEL

#include <QtGui>
#include <cmath>
#include "my_signal.h"
#include "my_point.h"
#include "Plot.h"


#define DEF_PLOT_W 400
#define DEF_PLOT_H 300
#define H_NUM_BORDER 100
#define DEF_BORDER 30
#define DISPLAY_DIG 5

#define COORD_W 150


class MathPlotPanel : public QWidget{
	Q_OBJECT
	private:
		QList< QStack<Signal> > sigList;
		QList<bool> analog;
		QImage drawArea;
		QRect plotViewport;
		
		QColor sigColor;
		QColor gridColor;
		QColor bgColor;
		
		double XStep;
		double YStep;
		
		QMenu * PlotPopUp;
		QAction * actionLoad;
		QAction * actionSaveAsImage;
		
		double totalMaxX;
		double totalMinX;
		double totalMaxY;
		double totalMinY;

		
	public:
		MathPlotPanel(QWidget * parent=0);
		virtual ~MathPlotPanel();
		


		bool isEmpty();
		
		// get <-> set
		int getSignalsCount();
		Signal getSignal(int index);
		Qt::GlobalColor getColorFor(int);
		bool isAnalog(int index);
		
		void setSignal(int,Signal &);
		void setAnalog(int index,bool a);
		
		void addSignal(Signal &);
		void removeSignal(int);

		
		void redrawAll();
		
	protected:
		void paintEvent(QPaintEvent *);
		void dragLeaveEvent(QDragLeaveEvent *);
		void dragMoveEvent(QDragMoveEvent *);
		void dragEnterEvent( QDragEnterEvent *);
		void dropEvent(QDropEvent *);
		void contextMenuEvent(QContextMenuEvent  *);
		void mouseMoveEvent ( QMouseEvent * event );

	private:
		void createMenus();
		void createUI();
		void initDrawArea();
		void drawDigitalSignal(int);
		void drawAnalogSignal(int);
		QPointF SignalToImage(const Point &p);
		Point ImageToSignal(const QPointF &p);
		void calcSteps();
		void addNumbers();
		void drawAxes();
		void calcMinMaxValues();
		
	public slots:
		void load();
		void saveAsImage();

	signals:
		void signalAdded();
};


#endif
