~xnox/ubuntu-calculator-app/fix-missing-trash.png

« back to all changes in this revision

Viewing changes to Simple/SimplePage.qml

  • Committer: Tarmac
  • Author(s): rpadovani
  • Date: 2014-02-12 10:36:08 UTC
  • mfrom: (215.1.6 optimization)
  • Revision ID: tarmac-20140212103608-uip99xf5lo4hv303
Some optimizations.

Approved by Mihir Soni, Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright 2013 Canonical Ltd.
 
2
 * Copyright 2013-2014 Canonical Ltd.
3
3
 *
4
4
 * This file is part of ubuntu-calculator-app.
5
5
 *
24
24
Page {
25
25
    property var formula: new F.Formula()
26
26
    property var screenFormula: [{_text:'', _operation: '', _number:''}]
27
 
    property bool calcKeyboardVisible: true
28
27
    property bool labelVisible: false
29
28
    property bool saveLabel: true
30
29
    /*
48
47
 
49
48
    function formulaPush(visual) {
50
49
        var added = false;
51
 
        try{
52
 
        var last = formula.push(visual);
53
 
        } catch(exception){
 
50
        try {
 
51
            var last = formula.push(visual);
 
52
        } catch(exception) {
54
53
            return false;
55
54
        }
56
55
 
59
58
            if (last.type === CALC.T_NUMBER || last.type === CALC.T_UNARY_MINUS) {
60
59
                screenFormula[screenFormula.length - 1]._number += last.value.toString();
61
60
                added = true;
62
 
            } else if (hasToBePushedOnSameLine(last)){
63
 
                /* If is not a number is an operator
64
 
                 * Plus and minus can stay in number position,
65
 
                 * in some cases.
66
 
                 * This is check by hasToBePushedOnSameLine()
67
 
                 */
 
61
            } else if (last.type === CALC.T_MINUS && screenFormula[screenFormula.length - 1]._number === ''){
 
62
                /*If it's a minus and there aren't numbers on same line yet*/
68
63
                screenFormula[screenFormula.length - 1]._number += last.value.toString(); // Push on the same line
69
64
                added = true;
70
65
            } else if (last.type === CALC.T_OPERATOR) { // This is if the first thing is an operator
72
67
            }
73
68
            else { // In all other cases operator is on an new line
74
69
                if (last.value !== undefined)  { // To avoid errors on console
 
70
                    screenFormula.push({_text:'', _operation: last.value.toString(), _number: ''});
75
71
                    added = true;
76
 
                    screenFormula.push({_text:'', _operation: last.value.toString(), _number: ''});
77
72
                }
78
73
            }
79
74
            formulaView.currentOperatorsChanged();
80
75
            formulaView.forceActiveFocus();
81
76
        }
82
77
        return added;
83
 
 
84
 
    }
85
 
 
86
 
    // Check if an element has to be pushed on same line of calc
87
 
    function hasToBePushedOnSameLine(element) {
88
 
        if (element.type === CALC.T_MINUS  // Is it a minus?
89
 
                && screenFormula[screenFormula.length - 1]._number === '' // There aren't numbers on same line yet?
90
 
                ) // End condition
91
 
            return true;
92
 
 
93
 
        return false;
94
78
    }
95
79
 
96
80
    function changeSign(){
213
197
 
214
198
            Keys.onPressed: {
215
199
                // No label focused
216
 
                if (calcKeyboardVisible) {
 
200
                if (!labelVisible) {
217
201
                    if (event.key === Qt.Key_Escape || event.key === Qt.Key_Delete || event.key === Qt.Key_Backspace )
218
202
                        formulaView.atYEnd ? buttonClicked('clear') : formulaView.positionViewAtBeginning();
219
203
                    else if (event.key === Qt.Key_Enter || event.key === Qt.Key_Return || event.key === Qt.Key_Equal)
227
211
                             event.text === "=") &&
228
212
                             event.text !== "")
229
213
                        buttonClicked(event.text)
230
 
 
231
 
                    event.accepted = true;
232
214
                }
233
215
                else {
234
216
                    if (event.key === Qt.Key_Enter || event.key === Qt.Key_Return || event.key === Qt.Key_Escape) {
235
217
                        if (event.key === Qt.Key_Escape)
236
218
                            saveLabel = false;
237
219
                        labelVisible = false;
238
 
                        calcKeyboardVisible = true;
239
220
                        // Remove the cursor from the label
240
221
                        storage.forceActiveFocus();
241
222
                    }
242
 
                    event.accepted = true;
243
223
                }
244
224
            }
