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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This file is part of Ubuntu Calculator App
*
* Ubuntu Calculator App is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Ubuntu Calculator App is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.4
import QtQuick.Window 2.2
import Ubuntu.Components 1.3
import Ubuntu.Components.Themes.Ambiance 1.3
import "ui"
import "upstreamcomponents"
import "engine"
import "engine/formula.js" as Formula
import Qt.labs.settings 1.0
MainView {
id: mainView
// objectName for functional testing purposes (autopilot-qt5)
objectName: "calculator";
applicationName: "com.ubuntu.calculator";
automaticOrientation: true
anchorToKeyboard: textInputField.visible ? false : true
width: units.gu(80);
height: units.gu(60);
// This is our engine
property var mathJs: mathJsLoader.item ? mathJsLoader.item.mathJs : null;
Loader {
id: mathJsLoader
source: "engine/MathJs.qml"
asynchronous: true
active: keyboardLoader.active
onLoaded: {
mathJs.config({
number: 'BigNumber'
});
}
}
// Long form of formula, which are saved in the storage/history
property string longFormula: "";
// Engine's short form of formula. It is displayed in TextInput field
property string shortFormula: "";
// The formula converted to human eye, which will be displayed in text input field
property string displayedInputText: "";
// If this is true we calculate a temporary result to show in the bottom label
property bool isFormulaIsValidToCalculate: false;
// Last immission
property var previousVisual;
// Becomes true after an user presses the "="
property bool isLastCalculate: false;
property var decimalPoint: Qt.locale().decimalPoint
// Var used to save favourite calcs
property bool isFavourite: false
// Var used to store currently edited calculation history item
property int editedCalculationIndex: -1
property var settings: Settings {
// Used for Welcome Wizard
property bool firstRun: true
}
// By default we delete selected calculation from history.
// If it is set to false, then editing will be invoked
property bool deleteSelectedCalculation: true;
// Var used to display calculation in multiline mode
// if width is not enough to display in one line
property bool isScreenIsWide: width > units.gu(60);
/**
* The function calls the Formula.deleteLastFormulaElement function and
* place the result in right vars
*/
function deleteLastFormulaElement() {
isFormulaIsValidToCalculate = false;
if (textInputField.cursorPosition === textInputField.length) {
longFormula = Formula.deleteLastFormulaElement(isLastCalculate, longFormula)
} else {
var truncatedSubstring = Formula.deleteLastFormulaElement(isLastCalculate, longFormula.slice(0, textInputField.cursorPosition))
longFormula = truncatedSubstring + longFormula.slice(textInputField.cursorPosition, longFormula.length);
}
shortFormula = longFormula;
displayedInputText = longFormula;
if (truncatedSubstring) {
textInputField.cursorPosition = truncatedSubstring.length;
}
}
/**
* Function to clear formula in input text field
*/
function clearFormula() {
isFormulaIsValidToCalculate = false;
shortFormula = "";
longFormula = "";
displayedInputText = "";
}
/**
* Format bigNumber
*/
function formatBigNumber(bigNumberToFormat) {
// Maximum length of the result number
var NUMBER_LENGTH_LIMIT = 14;
if (bigNumberToFormat.toString().length > NUMBER_LENGTH_LIMIT) {
var resultLength = mathJs.format(bigNumberToFormat, {exponential: {lower: 1e-10, upper: 1e10},
precision: NUMBER_LENGTH_LIMIT}).toString().length;
return mathJs.format(bigNumberToFormat, {exponential: {lower: 1e-10, upper: 1e10},
precision: (NUMBER_LENGTH_LIMIT - resultLength + NUMBER_LENGTH_LIMIT)}).toString();
}
return bigNumberToFormat.toString()
}
function formulaPush(visual) {
// If the user press a number after the press of "=" we start a new
// formula, otherwise we continue with the old one
if ((!isNaN(visual) || (visual === ".")) && isLastCalculate) {
isFormulaIsValidToCalculate = false;
longFormula = displayedInputText = shortFormula = "";
}
// Add zero when decimal separator is not after number
if ((visual === ".") && ((isNaN(displayedInputText.slice(textInputField.cursorPosition - 1, textInputField.cursorPosition))) || (longFormula === ""))) {
visual = "0.";
}
isLastCalculate = false;
// Validate whole longFormula if the cursor is at the end of string
if (textInputField.cursorPosition === textInputField.length) {
if (visual === "()") {
visual = Formula.determineBracketTypeToAdd(longFormula)
}
if (Formula.validateStringForAddingToFormula(longFormula, visual) === false) {
errorAnimation.restart();
return;
}
} else {
if (visual === "()") {
visual = Formula.determineBracketTypeToAdd(longFormula.slice(0, textInputField.cursorPosition))
}
if (Formula.validateStringForAddingToFormula(longFormula.slice(0, textInputField.cursorPosition), visual) === false) {
errorAnimation.restart();
return;
}
}
// We save the value until next value is pushed
previousVisual = visual;
// If we add an operator after an operator we know has priority,
// we display a temporary result instead the all operation
if (isNaN(visual) && (visual.toString() !== ".") && isFormulaIsValidToCalculate) {
try {
shortFormula = formatBigNumber(mathJs.eval(shortFormula));
} catch(exception) {
console.log("Debug: Temp result: " + exception.toString() + " engine formula: " + shortFormula);
}
isFormulaIsValidToCalculate = false;
}
// Adding the new operator to the formula
if (textInputField.cursorPosition === textInputField.length ) {
longFormula += visual.toString();
shortFormula += visual.toString();
displayedInputText = shortFormula;
} else {
longFormula = longFormula.slice(0, textInputField.cursorPosition) + visual.toString() + longFormula.slice(textInputField.cursorPosition, longFormula.length);
shortFormula = longFormula;
var preservedCursorPosition = textInputField.cursorPosition;
displayedInputText = shortFormula;
textInputField.cursorPosition = preservedCursorPosition + visual.length;
}
// Add here operators that have always priority
if (visual.toString() === ")") {
isFormulaIsValidToCalculate = true;
}
}
function calculate() {
if ((longFormula === '') || (isLastCalculate === true)) {
errorAnimation.restart();
return;
}
// We try to balance brackets to avoid mathJs errors
var numberOfOpenedBrackets = (longFormula.match(/\(/g) || []).length -
(longFormula.match(/\)/g) || []).length;
for (var i = 0; i < numberOfOpenedBrackets; i++) {
formulaPush(')');
}
try {
var result = mathJs.eval(longFormula);
result = formatBigNumber(result)
} catch(exception) {
// If the formula isn't right and we added brackets, we remove them
for (var i = 0; i < numberOfOpenedBrackets; i++) {
deleteLastFormulaElement();
}
console.log("[LOG]: Unable to calculate formula : \"" + longFormula + "\", math.js: " + exception.toString());
errorAnimation.restart();
return false;
}
isLastCalculate = true;
if (result === longFormula) {
errorAnimation.restart();
return;
}
calculationHistory.addCalculationToScreen(longFormula, result, false, "");
editedCalculationIndex = -1;
longFormula = result;
shortFormula = result;
favouriteTextField.text = "";
displayedInputText = result;
}
PageStack {
id: mainStack
Component.onCompleted: {
// Show the welcome wizard only when running the app for the first time
if (settings.firstRun) {
console.log("[LOG]: Detecting first time run by user. Starting welcome wizard.")
push(Qt.resolvedUrl("welcomewizard/WelcomeWizard.qml"))
} else {
push(calculatorPage);
calculatorPage.forceActiveFocus();
}
}
onHeightChanged: scrollableView.scrollToBottom();
anchors.fill: parent
PageWithBottomEdge {
id: calculatorPage
title: i18n.tr("Calculator")
anchors.fill: parent
visible: false
bottomEdgeTitle: i18n.tr("Favorite")
bottomEdgePageSource: "ui/FavouritePage.qml"
bottomEdgeEnabled: textInputField.visible
state: visualModel.isInSelectionMode ? "selection" : "default"
states: [
State {
name: "default"
PropertyChanges {
target: scrollableView
clip: false
}
PropertyChanges {
target: calculatorPage.head
visible: false
preset: ""
}
},
State {
name: "selection"
PropertyChanges {
target: scrollableView
clip: true
}
PropertyChanges {
target: calculatorPage.head
visible: true
preset: "select"
}
}
]
CalculationHistory {
id: calculationHistory
}
// Some special keys like backspace captured in TextField,
// are for some reason not sent to the application but to the text input
Keys.onPressed: textInputField.keyPress(event)
Keys.onReleased: textInputField.keyRelease(event)
head.visible: false
head.locked: true
head.backAction: Action {
objectName: "cancelSelectionAction"
iconName: "close"
text: i18n.tr("Cancel")
onTriggered: visualModel.cancelSelection()
}
head.actions: [
Action {
id: selectAllAction
objectName: "selectAllAction"
iconName: visualModel.selectedItems.count < visualModel.items.count ?
"select" : "select-none"
text: visualModel.selectedItems.count < visualModel.items.count ?
i18n.tr("Select All") : i18n.tr("Select None")
onTriggered: visualModel.selectAll()
},
Action {
id: copySelectedAction
objectName: "copySelectedAction"
iconName: "edit-copy"
text: i18n.tr("Copy")
onTriggered: calculatorPage.copySelectedCalculations()
enabled: visualModel.selectedItems.count > 0
},
Action {
id: multiDeleteAction
objectName: "multiDeleteAction"
iconName: "delete"
text: i18n.tr("Delete")
onTriggered: calculatorPage.deleteSelectedCalculations()
enabled: visualModel.selectedItems.count > 0
}
]
Component {
id: emptyDelegate
Item { }
}
Component {
id: screenDelegateComponent
Screen {
id: screenDelegate
width: parent ? parent.width : 0
property var model: itemModel
visible: model.dbId !== -1
selectMode: visualModel.isInSelectionMode
selected: visualModel.isSelected(visualDelegate)
property var removalAnimation
function remove() {
removalAnimation.start();
}
// parent is the loader component
property var visualDelegate: parent ? parent : null
onSwipedChanged: {
visualModel.updateSwipeState(screenDelegate);
}
onClicked: {
if (visualModel.isInSelectionMode) {
if (!visualModel.selectItem(visualDelegate)) {
visualModel.deselectItem(visualDelegate);
}
}
}
onPressAndHold: {
visualModel.startSelection();
visualModel.selectItem(visualDelegate);
}
leadingActions: ListItemActions {
actions: [
Action {
id: screenDelegateDeleteAction
iconName: "delete"
text: i18n.tr("Delete")
onTriggered: {
screenDelegate.remove();
}
}
]
}
trailingActions: ListItemActions {
actions: [
Action {
id: screenDelegateCopyAction
iconName: "edit-copy"
text: i18n.tr("Copy")
onTriggered: {
var mimeData = Clipboard.newData();
mimeData.text = model.formula + "=" + model.result;
Clipboard.push(mimeData);
}
},
Action {
id: screenDelegateEditAction
iconName: "edit"
text: i18n.tr("Edit")
onTriggered: {
longFormula = model.formula;
shortFormula = model.result;
displayedInputText = model.formula;
isLastCalculate = false;
previousVisual = "";
scrollableView.scrollToBottom();
}
},
Action {
id: screenDelegateFavouriteAction
iconName: (mainView.editedCalculationIndex == model.index || model.isFavourite) ? "starred" : "non-starred"
text: i18n.tr("Add to favorites")
onTriggered: {
if (model.isFavourite) {
calculationHistory.updateCalculationInDatabase(model.index, model.dbId, !model.isFavourite, "");
editedCalculationIndex = -1;
textInputField.visible = true;
textInputField.forceActiveFocus();
} else {
editedCalculationIndex = model.index;
textInputField.visible = false;
favouriteTextField.forceActiveFocus();
scrollableView.scrollToBottom();
}
model.isFavourite = !model.isFavourite;
}
}
]
}
removalAnimation: SequentialAnimation {
alwaysRunToEnd: true
ScriptAction {
script: {
if (visualModel.currentSwipedItem === screenDelegate) {
visualModel.currentSwipedItem = null;
}
}
}
UbuntuNumberAnimation {
target: screenDelegate
property: "height"
to: 0
}
ScriptAction {
script: {
calculationHistory.deleteCalc(model.dbId, model.index);
}
}
}
}
}
function deleteSelectedCalculations() {
deleteSelectedCalculation = true;
visualModel.endSelection();
}
function copySelectedCalculations() {
deleteSelectedCalculation = false;
visualModel.endSelection();
}
MultipleSelectionVisualModel {
id: visualModel
model: calculationHistory.getContents()
onSelectionDone: {
if(deleteSelectedCalculation === true) {
for(var i = 0; i < items.count; i++) {
calculationHistory.deleteCalc(items.get(i).model.dbId, items.get(i).model.index);
}
} else {
var mimeData = Clipboard.newData();
mimeData.text = "";
for(var j = 0; j < items.count; j++) {
if (items.get(j).model.dbId !== -1) {
mimeData.text = mimeData.text + items.get(j).model.formula + "=" + items.get(j).model.result + "\n";
}
}
Clipboard.push(mimeData);
}
}
delegate: Loader {
property var itemModel: model
width: parent.width
height: model.dbId !== -1 ? item.height : 0;
sourceComponent: screenDelegateComponent
asynchronous: true
}
}
ScrollableView {
anchors {
fill: parent
bottomMargin: textInputField.visible ? 0 : -keyboardLoader.height
}
id: scrollableView
objectName: "scrollableView"
visible: keyboardLoader.status == Loader.Ready
Component.onCompleted: {
// FIXME: workaround for qtubuntu not returning values depending on the grid unit definition
// for Flickable.maximumFlickVelocity and Flickable.flickDeceleration
var scaleFactor = units.gridUnit / 8;
maximumFlickVelocity = maximumFlickVelocity * scaleFactor;
flickDeceleration = flickDeceleration * scaleFactor;
}
Repeater {
id: formulaView
model: visualModel
}
Rectangle {
width: parent.width
height: units.gu(6)
TextField {
id: favouriteTextField
anchors {
right: parent.right
rightMargin: units.gu(1)
}
width: parent.width - units.gu(3)
height: parent.height
visible: !textInputField.visible
font.italic: true
font.pixelSize: height * 0.5
verticalAlignment: TextInput.AlignVCenter
// TRANSLATORS: this is a time formatting string, see
// http://qt-project.org/doc/qt-5/qml-qtqml-date.html#details for
// valid expressions
placeholderText: Qt.formatDateTime(new Date(), i18n.tr("dd MMM yyyy"))
// remove ubuntu shape
style: TextFieldStyle {
background: Item {
}
}
onAccepted: {
textInputField.visible = true;
textInputField.forceActiveFocus();
if (editedCalculationIndex >= 0) {
calculationHistory.updateCalculationInDatabase(editedCalculationIndex,
calculationHistory.getContents().get(editedCalculationIndex).dbId,
true,
favouriteTextField.text);
favouriteTextField.text = "";
editedCalculationIndex = -1;
}
}
}
TextField {
id: textInputField
objectName: "textInputField"
width: parent.width - units.gu(2)
height: parent.height
color: UbuntuColors.orange
// remove ubuntu shape
style: TextFieldStyle {
background: Item {
Rectangle {
color: "#EFEEEE"
width: parent.width
height: parent.height
}
}
}
text: Formula.returnFormulaToDisplay(displayedInputText, i18n, decimalPoint)
font.pixelSize: height * 0.7
horizontalAlignment: TextInput.AlignRight
anchors {
right: parent.right
rightMargin: units.gu(1)
}
// Need to capture special keys like backspace here,
// as they are for some reason not sent to the application but to the text input
Keys.onPressed: keyPress(event)
Keys.onReleased: keyRelease(event)
function keyPress(event) {
if (!(event.modifiers & Qt.ControlModifier || event.modifiers & Qt.AltModifier)) { // Shift needs to be passed through as it may be required for some special keys
keyboardLoader.item.pressedKey = event.key;
keyboardLoader.item.pressedKeyText = event.text;
} else if (event.modifiers & Qt.ControlModifier) {
if (event.key === Qt.Key_C) { // Copy action
var mimeData = Clipboard.newData();
mimeData.text = textInputField.selectedText;
Clipboard.push(mimeData);
} else if (event.key === Qt.Key_V) { // Paste action
if (Clipboard.data.text && Clipboard.data.text !== "") {
var data = Clipboard.data.text;
// Get all accepted characters (i.e. those which can be entered) from the current keyboard
var acceptedBits = [];
for (var i = 0; i < keyboardLoader.item.children.length; i++) {
var model = keyboardLoader.item.children[i].keyboardModel;
if (model) {
for (var j = 0; j < model.length; j++) {
var item = model[j];
if (!item.action) {
if (item.number || item.forceNumber)
acceptedBits.push({ "chars": item.number, "push": item.number });
if (item.pushText)
acceptedBits.push({ "chars": item.pushText, "push": item.pushText });
if (item.text)
acceptedBits.push({ "chars": item.text, "push": item.pushText ? item.pushText : item.text });
if (item.pasteTexts)
for (var pos = 0; pos < item.pasteTexts.length; pos++)
acceptedBits.push({ "chars": item.pasteTexts[pos], "push": item.pasteTexts[pos] });
}
}
}
}
// Extract the part of the clipboard data which can be pasted
var paste = "";
var pos = 0;
while (pos < data.length) {
// Check if the string starts with an accepted string
for (i = 0; i < acceptedBits.length; i++) {
if (data.substring(pos, pos + (acceptedBits[i].chars.length ? acceptedBits[i].chars.length : 1)) === acceptedBits[i].chars.toString()) {
paste += acceptedBits[i].push;
pos += acceptedBits[i].chars.length ? acceptedBits[i].chars.length : 1;
break;
}
}
// Skip one char if it could not be found
if (i === acceptedBits.length)
pos++;
}
// Push the paste string
formulaPush(paste);
} else {
console.log("Debug: paste failed as the clipboard contains no text");
}
scrollableView.scrollToBottom();
}
}
}
function keyRelease(event) {
keyboardLoader.item.pressedKey = -1;
keyboardLoader.item.pressedKeyText = "";
}
readOnly: true
selectByMouse: true
cursorVisible: true
onCursorPositionChanged:
if (cursorPosition !== length ) {
// Count cursor position from the end of line
var preservedCursorPosition = length - cursorPosition;
displayedInputText = longFormula;
cursorPosition = length - preservedCursorPosition;
} else {
displayedInputText = shortFormula;
}
SequentialAnimation {
id: errorAnimation
running: false
PropertyAnimation {
target: textInputField
properties: "color"
to: "#000000"
duration: UbuntuAnimation.SnapDuration
}
PauseAnimation {
duration: UbuntuAnimation.SnapDuration
}
PropertyAnimation {
target: textInputField
properties: "color"
to: UbuntuColors.orange
duration: UbuntuAnimation.SnapDuration
}
}
}
}
Loader {
id: keyboardLoader
width: parent.width
enabled: mathJs != null
// FIXME: this works around the fact that the final size
// of keyboardLoader (and of mainView) is only set by the window
// manager quite late; this avoids unnecessary reloads of the
// source
active: false
property bool sizeReady: Window.active
onSizeReadyChanged: if (sizeReady) keyboardLoader.active = true
source: scrollableView.width > scrollableView.height ? "ui/LandscapeKeyboard.qml" : "ui/PortraitKeyboard.qml"
opacity: ((y + height) >= scrollableView.contentY) &&
(y <= (scrollableView.contentY + scrollableView.height)) ? 1 : 0
}
}
}
}
}
|