/***************************************************************************
 *   Copyright (C) 2005  Tobi Vollebregt                                   *
 *   tobivollebregt@gmail.com                                              *
 *                                                                         *
 *   Copyright (C) 2005 by Joe Ferris                                      *
 *   jferris@optimistictech.com                                            *
 *                                                                         *
 *   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 Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include <tdeapplication.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <krun.h>
#include <kurl.h>
#include <tdelocale.h>

#include <tqclipboard.h>

#include "calculatorcatalog.h"
#include "expression.h"
#include "katapultitem.h"
#include "actionevalexpr.h"


static TQString formatNumber(const TQString& numStr)
{
	return TDEGlobal::locale()->formatNumber(numStr, false, 0);
}

ActionEvaluateExpression::ActionEvaluateExpression()
	: KatapultAction(), _expr(0)
{
}

ActionEvaluateExpression::~ActionEvaluateExpression()
{
}

TQString ActionEvaluateExpression::text() const
{
	if (_expr->parseError()) {
		return i18n("Evaluate Expression");
	} else {
		// Format result.
		int digits = _expr->catalog()->fracDigits();
		TQChar f = _expr->catalog()->scientific() ? 'g' : 'f';
		TQString num = TQString::number(_expr->result(), f, digits);
		// Strip trailing zeroes.
		if (f == 'f' && digits != 0) {
			while (num.endsWith("0")) {
				num = num.left(num.length() - 1);
			}
			if (num.endsWith(".")) {
				num = num.left(num.length() - 1);
			}
		}
		// Localize (choose right implementation based on KDE version, see above).
		return formatNumber(num);
	}
}

TQPixmap ActionEvaluateExpression::icon(int size) const
{
	return TDEGlobal::iconLoader()->loadIcon("xcalc", TDEIcon::NoGroup, size);
}

bool ActionEvaluateExpression::accepts(const KatapultItem* item) const
{
	bool accept = strcmp(item->className(), "Expression") == 0;
	if (accept) {
		_expr = (const Expression*)item;
	}
	return accept;
}

void ActionEvaluateExpression::execute(const KatapultItem* item) const
{
	if (strcmp(item->className(), "Expression") == 0) {
		_expr = (const Expression*)item;

		//evaluate expression with assignments enabled
		_expr->evaluate(true);

		// Copy calculation and result into clipboard (unless there's a parse error).
		if (!_expr->parseError()) {
			TQClipboard *cb = TQApplication::clipboard();
			TQString s = _expr->catalog()->formatString();
			s.replace("%1", _expr->text());
			s.replace("%2", text());
			cb->setText(s, TQClipboard::Clipboard);
			cb->setText(s, TQClipboard::Selection);
		}
	}
}
