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
|
/*
* Copyright (C) 2014 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.3
import Ubuntu.Components 1.1
import Ubuntu.Components.Themes.Ambiance 0.1
import "ui"
import "upstreamcomponents"
import "engine"
import "engine/math.js" as MathJs
import "engine/formula.js" as Formula
MainView {
id: mainView
// objectName for functional testing purposes (autopilot-qt5)
objectName: "calculator";
applicationName: "com.ubuntu.calculator";
// Removes the old toolbar and enables new features of the new header.
useDeprecatedToolbar: false;
automaticOrientation: true
width: units.gu(40);
height: units.gu(70);
focus: true
// This is our engine
property var mathJs: MathJs.mathJs;
// 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
state: visualModel.isInSelectionMode ? "selection" : "default"
states: [
State {
name: "default"
StateChangeScript {
script: header.hide()
}
PropertyChanges {
target: scrollableView
clip: false
}
},
State {
name: "selection"
StateChangeScript {
script: header.show()
}
PropertyChanges {
target: scrollableView
clip: true
}
}
]
/**
* The function calls the Formula.deleteLastFormulaElement function and
* place the result in right vars
*/
function deleteLastFormulaElement() {
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 = Formula.returnFormulaToDisplay(longFormula);
if (truncatedSubstring) {
textInputField.cursorPosition = truncatedSubstring.length;
}
}
function validateStringForAddingToFormula(formula, stringToAddToFormula) {
if (Formula.isOperator(stringToAddToFormula)) {
return Formula.couldAddOperator(formula, stringToAddToFormula);
}
if (stringToAddToFormula === ".") {
return Formula.couldAddDot(formula);
}
if (stringToAddToFormula === ")") {
return Formula.couldAddCloseBracket(formula);
}
return true;
}
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) && isLastCalculate) {
longFormula = displayedInputText = shortFormula = "";
}
isLastCalculate = false;
// Validate whole longFormula if the cursor is at the end of string
if (textInputField.cursorPosition === textInputField.length) {
if (validateStringForAddingToFormula(longFormula, visual) === false) {
return;
}
} else {
if (validateStringForAddingToFormula(longFormula.slice(0, textInputField.cursorPosition), visual) === false) {
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 = mathJs.eval(shortFormula);
} catch(exception) {
console.log("Error: " + 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();
} else {
longFormula = longFormula.slice(0, textInputField.cursorPosition) + visual.toString() + longFormula.slice(textInputField.cursorPosition, longFormula.length);
shortFormula = longFormula;
}
var preservedCursorPosition = textInputField.cursorPosition;
displayedInputText = Formula.returnFormulaToDisplay(shortFormula);
textInputField.cursorPosition = preservedCursorPosition + visual.length;
// Add here operators that have always priority
if ((visual.toString() === "*") || (visual.toString() === ")")) {
isFormulaIsValidToCalculate = true;
}
}
function calculate() {
if ((longFormula === '') || (isLastCalculate === true)) {
return;
}
try {
var result = mathJs.eval(longFormula);
} catch(exception) {
console.log("Error: math.js" + exception.toString() + " engine formula:" + longFormula);
return false;
}
result = result.toString()
isLastCalculate = true;
if (result === longFormula) {
return;
}
displayedInputText = Formula.returnFormulaToDisplay(result);
calculationHistory.addCalculationToScreen(longFormula, result);
longFormula = result;
shortFormula = result;
}
CalculationHistory {
id: calculationHistory
}
Keys.onPressed: {
keyboardLoader.item.pressedKey = event.key;
keyboardLoader.item.pressedKeyText = event.text;
}
Keys.onReleased: {
keyboardLoader.item.pressedKey = -1;
keyboardLoader.item.pressedKeyText = "";
}
Header {
id: header
visible: true
useDeprecatedToolbar: false
property color dividerColor: "#babbbc"
config: PageHeadConfiguration {
backAction: Action {
objectName: "cancelSelectionAction"
iconName: "close"
text: i18n.tr("Cancel")
onTriggered: visualModel.cancelSelection()
}
actions: [
Action {
id: selectAllAction
objectName: "selectAllAction"
iconName: "select"
onTriggered: visualModel.selectAll()
},
Action {
id: multiDeleteAction
objectName: "multiDeleteAction"
iconName: "delete"
onTriggered: visualModel.endSelection()
}
]
}
}
Component {
id: emptyDelegate
Item { }
}
Component {
id: screenDelegateComponent
Screen {
id: screenDelegate
width: parent ? parent.width : 0
property var model: itemModel
visible: model.dbId != -1
selectionMode: visualModel.isInSelectionMode
selected: visualModel.isSelected(visualDelegate)
property var removalAnimation
function remove() {
removalAnimation.start();
}
// parent is the loader component
property var visualDelegate: parent ? parent : null
onSwippingChanged: {
visualModel.updateSwipeState(screenDelegate);
}
onSwipeStateChanged: {
visualModel.updateSwipeState(screenDelegate);
}
onItemClicked: {
if (visualModel.isInSelectionMode) {
if (!visualModel.selectItem(visualDelegate)) {
visualModel.deselectItem(visualDelegate);
}
}
}
onItemPressAndHold: {
visualModel.startSelection();
visualModel.selectItem(visualDelegate);
}
leftSideAction: screenDelegateDeleteAction.item
Loader {
id: screenDelegateDeleteAction
sourceComponent: Action {
iconName: "delete"
text: i18n.tr("Delete")
onTriggered: {
screenDelegate.remove();
}
}
}
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);
}
}
}
}
}
MultipleSelectionVisualModel {
id: visualModel
model: calculationHistory.getContents()
onSelectionDone: {
for (var i = 0; i < items.count; i++) {
calculationHistory.deleteCalc(items.get(i).model.dbId, items.get(i).model.index);
}
}
delegate: Component {
Loader {
property var itemModel: model
width: parent.width
height: model.dbId != -1 ? item.height : 0;
sourceComponent: screenDelegateComponent
opacity: ((y+height) >= scrollableView.contentY) && (y <= (scrollableView.contentY + scrollableView.height)) ? 1 : 0
onOpacityChanged: {
if (this.hasOwnProperty('item') && this.item != null) {
if (opacity > 0) {
sourceComponent = screenDelegateComponent;
} else {
this.item.visible = false;
sourceComponent = emptyDelegate;
}
}
}
}
}
}
ScrollableView {
anchors {
top: header.bottom
bottom: parent.bottom
left: parent.left
right: parent.right
}
id: scrollableView
objectName: "scrollableView"
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
}
TextField {
id: textInputField
objectName: "textInputField"
width: contentWidth + units.gu(3)
// TODO: Make sure this bug gets fixed in SDK:
// https://bugs.launchpad.net/ubuntu/+source/ubuntu-ui-toolkit/+bug/1320885
//width: parent.width
height: units.gu(6)
// remove ubuntu shape
style: TextFieldStyle {
background: Item {
}
}
text: displayedInputText
font.pixelSize: height * 0.7
//horizontalAlignment: TextInput.AlignRight
anchors {
right: parent.right
rightMargin: units.gu(1)
}
readOnly: true
selectByMouse: true
cursorVisible: true
onCursorPositionChanged:
if (cursorPosition !== length ) {
// Count cursor position from the end of line
var preservedCursorPosition = length - cursorPosition;
displayedInputText = Formula.returnFormulaToDisplay(longFormula);
cursorPosition = length - preservedCursorPosition;
} else {
displayedInputText = Formula.returnFormulaToDisplay(shortFormula);
}
}
Loader {
id: keyboardLoader
width: parent.width
source: mainView.width > mainView.height ? "ui/LandscapeKeyboard.qml" : "ui/PortraiKeyboard.qml"
opacity: ((y+height) >= scrollableView.contentY) && (y <= (scrollableView.contentY + scrollableView.height)) ? 1 : 0
}
}
}
|