diff options
Diffstat (limited to 'QMLWidgets/Full_Gauge/RPMGauge.qml')
-rw-r--r-- | QMLWidgets/Full_Gauge/RPMGauge.qml | 211 |
1 files changed, 135 insertions, 76 deletions
diff --git a/QMLWidgets/Full_Gauge/RPMGauge.qml b/QMLWidgets/Full_Gauge/RPMGauge.qml index 382731d..e440a13 100644 --- a/QMLWidgets/Full_Gauge/RPMGauge.qml +++ b/QMLWidgets/Full_Gauge/RPMGauge.qml @@ -2,8 +2,8 @@ import QtQuick 2.9 Item {
id: root
- width: 400
- height: 400
+ width: 300
+ height: 300
property real value: 0
property real minValue: 0
@@ -13,7 +13,27 @@ Item { property color primaryColor: "#16CCBA"
property color secondaryColor: "#00000000"
property int animationDuration: 1000
- property real startAngle: 0
+ property real startAngle: Math.PI * 3 / 4
+ property real fullAngle: Math.PI * 3 / 2
+
+ property real gaugeRadius: 0
+ property real tickLength: 0
+
+ signal rpmValueChanged(int value)
+
+ Component.onCompleted: {
+ calculateGeometry();
+ updateGaugeValue(value); // Initialize the gauge display
+ }
+
+ onWidthChanged: calculateGeometry()
+ onHeightChanged: calculateGeometry()
+
+ function calculateGeometry() {
+ const side = Math.min(width, height);
+ gaugeRadius = (side - side * 0.20) / 2;
+ tickLength = gaugeRadius * 0.1;
+ }
Rectangle {
anchors.fill: parent
@@ -29,7 +49,6 @@ Item { sourceSize.height: width
fillMode: Image.PreserveAspectFit
anchors.fill: parent
- anchors.centerIn: parent
scale: 1
opacity: 0.7
z: 0
@@ -40,9 +59,8 @@ Item { source: "./assets/Ellipse 1.svg"
sourceSize.width: width
sourceSize.height: width
- anchors.fill: parent
- anchors.centerIn: parent
fillMode: Image.PreserveAspectFit
+ anchors.fill: parent
scale: 1
opacity: 0.7
z: 1
@@ -52,49 +70,81 @@ Item { 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.20) / 2
- var startAngle = Math.PI * 3 / 4
- var fullAngle = Math.PI * 3 / 2
- var progressAngle = startAngle + (degree / 270) * fullAngle
-
- ctx.reset()
- ctx.lineCap = 'round'
-
- ctx.lineWidth = side * 0.1
- ctx.beginPath()
- ctx.arc(center.x, center.y, radius, startAngle, progressAngle)
- ctx.strokeStyle = secondaryColor
- ctx.stroke()
-
- ctx.lineWidth = 20
- ctx.beginPath()
- ctx.arc(center.x, center.y, radius, startAngle, progressAngle)
- ctx.strokeStyle = primaryColor
- ctx.stroke()
-
- // draw tick marks
- ctx.lineWidth = 10
- 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 - 20) * Math.cos(angle)
- var y2 = center.y + (radius - 20) * Math.sin(angle)
- ctx.moveTo(x1, y1)
- ctx.lineTo(x2, y2)
+ var ctx = getContext("2d");
+ var center = Qt.point(width / 2, height / 2);
+
+ ctx.reset();
+ ctx.lineCap = 'round';
+
+ drawBackground(ctx, center);
+ drawProgress(ctx, center, value);
+ drawTicks(ctx, center);
+ drawProgressTick(ctx, center, value);
+ }
+
+ function drawBackground(ctx, center) {
+ ctx.lineWidth = gaugeRadius * 0.1;
+ ctx.beginPath();
+ ctx.arc(center.x, center.y, gaugeRadius, startAngle, startAngle + fullAngle);
+ ctx.strokeStyle = secondaryColor;
+ ctx.stroke();
+ }
+
+ function drawProgress(ctx, center, progress) {
+ ctx.lineWidth = 20;
+
+ var gradient = ctx.createLinearGradient(
+ center.x - gaugeRadius, center.y,
+ center.x + gaugeRadius, center.y
+ );
+
+ gradient.addColorStop(0.0, "#00FF00");
+ //gradient.addColorStop(0.3, primaryColor);
+ gradient.addColorStop(0.4, "#FFD700");
+ gradient.addColorStop(1, "#FF0000");
+
+ ctx.beginPath();
+ ctx.arc(center.x, center.y, gaugeRadius, startAngle, startAngle + (progress / (maxValue - minValue)) * fullAngle);
+ ctx.strokeStyle = gradient;
+ ctx.stroke();
+ }
+
+ function drawTicks(ctx, center) {
+ ctx.lineWidth = tickLength / 2;
+ ctx.strokeStyle = '#FFFFFF';
+ ctx.beginPath();
+
+ const tickCount = 10; // Adjust the number of ticks as needed
+
+ for (let i = 0; i <= tickCount; i++) {
+ const angle = startAngle + (i / tickCount) * fullAngle;
+ const x1 = center.x + gaugeRadius * Math.cos(angle);
+ const y1 = center.y + gaugeRadius * Math.sin(angle);
+ const x2 = center.x + (gaugeRadius - tickLength) * Math.cos(angle);
+ const y2 = center.y + (gaugeRadius - tickLength) * Math.sin(angle);
+ ctx.moveTo(x1, y1);
+ ctx.lineTo(x2, y2);
}
- ctx.stroke()
+
+ ctx.stroke();
+ }
+
+ function drawProgressTick(ctx, center, progress) {
+ ctx.lineWidth = tickLength * 1.5;
+ ctx.strokeStyle = '#FFFFFF';
+ ctx.beginPath();
+
+ const progressAngle = startAngle + (progress / (maxValue - minValue)) * fullAngle;
+ const x1 = center.x + gaugeRadius * Math.cos(progressAngle);
+ const y1 = center.y + gaugeRadius * Math.sin(progressAngle);
+ const x2 = center.x + (gaugeRadius - tickLength * 1.5) * Math.cos(progressAngle);
+ const y2 = center.y + (gaugeRadius - tickLength * 1.5) * Math.sin(progressAngle);
+
+ ctx.moveTo(x1, y1);
+ ctx.lineTo(x2, y2);
+ ctx.stroke();
}
}
@@ -111,7 +161,6 @@ Item { id: gaugeUnit
anchors.top: gaugeText.bottom
anchors.horizontalCenter: gaugeText.horizontalCenter
-
text: unit
font.pixelSize: 18
font.bold: true
@@ -125,41 +174,51 @@ Item { onValueChanged: updateGaugeValue(value)
function updateGaugeValue(value) {
- canvas.degree = value * 270
- gaugeText.text = (value * (maxValue - minValue) + minValue).toFixed(0)
- }
+ canvas.requestPaint(); // Request a repaint to reflect changes.
+ gaugeText.text = (value).toFixed(0); // Display the current value directly.
+ rpmValueChanged(value); // Emit the signal with the current value.
+ }
- MouseArea {
- anchors.fill: parent
- hoverEnabled: true
+ MouseArea {
+ id: dragArea
+ anchors.fill: parent
- property bool isDragging: false
- property real initialValue: 0
+ property bool isDragging: false
- onEntered: cursorShape = Qt.PointingHandCursor
- onPressed: {
- isDragging = true
- initialValue = root.value
- }
- onReleased: isDragging = false
+ onEntered: cursorShape = Qt.PointingHandCursor
- onMouseXChanged: updateDragValue(mouse.x, mouse.y)
- onMouseYChanged: updateDragValue(mouse.x, mouse.y)
+ onPressed: {
+ isDragging = true;
+ updateDragValue(mouseX, mouseY); // Update immediately on press.
+ }
+
+ onReleased: isDragging = false
- function updateDragValue(x, y) {
- if (!isDragging) return
+ onPositionChanged:
+ if (isDragging) {
+ updateDragValue(mouseX, mouseY); // Use formal parameters instead of injected ones.
+ }
- 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)
+ function updateDragValue(x, y) {
+ const centerX = width / 2;
+ const centerY = height / 2;
- // 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
+ // 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);
- root.value = gaugeValue
- }
- }
+ // 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;
+ }
+ }
}
|