You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.9 KiB
QML

4 years ago
import QtQuick 2.9
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5
4 years ago
4 years ago
import FishUI 1.0 as FishUI
4 years ago
import Cutefish.Calculator 1.0
Item {
id: zone
ColumnLayout {
id: layout
anchors.fill: parent
anchors.margins: 0
ListView {
id: listView
model: ListModel { id: historyModel }
clip: true
Layout.fillHeight: true
Layout.fillWidth: true
flickableDirection: Flickable.VerticalFlick
ScrollBar.vertical: ScrollBar {}
onCountChanged: {
listView.currentIndex = count - 1
}
delegate: Item {
4 years ago
height: label.implicitHeight + FishUI.Units.largeSpacing * 2
4 years ago
width: parent ? parent.width : undefined
Label {
id: label
anchors.fill: parent
horizontalAlignment: Qt.AlignRight
text: historyModel.count > 0 ? historyModel.get(index).text : ""
4 years ago
elide: Text.ElideMiddle
4 years ago
color: FishUI.Theme.disabledTextColor
4 years ago
4 years ago
leftPadding: FishUI.Units.largeSpacing
rightPadding: FishUI.Units.largeSpacing
4 years ago
MouseArea {
hoverEnabled: true
}
}
}
}
4 years ago
TextField {
4 years ago
id: textField
Layout.preferredHeight: 50
4 years ago
Layout.fillWidth: true
Keys.onReturnPressed: appendToTextField('=')
Keys.onEnterPressed: appendToTextField('=')
4 years ago
selectByMouse: true
horizontalAlignment: TextInput.AlignRight
focus: Qt.StrongFocus
font.pixelSize: 24
background: Rectangle {
border.width: 0
color: "transparent"
}
4 years ago
leftPadding: FishUI.Units.largeSpacing
rightPadding: FishUI.Units.largeSpacing
4 years ago
}
}
function appendToTextField(text) {
if (text === '=') {
var res = calculate(textField.text)
if (res !== '') {
var expressionText = textField.text
4 years ago
textField.text = res
4 years ago
// If the expression and the result area equal,
// it will not be added to the ListView
if (expressionText !== res) {
expressionText = expressionText + " = " + res
historyModel.append({"text": expressionText})
}
}
4 years ago
} else if (text === 'C') {
4 years ago
if (textField.text != "")
textField.clear()
else
historyModel.clear()
4 years ago
} else if (text === 'BACKSPACE') {
4 years ago
// backspace
textField.remove(textField.cursorPosition, textField.cursorPosition - 1)
} else {
textField.insert(textField.cursorPosition, text)
}
}
}