/***************************************************************************
 *   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 "queryfieldbase.h"
 
 QueryFieldBase::QueryFieldBase(
	QWidget* parent,  const char* name, WFlags fl )
	: QWidget(parent, name, fl )
{
	bool turnOn_numberingForBooleanSearch = FALSE;
	if (turnOn_numberingForBooleanSearch)
	{
		number = new KPushButton(KGuiItem(
				"17",
		QString::null,
		"",
		""),
		this,"");
		number->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)5, 0, 0, number->sizePolicy().hasHeightForWidth() ) );
	//FIXME:not nice scaling here but I want all number buttons to have the same length
		number->setMinimumSize( QSize( 35, 1 ) );
		number->setMaximumSize( QSize( 35, 32767 ) );
		QFont number_font(  number->font() );
		number_font.setBold( TRUE );
		number->setFont( number_font ); 
		number->setAcceptDrops( TRUE );
		number->setToggleButton( TRUE );
		number->setOn( TRUE );
		number->setProperty( "isDragEnabled", QVariant( TRUE, 0 ) );
	}
	else
	{
		number = 0;
	}
	queryFieldAdd = new KPushButton(KGuiItem(
			"queryFieldAdd",
	"edit_add",
	"add search field",
	"add search field"),
	this,"add");
	queryFieldAdd->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, 0, 0, queryFieldAdd->sizePolicy().hasHeightForWidth() ) );
	queryFieldAdd->setToggleButton( TRUE );
	//if turned ON, bug in update state on init
	queryFieldAdd->setOn( FALSE );
	queryFieldAdd->setFlat( TRUE );
	
	QToolTip::add(queryFieldAdd , tr2i18n( "When ON, all search-results must match this query. " ) );
	
	queryFieldSub = new KPushButton(KGuiItem(
			"queryFieldSub",
	"edit_remove",
	"",
	""),
	this,"");
	 
	queryFieldSub->setEnabled( TRUE );
	queryFieldSub->setToggleButton( TRUE );
	queryFieldSub->setOn( FALSE );
	QToolTip::add(queryFieldSub , tr2i18n( "When ON, all results matching this query will be hidden. " ) );
	mainField = new KComboBox( FALSE, this, "mainField" );
	
	spacer7 = new QSpacerItem( 5, 16, QSizePolicy::Minimum, QSizePolicy::Expanding );
	languageChange();
// 	resize( QSize(180, 40).expandedTo(minimumSizeHint()) );
	clearWState( WState_Polished );

	
	//layout
	if (number != 0)
	{
		number->setSizePolicy(
			QSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding,1,1,FALSE)
				    );
	}
	queryFieldAdd->setSizePolicy(
			QSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding,1,1,FALSE)			       );
	queryFieldSub->setSizePolicy(
			QSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding,1,1,FALSE)
				    );
	mainField->setSizePolicy(
			QSizePolicy(QSizePolicy::Minimum,QSizePolicy::Expanding,1,1,FALSE)
				);
	layoutA = new QHBoxLayout(this);
	if (number != 0)
	{
		layoutA->addWidget(number);
	}

	layoutA->addWidget(queryFieldAdd);
	layoutA->addWidget(queryFieldSub);
	layoutA->addSpacing(10);

	layoutA->addWidget(mainField);
// 	layoutA->addSpacing(10);


	
    // signals and slots connections
	connect( queryFieldAdd, SIGNAL( toggled(bool) ), queryFieldSub, SLOT( setDisabled(bool) ) );
	connect( queryFieldSub, SIGNAL( toggled(bool) ), queryFieldAdd, SLOT( setDisabled(bool) ) );
	connect( mainField, SIGNAL( textChanged(const QString&) ), this, SLOT( slotMainFieldChanged(const QString&) ) );
}

/*
 *  Destroys the object and frees any allocated resources
 */
QueryFieldBase::~QueryFieldBase()
{
    // no need to delete child widgets, Qt does it all for us
}

/*
 *  Sets the strings of the subwidgets using the current
 *  language.
 */
void QueryFieldBase::languageChange()
{
	setCaption( tr2i18n( "Form1" ) );
	if (number != 0)
	{
		number->setText( tr2i18n( "&99" ) );
		number->setAccel( QKeySequence( tr2i18n( "Alt+9" ) ) );
	}
	queryFieldAdd->setText( QString::null );
	queryFieldAdd->setAccel( QKeySequence( tr2i18n( "Ctrl++" ) ) );
	queryFieldSub->setText( QString::null );
	queryFieldSub->setAccel( QKeySequence( tr2i18n( "Ctrl+-, Ctrl+S, Ctrl+-, Ctrl++" ) ) );
	mainField->clear();
	
	mainField->insertItem( tr2i18n( "Any string" ) );
	mainField->insertItem( tr2i18n( "Content" ) );
	mainField->insertItem( tr2i18n( "MetaData" ) );
	mainField->insertItem( tr2i18n( "Mod. Time" ) );
	mainField->insertItem( tr2i18n( "URL" ) );
	mainField->insertItem( tr2i18n( "Media" ) );
}

void QueryFieldBase::slotMainFieldChanged(const QString&)
{
	qWarning( "QueryFieldBase::slotMainFieldChanged(const QString&): Not implemented yet" );
}

#include "queryfieldbase.moc"
