This repository has been archived on 2022-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
dasschmalter/Das Schmalter App/Qt-Code/main.qml

366 lines
11 KiB
QML
Raw Normal View History

2019-02-23 10:33:02 +01:00
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4
Window {
visible: true
width: 640
height: 480
2019-02-23 14:55:38 +01:00
title: qsTr("Das Schmalter")
2019-02-23 10:33:02 +01:00
Page {
id: app
2019-02-23 21:49:41 +01:00
state: "OFF"
2019-02-23 10:33:02 +01:00
anchors.fill: parent
2019-02-23 14:55:38 +01:00
2019-02-23 21:49:41 +01:00
property string ipAdress: _cppAppSettings.loadSetting("ip-adress")
Label {
id: currentStateLa
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: parent.height * 0.01
anchors.top: parent.top
font.pixelSize: app.landscape() ? parent.height * 0.1:parent.width * 0.1
text: "current state: " + app.state
}
2019-02-23 10:33:02 +01:00
Button {
id: onOffBt
2019-02-23 21:49:41 +01:00
property int animationDuration: 300
property int animationDelay: 50
width: app.landscape() ? parent.height * 0.8:parent.width * 0.8
2019-02-23 14:55:38 +01:00
height: width
anchors.centerIn: parent
2019-02-23 10:33:02 +01:00
2019-02-23 14:55:38 +01:00
background: Item {
id: lamp
2019-02-23 21:49:41 +01:00
2019-02-23 14:55:38 +01:00
Image {
id: lampOn1
source: "on1.png"
anchors.fill: parent
scale: 0
2019-02-23 15:52:11 +01:00
Behavior on scale {
SequentialAnimation {
PauseAnimation {
2019-02-23 21:49:41 +01:00
duration: onOffBt.animationDelay * 0
2019-02-23 15:52:11 +01:00
}
NumberAnimation {
2019-02-23 21:49:41 +01:00
duration: onOffBt.animationDuration
2019-02-23 15:52:11 +01:00
properties: "scale"
2019-02-23 21:49:41 +01:00
from: app.state === "ON" ? 0:1
to: app.state === "ON" ? 1:0
2019-02-23 15:52:11 +01:00
target: lampOn1
}
}
}
2019-02-23 14:55:38 +01:00
}
Image {
id: lampOn2
source: "on2.png"
anchors.fill: parent
scale: 0
2019-02-23 15:52:11 +01:00
Behavior on scale {
SequentialAnimation {
PauseAnimation {
2019-02-23 21:49:41 +01:00
duration: onOffBt.animationDelay * 1
2019-02-23 15:52:11 +01:00
}
NumberAnimation {
2019-02-23 21:49:41 +01:00
duration: onOffBt.animationDuration
2019-02-23 15:52:11 +01:00
properties: "scale"
2019-02-23 21:49:41 +01:00
from: app.state === "ON" ? 0:1
to: app.state === "ON" ? 1:0
2019-02-23 15:52:11 +01:00
target: lampOn2
}
}
}
2019-02-23 14:55:38 +01:00
}
Image {
id: lampOn3
source: "on3.png"
anchors.fill: parent
scale: 0
2019-02-23 15:52:11 +01:00
Behavior on scale {
SequentialAnimation {
PauseAnimation {
2019-02-23 21:49:41 +01:00
duration: onOffBt.animationDelay * 2
2019-02-23 15:52:11 +01:00
}
NumberAnimation {
2019-02-23 21:49:41 +01:00
duration: onOffBt.animationDuration
2019-02-23 15:52:11 +01:00
properties: "scale"
2019-02-23 21:49:41 +01:00
from: app.state === "ON" ? 0:1
to: app.state === "ON" ? 1:0
2019-02-23 15:52:11 +01:00
target: lampOn3
}
}
}
2019-02-23 14:55:38 +01:00
}
Image {
id: lampOn4
source: "on4.png"
anchors.fill: parent
scale: 0
2019-02-23 15:52:11 +01:00
Behavior on scale {
SequentialAnimation {
PauseAnimation {
2019-02-23 21:49:41 +01:00
duration: onOffBt.animationDelay * 3
2019-02-23 15:52:11 +01:00
}
NumberAnimation {
2019-02-23 21:49:41 +01:00
duration: onOffBt.animationDuration
2019-02-23 15:52:11 +01:00
properties: "scale"
2019-02-23 21:49:41 +01:00
from: app.state === "ON" ? 0:1
to: app.state === "ON" ? 1:0
2019-02-23 15:52:11 +01:00
target: lampOn4
}
}
}
2019-02-23 14:55:38 +01:00
}
Image {
id: lampOn5
2019-02-23 15:52:11 +01:00
source: "on5.png"
2019-02-23 14:55:38 +01:00
anchors.fill: parent
scale: 0
2019-02-23 15:52:11 +01:00
Behavior on scale {
SequentialAnimation {
PauseAnimation {
2019-02-23 21:49:41 +01:00
duration: onOffBt.animationDelay * 4
2019-02-23 15:52:11 +01:00
}
NumberAnimation {
2019-02-23 21:49:41 +01:00
duration: onOffBt.animationDuration
2019-02-23 15:52:11 +01:00
properties: "scale"
2019-02-23 21:49:41 +01:00
from: app.state === "ON" ? 0:1
to: app.state === "ON" ? 1:0
2019-02-23 15:52:11 +01:00
target: lampOn5
}
}
}
2019-02-23 14:55:38 +01:00
}
Image {
id: lampOff
source: "off.png"
anchors.fill: parent
scale: 1
}
2019-02-23 21:49:41 +01:00
Image {
id: lampOn0
source: "on0.png"
anchors.fill: parent
opacity: 0
Behavior on opacity {
NumberAnimation {
duration: 200
}
}
}
}
onClicked: {
app.toggleLigth()
}
}
Button {
id: settingsBt
anchors {
bottom: parent.bottom
left: parent.left
leftMargin: ( app.width - width ) * 0.5
2019-02-23 14:55:38 +01:00
}
2019-02-23 21:49:41 +01:00
text: "settings"
2019-02-23 14:55:38 +01:00
onClicked: {
2019-02-23 21:49:41 +01:00
settingsDia.open()
}
}
Dialog {
id: settingsDia
modal: true
x: ( app.width - width ) * 0.5
y: ( app.height - height ) * 0.5
title: "Settings"
contentItem: Item {
Row {
spacing: 10
Label {
id: settingsIpAdressLa
text: "ip-adress"
}
TextField {
id: settingsIpAdressTf
text: _cppAppSettings.loadSetting("ip-adress")
anchors.verticalCenter: settingsIpAdressLa.verticalCenter
onTextChanged: {
_cppAppSettings.writeSetting("ip-adress", text)
app.ipAdress = text
}
}
}
}
}
function getState(){
sendRequest("http://"+app.ipAdress+"/api/state")
}
function requestFinished(){
}
function toggleLigth(){
if(app.state == 'OFF'){
sendRequest("http://"+app.ipAdress+"/api/on")
}
else if(app.state == 'ON'){
sendRequest("http://"+app.ipAdress+"/api/off")
}
else{
alert("ERROR! " + currentState);
}
getState();
}
function sendRequest(link){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = (function(response) {
return function(){
if(response.readyState === 4 && response.status === 200){
console.log(response.responseText)
if(response.responseText !== ""){
var responseObj = JSON.parse(response.responseText);
if(responseObj["command"] === "state"){
if(app.state === responseObj["response"]){
return;
}
console.log("statechanged")
app.state = responseObj["response"]
if(currentState == 'OFF'){
document.getElementById("toggleLightBt").classList.remove("disabled")
document.getElementById("toggleLightBt").innerHTML = "Turn on";
}
else if(currentState == 'ON'){
document.getElementById("toggleLightBt").classList.remove("disabled")
document.getElementById("toggleLightBt").innerHTML = "Turn off";
}
else{
document.getElementById("toggleLightBt").classList.add("disabled")
document.getElementById("toggleLightBt").innerHTML = "ERROR";
document.getElementById("toggleLightBt").innerHTML = "Unkwon error while connecting to Das Schmalter";
}
}
}
else if(this.readyState === 4){
document.getElementById("errorDiv").innerHTML = "Error while connecting to Das Schmalter: "+this.status
2019-02-23 14:55:38 +01:00
}
2019-02-23 10:33:02 +01:00
}
2019-02-23 21:49:41 +01:00
}})(xmlhttp);
xmlhttp.open("GET", link);
xmlhttp.send();
console.log("request sent")
}
function landscape(){
return(app.width > app.height)
}
Timer {
id: refreshTimer
running: true
interval: 200
onTriggered: {
app.getState();
refreshTimer.start()
}
2019-02-23 10:33:02 +01:00
}
2019-02-23 15:52:11 +01:00
2019-02-23 14:55:38 +01:00
states: [
State {
2019-02-23 21:49:41 +01:00
name: "ON"
2019-02-23 14:55:38 +01:00
PropertyChanges {
2019-02-23 15:52:11 +01:00
target: lampOn0
2019-02-23 21:49:41 +01:00
opacity: 1
2019-02-23 15:52:11 +01:00
}
PropertyChanges {
target: lampOn1
scale: 1
}
PropertyChanges {
target: lampOn2
scale: 1
}
PropertyChanges {
target: lampOn3
scale: 1
}
PropertyChanges {
target: lampOn4
scale: 1
}
PropertyChanges {
target: lampOn5
2019-02-23 14:55:38 +01:00
scale: 1
}
2019-02-23 21:49:41 +01:00
},
State {
name: "OFF"
PropertyChanges {
target: lampOn0
opacity: 0
}
2019-02-23 14:55:38 +01:00
PropertyChanges {
2019-02-23 21:49:41 +01:00
target: lampOn1
scale: 0
}
PropertyChanges {
target: lampOn2
scale: 0
}
PropertyChanges {
target: lampOn3
scale: 0
}
PropertyChanges {
target: lampOn4
scale: 0
}
PropertyChanges {
target: lampOn5
2019-02-23 14:55:38 +01:00
scale: 0
}
}
]
2019-02-23 10:33:02 +01:00
}
}