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
|
/*
* Copyright 2010 Matteo Agostinelli <agostinelli@gmail.com>
*
* 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 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 Library 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 "qalculate_engine.h"
#include <libqalculate/Calculator.h>
#include <libqalculate/ExpressionItem.h>
#include <libqalculate/Unit.h>
#include <libqalculate/Prefix.h>
#include <libqalculate/Variable.h>
#include <libqalculate/Function.h>
#include <QFile>
#include <QApplication>
#include <QClipboard>
#include <QDebug>
#include <KLocalizedString>
#include <KProtocolManager>
#include <KIO/Job>
QAtomicInt QalculateEngine::s_counter;
QalculateEngine::QalculateEngine(QObject* parent):
QObject(parent)
{
m_lastResult = "";
s_counter.ref();
if (!CALCULATOR) {
new Calculator();
CALCULATOR->terminateThreads();
CALCULATOR->loadGlobalDefinitions();
CALCULATOR->loadLocalDefinitions();
CALCULATOR->loadGlobalCurrencies();
CALCULATOR->loadExchangeRates();
}
}
QalculateEngine::~QalculateEngine()
{
if (s_counter.deref()) {
delete CALCULATOR;
CALCULATOR = NULL;
}
}
void QalculateEngine::updateExchangeRates()
{
QUrl source = QUrl("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
QUrl dest = QUrl::fromLocalFile(QFile::decodeName(CALCULATOR->getExchangeRatesFileName().c_str()));
KIO::Job* getJob = KIO::file_copy(source, dest, -1, KIO::Overwrite | KIO::HideProgressInfo);
connect( getJob, SIGNAL(result(KJob*)), this, SLOT(updateResult(KJob*)) );
}
void QalculateEngine::updateResult(KJob* job)
{
if (job->error()) {
qDebug() << i18n("The exchange rates could not be updated. The following error has been reported: %1",job->errorString());
} else {
// the exchange rates have been successfully updated, now load them
CALCULATOR->loadExchangeRates();
}
}
QString QalculateEngine::evaluate(const QString& expression)
{
if (expression.isEmpty()) {
return "";
}
QString input = expression;
QByteArray ba = input.replace(QChar(0xA3), "GBP").replace(QChar(0xA5), "JPY").replace('$', "USD").replace(QChar(0x20AC), "EUR").toLatin1();
const char *ctext = ba.data();
CALCULATOR->terminateThreads();
EvaluationOptions eo;
eo.auto_post_conversion = POST_CONVERSION_BEST;
eo.keep_zero_units = false;
eo.parse_options.angle_unit = ANGLE_UNIT_RADIANS;
eo.structuring = STRUCTURING_SIMPLIFY;
CALCULATOR->setPrecision(16);
MathStructure result = CALCULATOR->calculate(ctext, eo);
PrintOptions po;
po.number_fraction_format = FRACTION_DECIMAL;
po.indicate_infinite_series = false;
po.use_all_prefixes = false;
po.use_denominator_prefix = true;
po.negative_exponents = false;
po.lower_case_e = true;
po.base_display = BASE_DISPLAY_NORMAL;
result.format(po);
m_lastResult = result.print(po).c_str();
return m_lastResult;
}
void QalculateEngine::copyToClipboard(bool flag)
{
Q_UNUSED(flag);
QApplication::clipboard()->setText(m_lastResult);
}
|