/**************************************************************************
 *   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 "queryfield.h"

#include <kpushbutton.h>
#include <kdebug.h>
#include <kcombobox.h>
#include <qlayout.h>
#include "klineedit.h"
#include "qlineedit.h"

//No real plugins yet , so a lot of repetitive code!
//but will be one day
#include <qsizepolicy.h> 
#include <kguiitem.h>
#include <kpushbutton.h>
#include <kiconloader.h>
#include <kglobal.h>
/*
helper-> Queryfield objects are children of viewport QueryField(this->viewport( )
helper gets geometry of the Querfield objects and adjust the scrollbar ansd view
Now, Queryfield is a QWidget made of 3 widgets: the baseWidget, the managerWidget and the SpecificPlugin
*/

QueryField::QueryField(QWidget* parent,  int /*itemNum*/ , int convenienceWidth , const char* name, WFlags fl)
	:QWidget(parent, name, fl ),
	m_convenienceWidth(convenienceWidth), m_newWidth(0), m_newHeight(0),m_minimumWidth(0),m_plus(FALSE),m_minus(FALSE),
	pMD(0),pC(0),pDT(0),
	pMedia(0),pURL(0),pAny(0),pl(0)
	

{
	kdDebug() << "QueryField::QueryField() INIT:"<< endl;

	//inits base and manager
	m_queryFieldBase 	= new QueryFieldBase   (this,name,fl);
	m_queryFieldManager     = new QueryFieldManager(this,name,fl);
	
	pl = new QHBoxLayout( this );
	pl->addWidget( m_queryFieldBase,1,0 );
	pl->addWidget( m_queryFieldManager,0,0);
	
	//the parent needs to know the Id of the queryfield click FIXME this->delete
	connect ( m_queryFieldManager->removeQueryField, SIGNAL(clicked() ),
		  this , SLOT(slotRemoveQueryField() ) );
	
	//mainField is the combobox-selection used to decide which plugin to show
	connect ( m_queryFieldBase->mainField , SIGNAL(activated(const QString &) ),
		  this , SLOT(slotCreatePlugin(const QString &)));
	
	connect ( m_queryFieldBase->queryFieldAdd , SIGNAL(toggled(bool)),
		  this , SLOT(slotQueryPlus(bool) ));

	connect ( m_queryFieldBase->queryFieldSub , SIGNAL(toggled(bool)),
		  this , SLOT(slotQueryMinus(bool) ));

	kdDebug() << "QueryField::QueryField() :"<< endl;

	//the date/time plugin is the longest one. So calculate geometries
	//as a function of it. This means
	
}


QueryField::~QueryField()
{
}



//informs the searchhelper 
void QueryField::slotRemoveQueryField()
{
	kdDebug() << "QueryField::slotRemoveQueryField CALLED:"  << endl;
	emit removeQueryFieldClicked( itemNum() );
}

void  QueryField::setItemNum(int k)
{
	m_itemNum = k;
}


int QueryField::itemNum( )
{
	return this->m_itemNum;
}

