C

Event Sender: Sending Messages to Applications

// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial

// This file is part of the Qt Safe Renderer module
import QtQuick
import QtQuick.Dialogs
import Qt.SafeRenderer

Window {
    visible: true
    width: 600
    height: 620
    title: qsTr("Event Sender")
    id: controller

    property int startSpeed: 0
    property int maxSpeed: 200
    property int stepSize: 1
    property int carNumber: 1
    property int currentIndex: 0
    property var currentItem: clusterDataControl.stringList[currentIndex];
    property string clusterIP: "127.0.0.1"
    //Icon states
    property bool isSpeedTextVisible: true
    property bool isTurnLeftVisible: leftSignalButton.blink
    property bool isTurnRightVisible: rightSignalButton.blink
    property bool isIconLightsVisible: iconLightsButton.pressed
    property bool isIconLampVisible: iconLampButton.pressed
    property bool isIconTyreVisible: iconTyreButton.pressed
    property bool isIconParkingBrakeVisible: iconParkingBrakeButton.pressed
    property bool isIconFuelVisible: iconFuelButton.pressed
    property bool isIconBatteryVisible: iconBatteryButton.pressed
    property bool isIconSeatbeltVisible: iconSeatbeltButton.pressed
    property bool isIconCoolantVisible: iconCoolantButton.pressed
    property bool connectionState: clusterDataControl.connectionBool

    property var iconButtons: {
        "turnright" : rightSignalButton,
        "turnleft": leftSignalButton,
        "iconCoolant": iconCoolantButton,
        "iconFuel": iconFuelButton,
        "iconParkingBrake": iconParkingBrakeButton,
        "iconTyre": iconTyreButton,
        "iconBattery": iconBatteryButton,
        "iconSeatbelt": iconSeatbeltButton,
        "iconLamp": iconLampButton,
        "iconLights": iconLightsButton
    }

    Connections {
        target: clusterDataControl
        onVehicleSpeedChanged: {
            QSafeMessageSender.setText("speedText", clusterDataControl.vehicleSpeed)
        }
    }

    Component.onCompleted: {
        if (connectionState === true) {
            //Hide icons to make sure icons state matches to button states
            QSafeMessageSender.changeState("turnleft", "hidden")
            QSafeMessageSender.changeState("turnright", "hidden")
            QSafeMessageSender.changeItemVisiblity("iconLights", false)
            QSafeMessageSender.changeItemVisiblity("iconLamp", false)
            QSafeMessageSender.changeItemVisiblity("iconTyre", false)
            QSafeMessageSender.changeItemVisiblity("iconParkingBrake", false)
            QSafeMessageSender.changeItemVisiblity("iconFuel", false)
            QSafeMessageSender.changeItemVisiblity("iconBattery", false)
            QSafeMessageSender.changeItemVisiblity("iconSeatbelt", false)
            QSafeMessageSender.changeItemVisiblity("iconCoolant", false)
        }
    }

    onIsTurnLeftVisibleChanged: {
        if (isTurnLeftVisible) {
            QSafeMessageSender.changeState("turnleft", "visible")
        } else {
            QSafeMessageSender.changeState("turnleft", "hidden")
        }
    }

    onIsTurnRightVisibleChanged: {
        if (isTurnRightVisible) {
            QSafeMessageSender.changeState("turnright", "visible")
        } else {
            QSafeMessageSender.changeState("turnright", "hidden")
        }
    }

    onIsIconLightsVisibleChanged: {
        QSafeMessageSender.changeItemVisiblity("iconLights", isIconLightsVisible)
    }

    onIsIconLampVisibleChanged: {
        QSafeMessageSender.changeItemVisiblity("iconLamp", isIconLampVisible)
    }

    onIsIconTyreVisibleChanged: {
        QSafeMessageSender.changeItemVisiblity("iconTyre", isIconTyreVisible)
    }

    onIsIconParkingBrakeVisibleChanged: {
        QSafeMessageSender.changeItemVisiblity("iconParkingBrake", isIconParkingBrakeVisible)
    }

    onIsIconFuelVisibleChanged: {
        QSafeMessageSender.changeItemVisiblity("iconFuel", isIconFuelVisible)
    }

    onIsIconBatteryVisibleChanged: {
        QSafeMessageSender.changeItemVisiblity("iconBattery", isIconBatteryVisible)
    }

    onIsIconSeatbeltVisibleChanged: {
        QSafeMessageSender.changeItemVisiblity("iconSeatbelt", isIconSeatbeltVisible)
    }

    onIsIconCoolantVisibleChanged: {
        QSafeMessageSender.changeItemVisiblity("iconCoolant", isIconCoolantVisible)
    }

    function changeConnectionState(newState) {
        connectionState(newState)
    }

    function changeState(icon, state) {
        if (iconButtons[icon]) {
            iconButtons[icon].pressed = state
        } else {
            console.warn("Icon not found:", icon)
        }
    }

    function resetSafeIconStates() {
        stepSize = 1
        currentIndex = 0
        startSpeed = 0
        timerIconOFF.stop()
        timerIconON.stop()
        timerDemoON.stop()
        clusterDataControl.vehicleSpeed = 0
        leftSignalButton.pressed = false
        rightSignalButton.pressed = false
        iconLightsButton.pressed = false
        iconLampButton.pressed = false
        iconTyreButton.pressed = false
        iconParkingBrakeButton.pressed = false
        iconFuelButton.pressed = false
        iconBatteryButton.pressed = false
        iconSeatbeltButton.pressed = false
        iconCoolantButton.pressed = false
    }

    Rectangle {
        id: leftBackgroundRect
        anchors {
            top: parent.top
            left: parent.left
            bottom: parent.bottom
        }
        width: 353
        color: "#484950"
        anchors.bottomMargin: 0
        anchors.leftMargin: 0
        anchors.topMargin: 0

        Car {
            id: car
            anchors {
                top: parent.top
                topMargin: 20
                horizontalCenter: parent.horizontalCenter
            }

            electricCar: carNumber === 0
            headLight: clusterDataControl.headLight
            leftFrontDoorOpen: clusterDataControl.frontLeftDoorOpen
            leftBackDoorOpen: clusterDataControl.rearLeftDoorOpen
            rightFrontDoorOpen: clusterDataControl.frontRightDoorOpen
            rightBackDoorOpen: clusterDataControl.rearRightDoorOpen
            leftBlink: clusterDataControl.leftTurnLight
            rightBlink: clusterDataControl.rightTurnLight
        }

        Button {
            id: carID
            y: 387
            anchors {
                horizontalCenter: parent.horizontalCenter
            }
            width: 240
            height: 70
            radius: 6
            color: "#d6d6d6"
            textColor: "#26282a"
            textBlinkColor: "#d6d6d6"
            blinkColor: "#26282a"
            fontPixelSize: 23
            text: "Change layout"
            anchors.horizontalCenterOffset: 0
            onPressedChanged: {
                if (pressed) {
                    clusterDataControl.setCarId(1)
                } else {
                    clusterDataControl.setCarId(2)
                }
            }
        }

        Timer {
            id: timerIconOFF
            interval: 1000 // The interval in milliseconds (10 seconds)
            repeat: true // The timer will not repeat after triggering
            running: false // The initial state of the timer
            // A handler that executes when the timer times out
            onTriggered: {
                if (controller.currentIndex >= 0 && controller.currentIndex < clusterDataControl.stringList.length) {
                    var currentItem = clusterDataControl.stringList[controller.currentIndex]
                    controller.changeState(currentItem, false)
                    controller.currentIndex--
                } else {
                    controller.currentIndex = 0
                    timerIconOFF.stop()
                    timerIconON.start()
                }
            }
        }

        Timer {
            id: timerIconON
            interval: 1000 // The interval in milliseconds (10 seconds)
            repeat: true // The timer will not repeat after triggering
            running: false // The initial state of the timer
            // A handler that executes when the timer times out
            onTriggered: {
                if (controller.currentIndex >= 0 && controller.currentIndex < clusterDataControl.stringList.length) {
                    var currentItem = clusterDataControl.stringList[controller.currentIndex]
                    controller.changeState(currentItem, true)
                    controller.currentIndex++
                } else {
                    controller.currentIndex = clusterDataControl.stringList.length - 1
                    timerIconON.stop()
                    timerIconOFF.start()
                }
            }
        }

        Timer {
            id: timerDemoON
            interval: 50 // The interval in milliseconds (10 seconds)
            repeat: true // The timer will not repeat after triggering
            running: false // The initial state of the timer
            // A handler that executes when the timer times out
            onTriggered: {
                if (controller.startSpeed >= 0 && controller.startSpeed < controller.maxSpeed) {
                    clusterDataControl.vehicleSpeed = controller.startSpeed
                    clusterDataControl.rpm = controller.startSpeed * 40
                    controller.startSpeed++;
                } else if (controller.startSpeed == controller.maxSpeed) {
                    clusterDataControl.vehicleSpeed = 0
                    clusterDataControl.rpm = controller.startSpeed
                    controller.stepSize--;
                }
            }
        }

        Button {
            id: demoMode
            anchors {
                top: carID.bottom
                topMargin: 15
                horizontalCenter: parent.horizontalCenter
            }
            width: 240
            height: 35
            radius: 6
            color: "#d6d6d6"
            textColor: "#26282a"
            textBlinkColor: "#d6d6d6"
            blinkColor: "#26282a"
            fontPixelSize: 14
            text: "Demo Mode"
            anchors.horizontalCenterOffset: 0
            onPressedChanged: {
                if (pressed) {
                    controller.resetSafeIconStates()
                    timerIconON.start()
                    timerDemoON.start()
                } else {
                    timerIconON.stop()
                    timerIconOFF.stop()
                    timerDemoON.stop()
                }
            }
        }

        Connection {
            id: connection
            y: 322
            width: 114
            height: 35
            anchors {
                horizontalCenter: parent.horizontalCenter
            }
            anchors.horizontalCenterOffset: -37
        }

        Button {
            id: killSwitchButton
            y: 556
            anchors {
                topMargin: 96
                horizontalCenter: parent.horizontalCenter
            }
            width: 188
            height: 50
            radius: 6
            color: "#d6d6d6"
            textColor: "#26282a"
            textBlinkColor: "#e50000"
            blinkColor: "#26282a"
            fontPixelSize: 14
            text: "Kill Switch"
            anchors.horizontalCenterOffset: 0

            MouseArea {
                id: killSwitchMouseArea
                anchors.fill: parent
                onClicked: {
                    if (connectionState === true) {
                        if (clusterDataControl.crashArg) {
                            // If Kill Switch is currently "On," turn it "Off"
                            clusterDataControl.setCrash(false);
                        } else {
                            // If Kill Switch is currently "Off," turn it "On"
                            clusterDataControl.setCrash(true);
                        }
                        // Reset buttons states to default
                        demoMode.pressed = false
                        carID.pressed = false
                    }
                    // Toggle the button state (it will automatically return to normal)
                    killSwitchButton.toggled = !killSwitchButton.toggled;
                }
            }
        }

        Slide {
            id: slide
            x: 57
            y: 233
            width: 115
            height: 89
        }

        ButtonHolder {
            id: leftSignalButton
            x: 388
            y: 52
            width: 75
            height: 50
            icon: "qrc:/Icon_TurnLeft_OFF.png"
            iconPressed: "qrc:/Icon_TurnLeft_ON.png"
            text: "Turn Left"
            blinkingEnabled: true
            onPressedChanged: {
                if (!hazardButton.pressed) {
                    if (leftSignalButton.pressed && rightSignalButton.pressed) {
                        rightSignalButton.pressed = false
                        car.rightBlink = false
                    }
                    clusterDataControl.leftTurnLight = leftSignalButton.pressed
                    car.leftBlink = leftSignalButton.pressed
                }
            }
            mouseEnabled: !hazardButton.pressed
        }

        ButtonHolder {
            id: rightSignalButton
            x: 490
            y: 52
            width: 74
            height: 50
            icon: "qrc:/Icon_TurnRight_OFF.png"
            iconPressed: "qrc:/Icon_TurnRight_ON.png"
            text: "Turn Right"
            blinkingEnabled: true
            onPressedChanged:  {
                if (!hazardButton.pressed) {
                    if (rightSignalButton.pressed && leftSignalButton.pressed) {
                        leftSignalButton.pressed = false
                        car.leftBlink = false
                    }
                    clusterDataControl.rightTurnLight = rightSignalButton.pressed
                    car.rightBlink = rightSignalButton.pressed
                }
            }
            mouseEnabled: !hazardButton.pressed
        }

        ButtonHolder {
            id: hazardButton
            icon: "qrc:/Icon_HazardWarning_OFF.png"
            iconPressed: "qrc:/Icon_HazardWarning_ON.png"
            text: "Hazard\nWarning"
            blinkingEnabled: false
            property bool leftPreviousState: false
            property bool rightPreviousState: false
            x: 389
            y: 141
            width: 75
            height: 50
            onPressedChanged: {
                clusterDataControl.hazardSignal = hazardButton.pressed
                if (hazardButton.pressed) {
                    if (leftSignalButton.pressed)
                        hazardButton.leftPreviousState = true
                    else
                        leftSignalButton.pressed = true
                    if (rightSignalButton.pressed)
                        hazardButton.rightPreviousState = true
                    else
                        rightSignalButton.pressed = true
                } else {
                    if (hazardButton.leftPreviousState)
                        hazardButton.leftPreviousState = false
                    else
                        leftSignalButton.pressed = false
                    if (hazardButton.rightPreviousState)
                        hazardButton.rightPreviousState = false
                    else
                        rightSignalButton.pressed = false
                }
                car.leftBlink = leftSignalButton.pressed
                car.rightBlink = rightSignalButton.pressed
            }
        }

        ButtonHolder {
            id: iconLightsButton
            x: 388
            y: 322
            width: 75
            height: 50
            icon: "qrc:/Icon_LowBeam_OFF.png"
            iconPressed: "qrc:/Icon_LowBeam_ON.png"
            text: "Low Beam"
            onPressedChanged: {
                clusterDataControl.headLight = iconLightsButton.pressed
            }
        }

        ButtonHolder {
            id: iconLampButton
            x: 389
            y: 500
            width: 75
            height: 50
            icon: "qrc:/Icon_BulbFailure_OFF.png"
            iconPressed: "qrc:/Icon_BulbFailure_ON.png"
            text: "Bulb Failure"
        }

        ButtonHolder {
            id: iconTyreButton
            x: 389
            y: 407
            width: 75
            height: 50
            text: "Tyre\nMalfunction"
            icon: "qrc:/Icon_TyreMalfunction_OFF.png"
            iconPressed: "qrc:/Icon_TyreMalfunction_ON.png"
        }

        ButtonHolder {
            id: iconParkingBrakeButton
            x: 389
            y: 234
            width: 75
            height: 50
            icon: "qrc:/Icon_ParkingBrake_OFF.png"
            iconPressed: "qrc:/Icon_ParkingBrake_ON.png"
            text: "Parking Brake"
            onPressedChanged: {
                clusterDataControl.brake = iconParkingBrakeButton.pressed
            }
        }

        Row {
            id: viewButtons
            spacing: 10
            enabled: (carNumber == 2 && !demoMode.pressed) || carNumber !== 2
            anchors {
                horizontalCenter: parent.horizontalCenter
                topMargin: 42
            }
        }

        Rectangle {
            id: textHolder
            x: 42
            y: 550
            border.color: "black"
            width: 260
        }

        ButtonHolder {
            id: iconFuelButton
            x: 490
            y: 141
            width: 74
            height: 50
            text: "Fuel"
            icon: "qrc:/Icon_Fuel_OFF.png"
            iconPressed: "qrc:/Icon_Fuel_ON.png"
            onPressedChanged: {
                if (pressed) {
                    clusterDataControl.gasLevel = 0
                }
                else {
                    clusterDataControl.gasLevel = 67
                }
            }
        }

        ButtonHolder {
            id: iconBatteryButton
            x: 490
            y: 234
            width: 74
            height: 50
            text: "Battery"
            icon: "qrc:/Icon_Battery_OFF.png"
            iconPressed: "qrc:/Icon_Battery_ON.png"
            onPressedChanged: {
                if (pressed) {
                    clusterDataControl.batteryPotential = 0
                }
                else {
                    clusterDataControl.batteryPotential = 80
                }
            }
        }

        ButtonHolder {
            id: iconSeatbeltButton
            x: 490
            y: 322
            width: 74
            height: 50
            text: "Seatbelt"
            icon: "qrc:/Icon_SeatBelt_OFF.png"
            iconPressed: "qrc:/Icon_SeatBelt_ON.png"
        }

        ButtonHolder {
            id: iconCoolantButton
            x: 490
            y: 407
            width: 74
            height: 50
            text: "Coolant"
            icon: "qrc:/Icon_Coolant_OFF.png"
            iconPressed: "qrc:/Icon_Coolant_ON.png"
        }
    }
}