
//  Copyright (C) 2009 by Arnaud Dupuis
//  a.dupuis@infinityperl.org
//  http://www.infinityperl.org
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the
//  Free Software Foundation, Inc.,
//  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

#ifndef ASSUMA_CUSTOMWIDGETS_H
#define ASSUMA_CUSTOMWIDGETS_H

#include <QLayout>
#include <QLabel>
#include <QWidget>
#include <QLineEdit>
#include <QTextEdit>
#include <QDateEdit>
#include <QDate>
#include <QLineEdit>
#include <QComboBox>
#include <QDoubleSpinBox>

// #ifdef ASSUMA_DEBUG
#include <QDebug>
// #endif

class AssumaCustomWidgetBase : public QWidget {
	Q_OBJECT
	public:
		AssumaCustomWidgetBase(QWidget *parent=0);
		QWidget* customFieldWidget();
		virtual QString valueAsString()=0;
		static QWidget* createEditor(QWidget *parent=0);
                QString labelAsString();
		QString id();
        signals:
                void valueChanged( const QString & );
	public slots:
		void setCustomFieldLabelString( const QString & );
		void setCustomFieldWidget( QWidget * );
                virtual void setCustomFieldValue( const QString & ) = 0;
		void setId( const QString & );
	private:
		QLabel *m_label;
		QWidget *m_widget;
		QString m_id;
};

class AssumaCustomWidgetOneLine : public AssumaCustomWidgetBase {
	Q_OBJECT
	public:
		AssumaCustomWidgetOneLine( QWidget *parent=0) : AssumaCustomWidgetBase(parent){
			QLineEdit *w = qobject_cast<QLineEdit *>(createEditor(this));
                        setCustomFieldWidget( w );
                        connect(w,SIGNAL(textChanged(const QString &)),this,SIGNAL(valueChanged(const QString &)));
		};
		static QWidget* createEditor(QWidget *parent){
			return new QLineEdit(parent);
		};
		QString valueAsString(){ 
			return qobject_cast<QLineEdit *>( customFieldWidget() )->text() ; 
		};
        public slots:
                void setCustomFieldValue( const QString & data ){ qobject_cast<QLineEdit *>( customFieldWidget() )->setText(data) ; };
};

class AssumaCustomWidgetMultipleLine : public AssumaCustomWidgetBase {
	Q_OBJECT
	public:
		AssumaCustomWidgetMultipleLine( QWidget *parent=0) : AssumaCustomWidgetBase(parent){
			QTextEdit *w = qobject_cast<QTextEdit *>(createEditor(this));
                        setCustomFieldWidget( w );
                        connect( w, SIGNAL(textChanged()), this, SLOT(onTextChanged()) );
		};
		QString valueAsString(){ 
			return qobject_cast<QTextEdit *>( customFieldWidget() )->toPlainText() ; 
		};
		static QWidget* createEditor(QWidget *parent){
			return new QTextEdit(parent);
		};
        public slots:
                void setCustomFieldValue( const QString & data ){ qobject_cast<QTextEdit *>( customFieldWidget() )->setPlainText(data) ; };
        private slots:
                void onTextChanged (){
                        emit(valueChanged(valueAsString()));
                };
};

class AssumaCustomWidgetDateEdit : public AssumaCustomWidgetBase {
	Q_OBJECT
        private slots:
                 void onDateChanged ( const QDate & /*date*/ ){
                         emit( valueChanged( valueAsString() ) );
                 };
	public:
		AssumaCustomWidgetDateEdit( QWidget *parent=0) : AssumaCustomWidgetBase(parent){
			QDateEdit *w = qobject_cast<QDateEdit *>(createEditor(this));
                        setCustomFieldWidget( w );
                        connect(w, SIGNAL(dateChanged ( const QDate & )), this, SLOT(onDateChanged(const QDate &)) );
		};
		static QWidget* createEditor(QWidget *parent){
			QDateEdit *w = new QDateEdit(parent);
			w->setCalendarPopup(true);
			w->setDisplayFormat("dd/MM/yyyy");
			return w;
		};
		void setDateDisplayFormat(const QString & format){ 
			qobject_cast<QDateEdit*>(customFieldWidget())->setDisplayFormat(format); 
		};
                QString valueAsString(){
			return qobject_cast<QDateEdit *>( customFieldWidget() )->text() ; 
		};
        public slots:
                void setCustomFieldValue( const QString & data ){
                        QDateEdit *w = qobject_cast<QDateEdit *>( customFieldWidget() ) ;
                        w->setDate( QDate::fromString( data, w->displayFormat() ) ) ;
                };
};

class AssumaCustomWidgetComboBox : public AssumaCustomWidgetBase {
	Q_OBJECT
	public:
		AssumaCustomWidgetComboBox( QWidget *parent=0) : AssumaCustomWidgetBase(parent){
			QComboBox *w = qobject_cast<QComboBox *>(createEditor(this));
                        setCustomFieldWidget( w );
                        connect( w, SIGNAL(activated(const QString &)), this, SIGNAL(valueChanged(const QString &)) );
		};
		static QWidget* createEditor(QWidget *parent){
			return new QComboBox(parent);
		};
		void addItem(const QString & item, const QIcon & icon = QIcon() ){
			QComboBox *w = qobject_cast<QComboBox*>( customFieldWidget() );
			w->addItem(icon,item);
		};
		QString valueAsString(){
			return qobject_cast<QComboBox *>( customFieldWidget() )->currentText() ; 
		};
        public slots:
                void setCustomFieldValue( const QString & data ){
                        QComboBox *w= qobject_cast<QComboBox *>( customFieldWidget() );
                        w->setCurrentIndex( w->findText ( data, Qt::MatchExactly | Qt::MatchCaseSensitive ) ) ;
                };
};

class AssumaCustomWidgetFeeEditor : public AssumaCustomWidgetBase {
	Q_OBJECT
	public:
		AssumaCustomWidgetFeeEditor( QWidget *parent=0) : AssumaCustomWidgetBase(parent){
			QDoubleSpinBox *w = qobject_cast<QDoubleSpinBox *>(createEditor(this));
			w->setMaximum(10000);
			setCustomFieldWidget( w );
                        connect( w, SIGNAL(valueChanged(const QString &)), this, SIGNAL(valueChanged(const QString &)) );
		};
		static QWidget* createEditor(QWidget *parent){
			return new QDoubleSpinBox(parent);
		};
		QString valueAsString(){
			QDoubleSpinBox *w = qobject_cast<QDoubleSpinBox *>( customFieldWidget() );
			return w->textFromValue( w->value() ) ; 
		};
        public slots:
                void setCustomFieldValue( const QString & data ){
                        QDoubleSpinBox *w= qobject_cast<QDoubleSpinBox *>( customFieldWidget() );
                        w->setValue( data.toDouble() );
                };
};

#endif