245
225
 
246
226
            Keys.onReleased: {
247
227
                // No label focused
248
 
                if (calcKeyboardVisible) {
 
228
                if (!labelVisible) {
249
229
                    if (event.key === Qt.Key_Escape || event.key === Qt.Key_Delete || event.key === Qt.Key_Backspace )
250
230
                        buttonReleased('clear');
251
231
                    else if (event.key === Qt.Key_Enter || event.key === Qt.Key_Return || event.key === Qt.Key_Equal)
260
240
                             event.text !== "")
261
241
                        buttonReleased(event.text)
262
242
                }
263
 
                event.accepted = true;
264
243
            }
265
244
 
266
245
            /*
342
321
 
343
322
            header: CalcKeyboard {
344
323
                id: calcKeyboard
345
 
                visible: calcKeyboardVisible
 
324
                visible: !labelVisible
346
325
            }
347
326
 
348
327
            delegate: Screen {
364
343
                }
365
344
 
366
345
                function saveMainLabel(newText) {
367
 
                    if (index !== 0 && saveLabel) {
368
 
                        var calculation = [];
369
 
                        var calc = storage.getCalculation(memory.get(index).dbId);
370
 
                        calc.mainLabel = newText
371
 
                        calculation.push({"calc": calc});
372
 
                        storage.updateCalculation(calculation, memory.get(index).dbId);
373
 
                    }
374
 
                    else {
375
 
                        saveLabel = true;
376
 
                        var oldLabel = storage.getCalculation(memory.get(index).dbId);
377
 
                        memory.get(index).mainLabel = oldLabel.mainLabel;
 
346
                    if (index !== 0) {
 
347
                        if (saveLabel) {
 
348
                            var calculation = [];
 
349
                            var calc = storage.getCalculation(memory.get(index).dbId);
 
350
                            calc.mainLabel = newText
 
351
                            calculation.push({"calc": calc});
 
352
                            storage.updateCalculation(calculation, memory.get(index).dbId);
 
353
                        }
 
354
                        else {
 
355
                            saveLabel = true;
 
356
                            var oldLabel = storage.getCalculation(memory.get(index).dbId);
 
357
                            memory.get(index).mainLabel = oldLabel.mainLabel;
 
358
                        }
378
359
                    }
379
360
                }
380
361
 
381
362
                function saveCalcLabel(idx, newText) {
382
 
                    if (index !== 0 && saveLabel) {
383
 
                        var calculation = [];
384
 
                        var calc = storage.getCalculation(memory.get(index).dbId);
385
 
                        calc.operators[idx]._text = newText;
386
 
                        calculation.push({"calc": calc});
387
 
                        storage.updateCalculation(calculation, memory.get(index).dbId);
388
 
                    }
389
 
                    else {
390
 
                        saveLabel = true
391
 
                        var oldLabel = storage.getCalculation(memory.get(index).dbId);
392
 
                        newText = oldLabel.operators[idx]._text;
393
 
                    }
394
 
 
 
363
                    if (index !== 0) {
 
364
                        if (saveLabel) {
 
365
                            var calculation = [];
 
366
                            var calc = storage.getCalculation(memory.get(index).dbId);
 
367
                            calc.operators[idx]._text = newText;
 
368
                            calculation.push({"calc": calc});
 
369
                            storage.updateCalculation(calculation, memory.get(index).dbId);
 
370
                        }
 
371
                        else {
 
372
                            saveLabel = true
 
373
                            var oldLabel = storage.getCalculation(memory.get(index).dbId);
 
374
                            newText = oldLabel.operators[idx]._text;
 
375
                        }
 
376
                    }
395
377
                    return newText;
396
378
                }
397
379