#ifndef PLOT_H
#define PLOT_H

#include <QtGui>
#include <limits>

#include "my_signal.h"
#include "SignalProperties.h"
#include "PlotProperties.h"

// Z-Values For Scene Items
#define GRID_Z 1.0
#define LINE_Z 2.0
#define AXES_Z 3.0
#define TEXT_Z 4.0
#define IMAGE_Z 9.0
#define SIGNAL_Z 10.0

// Animation State Values
#define RUNNING 0
#define STOPED  1
#define PAUSED  2

// Sizes
#define DEFSCENE_W 300
#define DEFSCENE_H 80

typedef unsigned int uint;
typedef unsigned long ul;

class PlotProperties;

class Plot : public QGraphicsView
{
     Q_OBJECT

	private:
		QGraphicsScene * scene;
		PlotProperties * propDlg;
		
		QGridLayout * topLayout;

		
		QStack<Signal> signal;
		SignalProperties signalProp;
		bool showGrid;
		bool analog;
		bool showLines;
		bool zoomLock;
		int gridSpace;
		int border;
		int sDist;
		
		int animationTimer;
		int animStatus;
		ul animPos;
		int animSleepTime;
		
		double XStep;
		double YStep;
		QColor * gridColor;
		QColor * axesColor;
		QColor * lineColor;
		QColor * bgColor;
		QColor * fgColor;
		QMenu * PlotPopUp;
		QMenu * animationMenu;
		QAction * actionLoad;
		QAction * actionSave;
		QAction * actionSaveAsImage;
		QAction * actionToggleGrid;
		QAction * actionDrawDigital;
		QAction * actionToggleLines;
		QAction * actionProperties;
		QAction * actionBack;
		QAction * actionLockZoom;
		QAction * actionStartStopAnim;
		QAction * actionPauseAnim;

		QList<QGraphicsItem *> linesList;
		QList<QGraphicsItem *> gridLinesList;
		
		QGraphicsPixmapItem * selPoint;
		// Drag 'n' Drop
		bool selStarted;
		QPoint selStart;
		QPoint selStop;
	 
	public:
		
	    Plot(QWidget *parent = 0);
	    Plot(QString &filename, QWidget *parent = 0);
	    virtual ~Plot();
	
	    	// Get
		QGraphicsScene * getScene();
	
		bool loadSignal(QString &filename);
		bool isEmpty();
		void redrawScene();
		
		// Set
		void setFgColor(QColor c);
		void setBgColor(QColor c);
		void setGridColor(QColor c);
		void setAxesColor(QColor c);
		void setLinesColor(QColor c);
		void setShowGrid(bool b);
		void setDrawAnalog(bool b);
		void setShowLines(bool b);
		void setGridSpace(int s);
		void setBorder(int b);
		void setAnimSleepTime(int time);
		void setSignal(const Signal &sig);
		void setSignalProperties(SignalProperties &sp);
		void setSamplesDist(int d);

		// Get
		QColor getFgColor();
		QColor getBgColor();
		QColor getGridColor();
		QColor getAxesColor();
		QColor getLinesColor();
		Signal & getSignal();
		bool getShowGrid();
		bool getDrawAnalog();
		bool getShowLines();
		int getGridSpace();
		int getBorder();
		int getAnimSleepTime();
		int getAnimStatus();
		SignalProperties & getSignalProperties();
		int getSamplesDist();
		
		//Animation
		void startAnimation();
		void pauseAnimation();
		void stopAnimation();
		void resumeAnimation();
		
	
	protected:
		void contextMenuEvent(QContextMenuEvent * event );
		void mousePressEvent(QMouseEvent *event);
		void mouseMoveEvent(QMouseEvent *event);
		void mouseReleaseEvent(QMouseEvent *event);
		void timerEvent(QTimerEvent * event);
		void dragEnterEvent(QDragEnterEvent * event);
		void dropEvent(QDropEvent *event);
		void dragMoveEvent(QDragMoveEvent *event);
		void dragLeaveEvent(QDragLeaveEvent * event);
	private:
		
		void connectSlots();
		void createUI();
		void createMenus();
		void setDefaults();
		void applySignalProperties();
	
		void resizeRectCalcStep();
		void drawGrid();
		void drawAxes();
		void drawLines();
		void clearScene();
		void drawAnalogSignal();
		void drawDigitalSignal();
		
		void animateAnalog();
		void animateDigital();
		
		void zoomIn();
		
		QPointF signalToScene(const Point & p);
		Point sceneToSignal(const QPointF & p);
		
	public slots:
		void load();
		void save();
		void saveAsImage();
		void toggleGrid();
		void toggleLines();
		void toggleZoomLock();
		void changeSignalType();
		void zoomOut();
		void showPropDlg();
		void startStopAnim();
		void pauseResumeAnim();

};

#endif

