2023-10-02 09:20:35 +00:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
2022-06-03 11:26:02 +00:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2018-06-26 13:53:07 +00:00
|
|
|
|
2023-10-02 09:20:35 +00:00
|
|
|
pragma ComponentBehavior: Bound
|
|
|
|
|
2020-12-01 12:57:32 +00:00
|
|
|
import QtQuick
|
|
|
|
import QtQuick.Window
|
2018-06-26 13:53:07 +00:00
|
|
|
|
|
|
|
Item {
|
|
|
|
id: display
|
2023-10-02 09:20:35 +00:00
|
|
|
readonly property real fontSize: Math.floor(Screen.pixelDensity * 5.0)
|
2018-06-26 13:53:07 +00:00
|
|
|
property bool enteringDigits: false
|
2021-09-14 08:36:23 +00:00
|
|
|
readonly property int maxDigits: (width / fontSize) + 1
|
2018-06-26 13:53:07 +00:00
|
|
|
property string displayedOperand
|
2023-10-02 09:20:35 +00:00
|
|
|
readonly property string errorString: qsTr("ERROR")
|
2021-09-14 08:36:23 +00:00
|
|
|
readonly property bool isError: displayedOperand === errorString
|
2018-06-26 13:53:07 +00:00
|
|
|
|
2023-10-02 09:20:35 +00:00
|
|
|
function displayOperator(operator) {
|
|
|
|
calculationsListView.model.append({ "operator": operator, "operand": "" })
|
2018-06-26 13:53:07 +00:00
|
|
|
enteringDigits = true
|
2023-10-02 09:20:35 +00:00
|
|
|
calculationsListView.positionViewAtEnd()
|
2018-06-26 13:53:07 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 09:20:35 +00:00
|
|
|
function newLine(operator, operand) {
|
2018-06-26 13:53:07 +00:00
|
|
|
displayedOperand = displayNumber(operand)
|
2023-10-02 09:20:35 +00:00
|
|
|
calculationsListView.model.append({ "operator": operator, "operand": displayedOperand })
|
2018-06-26 13:53:07 +00:00
|
|
|
enteringDigits = false
|
2023-10-02 09:20:35 +00:00
|
|
|
calculationsListView.positionViewAtEnd()
|
2018-06-26 13:53:07 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 09:20:35 +00:00
|
|
|
function appendDigit(digit) {
|
2018-06-26 13:53:07 +00:00
|
|
|
if (!enteringDigits)
|
2023-10-02 09:20:35 +00:00
|
|
|
calculationsListView.model.append({ "operator": "", "operand": "" })
|
|
|
|
const i = calculationsListView.model.count - 1;
|
|
|
|
calculationsListView.model.get(i).operand = calculationsListView.model.get(i).operand + digit;
|
2018-06-26 13:53:07 +00:00
|
|
|
enteringDigits = true
|
2023-10-02 09:20:35 +00:00
|
|
|
calculationsListView.positionViewAtEnd()
|
2018-06-26 13:53:07 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 09:20:35 +00:00
|
|
|
function setDigit(digit) {
|
|
|
|
const i = calculationsListView.model.count - 1;
|
|
|
|
calculationsListView.model.get(i).operand = digit;
|
|
|
|
calculationsListView.positionViewAtEnd()
|
2018-06-26 13:53:07 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 09:20:35 +00:00
|
|
|
function clear() {
|
2018-06-26 13:53:07 +00:00
|
|
|
displayedOperand = ""
|
|
|
|
if (enteringDigits) {
|
2023-10-02 09:20:35 +00:00
|
|
|
const i = calculationsListView.model.count - 1
|
2018-06-26 13:53:07 +00:00
|
|
|
if (i >= 0)
|
2023-10-02 09:20:35 +00:00
|
|
|
calculationsListView.model.remove(i)
|
2018-06-26 13:53:07 +00:00
|
|
|
enteringDigits = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a string representation of a number that fits in
|
|
|
|
// display.maxDigits characters, trying to keep as much precision
|
|
|
|
// as possible. If the number cannot be displayed, returns an
|
|
|
|
// error string.
|
|
|
|
function displayNumber(num) {
|
2023-10-02 09:20:35 +00:00
|
|
|
if (typeof(num) !== "number")
|
2018-06-26 13:53:07 +00:00
|
|
|
return errorString;
|
|
|
|
|
2021-09-14 08:36:23 +00:00
|
|
|
const intNum = parseInt(num);
|
|
|
|
const intLen = intNum.toString().length;
|
2018-06-26 13:53:07 +00:00
|
|
|
|
|
|
|
// Do not count the minus sign as a digit
|
2021-09-14 08:36:23 +00:00
|
|
|
const maxLen = num < 0 ? maxDigits + 1 : maxDigits;
|
2018-06-26 13:53:07 +00:00
|
|
|
|
|
|
|
if (num.toString().length <= maxLen) {
|
|
|
|
if (isFinite(num))
|
|
|
|
return num.toString();
|
|
|
|
return errorString;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Integer part of the number is too long - try
|
|
|
|
// an exponential notation
|
2023-10-02 09:20:35 +00:00
|
|
|
if (intNum === num || intLen > maxLen - 3) {
|
2021-09-14 08:36:23 +00:00
|
|
|
const expVal = num.toExponential(maxDigits - 6).toString();
|
2018-06-26 13:53:07 +00:00
|
|
|
if (expVal.length <= maxLen)
|
|
|
|
return expVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try a float presentation with fixed number of digits
|
2021-09-14 08:36:23 +00:00
|
|
|
const floatStr = parseFloat(num).toFixed(maxDigits - intLen - 1).toString();
|
2018-06-26 13:53:07 +00:00
|
|
|
if (floatStr.length <= maxLen)
|
|
|
|
return floatStr;
|
|
|
|
|
|
|
|
return errorString;
|
|
|
|
}
|
|
|
|
|
|
|
|
Item {
|
|
|
|
id: theItem
|
2021-07-02 08:17:04 +00:00
|
|
|
width: display.width + 32
|
|
|
|
height: display.height
|
2018-06-26 13:53:07 +00:00
|
|
|
|
|
|
|
Rectangle {
|
|
|
|
id: rect
|
|
|
|
x: 16
|
|
|
|
color: "white"
|
2021-07-02 08:17:04 +00:00
|
|
|
height: theItem.height
|
2018-06-26 13:53:07 +00:00
|
|
|
width: display.width - 16
|
|
|
|
}
|
|
|
|
Image {
|
|
|
|
anchors.right: rect.left
|
|
|
|
source: "images/paper-edge-left.png"
|
2021-07-02 08:17:04 +00:00
|
|
|
height: theItem.height
|
2018-06-26 13:53:07 +00:00
|
|
|
fillMode: Image.TileVertically
|
|
|
|
}
|
|
|
|
Image {
|
|
|
|
anchors.left: rect.right
|
|
|
|
source: "images/paper-edge-right.png"
|
2021-07-02 08:17:04 +00:00
|
|
|
height: theItem.height
|
2018-06-26 13:53:07 +00:00
|
|
|
fillMode: Image.TileVertically
|
|
|
|
}
|
|
|
|
|
|
|
|
Image {
|
|
|
|
id: grip
|
|
|
|
source: "images/paper-grip.png"
|
2021-07-02 08:17:04 +00:00
|
|
|
anchors.horizontalCenter: theItem.horizontalCenter
|
|
|
|
anchors.bottom: theItem.bottom
|
2018-06-26 13:53:07 +00:00
|
|
|
anchors.bottomMargin: 20
|
|
|
|
}
|
|
|
|
|
|
|
|
ListView {
|
2023-10-02 09:20:35 +00:00
|
|
|
id: calculationsListView
|
|
|
|
x: 16
|
|
|
|
y: 30
|
2018-06-26 13:53:07 +00:00
|
|
|
width: display.width
|
|
|
|
height: display.height - 50 - y
|
2023-10-02 09:20:35 +00:00
|
|
|
clip: true
|
2018-06-26 13:53:07 +00:00
|
|
|
delegate: Item {
|
|
|
|
height: display.fontSize * 1.1
|
2023-10-02 09:20:35 +00:00
|
|
|
width: calculationsListView.width
|
|
|
|
|
|
|
|
required property string operator
|
|
|
|
required property string operand
|
|
|
|
|
2018-06-26 13:53:07 +00:00
|
|
|
Text {
|
|
|
|
x: 6
|
|
|
|
font.pixelSize: display.fontSize
|
|
|
|
color: "#6da43d"
|
2023-10-02 09:20:35 +00:00
|
|
|
text: parent.operator
|
2018-06-26 13:53:07 +00:00
|
|
|
}
|
|
|
|
Text {
|
|
|
|
font.pixelSize: display.fontSize
|
2023-10-02 09:20:35 +00:00
|
|
|
anchors.right: parent.right
|
2018-06-26 13:53:07 +00:00
|
|
|
anchors.rightMargin: 22
|
2023-10-02 09:20:35 +00:00
|
|
|
text: parent.operand
|
2018-06-26 13:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
model: ListModel { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|