/***************************************************************************
 *   Copyright (C) 2005 by xavier helluy   *
 *   xavier.helluy@web.de   *
 *                                                                         *
 *   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 2 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.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/

#include "queryfieldtime.h"
#include <qsizepolicy.h> 
#include <kguiitem.h>
#include <kpushbutton.h>
#include <kiconloader.h>
#include <kglobal.h>
#include <kdebug.h>

QueryFieldTime::QueryFieldTime( QWidget* parent, const char* name, WFlags fl )
	: QWidget( parent, name, fl )
{
// 	sup = new KPushButton(KGuiItem("","1leftarrow",
// 			      "add search field","add search field"),this,"add");
	min =  new QLabel(this);
	min->setPixmap(  KGlobal::iconLoader()->loadIcon((QString) "1rightarrow",KIcon::Small ));
	
// 	new KPushButton(KGuiItem("","1rightarrow","add search field",
// 				"add search field"),this,"add");
	
	sup = new QLabel(this);
	sup->setPixmap(  KGlobal::iconLoader()->loadIcon((QString) "1leftarrow",KIcon::Small ));
	
// 	sup->setMinimumSize(20,26);
	sup->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)1, 0, 0, FALSE ) );
	min->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)1, 0, 0, FALSE ) );

	minDate = new KDateEdit( this, "minDate" );
	minDate->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 0, 0, FALSE ) );
	minTime = new KTimeEdit( this,QTime::currentTime (), "minTime" );
	minTime->setSizePolicy(			QSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding,1,1,FALSE)
			      );
	spacer14 = new QSpacerItem( 21, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
	maxDate = new KDateEdit( this, "maxDate" );
	maxDate->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 0, 0, FALSE ) );
	maxTime = new KTimeEdit( this,QTime::currentTime (), "maxTime" );
	maxTime->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 0, 0, FALSE ) );
//     resize( QSize(491, 50).expandedTo(minimumSizeHint()) );
	clearWState( WState_Polished );
    
	layoutA = new QHBoxLayout(this);
	layoutA->addWidget(min);
	layoutA->addWidget(minDate);
	layoutA->addWidget(minTime);
	layoutA->addWidget(sup);
	layoutA->addWidget(maxDate);
	layoutA->addWidget(maxTime);
// 	this->show();
	this->setShown(TRUE);


    //     setGeometry(0,0,newWidth, layoutA->minimumSize().height() );
	kdDebug() << "QueryField:: DateTime:"  << layoutA->minimumSize()  << endl;
	kdDebug() << "QueryField:: DateTime:"  << layoutA->maximumSize()  << endl;
    

    
}


QString QueryFieldTime::getQuery()
{
	//QDate:isValid() QTime::isValid()
	QTime time1 = this->minTime->getTime();
	QTime time2 = this->maxTime->getTime();
	QDate date1 = this->minDate->date();
	QDate date2 = this->maxDate->date();
	
	QDateTime *dt1;
	QDateTime *dt2;
	QString dt1str;
	QString dt2str;
	if (time1.isValid() && date1.isValid())
	{
		dt1    = new QDateTime(date1, time1);
// 		dt1str = dt1->toString(Qt:ISODate);
		dt1str = dt1->toString("yyyyMMddhhmm");
	}
	if (time2.isValid() && date2.isValid())
	{
		dt2    = new QDateTime(date2, time2);
// 		dt2str = dt2->toString(Qt:ISODate);
		dt2str = dt2->toString("yyyyMMddhhmm");
	}
	
	//this query is standard-query parser resistant
	//it should not be sent to standard-query parser, but it CAN
	QString query;
	if (!(dt1str.isEmpty() || dt2str.isEmpty()))
	{
		query = "dateKey:[" + dt1str + " " + dt2str + "]";
	}
// 	KGlobal::locale()->formatDateTime( t ); no locale dependent
	delete dt1;
	delete dt2;
	return query;	
}

#include "queryfieldtime.moc"
