#ifndef INTERVAL_WIDGETS_H
#define INTERVAL_WIDGETS_H

#include "QbTimeWidgets_qt4.h"
#include "IntervalSliders.h"

class TimeIntervalWidget: public QWidget
{
	Q_OBJECT

	public:
		
		TimeIntervalWidget(QWidget* parent=NULL);

		~TimeIntervalWidget();

		QmjdTime getStart() const;
		QmjdTime getEnd() const;
		double getDuration() const;
		QmjdTimeInterval getInterval() const;

		void setStart(const QmjdTime& start);
		void setEnd(const QmjdTime& end);
		void setInterval(const QmjdTime& start, const QmjdTime& end, bool setViewRangeToo=true);
		void setInterval(const QmjdTimeInterval& interval, bool setViewRangeToo=true);
		void setInterval(const QdObject_var& obj, bool setViewRangeToo=true);

		void clearEventTable();
		void setEventTable(const QdTimeIntervalSeq& et);
		QdTimeIntervalSeq getEventTable() const;
		void setAvailabilityRange(const QmjdTime& start, const QmjdTime& end);
		void setAvailabilityRange(const QmjdTimeInterval& interval);

        void setViewRange(const QmjdTimeInterval& tintv);
        QmjdTimeInterval getViewRange() const;

        void setBarString(const QString& s)
        {
            slider->setBarString(s);
        }
        
		void resetView();

		void expand();
		void collapse();

		void setAltPalette(bool alt);

		bool hasFocus() const;
		
		
	signals:
		void intervalUpdated();

	public slots:

		void expand_collapse();
        void expand_collapse_slider();
        void expand_collapse_fields();
		
		void updateStartEnd();
		void updateStartCentre();
		void updateStartDuration();
		void updateEndCentre();
		void updateEndDuration();
		void updateCentreDuration();
		void updateSelection();
		void updateFields();
		void startFocus();
		void endFocus();
		void centreFocus();
		void durationFocus();

		void dropEvent(QDropEvent *event);
		void dragEnterEvent(QDragEnterEvent *event);

		void updateAllFields();

		void appendIntervalSlot();
		void changeEventSlot();
		void changeIntervalSlot();

		void saveInterval();
		void saveEventTable();

		void nextEvent();
		void previousEvent();
		
	protected:

		void updateDisplay();

		int current, previous;
		
		QbDateTimeWidget* fromWidget;
		QbDateTimeWidget* toWidget;
		QLabel* centreLabel;
		QbDateTimeWidget* centreWidget;
		QLabel* durationLabel;
		QbDurationWidget* durationWidget;
		TimeSlider* slider;

		QmjdTimeInterval currentInterval;
		QdTimeIntervalSeq events;

		QPushButton* previousEventButton;
		QPushButton* nextEventButton;
		
};

class ScalarIntervalWidget: public QWidget
{
	Q_OBJECT

	public:
		ScalarIntervalWidget(QWidget* parent=NULL);

		~ScalarIntervalWidget();

		double getStart() const;
		double getEnd() const;
		double getLength() const;

		void setStart(const double start);
		void setEnd(const double end);
		void setInterval(double start, double end, bool setViewRangeToo=true);

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

		void resetView(){}

		void expand();
		void collapse();

		bool hasFocus() const;

		void setAltPalette(bool alt);

        void setBarString(const QString& s)
        {
            slider->setBarString(s);
        }

	signals:
		void intervalUpdated();

	public slots:

		void expand_collapse();
		
		void updateStartEnd();
		void updateStartCentre();
		void updateStartLength();
		void updateEndCentre();
		void updateEndLength();
		void updateCentreLength();
		void updateSelection();
		void updateFields();
		void startFocus();
		void endFocus();
		void centreFocus();
		void lengthFocus();
		void updateAllFields();
		
	protected:

		

		void updateDisplay();

		int current, previous;
		
		QguWLinput* fromWidget;
		QguWLinput* toWidget;
		QLabel* centreLabel;
		QguWLinput* centreWidget;
		QLabel* lengthLabel;
		QguWLinput* lengthWidget;

		double currentStart;
		double currentEnd;

		ScalarIntervalSlider* slider;

		bool altPalette;
};

#endif
