mathtrainingstuff/Calculator.qml

144 lines
3.2 KiB
QML
Raw Normal View History

2018-11-23 17:14:09 +01:00
import QtQuick 2.11
import QtQuick.Controls 2.4
import "./"
2018-11-23 17:14:09 +01:00
Item {
id: calculator
property int sum: 0
property int nextNum: 0
property int lastNum: 0
2018-11-26 19:33:59 +01:00
property int actualNumCount: 0
property int min: 1
property int max: 9
2018-11-23 17:14:09 +01:00
property int tickInterval: 1000
property int numCount: 10
2018-11-26 19:33:59 +01:00
property bool endPageVisibility: false
property string endPageLabelText: "You Lose!"
2018-11-23 17:14:09 +01:00
signal pageOpened()
onPageOpened: {
calculator.state = "starting"
}
states: [
State {
name: "starting"
PropertyChanges {
target: calculatorStack
currPage: calculatorStartPageComp
}
},
State {
name: "running"
PropertyChanges {
target: calculatorStack
currPage: calculatorMainPageComp
}
},
State {
name: "ending"
PropertyChanges {
target: calculatorStack
currPage: calculatorEndPageComp
2018-11-23 17:14:09 +01:00
}
}
]
StackView {
id: calculatorStack
property var currPage;
anchors.fill: parent
onCurrPageChanged: {
calculatorStack.replace(currPage)
}
onCurrentItemChanged: {
calculatorStack.currentItem.pageOpened()
}
Component {
id: calculatorStartPageComp
CalculatorStartPage {
2018-11-23 17:14:09 +01:00
}
}
Component {
id: calculatorMainPageComp
CalculatorMainPage {
}
}
2018-11-23 17:14:09 +01:00
Component {
id: calculatorEndPageComp
CalculatorEndPage {
2018-11-26 19:33:59 +01:00
id: calculatorEndPage
2018-11-23 17:14:09 +01:00
}
}
replaceExit: Transition {
NumberAnimation {
from: 1
to: 0
property: "opacity"
duration: 200
easing.type: Easing.InOutQuad
}
}
replaceEnter: Transition {
NumberAnimation {
from: 0
to: 1
property: "opacity"
duration: 200
easing.type: Easing.InOutQuad
}
}
}
/*-------------------------
---------functions---------
-------------------------*/
function nextNumber(){
var randNum = 0
var range = calculator.max - calculator.min
lastNum = nextNum
while(randNum===0 || randNum === lastNum){
randNum = Math.floor((Math.random()*(range+1))+min)
2018-11-23 17:14:09 +01:00
}
nextNum = randNum
2018-11-26 19:33:59 +01:00
calculator.sum += randNum
actualNumCount += 1
2018-11-23 17:14:09 +01:00
}
2018-11-26 19:33:59 +01:00
function start(min, max) {
2018-11-23 17:14:09 +01:00
calculator.nextNumber()
calculator.state = "running"
}
2018-11-26 19:33:59 +01:00
function reset() {
calculator.state = "starting"
calculator.sum = 0
calculator.actualNumCount = 0
calculator.endPageLabelText = "You Lose!"
calculator.endPageVisibility = false
calculator.lastNum = 0
calculator.nextNum = 0
}
function checkSum(sumInputText) {
if (calculator.sum === parseInt(sumInputText)) {
calculator.endPageLabelText = "You Won!"
}
2018-11-26 19:33:59 +01:00
calculator.endPageVisibility = true
}
2018-11-23 17:14:09 +01:00
}