//  QbTimeWidgets.h
//
// Time Edition Widgets
//
// November 2008 A Rochel
//

#ifndef QBTIME_WIDGETS_H
#define QBTIME_WIDGETS_H

#include <QObject>
#include "QdTime.h"     // QDOS
#include "KVDataBase.h"
#include "Viostream.h"
#include "TimeUtils.h"

#include <qvalidator.h>
#include "qt_version.h"
#include <qobject.h>
#include <qtooltip.h>

#include <QDateTimeEdit>
#include <QLineEdit>
#include <QDropEvent>
#include <QLayout>
#include <QPushButton>
#include <QSpinBox>

/*
class QLabel;
class QLineEdit;*/

class QbDurationWidget:public QLineEdit
{
	Q_OBJECT

	public:
		QbDurationWidget(QWidget *);
		
		bool getDuration(QDOS::Time &);
		
		void setDuration(const QDOS::Time&);
		void setActive(bool);
		
	signals:
		void getFocus();
		
	protected slots:
		void focusInEvent(QFocusEvent *);
		
		void dropEvent(QDropEvent *event);
		
		void dragEnterEvent(QDragEnterEvent *event);
		
	protected:
		bool active;
		QColor activeColor, inactiveColor;
		QColor activeText, inactiveText;

		QRegExpValidator * validator;
};

class QbDateTimeLineEdit:public QLineEdit
{
	Q_OBJECT
			
	public:
		QbDateTimeLineEdit ( QWidget * parent = 0 );

		QbDateTimeLineEdit ( const QDateTime & datetime, QWidget * parent = 0 );
		
		QDOS::Time time();
		
		void setTime(const QDOS::Time&);
		
		void setActive(bool);
		
		bool edited() {return hasBeenEdited;}
		
		double value();
		
		void setDouble(double value);
		
		void addToCurrent(int delta=0);
		bool active;
	signals:
		void getFocus();
		void valueChanged();
		void textEdited(const QString &);
		
	protected slots:
		void focusInEvent(QFocusEvent *);
		void slotValueChanged();
		void keyPressEvent(QKeyEvent *);
		
		void wheelEvent(QWheelEvent *);
		
		void increaseCurrent()
		{
			addToCurrent(1);
		}
		
		void decreaseCurrent()
		{
			addToCurrent(-1);
		}
		
		void mouseDoubleClickEvent ( QMouseEvent * event );

	protected:
				
		QRegExpValidator * validator;
		
		QColor activeColor, inactiveColor;
		QColor activeText, inactiveText;
		bool hasBeenEdited;
		QDOS::Time previousTime;
};

class QbDateTimeWidget:public QAbstractSpinBox
{
	Q_OBJECT
	
	public:
		QbDateTimeWidget(const QDateTime& dt, QWidget* parent=0);
		
		QbDateTimeWidget(QWidget* parent=0);
		
		void stepBy(int steps);
		
		QDOS::Time time(){return editor->time();}
		
		void setTime(const QDOS::Time& t){editor->setTime(t);}
		
		void setActive(bool a){editor->setActive(a);}
		
		bool edited(){return editor->edited();}
		
		double value(){return editor->value();}
		
		void setDouble(double value){editor->setDouble(value);}
	
	signals:
		void getFocus();
		void valueChanged();
		void textEdited(const QString &);
		
	protected slots:
		void dropEvent(QDropEvent *event);
		void dragEnterEvent(QDragEnterEvent *event);
		void droppedObjStart();
		void droppedObjCentre();
		void droppedObjEnd();
		
	protected:
	
		QbDateTimeLineEdit* editor;
		QAbstractSpinBox::StepEnabled stepEnabled () const;
		QDOS::QdObject_var droppedObject;
	
};

class QbTimeIntervalWidget:public QWidget
{
	Q_OBJECT
			
	public:
		QbTimeIntervalWidget(QWidget* parent);
		
#if QT_VERSION >= 0x040000 && QT_VERSION < 0x040300
		QbDateTimeLineEdit * _start_time_field;
		QbDateTimeLineEdit * _end_time_field;
		QbDateTimeLineEdit * _centre_time_field;
#else
		QbDateTimeWidget * _start_time_field;
		QbDateTimeWidget * _end_time_field;
		QbDateTimeWidget * _centre_time_field;
#endif
		QbDurationWidget* _duration_field;
		
		void setTimeInterval(QDOS::QdObject_var obj);
		void setTimeInterval(const QDOS::TimeInterval& obj);
		
		QDOS::QdTimeInterval_var getTimeInterval();
		
		QDOS::Time getDuration();
		
	signals:
		
		void intervalUpdated();
		
	public slots:
		
		void dropEvent(QDropEvent *event);
		void startFocus();
		void endFocus();
		void centreFocus();
		void durationFocus();
		void dragEnterEvent(QDragEnterEvent *event);

		void updateFields();
		
	private:
		
		QGridLayout * layout;
		int current, previous;
		
		void updateStartEnd();
		void updateStartCentre();
		void updateStartDuration();
		void updateEndCentre();
		void updateEndDuration();
		void updateCentreDuration();
		void updateSelection();

		bool allowUpdate;
};


#endif // QBTIME_WIDGETS_H
