C

Qt Quick Ultralite Thermostat Demo

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

import QtQuick 2.15
import QtQuickUltralite.Extras 2.0
import Thermo 1.0

Item {
    id: root
    width: Theme.sliderHandleSize
    height: Theme.sliderHandleSize

    Image {
        anchors.centerIn: parent
        anchors.verticalCenterOffset: 6 // account for shadow
        source: "slider-handle.png"
    }

    property alias mouseX: ta.mouseX

    property int minX: -1
    property int maxX: -1
    property int currentIndex: -1

    property string hour: Math.floor(currentIndex/2)
    property string minute: currentIndex%2 == 1 ? "30" : "00"

    Row {
        id: time
        anchors.bottom: parent.top
        anchors.bottomMargin: 4
        Text {
            text: root.hour
            color: "#3d464d"
            font.pixelSize: Theme.sliderHandleLabelFontSize
        }
        StaticText {
            text: ":"
            color: "#3d464d"
            font.pixelSize: Theme.sliderHandleLabelFontSize
            horizontalAlignment: Text.AlignLeft
        }
        Text {
            id: txt
            text: root.minute
            color: "#3d464d"
            font.pixelSize: Theme.sliderHandleLabelFontSize
        }
    }

    MouseArea {
        id: ta
        anchors.fill: parent
        anchors.margins: -10
        property real pressedX: 0
        property real pressedY: 0
        onPressed: {
            pressedX = mouse.x
            pressedY = mouse.y
        }
        onMouseXChanged: {
            var newX = root.x - (ta.pressedX - ta.mouseX)
            root.x = Math.max(root.minX, Math.min(root.maxX, newX))
        }
    }
}