/* This file is part of the KDE project
   Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kexibooltableedit.h"

#include <kexidb/field.h>

#include <tqpainter.h>
#include <tqapplication.h>
#include <tqclipboard.h>

#include <tdeglobal.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tdeglobalsettings.h>


KexiBoolTableEdit::KexiBoolTableEdit(KexiTableViewColumn &column, TQWidget *parent)
 : KexiTableEdit(column, parent)
{
	setName("KexiBoolTableEdit");
	kdDebug() << "KexiBoolTableEdit: m_origValue.typeName()==" << m_origValue.typeName() << endl;
	kdDebug() << "KexiBoolTableEdit: type== " << field()->typeName() << endl;
	m_hasFocusableWidget = false;
	m_acceptEditorAfterDeleteContents = true;
	m_usesSelectedTextColor = false;
}

KexiBoolTableEdit::~KexiBoolTableEdit()
{
}

void KexiBoolTableEdit::setValueInternal(const TQVariant& /*add*/, bool /*removeOld*/)
{
	m_currentValue = m_origValue;
	//nothing to do more...
}

bool KexiBoolTableEdit::valueIsNull()
{
	return m_currentValue.isNull();
}

bool KexiBoolTableEdit::valueIsEmpty()
{
	return m_currentValue.isNull();
}

TQVariant KexiBoolTableEdit::value()
{
//	ok = true;
	return m_currentValue;
}

void KexiBoolTableEdit::clear()
{
	if (field()->isNotNull())
		m_currentValue = TQVariant(false);
	else
		m_currentValue = TQVariant();
}

bool KexiBoolTableEdit::cursorAtStart()
{
	return true;
}

bool KexiBoolTableEdit::cursorAtEnd()
{
	return true;
}

void KexiBoolTableEdit::setupContents( TQPainter *p, bool focused, const TQVariant& val, 
	TQString &txt, int &align, int &x, int &y_offset, int &w, int &h  )
{
	Q_UNUSED(focused);
	Q_UNUSED(txt);
	Q_UNUSED(align);
	Q_UNUSED(x);
#ifdef TQ_WS_WIN
//	x = 1;
	y_offset = -1;
#else
//	x = 1;
	y_offset = 0;
#endif
	if (p) {
		int s = TQMAX(h - 5, 12);
		s = TQMIN( h-3, s );
		s = TQMIN( w-3, s );//avoid too large box
		TQRect r( TQMAX( w/2 - s/2, 0 ) , h/2 - s/2 /*- 1*/, s, s);
//already set ouotside:		p->setPen(TQPen(colorGroup().text(), 1));
		p->drawRect(r);
		if (val.isNull()) { // && !field()->isNotNull()) {
			p->drawText( r, TQt::AlignCenter, "?" );
		}
		else if (val.toBool()) {
			p->drawLine(r.x(), r.y(), r.right(), r.bottom());
			p->drawLine(r.x(), r.bottom(), r.right(), r.y());
		}
	}
}

void KexiBoolTableEdit::clickedOnContents()
{
	if (field()->isNotNull())
		m_currentValue = TQVariant( !m_currentValue.toBool() );
	else {
		// null allowed: use the cycle: true -> false -> null
		if (m_currentValue.isNull())
			m_currentValue = TQVariant( true );
		else
			m_currentValue = m_currentValue.toBool() ? TQVariant( false ) : TQVariant();
	}
}

void KexiBoolTableEdit::handleAction(const TQString& actionName)
{
	if (actionName=="edit_paste") {
		emit editRequested();
		bool ok;
		const int value = tqApp->clipboard()->text( TQClipboard::Clipboard ).toInt(&ok);
		if (ok) {
			m_currentValue = (value==0) ? TQVariant(false) : TQVariant(true);
		}
		else {
			m_currentValue = field()->isNotNull() 
				? TQVariant(false) : TQVariant();
		}
		repaintRelatedCell();
	}
	else if (actionName=="edit_cut") {
		emit editRequested();
//! @todo handle defaultValue...
		m_currentValue = field()->isNotNull() 
			? TQVariant(false) : TQVariant();
		handleCopyAction(m_origValue, TQVariant());
		repaintRelatedCell();
	}
}

void KexiBoolTableEdit::handleCopyAction(const TQVariant& value, const TQVariant& visibleValue)
{
	Q_UNUSED(visibleValue);
	if (value.type()==TQVariant::Bool)
		tqApp->clipboard()->setText(value.toBool() ? "1" : "0");
	else
		tqApp->clipboard()->setText(TQString());
}

int KexiBoolTableEdit::widthForValue( TQVariant &val, const TQFontMetrics &fm )
{
	Q_UNUSED(fm);
	return val.toPixmap().width();
}

//======================================================

KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiBoolEditorFactoryItem, KexiBoolTableEdit)

#include "kexibooltableedit.moc"

