summaryrefslogtreecommitdiffstats
path: root/katapult/plugins/catalogs/calculatorcatalog/actionevalexpr.cpp
blob: 219fabf71fb339e4efaae2a483145daa13f03cce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/***************************************************************************
 *   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 <kapplication.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <krun.h>
#include <kurl.h>
#include <klocale.h>

#include <tqclipboard.h>

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


// Conditional compilation to not bloat the lib if
// KDE already has the following functions.
// Prevents a deprecated function warning too.

#if !KDE_IS_VERSION(3, 5, 0)

// Copied some code from KDE 3.5 to make it compile on 3.4.

// insert (thousands)-"separator"s into the non-fractional part of str
static void _insertSeparator(TQString &str, const TQString &separator, const TQString &decimalSymbol)
{
	// leave fractional part untouched
	TQString mainPart = str.section(decimalSymbol, 0, 0);
	TQString fracPart = str.section(decimalSymbol, 1, 1, TQString::SectionIncludeLeadingSep);
	if (fracPart==decimalSymbol) fracPart=TQString();
	for (int pos = mainPart.length() - 3; pos > 0; pos -= 3)
		mainPart.insert(pos, separator);
	str = mainPart + fracPart;
}

// This was KLocale::formatNumber(const TQString&, bool, int)

static TQString formatNumber(const TQString &numStr)
{
	TQString tmpString = numStr;

	// Skip the sign (for now)
	bool neg = (tmpString[0] == '-');
	if (neg  ||  tmpString[0] == '+') tmpString.remove(0, 1);

	// Split off exponential part (including 'e'-symbol)
	TQString mantString = tmpString.section('e', 0, 0, TQString::SectionCaseInsensitiveSeps);
	TQString expString = tmpString.section('e', 1, 1, TQString::SectionCaseInsensitiveSeps | TQString::SectionIncludeLeadingSep);
	if (expString.length()==1) expString=TQString();

	// Replace dot with locale decimal separator
	mantString.replace(TQChar('.'), KGlobal::locale()->decimalSymbol());

	// Insert the thousand separators
	_insertSeparator(mantString, KGlobal::locale()->thousandsSeparator(), KGlobal::locale()->decimalSymbol());

	// How can we know where we should put the sign?
	mantString.prepend(neg?KGlobal::locale()->negativeSign():KGlobal::locale()->positiveSign());

	return mantString +  expString;
}

#else
// TDE_VERSION >= 3.5.0

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

#endif


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 KGlobal::iconLoader()->loadIcon("xcalc", KIcon::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);
		}
	}
}