C

Qt Quick Ultralite Thermostat Demo

// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial
import QtQuick 2.15
import Thermo 1.0
import QtQuickUltralite.Extras 2.0
import Benchmark 1.0
import QtQuickUltralite.Profiling

Item {
    id: window
    width: Theme.screenWidth
    height: Theme.screenHeight

    function handleViewSwitched(selectedIndex : int) {
        bottomBar.selected = selectedIndex
        GlobalState.previousSelectedIndex = GlobalState.selectedIndex
        GlobalState.selectedIndex = selectedIndex
    }

    Image {
        id: topBarShadow
        source: "top-bar-shadow.png"
        anchors.top: topBar.bottom
    }

    Rectangle {
        id: centerBg
        color: "#F5F7F8"
        anchors.top: topBarShadow.bottom
        width: parent.width
        height: window.height - bottomBar.height - topBarShadow.height
    }

    TopBar {
        id: topBar
        width: parent.width
        height: Theme.topBarHeight
        z: 11

        showBackButton: !GlobalState.showMain
        title: GlobalState.selectedRoom.name

        onBackClicked: {
            GlobalState.showMain = true
        }
        onSettingsClicked: {
            languageDialog.show()
        }
    }

    Item {
        id: mainItem
        width: parent.width
        anchors.top: topBar.bottom
        height: parent.height - topBar.height - bottomBar.height

        Loader {
            id: mainView
            anchors.fill: parent
            source: "../../PlacesView.qml"

            Connections {
                target: GlobalState
                function onShowMainChanged(showMain: bool) {
                    if (GlobalState.showMain) {
                        GlobalState.selectedIndex = 0
                        GlobalState.previousSelectedIndex = GlobalState.currentViewIndex
                        GlobalState.selectedRoom.tempChanger.running = true
                        mainItem.height = window.height - topBar.height - bottomBar.height
                    } else {
                        GlobalState.selectedIndex = 3
                        GlobalState.previousSelectedIndex = GlobalState.currentViewIndex
                        GlobalState.selectedRoom.tempChanger.running = false
                        mainItem.height = window.height - topBar.height
                    }
                }
                function onLoadNext() {
                    if (GlobalState.selectedIndex !== GlobalState.currentViewIndex) {
                        switch(GlobalState.selectedIndex) {
                            case 1:
                                GlobalState.scheduleViewLoaded = false
                                mainView.source = "../../ScheduleView.qml"
                                break;
                            case 2:
                                mainView.source = "../../StatsView.qml"
                                break;
                            case 3:
                                mainView.source = "../../RoomView.qml"
                                break;
                            default:
                                GlobalState.placesLoaded = false
                                mainView.source = "../../PlacesView.qml"
                        }

                        GlobalState.currentViewIndex = GlobalState.selectedIndex
                    }
                }
            }
        }
    }

    BottomBar {
        id:bottomBar
        z: 10 // give precedence to bottom bar touch areas
        width: parent.width
        height: Theme.bottomBarHeight
        anchors.bottom: parent.bottom
        property int shift: !GlobalState.showMain ? -height : 0
        visible: shift > -height
        Behavior on shift { NumberAnimation { } }
        anchors.bottomMargin: shift
        onViewSwitched: handleViewSwitched(index)
    }

    LanguageDialog {
        id: languageDialog
        anchors.fill: parent
        z: 11
    }

    Component.onCompleted: {
        if(Qt.uiLanguage == "") {
            Qt.uiLanguage = "en_GB"
        }

        QulPerf.recording = true
        GlobalState.weatherStatusTimerEnable = false

        Rooms.livingRoom.tempChanger.running = false
        Rooms.diningRoom.tempChanger.running = false
        Rooms.garageRoom.tempChanger.running = false
        Rooms.kitchenRoom.tempChanger.running = false
        Rooms.kidsRoom.tempChanger.running = false
        Rooms.kids2Room.tempChanger.running = false

        Rooms.livingRoom.statusChanger.running = false
        Rooms.diningRoom.statusChanger.running = false
        Rooms.garageRoom.statusChanger.running = false
        Rooms.kitchenRoom.statusChanger.running = false
        Rooms.kidsRoom.statusChanger.running = false
        Rooms.kids2Room.statusChanger.running = false
    }

    property int simulationState: 0;
    property int repetitionCount: 0;
    property int flickDirection: 1;

    Timer {
        id: simulatorTimer
        interval: 100
        running: true
        repeat: true
        onTriggered: {
            switch(simulationState) {
                case 0:
                        repetitionCount = 0;
                        bottomBar.viewSwitched(0);
                        simulationState = 1;
                        break;

                case 1:
                        if(!FlickControl.running()) {
                            FlickControl.startFlick(window.width/2, window.height/2, (flickDirection?1:-1) * 20, 0);
                            flickDirection = 1 - flickDirection;
                            if(++repetitionCount >= 16) {
                                simulatorTimer.interval = 1000
                                simulationState = 2;
                                bottomBar.viewSwitched(0);
                            }
                        }
                        break;

                case 2:
                        repetitionCount = 0;
                        bottomBar.viewSwitched(1);
                        simulatorTimer.interval = 300
                        simulationState = 3;
                        break;

                case 3:
                        if(++repetitionCount >= 8)
                        {
                            repetitionCount = 0;
                            bottomBar.viewSwitched(2);
                            simulatorTimer.interval = 100
                            simulationState = 4;
                        }
                        GlobalState.listViewIndex = repetitionCount;
                        break;

                case 4:
                        if(++repetitionCount >= 16)
                        {
                            repetitionCount = 0;
                            bottomBar.viewSwitched(0);
                            simulationState = 5;
                        }
                        GlobalState.statsContentViewIndex = 1 - GlobalState.statsContentViewIndex;
                        break;

                case 5:
                        simulationState = 0;
                        break;

                default: break;
            }
        }
    }

    QulPerfOverlay {
        id: benchmarkResult
        anchors.horizontalCenter: parent.horizontalCenter;
        anchors.verticalCenter: parent.verticalCenter;
        visible: false
    }

    Timer {
        id: benchmarkTimer
        interval: 30000
        running: true
        repeat: false
        onTriggered: {
            simulatorTimer.running = false
            QulPerf.recording = false
            mainItem.visible = false
            benchmarkResult.visible = true
            bottomBar.visible = false
        }
    }
}