aboutsummaryrefslogtreecommitdiffstats
path: root/QMLWidgets/Half_Gauge/CoolantGauge.qml
blob: 6e1d583523c86f70e827d1721908ced4b646e772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import QtQuick 2.9

Item {
    id: root
    width: 200
    height: 200

    property real value: 80
    property real minValue: 0
    property real maxValue: 120
    property string unit: "°C"

    property color primaryColor: "#16CCBA"
    property color secondaryColor: "#00000000"
    property int animationDuration: 500
    property real startAngle: 0

    Rectangle {
        anchors.fill: parent
        color: "#041F2B"
        z: -1
    }

    Canvas {
        id: canvas
        anchors.fill: parent
        antialiasing: true
        property real degree: 0

        onDegreeChanged: requestPaint()

        onPaint: {
            var ctx = getContext("2d")
            var center = Qt.point(width / 2, height / 2)
            var side = Math.min(width, height)
            var radius = (side - side * 0.25) / 2
            var startAngle = Math.PI * 2 / 3
            var fullAngle = Math.PI * 2 / 3
            var progressAngle = startAngle + (degree / 270) * fullAngle

            ctx.reset()
            ctx.lineCap = 'square'

            // background arc
            ctx.lineWidth = 25
            ctx.beginPath()
            ctx.arc(center.x, center.y, radius, startAngle, startAngle + fullAngle)
            ctx.strokeStyle = '#000000'
            ctx.stroke()

            // fill arc
            ctx.lineWidth = 15
            ctx.beginPath()
            ctx.arc(center.x, center.y, radius, startAngle, progressAngle)
            ctx.strokeStyle = primaryColor
            ctx.stroke()

            // draw tick marks
            ctx.lineWidth = 5
            ctx.strokeStyle = '#FFFFFF'
            ctx.beginPath()
            var tickCount = 2; // Number of tick marks
            for (var i = 0; i < tickCount; i++) {
                var angle = startAngle + (i / (tickCount - 1)) * (progressAngle - startAngle)
                var x1 = center.x + radius * Math.cos(angle)
                var y1 = center.y + radius * Math.sin(angle)
                var x2 = center.x + (radius - 10) * Math.cos(angle)
                var y2 = center.y + (radius - 10) * Math.sin(angle)
                ctx.moveTo(x1, y1)
                ctx.lineTo(x2, y2)
            }
            ctx.stroke()
        }
    }

    Text {
        id: gaugeText
        anchors.centerIn: parent
        text: "0"
        font.pixelSize: 0.3 * Math.min(root.width, root.height)
        font.bold: true
        color: "#FFFFFF"
    }

    Text {
        id: gaugePercentage
        anchors.verticalCenter: gaugeText.verticalCenter
        anchors.left: gaugeText.right
        text: "%"
        font.pixelSize: 0.15 * Math.min(root.width, root.height)
        font.bold: true
        color: "#FFFFFF"
    }

    Text {
        id: gaugeUnit
        anchors.top: gaugeText.bottom
        anchors.horizontalCenter: gaugeText.horizontalCenter
        
        text: unit
        font.pixelSize: 18
        font.bold: true
        color: "#FFFFFF" 
    }

    Behavior on value {
        NumberAnimation { duration: animationDuration }
    }
    
    onValueChanged: updateGaugeValue(value)

    function updateGaugeValue(value) {
        canvas.degree = value * 270
        gaugeText.text = (value * (maxValue - minValue) + minValue).toFixed(0)
    }

    MouseArea {
        anchors.fill: parent
        hoverEnabled: true

        property bool isDragging: false
        property real initialValue: 0

        onEntered: cursorShape = Qt.PointingHandCursor
        onPressed: {
            isDragging = true
            initialValue = root.value
        }
        onReleased: isDragging = false

        onMouseXChanged: updateDragValue(mouse.x, mouse.y)
        onMouseYChanged: updateDragValue(mouse.x, mouse.y)

        function updateDragValue(x, y) {
            if (!isDragging) return

            var centerX = width / 2
            var centerY = height / 2
            var dx = x - centerX
            var dy = centerY - y // Note: y-axis is inverted
            var angle = Math.atan2(dy, dx) * (180 / Math.PI)

            // Convert angle to gauge value (0 to 1)
            var gaugeValue = (angle + 135) / 270; // Adjust based on your gauge's start angle
            gaugeValue = Math.max(0, Math.min(gaugeValue, 1)); // Clamp value

            root.value = gaugeValue;
        }
    }
}