-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculateengine.cpp
More file actions
88 lines (71 loc) · 2.28 KB
/
Copy pathcalculateengine.cpp
File metadata and controls
88 lines (71 loc) · 2.28 KB
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
#include "calculateengine.h"
#include <QDebug>
#include "monthmodel.h"
EngineInitializer::EngineInitializer()
{
/*evaluate(QString::fromLatin1("function pad(num, size)"
"{"
" num = num.toString();"
" while (num.length < size) num = '0' + num;"
" return num;"
"}"));*/
evaluate(QString::fromLatin1("function month(year, month)"
"{"
" return data[year][month]"
"}"));
}
QString CalculateEngine::calculate(const QString& expression) const
{
if (expression.isEmpty())
return expression;
// calculating
QScriptValue result = _engine().evaluate(QString::fromLatin1("Number(%1).toFixed(2)").arg(expression));
if (_engine().hasUncaughtException())
{
int line = _engine().uncaughtExceptionLineNumber();
qDebug() << "Uncaught exception at line" << line << ":" << result.toString();
return QString::fromLatin1("ERROR");
}
const QString ret = result.toString();
if (ret == QString::fromLatin1("undefined"))
{
qDebug() << "undefined value for expression " << expression;
return QString();
}
return ret;
}
void CalculateEngine::setMonthModel(int y, int n, MonthModel* m) const
{
QScriptEngine& e = _engine();
QScriptValue data = e.globalObject().property("data");
if (!data.isValid())
{
data = e.newObject();
e.globalObject().setProperty("data", data);
}
QScriptValue year = data.property(y);
if (!year.isValid())
{
year = e.newObject();
data.setProperty(y, year);
}
year.setProperty(n, e.newQObject(m));
}
void CalculateEngine::removeModel(int y, int n) const
{
QScriptValue data = _engine().globalObject().property("data");
if (!data.isValid())
return;
QScriptValue year = data.property(y);
if (!year.isValid())
return;
QScriptValue month = year.property(n);
if (!month.isValid())
return;
year.setProperty(n, QScriptValue());
}
EngineInitializer& CalculateEngine::_engine()
{
static EngineInitializer engine;
return engine;
}