aboutsummaryrefslogtreecommitdiffstats
path: root/QMLWidgets/Half_Gauge/CoolantGauge.qml
blob: fe402de1751f25a422e567667a5e1bcbd58fb23d (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import QtQuick 2.9

Item {
    id: root
    width: 200
    height: 200

    property real value: 0
    property real minValue: 0
    property real maxValue: 120
    property string unit: "°C"
    property string iconSource: "qrc:/Carbon_Icons/carbon_icons/temperature--water.svg"

    property color primaryColor: "#16CCBA"
    property color secondaryColor: "#00000000"
    property int animationDuration: 1000
    property real startAngle: Math.PI * 5 / 4
    property real fullAngle: Math.PI * 1 / 2 

    signal coolantTempValueChanged(int value)

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

    Canvas {
        id: canvas
        anchors.fill: parent
        antialiasing: true

        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 progressAngle = startAngle + (value / (maxValue - minValue)) * fullAngle;

            ctx.reset();
            ctx.lineCap = 'round';

            // 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();
            const tickCount = 2; // Adjust the number of ticks as needed
            
            for (let i = 0; i <= tickCount; i++) {
                const angle = startAngle + (i / tickCount) * fullAngle;
                const x1 = center.x + radius * Math.cos(angle);
                const y1 = center.y + radius * Math.sin(angle);
                const x2 = center.x + (radius - 10) * Math.cos(angle);
                const y2 = center.y + (radius - 10) * Math.sin(angle);
                ctx.moveTo(x1, y1);
                ctx.lineTo(x2, y2);
            }
            
            ctx.stroke();

            // Draw progress tick mark
            drawProgressTick(ctx, center, progressAngle, radius);
        }

        function drawProgressTick(ctx, center, angle, radius) {
            ctx.lineWidth = 8; // Thickness of the progress tick mark
            ctx.strokeStyle = '#FFFFFF'; // Color of the progress tick mark
            ctx.beginPath();

            const x1 = center.x + radius * Math.cos(angle);
            const y1 = center.y + radius * Math.sin(angle);
            const x2 = center.x + (radius - 15) * Math.cos(angle); // Adjust length as needed
            const y2 = center.y + (radius - 15) * 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: gaugeUnit
        anchors.verticalCenter: gaugeText.verticalCenter
        anchors.left: gaugeText.right
        text: unit
        font.pixelSize: 0.15 * Math.min(root.width, root.height)
        font.bold: true
        color: "#FFFFFF"
    }

    Image {
        source: iconSource
        anchors.top: gaugeText.bottom
        anchors.horizontalCenter: gaugeText.horizontalCenter
        width: 0.15 * Math.min(root.width, root.height)
        fillMode: Image.PreserveAspectFit
        antialiasing: true
    }

    Behavior on value {
        NumberAnimation { duration: animationDuration }
    }

    onValueChanged: updateGaugeValue(value)

    function updateGaugeValue(value) {
        canvas.requestPaint(); // Request a repaint to reflect changes.
        gaugeText.text = value.toFixed(0); // Display the current value directly.
        coolantTempValueChanged(value); // Emit the signal with the current value.
    }

   MouseArea {
       id: dragArea
       anchors.fill: parent

       property bool isDragging: false

       onEntered: cursorShape = Qt.PointingHandCursor

       onPressed: {
           isDragging = true;
           updateDragValue(mouseX, mouseY); // Update immediately on press.
       }
       
       onReleased: isDragging = false

       onPositionChanged:
           if (isDragging) {
               updateDragValue(mouseX, mouseY); // Use formal parameters instead of injected ones.
           }

       function updateDragValue(x, y) {
           const centerX = width / 2;
           const centerY = height / 2;

           // Calculate angle from the center to the mouse position.
           const dx = x - centerX;
           const dy = centerY - y; // Note that y-axis is inverted in QML.
           const angle = Math.atan2(dy, dx);

           // Normalize angle to [startAngle .. startAngle + fullAngle]
           let normalizedAngle = angle < startAngle ? angle + Math.PI * 2 : angle;

           // Calculate the normalized value based on the angle.
           let normalizedValue = ((normalizedAngle - startAngle) / fullAngle) * (maxValue - minValue);

           // Clamp value within min and max range.
           normalizedValue = Math.max(minValue, Math.min(maxValue, normalizedValue));

           // Update the root value.
           root.value = normalizedValue;
       }
   }
}