36
36
// This is our engine
37
37
property var mathJs: MathJs.mathJs
38
// The formula we save in the storage and we show in the history
39
property var formula: ""
40
// The formula we show while user is typing
39
// The formula we will give to calculation engine, save in the storage
40
// This string needs to be converted to be displayed in history
41
property var engineFormula: ""
43
// The formula or temporary reuslty which will be displayed in text input field
44
property var displayedInputText: ""
46
// The result which we show while user is typing
41
47
property var tempResult: ""
42
49
// If this is true we calculate a temporary result to show in the bottom label
43
property bool isTempResultToCalc: false
50
property bool isFormulaIsValidToCalculate: false
45
53
property var previousVisual
46
55
// Becomes true after an user presses the "="
47
56
property bool isLastCalculate: false
58
property var numberOfOpenedBrackets: 0
60
function validateStringForAddingToFormula(stringToAddToFormula) {
61
// Check if the value is valid
62
if (isNaN(stringToAddToFormula) && (isNaN(previousVisual) &&
63
previousVisual !== ")")) {
64
// Not two operator one after other
68
// Do not allow adding too much closing brackets
69
if (stringToAddToFormula === "(") {
70
numberOfOpenedBrackets = numberOfOpenedBrackets + 1
71
} else if (stringToAddToFormula === ")") {
72
if (numberOfOpenedBrackets < 1) {
75
numberOfOpenedBrackets = numberOfOpenedBrackets - 1
80
function returnFormulaToDisplay(engineFormulaToDisplay) {
81
var engineToVisualMap = {
85
'.': Qt.locale().decimalPoint
87
for (var engineElement in engineToVisualMap) {
88
//FIXME need to add 'g' flag, but "new RegExp(engineElement, 'g');" is not working for me
89
engineFormulaToDisplay = engineFormulaToDisplay.replace(engineElement, engineToVisualMap[engineElement]);
91
return engineFormulaToDisplay;
49
96
function formulaPush(visual) {
50
97
// If the user press a number after the press of "=" we start a new
51
98
// formula, otherwise we continue with the old one
52
99
if (!isNaN(visual) && isLastCalculate) {
53
formula = tempResult = "";
100
engineFormula = displayedInputText = tempResult = "";
55
102
isLastCalculate = false;
57
// Check if the value is valid
58
if (isNaN(visual) && (isNaN(previousVisual) &&
59
previousVisual != ")")) {
60
// Not two operator one after other
105
if (validateStringForAddingToFormula(visual) === false) {
109
console.log("Error: " + exception.toString());
64
114
// We save the value until next value is pushed
65
115
previousVisual = visual;
67
117
// Adding the new operator to the formula
68
formula += visual.toString();
118
engineFormula += visual.toString();
70
120
// If we add an operator after an operator we know has priority,
71
121
// we display a temporary result instead the all operation
72
if (isNaN(visual) && isTempResultToCalc) {
73
tempResult = mathJs.eval(tempResult);
74
isTempResultToCalc = false;
122
if (isNaN(visual) && isFormulaIsValidToCalculate) {
124
tempResult = mathJs.eval(tempResult);
126
console.log("Error: math.js" + exception.toString() + " engine formula:" + tempResult);
129
isFormulaIsValidToCalculate = false;
77
132
tempResult += visual.toString();
135
displayedInputText = returnFormulaToDisplay(tempResult)
137
console.log("Error: " + exception.toString());
79
141
// Add here operators that have always priority
80
if (visual.toString() == "*") {
81
isTempResultToCalc = true;
142
if ((visual.toString() === "*") || (visual.toString() === ")")) {
143
isFormulaIsValidToCalculate = true;
85
147
function calculate() {
86
if (isNaN(previousVisual) && previousVisual !== ")") {
87
// If the last char isn't a number or a ) we don't calculate the result
149
var result = mathJs.eval(engineFormula);
151
console.log("Error: math.js" + exception.toString() + " engine formula:" + engineFormula);
90
155
isLastCalculate = true;
91
var result = mathJs.eval(formula);
92
157
result = result.toString();
93
console.log(formula +" = " + result);
160
displayedInputText = returnFormulaToDisplay(result)
162
console.log("Error: " + exception.toString());
166
historyModel.append({"formulaToDisplay":returnFormulaToDisplay(engineFormula), "result":displayedInputText});
167
engineFormula = result;
95
168
tempResult = result;
97
historyModel.append({"result":result});
169
numberOfOpenedBrackets = 0;