| 1 |
/* |
| 2 |
* Copyright (C) 2017-2019 UBports |
| 3 |
* Copyright (C) 2014-2015 Canonical Ltd |
| 4 |
* |
| 5 |
* This file is part of Lomiri Calculator App |
| 6 |
* |
| 7 |
* Lomiri Calculator App is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License version 3 as |
| 9 |
* published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Lomiri Calculator App is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
import QtQuick 2.4 |
| 20 |
import QtQuick.Window 2.2 |
| 21 |
import Lomiri.Components 1.3 |
| 22 |
import Lomiri.Components.Themes.Ambiance 1.3 |
| 23 |
import QtQuick.Controls.Suru 2.2 |
| 24 |
import Lomiri.Components.Popups 1.3 |
| 25 |
|
| 26 |
import "ui" |
| 27 |
import "upstreamcomponents" |
| 28 |
import "engine" |
| 29 |
import "engine/formula.js" as Formula |
| 30 |
import Qt.labs.settings 1.0 |
| 31 |
|
| 32 |
MainView { |
| 33 |
id: mainView |
| 34 |
// objectName for functional testing purposes (autopilot-qt5) |
| 35 |
objectName: "calculator"; |
| 36 |
applicationName: "calculator.ubports"; |
| 37 |
|
| 38 |
automaticOrientation: true |
| 39 |
anchorToKeyboard: textInputField.visible ? false : true |
| 40 |
|
| 41 |
Suru.theme: theme.name === "Lomiri.Components.Themes.SuruDark" ? Suru.Dark : Suru.Light |
| 42 |
width: units.gu(80); |
| 43 |
height: units.gu(60); |
| 44 |
|
| 45 |
// This is our engine |
| 46 |
property var mathJs: mathJsLoader.item ? mathJsLoader.item.mathJs : null; |
| 47 |
|
| 48 |
Loader { |
| 49 |
id: mathJsLoader |
| 50 |
source: "engine/MathJs.qml" |
| 51 |
asynchronous: true |
| 52 |
active: keyboardLoader.active |
| 53 |
|
| 54 |
onLoaded: { |
| 55 |
mathJs.config({ |
| 56 |
number: 'BigNumber' |
| 57 |
}); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// Long form of formula, which are saved in the storage/history |
| 62 |
property string longFormula: ""; |
| 63 |
|
| 64 |
// Engine's short form of formula. It is displayed in TextInput field |
| 65 |
property string shortFormula: ""; |
| 66 |
|
| 67 |
// The formula converted to human eye, which will be displayed in text input field |
| 68 |
property string displayedInputText: ""; |
| 69 |
|
| 70 |
// If this is true we calculate a temporary result to show in the bottom label |
| 71 |
property bool isFormulaIsValidToCalculate: false; |
| 72 |
|
| 73 |
// Last immission |
| 74 |
property var previousVisual; |
| 75 |
|
| 76 |
// Becomes true after an user presses the "=" |
| 77 |
property bool isLastCalculate: false; |
| 78 |
|
| 79 |
property var decimalPoint: Qt.locale().decimalPoint |
| 80 |
|
| 81 |
// Var used to store calculation history position |
| 82 |
property var historyPosition: calculationHistory.getContents().count; |
| 83 |
|
| 84 |
// Var used to save if user is using history. |
| 85 |
property bool isUsingHistory: false; |
| 86 |
|
| 87 |
// Var used to store the last formula which is being written. |
| 88 |
property string lastWrittenFormula: ""; |
| 89 |
|
| 90 |
// Var used to store currently edited calculation history item |
| 91 |
property int editedCalculationIndex: -1 |
| 92 |
|
| 93 |
// By default we delete selected calculation from history. |
| 94 |
// If it is set to false, then editing will be invoked |
| 95 |
property bool deleteSelectedCalculation: true; |
| 96 |
|
| 97 |
// Var used to display calculation in multiline mode |
| 98 |
// if width is not enough to display in one line |
| 99 |
property bool isScreenIsWide: width > units.gu(60); |
| 100 |
|
| 101 |
// Used to determine which keyboard to use |
| 102 |
property bool isLandscape: scrollableView.width > scrollableView.height |
| 103 |
|
| 104 |
onIsLandscapeChanged: bottomEdge.collapse() |
| 105 |
|
| 106 |
/** |
| 107 |
* The function calls the Formula.deleteLastFormulaElement function and |
| 108 |
* place the result in right vars |
| 109 |
*/ |
| 110 |
function deleteLastFormulaElement() { |
| 111 |
isFormulaIsValidToCalculate = false; |
| 112 |
if (textInputField.cursorPosition === textInputField.length) { |
| 113 |
longFormula = Formula.deleteLastFormulaElement(isLastCalculate, longFormula) |
| 114 |
} else { |
| 115 |
var truncatedSubstring = Formula.deleteLastFormulaElement(isLastCalculate, longFormula.slice(0, textInputField.cursorPosition)) |
| 116 |
longFormula = truncatedSubstring + longFormula.slice(textInputField.cursorPosition, longFormula.length); |
| 117 |
} |
| 118 |
shortFormula = longFormula; |
| 119 |
|
| 120 |
displayedInputText = longFormula; |
| 121 |
if (truncatedSubstring) { |
| 122 |
textInputField.cursorPosition = truncatedSubstring.length; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Function to clear formula in input text field |
| 128 |
*/ |
| 129 |
function clearFormula() { |
| 130 |
isFormulaIsValidToCalculate = false; |
| 131 |
shortFormula = ""; |
| 132 |
longFormula = ""; |
| 133 |
displayedInputText = ""; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Format bigNumber |
| 138 |
*/ |
| 139 |
function formatBigNumber(bigNumberToFormat) { |
| 140 |
// Maximum length of the result number |
| 141 |
var NUMBER_LENGTH_LIMIT = 14; |
| 142 |
|
| 143 |
if (bigNumberToFormat.toString().length > NUMBER_LENGTH_LIMIT) { |
| 144 |
var resultLength = mathJs.format(bigNumberToFormat, {exponential: {lowerExp: 1e-10, upperExp: 1e10}, |
| 145 |
precision: NUMBER_LENGTH_LIMIT}).toString().length; |
| 146 |
|
| 147 |
return mathJs.format(bigNumberToFormat, {exponential: {lowerExp: 1e-10, upperExp: 1e10}, |
| 148 |
precision: (NUMBER_LENGTH_LIMIT - resultLength + NUMBER_LENGTH_LIMIT)}).toString(); |
| 149 |
} |
| 150 |
return bigNumberToFormat.toString(); |
| 151 |
} |
| 152 |
|
| 153 |
function resetFormulaFontColor(handler) { |
| 154 |
if (textInputField.color !== Suru.foregroundColor) { |
| 155 |
resetAnimationPt1.completionHandler = handler; |
| 156 |
resetAnimationPt1.restart(); |
| 157 |
} else { |
| 158 |
handler(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
function formulaPush(visual) { |
| 163 |
// If the user press a number after the press of "=" we start a new |
| 164 |
// formula, otherwise we continue with the old one |
| 165 |
if ((!isNaN(visual) || (visual === ".")) && isLastCalculate) { |
| 166 |
isFormulaIsValidToCalculate = false; |
| 167 |
longFormula = displayedInputText = shortFormula = ""; |
| 168 |
} |
| 169 |
// Add zero when decimal separator is not after number |
| 170 |
if ((visual === ".") && ((isNaN(displayedInputText.slice(textInputField.cursorPosition - 1, textInputField.cursorPosition))) || (longFormula === ""))) { |
| 171 |
visual = "0."; |
| 172 |
} |
| 173 |
isLastCalculate = false; |
| 174 |
|
| 175 |
// Validate whole longFormula if the cursor is at the end of string |
| 176 |
if (textInputField.cursorPosition === textInputField.length) { |
| 177 |
if (visual === "()") { |
| 178 |
visual = Formula.determineBracketTypeToAdd(longFormula) |
| 179 |
} |
| 180 |
if (Formula.validateStringForAddingToFormula(longFormula, visual) === false) { |
| 181 |
errorAnimation.restart(); |
| 182 |
return; |
| 183 |
} |
| 184 |
} else { |
| 185 |
if (visual === "()") { |
| 186 |
visual = Formula.determineBracketTypeToAdd(longFormula.slice(0, textInputField.cursorPosition)) |
| 187 |
} |
| 188 |
if (Formula.validateStringForAddingToFormula(longFormula.slice(0, textInputField.cursorPosition), visual) === false) { |
| 189 |
errorAnimation.restart(); |
| 190 |
return; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// We save the value until next value is pushed |
| 195 |
previousVisual = visual; |
| 196 |
|
| 197 |
// If we add an operator after an operator we know has priority, |
| 198 |
// we display a temporary result instead the all operation |
| 199 |
if (isNaN(visual) && (visual.toString() !== ".") && isFormulaIsValidToCalculate) { |
| 200 |
try { |
| 201 |
shortFormula = formatBigNumber(mathJs.evaluate(shortFormula)); |
| 202 |
} catch(exception) { |
| 203 |
console.log("Debug: Temp result: " + exception.toString() + " engine formula: " + shortFormula); |
| 204 |
} |
| 205 |
|
| 206 |
isFormulaIsValidToCalculate = false; |
| 207 |
} |
| 208 |
|
| 209 |
// Adding the new operator to the formula |
| 210 |
if (textInputField.cursorPosition === textInputField.length ) { |
| 211 |
longFormula += visual.toString(); |
| 212 |
shortFormula += visual.toString(); |
| 213 |
displayedInputText = shortFormula; |
| 214 |
} else { |
| 215 |
longFormula = longFormula.slice(0, textInputField.cursorPosition) + visual.toString() + longFormula.slice(textInputField.cursorPosition, longFormula.length); |
| 216 |
shortFormula = longFormula; |
| 217 |
var preservedCursorPosition = textInputField.cursorPosition; |
| 218 |
displayedInputText = shortFormula; |
| 219 |
textInputField.cursorPosition = preservedCursorPosition + visual.length; |
| 220 |
} |
| 221 |
|
| 222 |
// Add here operators that have always priority |
| 223 |
if (visual.toString() === ")") { |
| 224 |
isFormulaIsValidToCalculate = true; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
function calculate() { |
| 229 |
if ((longFormula === '') || (isLastCalculate === true)) { |
| 230 |
errorAnimation.restart(); |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
// We try to balance brackets to avoid mathJs errors |
| 235 |
var numberOfOpenedBrackets = (longFormula.match(/\(/g) || []).length - |
| 236 |
(longFormula.match(/\)/g) || []).length; |
| 237 |
|
| 238 |
for (var i = 0; i < numberOfOpenedBrackets; i++) { |
| 239 |
formulaPush(')'); |
| 240 |
} |
| 241 |
|
| 242 |
try { |
| 243 |
var result = mathJs.evaluate(longFormula); |
| 244 |
|
| 245 |
result = formatBigNumber(result) |
| 246 |
|
| 247 |
} catch(exception) { |
| 248 |
// If the formula isn't right and we added brackets, we remove them |
| 249 |
for (var i = 0; i < numberOfOpenedBrackets; i++) { |
| 250 |
deleteLastFormulaElement(); |
| 251 |
} |
| 252 |
console.log("[LOG]: Unable to calculate formula : \"" + longFormula + "\", math.js: " + exception.toString()); |
| 253 |
|
| 254 |
errorAnimation.restart(); |
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
isLastCalculate = true; |
| 259 |
|
| 260 |
if (result === longFormula) { |
| 261 |
errorAnimation.restart(); |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
calculationHistory.addCalculationToScreen(longFormula, result, false, ""); |
| 266 |
editedCalculationIndex = -1; |
| 267 |
longFormula = result; |
| 268 |
shortFormula = result; |
| 269 |
displayedInputText = result; |
| 270 |
} |
| 271 |
|
| 272 |
PageStack { |
| 273 |
id: mainStack |
| 274 |
|
| 275 |
Component.onCompleted: { |
| 276 |
push(calculatorPage); |
| 277 |
//calculatorPage.forceActiveFocus(); |
| 278 |
} |
| 279 |
|
| 280 |
onHeightChanged: scrollableView.scrollToBottom(); |
| 281 |
anchors.fill: parent |
| 282 |
|
| 283 |
Page { |
| 284 |
id: calculatorPage |
| 285 |
title: i18n.tr("Calculator") |
| 286 |
|
| 287 |
header: PageHeader { |
| 288 |
id: mainHeader |
| 289 |
visible: false |
| 290 |
|
| 291 |
leadingActionBar.actions: Action { |
| 292 |
objectName: "cancelSelectionAction" |
| 293 |
iconName: "close" |
| 294 |
text: i18n.tr("Cancel") |
| 295 |
onTriggered: visualModel.cancelSelection() |
| 296 |
} |
| 297 |
|
| 298 |
trailingActionBar { |
| 299 |
actions: [ |
| 300 |
Action { |
| 301 |
id: selectAllAction |
| 302 |
objectName: "selectAllAction" |
| 303 |
iconName: visualModel.selectedItems.count < visualModel.items.count ? |
| 304 |
"select" : "select-none" |
| 305 |
text: visualModel.selectedItems.count < visualModel.items.count ? |
| 306 |
i18n.tr("Select All") : i18n.tr("Select None") |
| 307 |
onTriggered: visualModel.selectAll() |
| 308 |
}, |
| 309 |
Action { |
| 310 |
id: copySelectedAction |
| 311 |
objectName: "copySelectedAction" |
| 312 |
iconName: "edit-copy" |
| 313 |
text: i18n.tr("Copy") |
| 314 |
onTriggered: calculatorPage.copySelectedCalculations() |
| 315 |
enabled: visualModel.selectedItems.count > 0 |
| 316 |
}, |
| 317 |
Action { |
| 318 |
id: multiDeleteAction |
| 319 |
objectName: "multiDeleteAction" |
| 320 |
iconName: "delete" |
| 321 |
text: i18n.tr("Delete") |
| 322 |
onTriggered: calculatorPage.deleteSelectedCalculations() |
| 323 |
enabled: visualModel.selectedItems.count > 0 |
| 324 |
} |
| 325 |
] |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
anchors.fill: parent |
| 330 |
visible: false |
| 331 |
|
| 332 |
state: visualModel.isInSelectionMode ? "selection" : "default" |
| 333 |
states: [ |
| 334 |
State { |
| 335 |
name: "default" |
| 336 |
PropertyChanges { |
| 337 |
target: scrollableView |
| 338 |
clip: false |
| 339 |
} |
| 340 |
PropertyChanges { |
| 341 |
target: mainHeader |
| 342 |
visible: false |
| 343 |
} |
| 344 |
}, |
| 345 |
State { |
| 346 |
name: "selection" |
| 347 |
PropertyChanges { |
| 348 |
target: scrollableView |
| 349 |
clip: true |
| 350 |
} |
| 351 |
PropertyChanges { |
| 352 |
target: mainHeader |
| 353 |
visible: true |
| 354 |
} |
| 355 |
} |
| 356 |
] |
| 357 |
|
| 358 |
CalculationHistory { |
| 359 |
id: calculationHistory |
| 360 |
} |
| 361 |
|
| 362 |
// Some special keys like backspace captured in TextField, |
| 363 |
// are for some reason not sent to the application but to the text input |
| 364 |
Keys.onPressed: {event.accepted = true; textInputField.keyPress(event)} |
| 365 |
Keys.onReleased: textInputField.keyRelease(event) |
| 366 |
|
| 367 |
Component { |
| 368 |
id: emptyDelegate |
| 369 |
|
| 370 |
Item { } |
| 371 |
} |
| 372 |
|
| 373 |
Component { |
| 374 |
id: screenDelegateComponent |
| 375 |
|
| 376 |
Screen { |
| 377 |
id: screenDelegate |
| 378 |
width: parent ? parent.width : 0 |
| 379 |
|
| 380 |
property var model: itemModel |
| 381 |
visible: model.dbId !== -1 |
| 382 |
|
| 383 |
selectMode: visualModel.isInSelectionMode |
| 384 |
selected: visualModel.isSelected(visualDelegate) |
| 385 |
|
| 386 |
property var removalAnimation |
| 387 |
function remove() { |
| 388 |
removalAnimation.start(); |
| 389 |
} |
| 390 |
|
| 391 |
// parent is the loader component |
| 392 |
property var visualDelegate: parent ? parent : null |
| 393 |
|
| 394 |
onSwipedChanged: { |
| 395 |
visualModel.updateSwipeState(screenDelegate); |
| 396 |
} |
| 397 |
|
| 398 |
onClicked: { |
| 399 |
if (visualModel.isInSelectionMode) { |
| 400 |
if (!visualModel.selectItem(visualDelegate)) { |
| 401 |
visualModel.deselectItem(visualDelegate); |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
onPressAndHold: { |
| 407 |
visualModel.startSelection(); |
| 408 |
visualModel.selectItem(visualDelegate); |
| 409 |
} |
| 410 |
|
| 411 |
leadingActions: ListItemActions { |
| 412 |
actions: [ |
| 413 |
Action { |
| 414 |
id: screenDelegateDeleteAction |
| 415 |
iconName: "delete" |
| 416 |
text: i18n.tr("Delete") |
| 417 |
onTriggered: { |
| 418 |
screenDelegate.remove(); |
| 419 |
} |
| 420 |
} |
| 421 |
] |
| 422 |
} |
| 423 |
|
| 424 |
trailingActions: ListItemActions { |
| 425 |
actions: [ |
| 426 |
Action { |
| 427 |
id: screenDelegateCopyAction |
| 428 |
iconName: "edit-copy" |
| 429 |
text: i18n.tr("Copy") |
| 430 |
onTriggered: { |
| 431 |
var mimeData = Clipboard.newData(); |
| 432 |
mimeData.text = model.formula + "=" + model.result; |
| 433 |
Clipboard.push(mimeData); |
| 434 |
} |
| 435 |
}, |
| 436 |
Action { |
| 437 |
id: screenDelegateEditAction |
| 438 |
iconName: "edit" |
| 439 |
text: i18n.tr("Edit") |
| 440 |
onTriggered: { |
| 441 |
longFormula = model.formula; |
| 442 |
shortFormula = model.result; |
| 443 |
displayedInputText = model.formula; |
| 444 |
isLastCalculate = false; |
| 445 |
previousVisual = ""; |
| 446 |
scrollableView.scrollToBottom(); |
| 447 |
} |
| 448 |
}, |
| 449 |
Action { |
| 450 |
id: screenDelegateEditResultAction |
| 451 |
iconName: "edit-paste" |
| 452 |
text: i18n.tr("Edit Result") |
| 453 |
onTriggered: { |
| 454 |
longFormula = model.result; |
| 455 |
shortFormula = model.result; |
| 456 |
displayedInputText = model.result; |
| 457 |
isLastCalculate = false; |
| 458 |
previousVisual = ""; |
| 459 |
scrollableView.scrollToBottom(); |
| 460 |
} |
| 461 |
} |
| 462 |
] |
| 463 |
} |
| 464 |
|
| 465 |
removalAnimation: SequentialAnimation { |
| 466 |
alwaysRunToEnd: true |
| 467 |
|
| 468 |
ScriptAction { |
| 469 |
script: { |
| 470 |
if (visualModel.currentSwipedItem === screenDelegate) { |
| 471 |
visualModel.currentSwipedItem = null; |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
LomiriNumberAnimation { |
| 477 |
target: screenDelegate |
| 478 |
property: "height" |
| 479 |
to: 0 |
| 480 |
} |
| 481 |
|
| 482 |
ScriptAction { |
| 483 |
script: { |
| 484 |
calculationHistory.deleteCalc(model.dbId, model.index); |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
function deleteSelectedCalculations() { |
| 492 |
deleteSelectedCalculation = true; |
| 493 |
visualModel.endSelection(); |
| 494 |
} |
| 495 |
|
| 496 |
function copySelectedCalculations() { |
| 497 |
deleteSelectedCalculation = false; |
| 498 |
visualModel.endSelection(); |
| 499 |
} |
| 500 |
|
| 501 |
MultipleSelectionVisualModel { |
| 502 |
id: visualModel |
| 503 |
model: calculationHistory.getContents() |
| 504 |
|
| 505 |
onSelectionDone: { |
| 506 |
if(deleteSelectedCalculation === true) { |
| 507 |
for(var i = 0; i < items.count; i++) { |
| 508 |
calculationHistory.deleteCalc(items.get(i).model.dbId, items.get(i).model.index); |
| 509 |
} |
| 510 |
} else { |
| 511 |
var mimeData = Clipboard.newData(); |
| 512 |
mimeData.text = ""; |
| 513 |
for(var j = 0; j < items.count; j++) { |
| 514 |
if (items.get(j).model.dbId !== -1) { |
| 515 |
mimeData.text = mimeData.text + items.get(j).model.formula + "=" + items.get(j).model.result + "\n"; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
Clipboard.push(mimeData); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
delegate: Loader { |
| 524 |
property var itemModel: model |
| 525 |
width: parent.width |
| 526 |
height: model.dbId !== -1 ? item.height : 0; |
| 527 |
sourceComponent: screenDelegateComponent |
| 528 |
asynchronous: true |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
ScrollableView { |
| 533 |
anchors { |
| 534 |
fill: parent |
| 535 |
bottomMargin: bottomEdge.enabled |
| 536 |
? bottomEdge.hint.height |
| 537 |
: units.gu(1) |
| 538 |
topMargin: mainHeader.visible |
| 539 |
? mainHeader.height |
| 540 |
: 0 |
| 541 |
} |
| 542 |
|
| 543 |
id: scrollableView |
| 544 |
objectName: "scrollableView" |
| 545 |
visible: keyboardLoader.status == Loader.Ready |
| 546 |
|
| 547 |
Component.onCompleted: { |
| 548 |
// FIXME: workaround for qtubuntu not returning values depending on the grid unit definition |
| 549 |
// for Flickable.maximumFlickVelocity and Flickable.flickDeceleration |
| 550 |
var scaleFactor = units.gridUnit / 8; |
| 551 |
maximumFlickVelocity = maximumFlickVelocity * scaleFactor; |
| 552 |
flickDeceleration = flickDeceleration * scaleFactor; |
| 553 |
} |
| 554 |
|
| 555 |
Repeater { |
| 556 |
id: formulaView |
| 557 |
model: visualModel |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
Rectangle { |
| 562 |
width: parent.width |
| 563 |
height: units.gu(6) |
| 564 |
color: Suru.backgroundColor |
| 565 |
|
| 566 |
|
| 567 |
TextField { |
| 568 |
id: textInputField |
| 569 |
objectName: "textInputField" |
| 570 |
|
| 571 |
height: parent.height |
| 572 |
|
| 573 |
anchors { |
| 574 |
fill: parent |
| 575 |
rightMargin: units.gu(2) |
| 576 |
leftMargin: units.gu(2) |
| 577 |
right: parent.right |
| 578 |
} |
| 579 |
|
| 580 |
color: Suru.foregroundColor |
| 581 |
style: TextFieldStyle { |
| 582 |
background: Item { |
| 583 |
|
| 584 |
LomiriShape { |
| 585 |
aspect: LomiriShape.Flat |
| 586 |
color: Suru.secondaryBackgroundColor |
| 587 |
width: parent.width |
| 588 |
height: parent.height |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
text: Formula.returnFormulaToDisplay(displayedInputText, i18n, decimalPoint) |
| 594 |
font.pixelSize: height * 0.7 |
| 595 |
horizontalAlignment: TextInput.AlignRight |
| 596 |
|
| 597 |
|
| 598 |
// Need to capture special keys like backspace here, |
| 599 |
// as they are for some reason not sent to the application but to the text input |
| 600 |
Keys.onPressed: keyPress(event) |
| 601 |
Keys.onReleased: keyRelease(event) |
| 602 |
|
| 603 |
function keyPress(event) { |
| 604 |
if (!(event.modifiers & Qt.ControlModifier || event.modifiers & Qt.AltModifier)) { // Shift needs to be passed through as it may be required for some special keys |
| 605 |
if((event.key === Qt.Key_Up || event.key === Qt.Key_Down) && event.accepted) { |
| 606 |
if(event.key === Qt.Key_Up && historyPosition > 1) |
| 607 |
historyPosition--; |
| 608 |
if(event.key === Qt.Key_Down && historyPosition < calculationHistory.getContents().count) |
| 609 |
historyPosition++; |
| 610 |
if(historyPosition !== calculationHistory.getContents().count) { |
| 611 |
isUsingHistory = true; |
| 612 |
clearFormula(); |
| 613 |
formulaPush(calculationHistory.getContents().get(historyPosition).formula); |
| 614 |
} |
| 615 |
else if(isUsingHistory) |
| 616 |
{ |
| 617 |
clearFormula(); |
| 618 |
formulaPush(lastWrittenFormula); |
| 619 |
isUsingHistory = false; |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
keyboardLoader.item.pressedKey = event.key; |
| 624 |
keyboardLoader.item.pressedKeyText = event.text; |
| 625 |
|
| 626 |
if (bottomEdge.contentItem) { |
| 627 |
bottomEdge.contentItem.pressedKey = event.key; |
| 628 |
bottomEdge.contentItem.pressedKeyText = event.text; |
| 629 |
} |
| 630 |
} else if (event.modifiers & Qt.ControlModifier) { |
| 631 |
if (event.key === Qt.Key_C) { // Copy action |
| 632 |
var mimeData = Clipboard.newData(); |
| 633 |
mimeData.text = textInputField.selectedText; |
| 634 |
Clipboard.push(mimeData); |
| 635 |
} else if (event.key === Qt.Key_V) { // Paste action |
| 636 |
if (Clipboard.data.text && Clipboard.data.text !== "") { |
| 637 |
var data = Clipboard.data.text; |
| 638 |
|
| 639 |
// Get all accepted characters (i.e. those which can be entered) from the current keyboard |
| 640 |
var acceptedBits = []; |
| 641 |
for (var i = 0; i < keyboardLoader.item.children.length; i++) { |
| 642 |
var model = keyboardLoader.item.children[i].keyboardModel; |
| 643 |
if (model) { |
| 644 |
for (var j = 0; j < model.length; j++) { |
| 645 |
var item = model[j]; |
| 646 |
if (!item.action) { |
| 647 |
if (item.number || item.forceNumber) |
| 648 |
acceptedBits.push({ "chars": item.number, "push": item.number }); |
| 649 |
if (item.pushText) |
| 650 |
acceptedBits.push({ "chars": item.pushText, "push": item.pushText }); |
| 651 |
if (item.text) |
| 652 |
acceptedBits.push({ "chars": item.text, "push": item.pushText ? item.pushText : item.text }); |
| 653 |
if (item.pasteTexts) |
| 654 |
for (var pos = 0; pos < item.pasteTexts.length; pos++) |
| 655 |
acceptedBits.push({ "chars": item.pasteTexts[pos], "push": item.pasteTexts[pos] }); |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
// Extract the part of the clipboard data which can be pasted |
| 662 |
var paste = ""; |
| 663 |
var pos = 0; |
| 664 |
while (pos < data.length) { |
| 665 |
// Check if the string starts with an accepted string |
| 666 |
for (i = 0; i < acceptedBits.length; i++) { |
| 667 |
if (data.substring(pos, pos + (acceptedBits[i].chars.length ? acceptedBits[i].chars.length : 1)) === acceptedBits[i].chars.toString()) { |
| 668 |
paste += acceptedBits[i].push; |
| 669 |
pos += acceptedBits[i].chars.length ? acceptedBits[i].chars.length : 1; |
| 670 |
break; |
| 671 |
} |
| 672 |
} |
| 673 |
// Skip one char if it could not be found |
| 674 |
if (i === acceptedBits.length) |
| 675 |
pos++; |
| 676 |
} |
| 677 |
|
| 678 |
// Push the paste string |
| 679 |
formulaPush(paste); |
| 680 |
} else { |
| 681 |
console.log("Debug: paste failed as the clipboard contains no text"); |
| 682 |
} |
| 683 |
|
| 684 |
scrollableView.scrollToBottom(); |
| 685 |
} else if (event.key === Qt.Key_O) { |
| 686 |
// TODO remove me, just for debugging |
| 687 |
bottomEdge.commit(); |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
function keyRelease(event) { |
| 693 |
keyboardLoader.item.pressedKey = -1; |
| 694 |
keyboardLoader.item.pressedKeyText = ""; |
| 695 |
|
| 696 |
if (bottomEdge.contentItem) { |
| 697 |
bottomEdge.contentItem.pressedKey = -1; |
| 698 |
bottomEdge.contentItem.pressedKeyText = ""; |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
//TODO: Find a better way of doing this? |
| 703 |
//Don't let keyboard to show |
| 704 |
onFocusChanged: focus = false |
| 705 |
focus: false |
| 706 |
selectByMouse: true |
| 707 |
cursorVisible: true |
| 708 |
onCursorPositionChanged: |
| 709 |
if (cursorPosition !== length ) { |
| 710 |
// Count cursor position from the end of line |
| 711 |
var preservedCursorPosition = length - cursorPosition; |
| 712 |
displayedInputText = longFormula; |
| 713 |
cursorPosition = length - preservedCursorPosition; |
| 714 |
} else { |
| 715 |
displayedInputText = shortFormula; |
| 716 |
} |
| 717 |
|
| 718 |
onTextChanged: { |
| 719 |
if(! isUsingHistory) { |
| 720 |
lastWrittenFormula = textInputField.text; |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
SequentialAnimation { |
| 725 |
id: errorAnimation |
| 726 |
running: false |
| 727 |
PropertyAnimation { |
| 728 |
target: textInputField |
| 729 |
properties: "color" |
| 730 |
to: Suru.backgroundColor |
| 731 |
duration: LomiriAnimation.SnapDuration |
| 732 |
} |
| 733 |
PauseAnimation { |
| 734 |
duration: LomiriAnimation.SnapDuration |
| 735 |
} |
| 736 |
PropertyAnimation { |
| 737 |
target: textInputField |
| 738 |
properties: "color" |
| 739 |
to: LomiriColors.orange |
| 740 |
duration: LomiriAnimation.SnapDuration |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
PropertyAnimation { |
| 745 |
id: resetAnimationPt1 |
| 746 |
running: false |
| 747 |
property var completionHandler: null |
| 748 |
target: textInputField |
| 749 |
properties: "color" |
| 750 |
to: Suru.backgroundColor |
| 751 |
duration: LomiriAnimation.SnapDuration |
| 752 |
onStopped: { |
| 753 |
if (completionHandler !== null) { |
| 754 |
completionHandler(); |
| 755 |
completionHandler = null; |
| 756 |
} |
| 757 |
resetAnimationPt2.restart(); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
SequentialAnimation { |
| 762 |
id: resetAnimationPt2 |
| 763 |
running: false |
| 764 |
PauseAnimation { |
| 765 |
duration: LomiriAnimation.SnapDuration |
| 766 |
} |
| 767 |
PropertyAnimation { |
| 768 |
target: textInputField |
| 769 |
properties: "color" |
| 770 |
to: Suru.foregroundColor |
| 771 |
duration: LomiriAnimation.SnapDuration |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
MouseArea { |
| 777 |
anchors.fill: parent |
| 778 |
propagateComposedEvents: true |
| 779 |
onClicked: { |
| 780 |
textInputField.cursorPosition = textInputField.positionAt(mouseX,TextInput.CursorOnCharacter) |
| 781 |
} |
| 782 |
onPressAndHold: { |
| 783 |
textInputField.cursorPosition = textInputField.positionAt(mouseX,TextInput.CursorOnCharacter) |
| 784 |
PopupUtils.open(Qt.resolvedUrl("ui/TextInputPopover.qml"), textInputField, {target: textInputField}) |
| 785 |
} |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
Loader { |
| 790 |
id: keyboardLoader |
| 791 |
width: parent.width |
| 792 |
enabled: mathJs != null |
| 793 |
// FIXME: this works around the fact that the final size |
| 794 |
// of keyboardLoader (and of mainView) is only set by the window |
| 795 |
// manager quite late; this avoids unnecessary reloads of the |
| 796 |
// source |
| 797 |
active: false |
| 798 |
property bool sizeReady: Window.active |
| 799 |
onSizeReadyChanged: if (sizeReady) keyboardLoader.active = true |
| 800 |
source: isLandscape ? "ui/LandscapeKeyboard.qml" : "ui/PortraitKeyboard.qml" |
| 801 |
opacity: ((y + height) >= scrollableView.contentY) && |
| 802 |
(y <= (scrollableView.contentY + scrollableView.height)) ? 1 : 0 |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
BottomEdge { |
| 807 |
id: bottomEdge |
| 808 |
|
| 809 |
height: keyboardLoader.height + hint.height - units.gu(1) |
| 810 |
|
| 811 |
onCollapseCompleted: { |
| 812 |
// FIXME this is a workaround that fixes a space below the |
| 813 |
// keyboard after the bottom edge is collapsed and the screen |
| 814 |
// is rotated |
| 815 |
scrollableView.contentY = 0; |
| 816 |
scrollableView.scrollToBottom(); |
| 817 |
} |
| 818 |
|
| 819 |
// TODO tap outside the bottom edge to close it |
| 820 |
|
| 821 |
contentUrl: Qt.resolvedUrl("ui/BottomEdgePage.qml") |
| 822 |
enabled: textInputField.visible && !isLandscape && mathJs != null |
| 823 |
hint.text: i18n.tr("Functions") |
| 824 |
hint.visible: enabled |
| 825 |
|
| 826 |
// delay loading bottom edge until after the first frame |
| 827 |
// is drawn to save on startup time |
| 828 |
preloadContent: false |
| 829 |
|
| 830 |
Timer { |
| 831 |
interval: 1 |
| 832 |
repeat: false |
| 833 |
running: true |
| 834 |
onTriggered: bottomEdge.preloadContent = true |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
Component.onCompleted: { |
| 841 |
i18n.domain = "lomiri-calculator-app"; |
| 842 |
i18n.bindtextdomain("lomiri-calculator-app", i18nDirectory) |
| 843 |
} |
| 844 |
} |