//TODO: string 
void QueryField::slotCreatePlugin(const QString & name)
{
	kdDebug() << "QueryField::slotCreatePlugin CALLED:"  << name << endl;
	
	
	if (name == tr2i18n("MetaData"))
	{
		kdDebug() << "QueryPlugin::QueryPlugin MetaData CALLED:"  << name << endl;
		
		if (pAny!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin MetaData pC:"  << name << endl;

			pl->remove(pWdg);	
			delete pAny;
			pAny = 0;
		}
		if (pURL!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin MetaData pC:"  << name << endl;

			pl->remove(pWdg);	
			delete pURL;
			pURL = 0;
		}
		if (pMedia!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin MetaData pC:"  << name << endl;

			pl->remove(pWdg);	
			delete pMedia;
			pMedia = 0;
		}
		
		if (pC!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin MetaData pC:"  << name << endl;

			pl->remove(pWdg);	
			delete pC;
			pC = 0;
		}
		if (pDT!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin MetaData pDR:"  << name << endl;

			pl->remove(pWdg);	
			delete pDT;
			pDT = 0;
		}
		
		if (pMD==0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin MetaData pMD:"  << name << endl;

			pMD = new QueryFieldMetaData(this , "MetaData");
			pQF = (QueryFieldIface * ) pMD;
			pWdg = (QWidget *) pMD;	
			pl->insertWidget( 1,pWdg,1,0 );
			//FIXME: /2
			m_newWidth = m_queryFieldBase->sizeHint().width() + 
					pWdg->sizeHint().width()/2 + 
					m_queryFieldManager->sizeHint().width();
			m_newHeight = m_queryFieldBase->sizeHint().height() + 
					pWdg->sizeHint().height() + 
					m_queryFieldManager->sizeHint().height();
// 			emit sizeChanged(m_newWidth);
		}
		else
		{
			kdDebug() << "QueryPlugin::QueryPlugin MetaData pMD: called and exists!"  << name << endl;
			//do geometry update

		}
		
		kdDebug() << "QueryPlugin::QueryPlugin MetaData CALLED:"  << name << endl;
		
	}
	else if (name == tr2i18n("Content"))
	{
		kdDebug() << "QueryPlugin::QueryPlugin Content CALLED:"  << name << endl;

		if (pAny!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Content pC:"  << name << endl;

			pl->remove(pWdg);	
			delete pAny;
			pAny = 0;
		}
		if (pURL!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Content pC:"  << name << endl;

			pl->remove(pWdg);	
			delete pURL;
			pURL = 0;
		}
		if (pMedia!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Content pMedia:"  << name << endl;

			pl->remove(pWdg);	
			delete pMedia;
			pMedia = 0;
		}
		if (pDT!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Content pDT:"  << name << endl;

			pl->remove(pWdg);	
			delete pDT;
			pDT = 0;
		}
		if (pMD!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Content pMD:"  << name << endl;

			pl->remove(pWdg);	
			delete pMD;
			pMD = 0;
		}
		if (pC==0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Content pC:"  << name << endl;

			pC = new QueryFieldContent(this , "Content");
			pQF = (QueryFieldIface * ) pC;
			pWdg = (QWidget *) pC;
			pl->insertWidget( 1,pWdg,1,0 );
			m_newWidth = m_queryFieldBase->sizeHint().width() + 
					pWdg->sizeHint().width() + 
					m_queryFieldManager->sizeHint().width();
			m_newHeight = m_queryFieldBase->sizeHint().height() + 
					pWdg->sizeHint().height() + 
					m_queryFieldManager->sizeHint().height();
// 			emit sizeChanged(m_newWidth);
		}
		
		kdDebug() << "QueryPlugin::QueryPlugin Content CALLED:"  << name << endl;
		
	}
	
	else if (name == tr2i18n("Mod. Time"))
	{
		kdDebug() << "QueryPlugin::QueryPlugin Mod. Time CALLED:"  << name << endl;

		if (pAny!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Mod Time Any:"  << name << endl;

			pl->remove(pWdg);	
			delete pAny;
			pAny = 0;
		}

		if (pURL!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin ModTime URL:"  << name << endl;

			pl->remove(pWdg);	
			delete pURL;
			pURL = 0;
		}
		if (pMedia!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin ModTime Media:"  << name << endl;

			pl->remove(pWdg);	
			delete pMedia;
			pMedia = 0;
		}
		if (pC!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Mod. Time pC:"  << name << endl;
			pl->remove(pWdg);	
			delete pC;
			pC = 0;
		}
		if (pMD!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Mod. Time pMD:"  << name << endl;
			pl->remove(pWdg);	
			delete pMD;
			pMD = 0;
		}
		if (pDT==0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Mod. Time pDT:"  << name << endl;

			pDT = new QueryFieldTime(this , "ModTime");
			pQF = (QueryFieldIface * ) pDT;
			pWdg = (QWidget *) pDT;	
			pl->insertWidget( 1,pWdg,1,0 );
			
			//test 
			m_newWidth = m_queryFieldBase->sizeHint().width() + 
					pWdg->sizeHint().width() + 
					m_queryFieldManager->sizeHint().width();
			m_newHeight = m_queryFieldBase->sizeHint().height() + 
					pWdg->sizeHint().height() + 
					m_queryFieldManager->sizeHint().height();
// 			emit sizeChanged(m_newWidth);

		}
		kdDebug() << "QueryPlugin::QueryPlugin Mod. Time CALLED:"  << name << endl;
		
	}
	
	else if (name == tr2i18n("URL"))
	{
		if (pAny!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin URL :"  << name << endl;

			pl->remove(pWdg);	
			delete pAny;
			pAny = 0;
		}

		if (pMedia!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin URL pC:"  << name << endl;

			pl->remove(pWdg);	
			delete pMedia;
			pMedia = 0;
		}
		if (pDT!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin URL pC:"  << name << endl;

			pl->remove(pWdg);	
			delete pDT;
			pDT = 0;
		}
		if (pC!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin URL pC:"  << name << endl;
			pl->remove(pWdg);	
			delete pC;
			pC = 0;
		}
		if (pMD!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin URL pMD:"  << name << endl;
			pl->remove(pWdg);	
			delete pMD;
			pMD = 0;
		}

		if (pURL==0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin URL pDT:"  << name << endl;

			pURL = new QueryFieldURL(this , "URL");
			pQF = (QueryFieldIface * ) pURL;
			pWdg = (QWidget *) pURL;	
			pl->insertWidget( 1,pWdg,1,0 );
			
			//test 
			m_newWidth = m_queryFieldBase->sizeHint().width() + 
					pWdg->sizeHint().width() + 
					m_queryFieldManager->sizeHint().width();
			m_newHeight = m_queryFieldBase->sizeHint().height() + 
					pWdg->sizeHint().height() + 
					m_queryFieldManager->sizeHint().height();
// 			emit sizeChanged(m_newWidth);

		}
	}
	
	else if (name == tr2i18n("Media"))
	{	
		if (pAny!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Media pC:"  << name << endl;

			pl->remove(pWdg);	
			delete pAny;
			pAny = 0;
		}

		if (pURL!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin MetaData pC:"  << name << endl;

			pl->remove(pWdg);	
			delete pURL;
			pURL = 0;
		}
		if (pDT!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin MetaData pC:"  << name << endl;

			pl->remove(pWdg);	
			delete pMedia;
			pMedia = 0;
		}
		if (pC!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Mod. Time pC:"  << name << endl;
			pl->remove(pWdg);	
			delete pC;
			pC = 0;
		}
		if (pMD!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Mod. Time pMD:"  << name << endl;
			pl->remove(pWdg);	
			delete pMD;
			pMD = 0;
		}
		if (pMedia==0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin MEDIA:"  << name << endl;

			pMedia = new QueryFieldMedia(this , "Media");
			pQF = (QueryFieldIface * ) pMedia;
			pWdg = (QWidget *) pMedia;	
			pl->insertWidget( 1,pWdg,1,0 );
			
			//test 
			m_newWidth = m_queryFieldBase->sizeHint().width() + 
					pWdg->sizeHint().width() + 
					m_queryFieldManager->sizeHint().width();
			m_newHeight = m_queryFieldBase->sizeHint().height() + 
					pWdg->sizeHint().height() + 
					m_queryFieldManager->sizeHint().height();
// 			emit sizeChanged(m_newWidth);
		}
	}
	
	else if (name == tr2i18n( "Any string" ))
	{
		if (pMedia!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Any String:"  << name << endl;

			pl->remove(pWdg);	
			delete pMedia;
			pMedia = 0;
		}
		if (pURL!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin ANY:"  << name << endl;

			pl->remove(pWdg);	
			delete pURL;
			pURL = 0;
		}
		if (pDT!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin ANY:"  << name << endl;

			pl->remove(pWdg);	
			delete pDT;
			pDT = 0;
		}
		if (pC!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin ANY:"  << name << endl;
			pl->remove(pWdg);	
			delete pC;
			pC = 0;
		}
		if (pMD!=0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin ANY:"  << name << endl;
			pl->remove(pWdg);	
			delete pMD;
			pMD = 0;
		}
		if (pAny==0)
		{
			kdDebug() << "QueryPlugin::QueryPlugin Any string:"  << name << endl;

			pAny = new QueryFieldAnyThing(this , "Any");
			pQF = (QueryFieldIface * ) pAny;
			pWdg = (QWidget *) pAny;	
			pl->insertWidget( 1,pWdg,1,0 );
			
			//test 
			m_newWidth = m_queryFieldBase->sizeHint().width() + 
					pWdg->sizeHint().width() + 
					m_queryFieldManager->sizeHint().width();
			m_newHeight = m_queryFieldBase->sizeHint().height() + 
					pWdg->sizeHint().height() + 
					m_queryFieldManager->sizeHint().height();
// 			emit sizeChanged(m_newWidth);
		}
	}
	
// 	this->sizePolicy().setHorData(QSizePolicy::Ignored);
// 	this->sizePolicy().setVerData(QSizePolicy::Fixed);
	//layout pWdg
	
	this->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed,0,0,FALSE));
	
	m_queryFieldBase->setSizePolicy(
			QSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed,0,0,FALSE)
				       );
	
	m_newHeight = m_queryFieldBase->mainField->height();
	
	kdDebug() << "QueryField::slotCreatePlugin base geo: " << m_queryFieldBase->sizeHint()<<endl;
	kdDebug() << "QueryField::slotCreatePlugin main geo: " << pWdg->sizeHint()<<endl;
	kdDebug() << "QueryField::slotCreatePlugin addR geo: " << m_queryFieldManager->sizeHint()<<endl;
	kdDebug() << "QueryField::slotCreatePlugin nw    : " << m_newWidth<<endl;
	kdDebug() << "QueryField::slotCreatePlugin nh    : " << m_newHeight<<endl;
	kdDebug() << "QueryField::slotCreatePlugin layout: " << pl->sizeHint()<<endl;

	kdDebug() << "QueryField::slotCreatePlugin : m_convenienceWidth" <<m_convenienceWidth<<endl;
	kdDebug() << "QueryField::slotCreatePlugin : m_convenienceWidth" <<m_minimumWidth<<endl;

	emit minimumSizeChanged();
	
	kdDebug() << "QueryField::slotCreatePlugin exp: " << this->sizePolicy().expanding()<<endl;
	kdDebug() << "QueryField::slotCreatePlugin geo: " << this->geometry()<<endl;
}


void QueryField::slotQueryPlus(bool sig)
{
	kdDebug() << "QueryField::slotQueryPlus CALLED" << endl;

	if (sig)
	{
		m_plus = TRUE;
		m_minus = FALSE;

	}
	else
	{
		m_plus = FALSE;
		m_minus = FALSE;

	}
}

void QueryField::slotQueryMinus(bool sig)
{
	kdDebug() << "QueryField::slotQueryMinus CALLED" << endl;

	if (sig)
	{
		m_plus = FALSE;
		m_minus = TRUE;
		
	}
	else
	{
		m_plus = FALSE;
		m_minus = FALSE;
	}
}

bool QueryField::getPlus()
{
	kdDebug() << "QueryField::getPlus() CALLED" <<  m_plus << endl;

	return m_plus;
}

bool QueryField::getMinus()
{
	kdDebug() << "QueryField::getMinus() CALLED" <<  m_minus << endl;

	return m_minus;
}


#include "queryfield.moc"

