~acerisara/ubuntu-calculator-app/operation-after-clear

« back to all changes in this revision

Viewing changes to app/ubuntu-calculator-app.qml

  • Committer: Bartosz Kosiorek
  • Date: 2014-12-09 16:53:49 UTC
  • mfrom: (25.2.2 improvePushFunction)
  • Revision ID: gang65@poczta.onet.pl-20141209165349-qi4flqq81y5nr6sa
Improved checks when push a new digit

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
    // Becomes true after an user presses the "="
59
59
    property bool isLastCalculate: false;
60
60
 
61
 
    property int numberOfOpenedBrackets: 0;
62
 
 
63
 
    // Property needed to
64
 
    property bool isAllowedToAddDot: true;
65
 
 
66
61
    property var decimalPoint: Qt.locale().decimalPoint
67
62
 
68
63
    /**
70
65
     * place the result in right vars
71
66
     */
72
67
    function deleteLastFormulaElement() {
73
 
        if (longFormula[longFormula.length - 1] === ".") {
74
 
            isAllowedToAddDot = true;
75
 
        }
76
 
 
77
68
        longFormula = Formula.deleteLastFormulaElement(isLastCalculate, longFormula);
78
69
        shortFormula = longFormula;
79
70
        displayedInputText = returnFormulaToDisplay(longFormula);
81
72
 
82
73
    function validateStringForAddingToFormula(stringToAddToFormula) {
83
74
        if (Formula.isOperator(stringToAddToFormula)) {
84
 
            if ((longFormula === '') && (stringToAddToFormula !== '-')) {
85
 
                // Do not add operator at beginning
86
 
                return false;
87
 
            }
88
 
 
89
 
            if ((Formula.isOperator(previousVisual) && previousVisual !== ")")) {
90
 
                // Not two operator one after otQt.locale().decimalPointher
91
 
                return false;
92
 
            }
93
 
        }
94
 
 
95
 
        if (isNaN(stringToAddToFormula)) {
96
 
            if (stringToAddToFormula !== ".") {
97
 
                isAllowedToAddDot = true
98
 
            }
99
 
        }
100
 
 
101
 
        if (stringToAddToFormula[stringToAddToFormula.length - 1] === "(") {
102
 
            numberOfOpenedBrackets = numberOfOpenedBrackets + 1
103
 
        } else if (stringToAddToFormula === ")") {
104
 
            // Do not allow closing brackets after opening bracket
105
 
            // and do not allow adding too much closing brackets
106
 
            if ((previousVisual === "(") || (numberOfOpenedBrackets < 1)) {
107
 
                return false;
108
 
            }
109
 
            numberOfOpenedBrackets = numberOfOpenedBrackets - 1
110
 
        } else if (stringToAddToFormula === ".") {
111
 
            //Do not allow to have two decimal separators in the same number
112
 
            if (isAllowedToAddDot === false) {
113
 
                return false;
114
 
            }
115
 
 
116
 
            isAllowedToAddDot = false;
117
 
        }
 
75
            return Formula.couldAddOperator(stringToAddToFormula, longFormula);
 
76
        }
 
77
 
 
78
        if (stringToAddToFormula === ".") {
 
79
            return Formula.couldAddDot(longFormula);
 
80
        }
 
81
 
 
82
        if (stringToAddToFormula === ")") {
 
83
            return Formula.couldAddCloseBracket(longFormula);
 
84
        }
 
85
 
118
86
        return true;
119
87
    }
120
88
 
197
165
        calculationHistory.addCalculationToDatabase(longFormula, result);
198
166
        longFormula = result;
199
167
        shortFormula = result;
200
 
        numberOfOpenedBrackets = 0;
201
 
        if (result % 1 != 0) {
202
 
            isAllowedToAddDot = false;
203
 
        } else {
204
 
            isAllowedToAddDot = true;
205
 
        }
206
168
    }
207
169
 
208
170
    CalculationHistory {