blob: 2755c08b71b54599a538a2dd55506b95b6760ce9 (
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
|
#include "qalculator.hpp"
#include <QObject>
#include <libqalculate/qalculate.h>
namespace caelestia {
Qalculator::Qalculator(QObject* parent)
: QObject(parent) {
if (!CALCULATOR) {
new Calculator();
CALCULATOR->loadExchangeRates();
CALCULATOR->loadGlobalDefinitions();
CALCULATOR->loadLocalDefinitions();
}
}
QString Qalculator::eval(const QString& expr, bool printExpr) const {
if (expr.isEmpty()) {
return QString();
}
EvaluationOptions eo;
PrintOptions po;
std::string parsed;
std::string result = CALCULATOR->calculateAndPrint(
CALCULATOR->unlocalizeExpression(expr.toStdString(), eo.parse_options), 100, eo, po, &parsed);
std::string error;
while (CALCULATOR->message()) {
if (!CALCULATOR->message()->message().empty()) {
if (CALCULATOR->message()->type() == MESSAGE_ERROR) {
error += "error: ";
} else if (CALCULATOR->message()->type() == MESSAGE_WARNING) {
error += "warning: ";
}
error += CALCULATOR->message()->message();
}
CALCULATOR->nextMessage();
}
if (!error.empty()) {
return QString::fromStdString(error);
}
if (printExpr) {
return QString("%1 = %2").arg(parsed).arg(result);
}
return QString::fromStdString(result);
}
} // namespace caelestia
|