#ifndef INTERVAL_SLIDERS_H
#define INTERVAL_SLIDERS_H

#include "QdTime.h"
#include <QWidget>
#include <QPainter>
#include <QDialog>

class TimeIntervalWidget;
class ScalarIntervalWidget;

class TimeIntervalDialog: public QDialog
{
	Q_OBJECT
	public:
		TimeIntervalDialog(const QDOS::QmjdTimeInterval& interval, QWidget* parent=0, Qt::WindowFlags f=0);
		
		~TimeIntervalDialog();
		
		QDOS::QmjdTimeInterval newRange;
		
	protected slots:
		
		void validated();
		void cancelled();
		
	protected:
		TimeIntervalWidget* widget;
};

class TimeSlider: public QWidget
{
	Q_OBJECT
	public:
		TimeSlider(TimeIntervalWidget* parent, QDOS::QmjdTimeInterval* currentInterval, QDOS::QdTimeIntervalSeq* currentEvents);

		~TimeSlider();

		void setViewRange(const QDOS::QmjdTimeInterval& range);
		void setViewRange(const QDOS::QdTimeIntervalSeq& events);
        inline QDOS::QmjdTimeInterval getViewRange() const
        {
            return viewRange;
        }
		void setAvailabilityRange(const QDOS::QmjdTimeInterval& range);
		void unsetAvailabilityRange();
        void updateDisplayedString();

        inline void setBarString(const QString& s)
        {
            barString=s;
            update();
        }

		
	public slots:
		inline void zoomIn(){zoom(0.5);}
		inline void zoomOut(){zoom(2.);}
		inline void offsetLeft(){offsetView(-0.5);}
		inline void offsetRight(){offsetView(0.5);}
		void resetView();
		void changeViewRangeSlot();

	signals:
		void intervalChanged();
		
	protected:
		
		void paintEvent(QPaintEvent* e);
		void leaveEvent(QEvent* e);
		void mouseMoveEvent(QMouseEvent* e);
		void mouseDoubleClickEvent(QMouseEvent* e); // selects an event
		void mousePressEvent(QMouseEvent* e);
		void mouseReleaseEvent(QMouseEvent* e);

		QDOS::QmjdTimeInterval* currentInterval;
		QDOS::QmjdTimeInterval availabilityRange;
		QDOS::QmjdTimeInterval viewRange;
		QDOS::QmjdTimeInterval savedViewRange;
		QDOS::QdTimeIntervalSeq* events;
		TimeIntervalWidget* parent;

		double timeToX(const QDOS::QmjdTime&);
		QDOS::QmjdTime xToTime(double);
		QString displayedString;
        QString barString;

		int mouseMode;
		double relativePosition;

		void zoom(double);
		void offsetView(double);
		bool hasAvailabilityRange;

		int getEventIndex(const QDOS::QmjdTime&);
		
};

class ScalarIntervalSlider: public QWidget
{
	Q_OBJECT
	public:
		ScalarIntervalSlider(ScalarIntervalWidget* parent, double* start, double* end);

		~ScalarIntervalSlider();

		void setViewRange(double start, double end);
		void setAvailabilityRange(double start, double end);
		void unsetAvailabilityRange();
        void updateDisplayedString();

        inline void setBarString(const QString& s)
        {
            barString=s;
            update();
        }

		
	public slots:
		inline void zoomIn(){zoom(0.5);}
		inline void zoomOut(){zoom(2.);}
		inline void offsetLeft(){offsetView(-0.5);}
		inline void offsetRight(){offsetView(0.5);}
		void resetView();
		void changeViewRangeSlot();

	signals:
		void intervalChanged();
		
	protected:
		
		void paintEvent(QPaintEvent* e);
		void leaveEvent(QEvent* e);
		void mouseMoveEvent(QMouseEvent* e);
		void mousePressEvent(QMouseEvent* e);
		void mouseReleaseEvent(QMouseEvent* e);

		double * currentStart;
		double * currentEnd;
		double availabilityStart;
		double availabilityEnd;
		double viewStart;
		double viewEnd;
		double savedViewStart;
		double savedViewEnd;
		ScalarIntervalWidget* parent;

		double valToX(double);
		double xToVal(double);
		QString displayedString;
        QString barString;

		int mouseMode;
		double relativePosition;

		void zoom(double);
		void offsetView(double);
		bool hasAvailabilityRange;

};


#endif
